Skip to content

[pull] main from doocs:main #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 90 additions & 4 deletions solution/3400-3499/3477.Fruits Into Baskets II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,118 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3477.Fr

<!-- solution:start -->

### 方法一
### 方法一:模拟

我们用一个长度为 $n$ 的布尔数组 $\textit{vis}$ 记录已经被使用的篮子,用一个答案变量 $\textit{ans}$ 记录所有未被放置的水果,初始时 $\textit{ans} = n$。

接下来,我们遍历每一种水果 $x$,对于当前水果,我们遍历所有的篮子,找出第一个未被使用,且容量大于等于 $x$ 的篮子 $i$。如果找到了,那么答案 $\textit{ans}$ 减 $1$。

遍历结束后,返回答案即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{fruits}$ 的长度。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def numOfUnplacedFruits(self, fruits: List[int], baskets: List[int]) -> int:
n = len(fruits)
vis = [False] * n
ans = n
for x in fruits:
for i, y in enumerate(baskets):
if y >= x and not vis[i]:
vis[i] = True
ans -= 1
break
return ans
```

#### Java

```java

class Solution {
public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
int n = fruits.length;
boolean[] vis = new boolean[n];
int ans = n;
for (int x : fruits) {
for (int i = 0; i < n; ++i) {
if (baskets[i] >= x && !vis[i]) {
vis[i] = true;
--ans;
break;
}
}
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int numOfUnplacedFruits(vector<int>& fruits, vector<int>& baskets) {
int n = fruits.size();
vector<bool> vis(n);
int ans = n;
for (int x : fruits) {
for (int i = 0; i < n; ++i) {
if (baskets[i] >= x && !vis[i]) {
vis[i] = true;
--ans;
break;
}
}
}
return ans;
}
};
```

#### Go

```go
func numOfUnplacedFruits(fruits []int, baskets []int) int {
n := len(fruits)
ans := n
vis := make([]bool, n)
for _, x := range fruits {
for i, y := range baskets {
if y >= x && !vis[i] {
vis[i] = true
ans--
break
}
}
}
return ans
}
```

#### TypeScript

```ts
function numOfUnplacedFruits(fruits: number[], baskets: number[]): number {
const n = fruits.length;
const vis: boolean[] = Array(n).fill(false);
let ans = n;
for (const x of fruits) {
for (let i = 0; i < n; ++i) {
if (baskets[i] >= x && !vis[i]) {
vis[i] = true;
--ans;
break;
}
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
94 changes: 90 additions & 4 deletions solution/3400-3499/3477.Fruits Into Baskets II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,118 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3477.Fr

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

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$.

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$.

After traversing, we return the answer.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{fruits}$.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def numOfUnplacedFruits(self, fruits: List[int], baskets: List[int]) -> int:
n = len(fruits)
vis = [False] * n
ans = n
for x in fruits:
for i, y in enumerate(baskets):
if y >= x and not vis[i]:
vis[i] = True
ans -= 1
break
return ans
```

#### Java

```java

class Solution {
public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
int n = fruits.length;
boolean[] vis = new boolean[n];
int ans = n;
for (int x : fruits) {
for (int i = 0; i < n; ++i) {
if (baskets[i] >= x && !vis[i]) {
vis[i] = true;
--ans;
break;
}
}
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int numOfUnplacedFruits(vector<int>& fruits, vector<int>& baskets) {
int n = fruits.size();
vector<bool> vis(n);
int ans = n;
for (int x : fruits) {
for (int i = 0; i < n; ++i) {
if (baskets[i] >= x && !vis[i]) {
vis[i] = true;
--ans;
break;
}
}
}
return ans;
}
};
```

#### Go

```go
func numOfUnplacedFruits(fruits []int, baskets []int) int {
n := len(fruits)
ans := n
vis := make([]bool, n)
for _, x := range fruits {
for i, y := range baskets {
if y >= x && !vis[i] {
vis[i] = true
ans--
break
}
}
}
return ans
}
```

#### TypeScript

```ts
function numOfUnplacedFruits(fruits: number[], baskets: number[]): number {
const n = fruits.length;
const vis: boolean[] = Array(n).fill(false);
let ans = n;
for (const x of fruits) {
for (let i = 0; i < n; ++i) {
if (baskets[i] >= x && !vis[i]) {
vis[i] = true;
--ans;
break;
}
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
18 changes: 18 additions & 0 deletions solution/3400-3499/3477.Fruits Into Baskets II/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int numOfUnplacedFruits(vector<int>& fruits, vector<int>& baskets) {
int n = fruits.size();
vector<bool> vis(n);
int ans = n;
for (int x : fruits) {
for (int i = 0; i < n; ++i) {
if (baskets[i] >= x && !vis[i]) {
vis[i] = true;
--ans;
break;
}
}
}
return ans;
}
};
15 changes: 15 additions & 0 deletions solution/3400-3499/3477.Fruits Into Baskets II/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
func numOfUnplacedFruits(fruits []int, baskets []int) int {
n := len(fruits)
ans := n
vis := make([]bool, n)
for _, x := range fruits {
for i, y := range baskets {
if y >= x && !vis[i] {
vis[i] = true
ans--
break
}
}
}
return ans
}
17 changes: 17 additions & 0 deletions solution/3400-3499/3477.Fruits Into Baskets II/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
int n = fruits.length;
boolean[] vis = new boolean[n];
int ans = n;
for (int x : fruits) {
for (int i = 0; i < n; ++i) {
if (baskets[i] >= x && !vis[i]) {
vis[i] = true;
--ans;
break;
}
}
}
return ans;
}
}
12 changes: 12 additions & 0 deletions solution/3400-3499/3477.Fruits Into Baskets II/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def numOfUnplacedFruits(self, fruits: List[int], baskets: List[int]) -> int:
n = len(fruits)
vis = [False] * n
ans = n
for x in fruits:
for i, y in enumerate(baskets):
if y >= x and not vis[i]:
vis[i] = True
ans -= 1
break
return ans
15 changes: 15 additions & 0 deletions solution/3400-3499/3477.Fruits Into Baskets II/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function numOfUnplacedFruits(fruits: number[], baskets: number[]): number {
const n = fruits.length;
const vis: boolean[] = Array(n).fill(false);
let ans = n;
for (const x of fruits) {
for (let i = 0; i < n; ++i) {
if (baskets[i] >= x && !vis[i]) {
vis[i] = true;
--ans;
break;
}
}
}
return ans;
}
Loading