Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eb6d08f

Browse files
feat: add solutions to lc problem: No.3679 (doocs#4725)
1 parent d7374bc commit eb6d08f

File tree

7 files changed

+322
-8
lines changed

7 files changed

+322
-8
lines changed

‎solution/3600-3699/3679.Minimum Discards to Balance Inventory/README.md‎

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,32 +91,141 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3679.Mi
9191

9292
<!-- solution:start -->
9393

94-
### 方法一
94+
### 方法一:模拟 + 滑动窗口
95+
96+
我们用一个哈希表 $\textit{cnt}$ 来记录当前窗口中每种物品的数量,用一个数组 $\textit{marked}$ 来记录每个物品是否被保留。
97+
98+
我们从左到右遍历数组,对于每个物品 $x$:
99+
100+
1. 如果当前天数 $i$ 大于等于窗口大小 $w,ドル则需要将窗口最左侧的物品数量减去 $\textit{marked}[i - w]$(如果该物品被保留的话)。
101+
2. 如果当前物品在窗口中的数量超过了 $m,ドル则需要丢弃该物品。
102+
3. 否则,保留该物品,并将其数量加一。
103+
104+
最终,答案即为被丢弃的物品数量。
105+
106+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 为数组长度。
95107

96108
<!-- tabs:start -->
97109

98110
#### Python3
99111

100112
```python
101-
113+
class Solution:
114+
def minArrivalsToDiscard(self, arrivals: List[int], w: int, m: int) -> int:
115+
cnt = Counter()
116+
n = len(arrivals)
117+
marked = [0] * n
118+
ans = 0
119+
for i, x in enumerate(arrivals):
120+
if i >= w:
121+
cnt[arrivals[i - w]] -= marked[i - w]
122+
if cnt[x] >= m:
123+
ans += 1
124+
else:
125+
marked[i] = 1
126+
cnt[x] += 1
127+
return ans
102128
```
103129

104130
#### Java
105131

106132
```java
107-
133+
class Solution {
134+
public int minArrivalsToDiscard(int[] arrivals, int w, int m) {
135+
Map<Integer, Integer> cnt = new HashMap<>();
136+
int n = arrivals.length;
137+
int[] marked = new int[n];
138+
int ans = 0;
139+
for (int i = 0; i < n; i++) {
140+
int x = arrivals[i];
141+
if (i >= w) {
142+
int prev = arrivals[i - w];
143+
cnt.merge(prev, -marked[i - w], Integer::sum);
144+
}
145+
if (cnt.getOrDefault(x, 0) >= m) {
146+
ans++;
147+
} else {
148+
marked[i] = 1;
149+
cnt.merge(x, 1, Integer::sum);
150+
}
151+
}
152+
return ans;
153+
}
154+
}
108155
```
109156

110157
#### C++
111158

112159
```cpp
113-
160+
class Solution {
161+
public:
162+
int minArrivalsToDiscard(vector<int>& arrivals, int w, int m) {
163+
unordered_map<int, int> cnt;
164+
int n = arrivals.size();
165+
vector<int> marked(n, 0);
166+
int ans = 0;
167+
for (int i = 0; i < n; i++) {
168+
int x = arrivals[i];
169+
if (i >= w) {
170+
cnt[arrivals[i - w]] -= marked[i - w];
171+
}
172+
if (cnt[x] >= m) {
173+
ans++;
174+
} else {
175+
marked[i] = 1;
176+
cnt[x] += 1;
177+
}
178+
}
179+
return ans;
180+
}
181+
};
114182
```
115183
116184
#### Go
117185
118186
```go
187+
func minArrivalsToDiscard(arrivals []int, w int, m int) (ans int) {
188+
cnt := make(map[int]int)
189+
n := len(arrivals)
190+
marked := make([]int, n)
191+
for i, x := range arrivals {
192+
if i >= w {
193+
cnt[arrivals[i-w]] -= marked[i-w]
194+
}
195+
if cnt[x] >= m {
196+
ans++
197+
} else {
198+
marked[i] = 1
199+
cnt[x]++
200+
}
201+
}
202+
return
203+
}
204+
```
119205

206+
#### TypeScript
207+
208+
```ts
209+
function minArrivalsToDiscard(arrivals: number[], w: number, m: number): number {
210+
const cnt = new Map<number, number>();
211+
const n = arrivals.length;
212+
const marked = Array<number>(n).fill(0);
213+
let ans = 0;
214+
215+
for (let i = 0; i < n; i++) {
216+
const x = arrivals[i];
217+
if (i >= w) {
218+
cnt.set(arrivals[i - w], (cnt.get(arrivals[i - w]) || 0) - marked[i - w]);
219+
}
220+
if ((cnt.get(x) || 0) >= m) {
221+
ans++;
222+
} else {
223+
marked[i] = 1;
224+
cnt.set(x, (cnt.get(x) || 0) + 1);
225+
}
226+
}
227+
return ans;
228+
}
120229
```
121230

122231
<!-- tabs:end -->

‎solution/3600-3699/3679.Minimum Discards to Balance Inventory/README_EN.md‎

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,141 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3679.Mi
8989

9090
<!-- solution:start -->
9191

92-
### Solution 1
92+
### Solution 1: Simulation + Sliding Window
93+
94+
We use a hash map $\textit{cnt}$ to record the quantity of each item type in the current window, and an array $\textit{marked}$ to record whether each item is kept.
95+
96+
We iterate through the array from left to right. For each item $x$:
97+
98+
1. If the current day $i$ is greater than or equal to the window size $w,ドル we subtract $\textit{marked}[i - w]$ from the count of the leftmost item in the window (if that item was kept).
99+
2. If the quantity of the current item in the window exceeds $m,ドル we discard the item.
100+
3. Otherwise, we keep the item and increment its count.
101+
102+
Finally, the answer is the number of items discarded.
103+
104+
The time complexity is $O(n),ドル and the space complexity is $O(n),ドル where $n$ is the length of the array.
93105

94106
<!-- tabs:start -->
95107

96108
#### Python3
97109

98110
```python
99-
111+
class Solution:
112+
def minArrivalsToDiscard(self, arrivals: List[int], w: int, m: int) -> int:
113+
cnt = Counter()
114+
n = len(arrivals)
115+
marked = [0] * n
116+
ans = 0
117+
for i, x in enumerate(arrivals):
118+
if i >= w:
119+
cnt[arrivals[i - w]] -= marked[i - w]
120+
if cnt[x] >= m:
121+
ans += 1
122+
else:
123+
marked[i] = 1
124+
cnt[x] += 1
125+
return ans
100126
```
101127

102128
#### Java
103129

104130
```java
105-
131+
class Solution {
132+
public int minArrivalsToDiscard(int[] arrivals, int w, int m) {
133+
Map<Integer, Integer> cnt = new HashMap<>();
134+
int n = arrivals.length;
135+
int[] marked = new int[n];
136+
int ans = 0;
137+
for (int i = 0; i < n; i++) {
138+
int x = arrivals[i];
139+
if (i >= w) {
140+
int prev = arrivals[i - w];
141+
cnt.merge(prev, -marked[i - w], Integer::sum);
142+
}
143+
if (cnt.getOrDefault(x, 0) >= m) {
144+
ans++;
145+
} else {
146+
marked[i] = 1;
147+
cnt.merge(x, 1, Integer::sum);
148+
}
149+
}
150+
return ans;
151+
}
152+
}
106153
```
107154

108155
#### C++
109156

110157
```cpp
111-
158+
class Solution {
159+
public:
160+
int minArrivalsToDiscard(vector<int>& arrivals, int w, int m) {
161+
unordered_map<int, int> cnt;
162+
int n = arrivals.size();
163+
vector<int> marked(n, 0);
164+
int ans = 0;
165+
for (int i = 0; i < n; i++) {
166+
int x = arrivals[i];
167+
if (i >= w) {
168+
cnt[arrivals[i - w]] -= marked[i - w];
169+
}
170+
if (cnt[x] >= m) {
171+
ans++;
172+
} else {
173+
marked[i] = 1;
174+
cnt[x] += 1;
175+
}
176+
}
177+
return ans;
178+
}
179+
};
112180
```
113181
114182
#### Go
115183
116184
```go
185+
func minArrivalsToDiscard(arrivals []int, w int, m int) (ans int) {
186+
cnt := make(map[int]int)
187+
n := len(arrivals)
188+
marked := make([]int, n)
189+
for i, x := range arrivals {
190+
if i >= w {
191+
cnt[arrivals[i-w]] -= marked[i-w]
192+
}
193+
if cnt[x] >= m {
194+
ans++
195+
} else {
196+
marked[i] = 1
197+
cnt[x]++
198+
}
199+
}
200+
return
201+
}
202+
```
117203

204+
#### TypeScript
205+
206+
```ts
207+
function minArrivalsToDiscard(arrivals: number[], w: number, m: number): number {
208+
const cnt = new Map<number, number>();
209+
const n = arrivals.length;
210+
const marked = Array<number>(n).fill(0);
211+
let ans = 0;
212+
213+
for (let i = 0; i < n; i++) {
214+
const x = arrivals[i];
215+
if (i >= w) {
216+
cnt.set(arrivals[i - w], (cnt.get(arrivals[i - w]) || 0) - marked[i - w]);
217+
}
218+
if ((cnt.get(x) || 0) >= m) {
219+
ans++;
220+
} else {
221+
marked[i] = 1;
222+
cnt.set(x, (cnt.get(x) || 0) + 1);
223+
}
224+
}
225+
return ans;
226+
}
118227
```
119228

120229
<!-- tabs:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int minArrivalsToDiscard(vector<int>& arrivals, int w, int m) {
4+
unordered_map<int, int> cnt;
5+
int n = arrivals.size();
6+
vector<int> marked(n, 0);
7+
int ans = 0;
8+
for (int i = 0; i < n; i++) {
9+
int x = arrivals[i];
10+
if (i >= w) {
11+
cnt[arrivals[i - w]] -= marked[i - w];
12+
}
13+
if (cnt[x] >= m) {
14+
ans++;
15+
} else {
16+
marked[i] = 1;
17+
cnt[x] += 1;
18+
}
19+
}
20+
return ans;
21+
}
22+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func minArrivalsToDiscard(arrivals []int, w int, m int) (ans int) {
2+
cnt := make(map[int]int)
3+
n := len(arrivals)
4+
marked := make([]int, n)
5+
for i, x := range arrivals {
6+
if i >= w {
7+
cnt[arrivals[i-w]] -= marked[i-w]
8+
}
9+
if cnt[x] >= m {
10+
ans++
11+
} else {
12+
marked[i] = 1
13+
cnt[x]++
14+
}
15+
}
16+
return
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int minArrivalsToDiscard(int[] arrivals, int w, int m) {
3+
Map<Integer, Integer> cnt = new HashMap<>();
4+
int n = arrivals.length;
5+
int[] marked = new int[n];
6+
int ans = 0;
7+
for (int i = 0; i < n; i++) {
8+
int x = arrivals[i];
9+
if (i >= w) {
10+
int prev = arrivals[i - w];
11+
cnt.merge(prev, -marked[i - w], Integer::sum);
12+
}
13+
if (cnt.getOrDefault(x, 0) >= m) {
14+
ans++;
15+
} else {
16+
marked[i] = 1;
17+
cnt.merge(x, 1, Integer::sum);
18+
}
19+
}
20+
return ans;
21+
}
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def minArrivalsToDiscard(self, arrivals: List[int], w: int, m: int) -> int:
3+
cnt = Counter()
4+
n = len(arrivals)
5+
marked = [0] * n
6+
ans = 0
7+
for i, x in enumerate(arrivals):
8+
if i >= w:
9+
cnt[arrivals[i - w]] -= marked[i - w]
10+
if cnt[x] >= m:
11+
ans += 1
12+
else:
13+
marked[i] = 1
14+
cnt[x] += 1
15+
return ans
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function minArrivalsToDiscard(arrivals: number[], w: number, m: number): number {
2+
const cnt = new Map<number, number>();
3+
const n = arrivals.length;
4+
const marked = Array<number>(n).fill(0);
5+
let ans = 0;
6+
7+
for (let i = 0; i < n; i++) {
8+
const x = arrivals[i];
9+
if (i >= w) {
10+
cnt.set(arrivals[i - w], (cnt.get(arrivals[i - w]) || 0) - marked[i - w]);
11+
}
12+
if ((cnt.get(x) || 0) >= m) {
13+
ans++;
14+
} else {
15+
marked[i] = 1;
16+
cnt.set(x, (cnt.get(x) || 0) + 1);
17+
}
18+
}
19+
return ans;
20+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /