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 631791e

Browse files
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‎

Lines changed: 79 additions & 4 deletions
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‎

Lines changed: 79 additions & 4 deletions
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 -->
Lines changed: 14 additions & 0 deletions
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+
};
Lines changed: 13 additions & 0 deletions
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+
}
Lines changed: 13 additions & 0 deletions
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+
}
Lines changed: 7 additions & 0 deletions
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]
Lines changed: 10 additions & 0 deletions
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‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 14 additions & 1 deletion
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 -->

0 commit comments

Comments
(0)

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