Skip to content

Commit 631791e

Browse files
authored
feat: add solutions to lc problem: No.1884 (doocs#3627)
No.1884.Egg Drop With 2 Eggs and N Floors
1 parent df12304 commit 631791e

File tree

13 files changed

+254
-13
lines changed

13 files changed

+254
-13
lines changed

solution/1800-1899/1884.Egg Drop With 2 Eggs and N Floors/README.md

+79-4
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,107 @@ tags:
6565

6666
<!-- solution:start -->
6767

68-
### 方法一
68+
### 方法一:动态规划
69+
70+
我们定义 $f[i]$ 表示有两枚鸡蛋,在 $i$ 层楼中确定 $f$ 的最小操作次数。初始时 $f[0] = 0$,其余 $f[i] = +\infty$。答案为 $f[n]$。
71+
72+
考虑 $f[i]$,我们可以枚举第一枚鸡蛋从第 $j$ 层楼扔下,其中 $1 \leq j \leq i$,此时有两种情况:
73+
74+
- 鸡蛋碎了,此时我们剩余一枚鸡蛋,需要在 $j - 1$ 层楼中确定 $f$,这需要 $j - 1$ 次操作,因此总操作次数为 $1 + (j - 1)$;
75+
- 鸡蛋没碎,此时我们剩余两枚鸡蛋,需要在 $i - j$ 层楼中确定 $f$,这需要 $f[i - j]$ 次操作,因此总操作次数为 $1 + f[i - j]$。
76+
77+
综上,我们可以得到状态转移方程:
78+
79+
$$
80+
f[i] = \min_{1 \leq j \leq i} \{1 + \max(j - 1, f[i - j])\}
81+
$$
82+
83+
最后,我们返回 $f[n]$ 即可。
84+
85+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为楼层数。
6986

7087
<!-- tabs:start -->
7188

7289
#### Python3
7390

7491
```python
75-
92+
class Solution:
93+
def twoEggDrop(self, n: int) -> int:
94+
f = [0] + [inf] * n
95+
for i in range(1, n + 1):
96+
for j in range(1, i + 1):
97+
f[i] = min(f[i], 1 + max(j - 1, f[i - j]))
98+
return f[n]
7699
```
77100

78101
#### Java
79102

80103
```java
81-
104+
class Solution {
105+
public int twoEggDrop(int n) {
106+
int[] f = new int[n + 1];
107+
Arrays.fill(f, 1 << 29);
108+
f[0] = 0;
109+
for (int i = 1; i <= n; i++) {
110+
for (int j = 1; j <= i; j++) {
111+
f[i] = Math.min(f[i], 1 + Math.max(j - 1, f[i - j]));
112+
}
113+
}
114+
return f[n];
115+
}
116+
}
82117
```
83118

84119
#### C++
85120

86121
```cpp
87-
122+
class Solution {
123+
public:
124+
int twoEggDrop(int n) {
125+
int f[n + 1];
126+
memset(f, 0x3f, sizeof(f));
127+
f[0] = 0;
128+
for (int i = 1; i <= n; i++) {
129+
for (int j = 1; j <= i; j++) {
130+
f[i] = min(f[i], 1 + max(j - 1, f[i - j]));
131+
}
132+
}
133+
return f[n];
134+
}
135+
};
88136
```
89137
90138
#### Go
91139
92140
```go
141+
func twoEggDrop(n int) int {
142+
f := make([]int, n+1)
143+
for i := range f {
144+
f[i] = 1 << 29
145+
}
146+
f[0] = 0
147+
for i := 1; i <= n; i++ {
148+
for j := 1; j <= i; j++ {
149+
f[i] = min(f[i], 1+max(j-1, f[i-j]))
150+
}
151+
}
152+
return f[n]
153+
}
154+
```
93155

156+
#### TypeScript
157+
158+
```ts
159+
function twoEggDrop(n: number): number {
160+
const f: number[] = Array(n + 1).fill(Infinity);
161+
f[0] = 0;
162+
for (let i = 1; i <= n; ++i) {
163+
for (let j = 1; j <= i; ++j) {
164+
f[i] = Math.min(f[i], 1 + Math.max(j - 1, f[i - j]));
165+
}
166+
}
167+
return f[n];
168+
}
94169
```
95170

96171
<!-- tabs:end -->

solution/1800-1899/1884.Egg Drop With 2 Eggs and N Floors/README_EN.md

+79-4
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,107 @@ Regardless of the outcome, it takes at most 14 drops to determine f.
6262

6363
<!-- solution:start -->
6464

65-
### Solution 1
65+
### Solution 1: Dynamic Programming
66+
67+
We define $f[i]$ to represent the minimum number of operations to determine $f$ in $i$ floors with two eggs. Initially, $f[0] = 0$, and the rest $f[i] = +\infty$. The answer is $f[n]$.
68+
69+
Considering $f[i]$, we can enumerate the first egg thrown from the $j$-th floor, where $1 \leq j \leq i$. At this point, there are two cases:
70+
71+
- The egg breaks. At this time, we have one egg left and need to determine $f$ in $j - 1$ floors, which requires $j - 1$ operations. Therefore, the total number of operations is $1 + (j - 1)$;
72+
- The egg does not break. At this time, we have two eggs left and need to determine $f$ in $i - j$ floors, which requires $f[i - j]$ operations. Therefore, the total number of operations is $1 + f[i - j]$.
73+
74+
In summary, we can obtain the state transition equation:
75+
76+
$$
77+
f[i] = \min_{1 \leq j \leq i} \{1 + \max(j - 1, f[i - j])\}
78+
$$
79+
80+
Finally, we return $f[n]$.
81+
82+
The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Where $n$ is the number of floors.
6683

6784
<!-- tabs:start -->
6885

6986
#### Python3
7087

7188
```python
72-
89+
class Solution:
90+
def twoEggDrop(self, n: int) -> int:
91+
f = [0] + [inf] * n
92+
for i in range(1, n + 1):
93+
for j in range(1, i + 1):
94+
f[i] = min(f[i], 1 + max(j - 1, f[i - j]))
95+
return f[n]
7396
```
7497

7598
#### Java
7699

77100
```java
78-
101+
class Solution {
102+
public int twoEggDrop(int n) {
103+
int[] f = new int[n + 1];
104+
Arrays.fill(f, 1 << 29);
105+
f[0] = 0;
106+
for (int i = 1; i <= n; i++) {
107+
for (int j = 1; j <= i; j++) {
108+
f[i] = Math.min(f[i], 1 + Math.max(j - 1, f[i - j]));
109+
}
110+
}
111+
return f[n];
112+
}
113+
}
79114
```
80115

81116
#### C++
82117

83118
```cpp
84-
119+
class Solution {
120+
public:
121+
int twoEggDrop(int n) {
122+
int f[n + 1];
123+
memset(f, 0x3f, sizeof(f));
124+
f[0] = 0;
125+
for (int i = 1; i <= n; i++) {
126+
for (int j = 1; j <= i; j++) {
127+
f[i] = min(f[i], 1 + max(j - 1, f[i - j]));
128+
}
129+
}
130+
return f[n];
131+
}
132+
};
85133
```
86134
87135
#### Go
88136
89137
```go
138+
func twoEggDrop(n int) int {
139+
f := make([]int, n+1)
140+
for i := range f {
141+
f[i] = 1 << 29
142+
}
143+
f[0] = 0
144+
for i := 1; i <= n; i++ {
145+
for j := 1; j <= i; j++ {
146+
f[i] = min(f[i], 1+max(j-1, f[i-j]))
147+
}
148+
}
149+
return f[n]
150+
}
151+
```
90152

153+
#### TypeScript
154+
155+
```ts
156+
function twoEggDrop(n: number): number {
157+
const f: number[] = Array(n + 1).fill(Infinity);
158+
f[0] = 0;
159+
for (let i = 1; i <= n; ++i) {
160+
for (let j = 1; j <= i; ++j) {
161+
f[i] = Math.min(f[i], 1 + Math.max(j - 1, f[i - j]));
162+
}
163+
}
164+
return f[n];
165+
}
91166
```
92167

93168
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int twoEggDrop(int n) {
4+
int f[n + 1];
5+
memset(f, 0x3f, sizeof(f));
6+
f[0] = 0;
7+
for (int i = 1; i <= n; i++) {
8+
for (int j = 1; j <= i; j++) {
9+
f[i] = min(f[i], 1 + max(j - 1, f[i - j]));
10+
}
11+
}
12+
return f[n];
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func twoEggDrop(n int) int {
2+
f := make([]int, n+1)
3+
for i := range f {
4+
f[i] = 1 << 29
5+
}
6+
f[0] = 0
7+
for i := 1; i <= n; i++ {
8+
for j := 1; j <= i; j++ {
9+
f[i] = min(f[i], 1+max(j-1, f[i-j]))
10+
}
11+
}
12+
return f[n]
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int twoEggDrop(int n) {
3+
int[] f = new int[n + 1];
4+
Arrays.fill(f, 1 << 29);
5+
f[0] = 0;
6+
for (int i = 1; i <= n; i++) {
7+
for (int j = 1; j <= i; j++) {
8+
f[i] = Math.min(f[i], 1 + Math.max(j - 1, f[i - j]));
9+
}
10+
}
11+
return f[n];
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def twoEggDrop(self, n: int) -> int:
3+
f = [0] + [inf] * n
4+
for i in range(1, n + 1):
5+
for j in range(1, i + 1):
6+
f[i] = min(f[i], 1 + max(j - 1, f[i - j]))
7+
return f[n]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function twoEggDrop(n: number): number {
2+
const f: number[] = Array(n + 1).fill(Infinity);
3+
f[0] = 0;
4+
for (let i = 1; i <= n; ++i) {
5+
for (let j = 1; j <= i; ++j) {
6+
f[i] = Math.min(f[i], 1 + Math.max(j - 1, f[i - j]));
7+
}
8+
}
9+
return f[n];
10+
}

solution/3100-3199/3171.Find Subarray With Bitwise OR Closest to K/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ tags:
8585

8686
根据题目描述,我们需要求出数组 $\textit{nums}$ 下标 $l$ 到 $r$ 的元素的按位或运算的结果,即 $\textit{nums}[l] \lor \textit{nums}[l + 1] \lor \cdots \lor \textit{nums}[r]$。其中 $\lor$ 表示按位或运算。
8787

88-
如果我们每次固定右端点 $r$,那么左端点 $l$ 的范围是 $[0, r]$。每次移动右端点 $r$,按位或的结果只会变大,我们用一个变量 $s$ 记录当前的按位或的结果,如果 $s$ 大于 $k$,我们就将左端点 $l$ 向右移动,直到 $s$ 小于等于 $k$。在移动左端点 $l$ 的过程中,我们需要维护一个数组 $cnt$,记录当前区间内每个二进制位上 $0$ 的个数,当 $cnt[h]$ 为 $0$ 时,说明当前区间内的元素在第 $h$ 位上都为 $1$,我们就可以将 $s$ 的第 $h$ 位设置为 $0$。
88+
如果我们每次固定右端点 $r$,那么左端点 $l$ 的范围是 $[0, r]$。每次移动右端点 $r$,按位或的结果只会变大,我们用一个变量 $s$ 记录当前的按位或的结果,如果 $s$ 大于 $k$,我们就将左端点 $l$ 向右移动,直到 $s$ 小于等于 $k$。在移动左端点 $l$ 的过程中,我们需要维护一个数组 $cnt$,记录当前区间内每个二进制位上 $0$ 的个数,当 $cnt[h]$ 为 $0$ 时,说明当前区间内的元素在第 $h$ 位上都为 $0$,我们就可以将 $s$ 的第 $h$ 位设置为 $0$。
8989

9090
时间复杂度 $O(n \times \log M)$,空间复杂度 $O(\log M)$。其中 $n$ 和 $M$ 分别是数组 $\textit{nums}$ 的长度和数组 $\textit{nums}$ 中元素的最大值。
9191

solution/3100-3199/3171.Find Subarray With Bitwise OR Closest to K/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ tags:
8383

8484
According to the problem description, we need to calculate the result of the bitwise OR operation of elements from index $l$ to $r$ in the array $\textit{nums}$, that is, $\textit{nums}[l] \lor \textit{nums}[l + 1] \lor \cdots \lor \textit{nums}[r]$, where $\lor$ represents the bitwise OR operation.
8585

86-
If we fix the right endpoint $r$, then the range of the left endpoint $l$ is $[0, r]$. Each time we move the right endpoint $r$, the result of the bitwise OR operation will only increase. We use a variable $s$ to record the current result of the bitwise OR operation. If $s$ is greater than $k$, we move the left endpoint $l$ to the right until $s$ is less than or equal to $k$. During the process of moving the left endpoint $l$, we need to maintain an array $cnt$ to record the number of $0$s on each binary digit in the current interval. When $cnt[h] = 0$, it means that all elements in the current interval have a $1$ on the $h^{th}$ bit, and we can set the $h^{th}$ bit of $s$ to $0$.
86+
If we fix the right endpoint $r$, then the range of the left endpoint $l$ is $[0, r]$. Each time we move the right endpoint $r$, the result of the bitwise OR operation will only increase. We use a variable $s$ to record the current result of the bitwise OR operation. If $s$ is greater than $k$, we move the left endpoint $l$ to the right until $s$ is less than or equal to $k$. During the process of moving the left endpoint $l$, we need to maintain an array $cnt$ to record the number of $0$s on each binary digit in the current interval. When $cnt[h] = 0$, it means that all elements in the current interval have a $0$ on the $h^{th}$ bit, and we can set the $h^{th}$ bit of $s$ to $0$.
8787

8888
The time complexity is $O(n \times \log M)$, and the space complexity is $O(\log M)$. Here, $n$ and $M$ respectively represent the length of the array $\textit{nums}$ and the maximum value in the array $\textit{nums}$.
8989

solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class Solution:
189189
def minimumAverage(self, nums: List[int]) -> float:
190190
nums.sort()
191191
n = len(nums)
192-
return min(nums[i] + nums[n - i - 1] for i in range(n // 2)) / 2
192+
return min(nums[i] + nums[-i - 1] for i in range(n // 2)) / 2
193193
```
194194

195195
#### Java
@@ -252,6 +252,19 @@ function minimumAverage(nums: number[]): number {
252252
}
253253
```
254254

255+
#### Rust
256+
257+
```rust
258+
impl Solution {
259+
pub fn minimum_average(mut nums: Vec<i32>) -> f64 {
260+
nums.sort();
261+
let n = nums.len();
262+
let ans = (0..n / 2).map(|i| nums[i] + nums[n - i - 1]).min().unwrap();
263+
ans as f64 / 2.0
264+
}
265+
}
266+
```
267+
255268
<!-- tabs:end -->
256269

257270
<!-- solution:end -->

solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README_EN.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class Solution:
187187
def minimumAverage(self, nums: List[int]) -> float:
188188
nums.sort()
189189
n = len(nums)
190-
return min(nums[i] + nums[n - i - 1] for i in range(n // 2)) / 2
190+
return min(nums[i] + nums[-i - 1] for i in range(n // 2)) / 2
191191
```
192192

193193
#### Java
@@ -250,6 +250,19 @@ function minimumAverage(nums: number[]): number {
250250
}
251251
```
252252

253+
#### Rust
254+
255+
```rust
256+
impl Solution {
257+
pub fn minimum_average(mut nums: Vec<i32>) -> f64 {
258+
nums.sort();
259+
let n = nums.len();
260+
let ans = (0..n / 2).map(|i| nums[i] + nums[n - i - 1]).min().unwrap();
261+
ans as f64 / 2.0
262+
}
263+
}
264+
```
265+
253266
<!-- tabs:end -->
254267

255268
<!-- solution:end -->

solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ class Solution:
22
def minimumAverage(self, nums: List[int]) -> float:
33
nums.sort()
44
n = len(nums)
5-
return min(nums[i] + nums[n - i - 1] for i in range(n // 2)) / 2
5+
return min(nums[i] + nums[-i - 1] for i in range(n // 2)) / 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
impl Solution {
2+
pub fn minimum_average(mut nums: Vec<i32>) -> f64 {
3+
nums.sort();
4+
let n = nums.len();
5+
let ans = (0..n / 2).map(|i| nums[i] + nums[n - i - 1]).min().unwrap();
6+
ans as f64 / 2.0
7+
}
8+
}

0 commit comments

Comments
 (0)