Skip to content

Commit 8406aa1

Browse files
authored
feat: add solutions to lc problem: No.3477 (#4139)
No.3477.Fruits Into Baskets II
1 parent 7c6fb30 commit 8406aa1

File tree

7 files changed

+257
-8
lines changed

7 files changed

+257
-8
lines changed

solution/3400-3499/3477.Fruits Into Baskets II/README.md

+90-4
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,118 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3477.Fr
8080

8181
<!-- solution:start -->
8282

83-
### 方法一
83+
### 方法一:模拟
84+
85+
我们用一个长度为 $n$ 的布尔数组 $\textit{vis}$ 记录已经被使用的篮子,用一个答案变量 $\textit{ans}$ 记录所有未被放置的水果,初始时 $\textit{ans} = n$。
86+
87+
接下来,我们遍历每一种水果 $x$,对于当前水果,我们遍历所有的篮子,找出第一个未被使用,且容量大于等于 $x$ 的篮子 $i$。如果找到了,那么答案 $\textit{ans}$ 减 $1$。
88+
89+
遍历结束后,返回答案即可。
90+
91+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{fruits}$ 的长度。
8492

8593
<!-- tabs:start -->
8694

8795
#### Python3
8896

8997
```python
90-
98+
class Solution:
99+
def numOfUnplacedFruits(self, fruits: List[int], baskets: List[int]) -> int:
100+
n = len(fruits)
101+
vis = [False] * n
102+
ans = n
103+
for x in fruits:
104+
for i, y in enumerate(baskets):
105+
if y >= x and not vis[i]:
106+
vis[i] = True
107+
ans -= 1
108+
break
109+
return ans
91110
```
92111

93112
#### Java
94113

95114
```java
96-
115+
class Solution {
116+
public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
117+
int n = fruits.length;
118+
boolean[] vis = new boolean[n];
119+
int ans = n;
120+
for (int x : fruits) {
121+
for (int i = 0; i < n; ++i) {
122+
if (baskets[i] >= x && !vis[i]) {
123+
vis[i] = true;
124+
--ans;
125+
break;
126+
}
127+
}
128+
}
129+
return ans;
130+
}
131+
}
97132
```
98133

99134
#### C++
100135

101136
```cpp
102-
137+
class Solution {
138+
public:
139+
int numOfUnplacedFruits(vector<int>& fruits, vector<int>& baskets) {
140+
int n = fruits.size();
141+
vector<bool> vis(n);
142+
int ans = n;
143+
for (int x : fruits) {
144+
for (int i = 0; i < n; ++i) {
145+
if (baskets[i] >= x && !vis[i]) {
146+
vis[i] = true;
147+
--ans;
148+
break;
149+
}
150+
}
151+
}
152+
return ans;
153+
}
154+
};
103155
```
104156
105157
#### Go
106158
107159
```go
160+
func numOfUnplacedFruits(fruits []int, baskets []int) int {
161+
n := len(fruits)
162+
ans := n
163+
vis := make([]bool, n)
164+
for _, x := range fruits {
165+
for i, y := range baskets {
166+
if y >= x && !vis[i] {
167+
vis[i] = true
168+
ans--
169+
break
170+
}
171+
}
172+
}
173+
return ans
174+
}
175+
```
108176

177+
#### TypeScript
178+
179+
```ts
180+
function numOfUnplacedFruits(fruits: number[], baskets: number[]): number {
181+
const n = fruits.length;
182+
const vis: boolean[] = Array(n).fill(false);
183+
let ans = n;
184+
for (const x of fruits) {
185+
for (let i = 0; i < n; ++i) {
186+
if (baskets[i] >= x && !vis[i]) {
187+
vis[i] = true;
188+
--ans;
189+
break;
190+
}
191+
}
192+
}
193+
return ans;
194+
}
109195
```
110196

111197
<!-- tabs:end -->

solution/3400-3499/3477.Fruits Into Baskets II/README_EN.md

+90-4
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,118 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3477.Fr
7878

7979
<!-- solution:start -->
8080

81-
### Solution 1
81+
### Solution 1: Simulation
82+
83+
We use a boolean array $\textit{vis}$ of length $n$ to record the baskets that have already been used, and a variable $\textit{ans}$ to record the number of fruits that have not been placed, initially $\textit{ans} = n$.
84+
85+
Next, we traverse each fruit $x$. For the current fruit, we traverse all the baskets to find the first unused basket $i$ with a capacity greater than or equal to $x$. If found, we decrement $\textit{ans}$ by $1$.
86+
87+
After traversing, we return the answer.
88+
89+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{fruits}$.
8290

8391
<!-- tabs:start -->
8492

8593
#### Python3
8694

8795
```python
88-
96+
class Solution:
97+
def numOfUnplacedFruits(self, fruits: List[int], baskets: List[int]) -> int:
98+
n = len(fruits)
99+
vis = [False] * n
100+
ans = n
101+
for x in fruits:
102+
for i, y in enumerate(baskets):
103+
if y >= x and not vis[i]:
104+
vis[i] = True
105+
ans -= 1
106+
break
107+
return ans
89108
```
90109

91110
#### Java
92111

93112
```java
94-
113+
class Solution {
114+
public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
115+
int n = fruits.length;
116+
boolean[] vis = new boolean[n];
117+
int ans = n;
118+
for (int x : fruits) {
119+
for (int i = 0; i < n; ++i) {
120+
if (baskets[i] >= x && !vis[i]) {
121+
vis[i] = true;
122+
--ans;
123+
break;
124+
}
125+
}
126+
}
127+
return ans;
128+
}
129+
}
95130
```
96131

97132
#### C++
98133

99134
```cpp
100-
135+
class Solution {
136+
public:
137+
int numOfUnplacedFruits(vector<int>& fruits, vector<int>& baskets) {
138+
int n = fruits.size();
139+
vector<bool> vis(n);
140+
int ans = n;
141+
for (int x : fruits) {
142+
for (int i = 0; i < n; ++i) {
143+
if (baskets[i] >= x && !vis[i]) {
144+
vis[i] = true;
145+
--ans;
146+
break;
147+
}
148+
}
149+
}
150+
return ans;
151+
}
152+
};
101153
```
102154
103155
#### Go
104156
105157
```go
158+
func numOfUnplacedFruits(fruits []int, baskets []int) int {
159+
n := len(fruits)
160+
ans := n
161+
vis := make([]bool, n)
162+
for _, x := range fruits {
163+
for i, y := range baskets {
164+
if y >= x && !vis[i] {
165+
vis[i] = true
166+
ans--
167+
break
168+
}
169+
}
170+
}
171+
return ans
172+
}
173+
```
106174

175+
#### TypeScript
176+
177+
```ts
178+
function numOfUnplacedFruits(fruits: number[], baskets: number[]): number {
179+
const n = fruits.length;
180+
const vis: boolean[] = Array(n).fill(false);
181+
let ans = n;
182+
for (const x of fruits) {
183+
for (let i = 0; i < n; ++i) {
184+
if (baskets[i] >= x && !vis[i]) {
185+
vis[i] = true;
186+
--ans;
187+
break;
188+
}
189+
}
190+
}
191+
return ans;
192+
}
107193
```
108194

109195
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int numOfUnplacedFruits(vector<int>& fruits, vector<int>& baskets) {
4+
int n = fruits.size();
5+
vector<bool> vis(n);
6+
int ans = n;
7+
for (int x : fruits) {
8+
for (int i = 0; i < n; ++i) {
9+
if (baskets[i] >= x && !vis[i]) {
10+
vis[i] = true;
11+
--ans;
12+
break;
13+
}
14+
}
15+
}
16+
return ans;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func numOfUnplacedFruits(fruits []int, baskets []int) int {
2+
n := len(fruits)
3+
ans := n
4+
vis := make([]bool, n)
5+
for _, x := range fruits {
6+
for i, y := range baskets {
7+
if y >= x && !vis[i] {
8+
vis[i] = true
9+
ans--
10+
break
11+
}
12+
}
13+
}
14+
return ans
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
3+
int n = fruits.length;
4+
boolean[] vis = new boolean[n];
5+
int ans = n;
6+
for (int x : fruits) {
7+
for (int i = 0; i < n; ++i) {
8+
if (baskets[i] >= x && !vis[i]) {
9+
vis[i] = true;
10+
--ans;
11+
break;
12+
}
13+
}
14+
}
15+
return ans;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def numOfUnplacedFruits(self, fruits: List[int], baskets: List[int]) -> int:
3+
n = len(fruits)
4+
vis = [False] * n
5+
ans = n
6+
for x in fruits:
7+
for i, y in enumerate(baskets):
8+
if y >= x and not vis[i]:
9+
vis[i] = True
10+
ans -= 1
11+
break
12+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function numOfUnplacedFruits(fruits: number[], baskets: number[]): number {
2+
const n = fruits.length;
3+
const vis: boolean[] = Array(n).fill(false);
4+
let ans = n;
5+
for (const x of fruits) {
6+
for (let i = 0; i < n; ++i) {
7+
if (baskets[i] >= x && !vis[i]) {
8+
vis[i] = true;
9+
--ans;
10+
break;
11+
}
12+
}
13+
}
14+
return ans;
15+
}

0 commit comments

Comments
 (0)