Skip to content

Commit bde04f3

Browse files
authored
feat: update lc problems (doocs#2768)
1 parent 2fedf84 commit bde04f3

File tree

32 files changed

+539
-258
lines changed

32 files changed

+539
-258
lines changed

lcci/16.11.Diving Board/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class Solution {
121121
if shorter == longer {
122122
return [shorter * k]
123123
}
124-
124+
125125
var ans = [Int](repeating: 0, count: k + 1)
126126
for i in 0...k {
127127
ans[i] = longer * i + shorter * (k - i)

lcci/16.11.Diving Board/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class Solution {
131131
if shorter == longer {
132132
return [shorter * k]
133133
}
134-
134+
135135
var ans = [Int](repeating: 0, count: k + 1)
136136
for i in 0...k {
137137
ans[i] = longer * i + shorter * (k - i)

lcci/16.14.Best Line/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ class Solution {
144144
let n = points.count
145145
var maxCount = 0
146146
var answer = [Int](repeating: 0, count: 2)
147-
147+
148148
for i in 0..<n {
149149
let x1 = points[i][0], y1 = points[i][1]
150150
for j in i + 1..<n {
151151
let x2 = points[j][0], y2 = points[j][1]
152152
var count = 2
153-
153+
154154
for k in j + 1..<n {
155155
let x3 = points[k][0], y3 = points[k][1]
156156
let a = (y2 - y1) * (x3 - x1)
@@ -159,7 +159,7 @@ class Solution {
159159
count += 1
160160
}
161161
}
162-
162+
163163
if maxCount < count {
164164
maxCount = count
165165
answer = [i, j]

lcci/16.14.Best Line/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ class Solution {
148148
let n = points.count
149149
var maxCount = 0
150150
var answer = [Int](repeating: 0, count: 2)
151-
151+
152152
for i in 0..<n {
153153
let x1 = points[i][0], y1 = points[i][1]
154154
for j in i + 1..<n {
155155
let x2 = points[j][0], y2 = points[j][1]
156156
var count = 2
157-
157+
158158
for k in j + 1..<n {
159159
let x3 = points[k][0], y3 = points[k][1]
160160
let a = (y2 - y1) * (x3 - x1)
@@ -163,7 +163,7 @@ class Solution {
163163
count += 1
164164
}
165165
}
166-
166+
167167
if maxCount < count {
168168
maxCount = count
169169
answer = [i, j]

lcci/16.15.Master Mind/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class Solution {
129129
var y = 0
130130
var cnt1: [Character: Int] = [:]
131131
var cnt2: [Character: Int] = [:]
132-
132+
133133
for i in solution.indices {
134134
let a = solution[i]
135135
let b = guess[i]
@@ -139,13 +139,13 @@ class Solution {
139139
cnt1[a, default: 0] += 1
140140
cnt2[b, default: 0] += 1
141141
}
142-
142+
143143
let colors = "RYGB"
144144
for c in colors {
145145
let minCount = min(cnt1[c, default: 0], cnt2[c, default: 0])
146146
y += minCount
147147
}
148-
148+
149149
return [x, y - x]
150150
}
151151
}

lcci/16.15.Master Mind/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class Solution {
136136
var y = 0
137137
var cnt1: [Character: Int] = [:]
138138
var cnt2: [Character: Int] = [:]
139-
139+
140140
for i in solution.indices {
141141
let a = solution[i]
142142
let b = guess[i]
@@ -146,13 +146,13 @@ class Solution {
146146
cnt1[a, default: 0] += 1
147147
cnt2[b, default: 0] += 1
148148
}
149-
149+
150150
let colors = "RYGB"
151151
for c in colors {
152152
let minCount = min(cnt1[c, default: 0], cnt2[c, default: 0])
153153
y += minCount
154154
}
155-
155+
156156
return [x, y - x]
157157
}
158158
}

solution/2100-2199/2105.Watering Plants II/README.md

+110-73
Original file line numberDiff line numberDiff line change
@@ -72,63 +72,55 @@
7272

7373
## 解法
7474

75-
### 方法一
75+
### 方法一:双指针 + 模拟
76+
77+
我们用两个变量 $a$ 和 $b$ 分别表示 Alice 和 Bob 的水量,初始时 $a = \text{capacityA}$, $b = \text{capacityB}$。然后用两个指针 $i$ 和 $j$ 分别指向植物数组的头尾,然后模拟 Alice 和 Bob 从两端向中间浇水的过程。
78+
79+
当 $i < j$ 时,我们分别判断 Alice 和 Bob 的水量是否足够浇水,如果不够,我们就重新灌满水罐。然后更新 $a$ 和 $b$ 的水量,同时移动指针 $i$ 和 $j$。最后我们还需要判断 $i$ 和 $j$ 是否相等,如果相等,我们还需要判断 $\max(a, b)$ 是否小于植物的水量,如果小于,我们需要再次重新灌满水罐。
80+
81+
时间复杂度 $O(n)$,其中 $n$ 是植物数组的长度。空间复杂度 $O(1)$。
7682

7783
<!-- tabs:start -->
7884

7985
```python
8086
class Solution:
8187
def minimumRefill(self, plants: List[int], capacityA: int, capacityB: int) -> int:
82-
i, j = 0, len(plants) - 1
83-
ans = 0
8488
a, b = capacityA, capacityB
85-
while i <= j:
86-
if i == j:
87-
if max(capacityA, capacityB) < plants[i]:
88-
ans += 1
89-
break
90-
if capacityA < plants[i]:
91-
capacityA = a - plants[i]
89+
ans = 0
90+
i, j = 0, len(plants) - 1
91+
while i < j:
92+
if a < plants[i]:
9293
ans += 1
93-
else:
94-
capacityA -= plants[i]
95-
if capacityB < plants[j]:
96-
capacityB = b - plants[j]
94+
a = capacityA
95+
a -= plants[i]
96+
if b < plants[j]:
9797
ans += 1
98-
else:
99-
capacityB -= plants[j]
100-
i += 1
101-
j -= 1
98+
b = capacityB
99+
b -= plants[j]
100+
i, j = i + 1, j - 1
101+
ans += i == j and max(a, b) < plants[i]
102102
return ans
103103
```
104104

105105
```java
106106
class Solution {
107107
public int minimumRefill(int[] plants, int capacityA, int capacityB) {
108+
int a = capacityA, b = capacityB;
109+
int ans = 0;
108110
int i = 0, j = plants.length - 1;
109-
int ans = 0, a = capacityA, b = capacityB;
110-
while (i <= j) {
111-
if (i == j) {
112-
if (Math.max(capacityA, capacityB) < plants[i]) {
113-
++ans;
114-
}
115-
break;
116-
}
117-
if (capacityA < plants[i]) {
118-
capacityA = a - plants[i];
111+
for (; i < j; ++i, --j) {
112+
if (a < plants[i]) {
119113
++ans;
120-
} else {
121-
capacityA -= plants[i];
114+
a = capacityA;
122115
}
123-
if (capacityB < plants[j]) {
124-
capacityB = b - plants[j];
116+
a -= plants[i];
117+
if (b < plants[j]) {
125118
++ans;
126-
} else {
127-
capacityB -= plants[j];
119+
b = capacityB;
128120
}
129-
++i;
130-
--j;
121+
b -= plants[j];
131122
}
123+
ans += i == j && Math.max(a, b) < plants[i] ? 1 : 0;
132124
return ans;
133125
}
134126
}
@@ -138,59 +130,104 @@ class Solution {
138130
class Solution {
139131
public:
140132
int minimumRefill(vector<int>& plants, int capacityA, int capacityB) {
133+
int a = capacityA, b = capacityB;
134+
int ans = 0;
141135
int i = 0, j = plants.size() - 1;
142-
int ans = 0, a = capacityA, b = capacityB;
143-
while (i <= j) {
144-
if (i == j) {
145-
if (max(capacityA, capacityB) < plants[i]) ++ans;
146-
break;
147-
}
148-
if (capacityA < plants[i]) {
149-
capacityA = a - plants[i];
136+
for (; i < j; ++i, --j) {
137+
if (a < plants[i]) {
150138
++ans;
151-
} else
152-
capacityA -= plants[i];
153-
154-
if (capacityB < plants[j]) {
155-
capacityB = b - plants[j];
139+
a = capacityA;
140+
}
141+
a -= plants[i];
142+
if (b < plants[j]) {
156143
++ans;
157-
} else
158-
capacityB -= plants[j];
159-
++i;
160-
--j;
144+
b = capacityB;
145+
}
146+
b -= plants[j];
161147
}
148+
ans += i == j && max(a, b) < plants[i];
162149
return ans;
163150
}
164151
};
165152
```
166153
167154
```go
168-
func minimumRefill(plants []int, capacityA int, capacityB int) int {
155+
func minimumRefill(plants []int, capacityA int, capacityB int) (ans int) {
156+
a, b := capacityA, capacityB
169157
i, j := 0, len(plants)-1
170-
ans, a, b := 0, capacityA, capacityB
171-
for i <= j {
172-
if i == j {
173-
if max(capacityA, capacityB) < plants[i] {
174-
ans++
175-
}
176-
break
177-
}
178-
if capacityA < plants[i] {
179-
capacityA = a - plants[i]
158+
for ; i < j; i, j = i+1, j-1 {
159+
if a < plants[i] {
180160
ans++
181-
} else {
182-
capacityA -= plants[i]
161+
a = capacityA
183162
}
184-
if capacityB < plants[j] {
185-
capacityB = b - plants[j]
163+
a -= plants[i]
164+
if b < plants[j] {
186165
ans++
187-
} else {
188-
capacityB -= plants[j]
166+
b = capacityB
189167
}
190-
i++
191-
j--
168+
b -= plants[j]
169+
}
170+
if i == j && max(a, b) < plants[i] {
171+
ans++
192172
}
193-
return ans
173+
return
174+
}
175+
```
176+
177+
```ts
178+
function minimumRefill(plants: number[], capacityA: number, capacityB: number): number {
179+
let [a, b] = [capacityA, capacityB];
180+
let ans = 0;
181+
let [i, j] = [0, plants.length - 1];
182+
for (; i < j; ++i, --j) {
183+
if (a < plants[i]) {
184+
++ans;
185+
a = capacityA;
186+
}
187+
a -= plants[i];
188+
if (b < plants[j]) {
189+
++ans;
190+
b = capacityB;
191+
}
192+
b -= plants[j];
193+
}
194+
ans += i === j && Math.max(a, b) < plants[i] ? 1 : 0;
195+
return ans;
196+
}
197+
```
198+
199+
```rust
200+
impl Solution {
201+
pub fn minimum_refill(plants: Vec<i32>, capacity_a: i32, capacity_b: i32) -> i32 {
202+
let mut a = capacity_a;
203+
let mut b = capacity_b;
204+
let mut ans = 0;
205+
let mut i = 0;
206+
let mut j = plants.len() - 1;
207+
208+
while i < j {
209+
if a < plants[i] {
210+
ans += 1;
211+
a = capacity_a;
212+
}
213+
a -= plants[i];
214+
215+
if b < plants[j] {
216+
ans += 1;
217+
b = capacity_b;
218+
}
219+
b -= plants[j];
220+
221+
i += 1;
222+
j -= 1;
223+
}
224+
225+
if i == j && a.max(b) < plants[i] {
226+
ans += 1;
227+
}
228+
229+
ans
230+
}
194231
}
195232
```
196233

0 commit comments

Comments
 (0)