Skip to content
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

feat: update lc problems #2768

Merged
merged 1 commit into from
May 9, 2024
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
2 changes: 1 addition & 1 deletion lcci/16.11.Diving Board/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class Solution {
if shorter == longer {
return [shorter * k]
}

var ans = [Int](repeating: 0, count: k + 1)
for i in 0...k {
ans[i] = longer * i + shorter * (k - i)
Expand Down
2 changes: 1 addition & 1 deletion lcci/16.11.Diving Board/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class Solution {
if shorter == longer {
return [shorter * k]
}

var ans = [Int](repeating: 0, count: k + 1)
for i in 0...k {
ans[i] = longer * i + shorter * (k - i)
Expand Down
6 changes: 3 additions & 3 deletions lcci/16.14.Best Line/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ class Solution {
let n = points.count
var maxCount = 0
var answer = [Int](repeating: 0, count: 2)

for i in 0..<n {
let x1 = points[i][0], y1 = points[i][1]
for j in i + 1..<n {
let x2 = points[j][0], y2 = points[j][1]
var count = 2

for k in j + 1..<n {
let x3 = points[k][0], y3 = points[k][1]
let a = (y2 - y1) * (x3 - x1)
Expand All @@ -159,7 +159,7 @@ class Solution {
count += 1
}
}

if maxCount < count {
maxCount = count
answer = [i, j]
Expand Down
6 changes: 3 additions & 3 deletions lcci/16.14.Best Line/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ class Solution {
let n = points.count
var maxCount = 0
var answer = [Int](repeating: 0, count: 2)

for i in 0..<n {
let x1 = points[i][0], y1 = points[i][1]
for j in i + 1..<n {
let x2 = points[j][0], y2 = points[j][1]
var count = 2

for k in j + 1..<n {
let x3 = points[k][0], y3 = points[k][1]
let a = (y2 - y1) * (x3 - x1)
Expand All @@ -163,7 +163,7 @@ class Solution {
count += 1
}
}

if maxCount < count {
maxCount = count
answer = [i, j]
Expand Down
6 changes: 3 additions & 3 deletions lcci/16.15.Master Mind/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class Solution {
var y = 0
var cnt1: [Character: Int] = [:]
var cnt2: [Character: Int] = [:]

for i in solution.indices {
let a = solution[i]
let b = guess[i]
Expand All @@ -139,13 +139,13 @@ class Solution {
cnt1[a, default: 0] += 1
cnt2[b, default: 0] += 1
}

let colors = "RYGB"
for c in colors {
let minCount = min(cnt1[c, default: 0], cnt2[c, default: 0])
y += minCount
}

return [x, y - x]
}
}
Expand Down
6 changes: 3 additions & 3 deletions lcci/16.15.Master Mind/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class Solution {
var y = 0
var cnt1: [Character: Int] = [:]
var cnt2: [Character: Int] = [:]

for i in solution.indices {
let a = solution[i]
let b = guess[i]
Expand All @@ -146,13 +146,13 @@ class Solution {
cnt1[a, default: 0] += 1
cnt2[b, default: 0] += 1
}

let colors = "RYGB"
for c in colors {
let minCount = min(cnt1[c, default: 0], cnt2[c, default: 0])
y += minCount
}

return [x, y - x]
}
}
Expand Down
183 changes: 110 additions & 73 deletions solution/2100-2199/2105.Watering Plants II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,63 +72,55 @@

## 解法

### 方法一
### 方法一:双指针 + 模拟

我们用两个变量 $a$ 和 $b$ 分别表示 Alice 和 Bob 的水量,初始时 $a = \text{capacityA}$, $b = \text{capacityB}$。然后用两个指针 $i$ 和 $j$ 分别指向植物数组的头尾,然后模拟 Alice 和 Bob 从两端向中间浇水的过程。

当 $i < j$ 时,我们分别判断 Alice 和 Bob 的水量是否足够浇水,如果不够,我们就重新灌满水罐。然后更新 $a$ 和 $b$ 的水量,同时移动指针 $i$ 和 $j$。最后我们还需要判断 $i$ 和 $j$ 是否相等,如果相等,我们还需要判断 $\max(a, b)$ 是否小于植物的水量,如果小于,我们需要再次重新灌满水罐。

时间复杂度 $O(n)$,其中 $n$ 是植物数组的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

```python
class Solution:
def minimumRefill(self, plants: List[int], capacityA: int, capacityB: int) -> int:
i, j = 0, len(plants) - 1
ans = 0
a, b = capacityA, capacityB
while i <= j:
if i == j:
if max(capacityA, capacityB) < plants[i]:
ans += 1
break
if capacityA < plants[i]:
capacityA = a - plants[i]
ans = 0
i, j = 0, len(plants) - 1
while i < j:
if a < plants[i]:
ans += 1
else:
capacityA -= plants[i]
if capacityB < plants[j]:
capacityB = b - plants[j]
a = capacityA
a -= plants[i]
if b < plants[j]:
ans += 1
else:
capacityB -= plants[j]
i += 1
j -= 1
b = capacityB
b -= plants[j]
i, j = i + 1, j - 1
ans += i == j and max(a, b) < plants[i]
return ans
```

```java
class Solution {
public int minimumRefill(int[] plants, int capacityA, int capacityB) {
int a = capacityA, b = capacityB;
int ans = 0;
int i = 0, j = plants.length - 1;
int ans = 0, a = capacityA, b = capacityB;
while (i <= j) {
if (i == j) {
if (Math.max(capacityA, capacityB) < plants[i]) {
++ans;
}
break;
}
if (capacityA < plants[i]) {
capacityA = a - plants[i];
for (; i < j; ++i, --j) {
if (a < plants[i]) {
++ans;
} else {
capacityA -= plants[i];
a = capacityA;
}
if (capacityB < plants[j]) {
capacityB = b - plants[j];
a -= plants[i];
if (b < plants[j]) {
++ans;
} else {
capacityB -= plants[j];
b = capacityB;
}
++i;
--j;
b -= plants[j];
}
ans += i == j && Math.max(a, b) < plants[i] ? 1 : 0;
return ans;
}
}
Expand All @@ -138,59 +130,104 @@ class Solution {
class Solution {
public:
int minimumRefill(vector<int>& plants, int capacityA, int capacityB) {
int a = capacityA, b = capacityB;
int ans = 0;
int i = 0, j = plants.size() - 1;
int ans = 0, a = capacityA, b = capacityB;
while (i <= j) {
if (i == j) {
if (max(capacityA, capacityB) < plants[i]) ++ans;
break;
}
if (capacityA < plants[i]) {
capacityA = a - plants[i];
for (; i < j; ++i, --j) {
if (a < plants[i]) {
++ans;
} else
capacityA -= plants[i];

if (capacityB < plants[j]) {
capacityB = b - plants[j];
a = capacityA;
}
a -= plants[i];
if (b < plants[j]) {
++ans;
} else
capacityB -= plants[j];
++i;
--j;
b = capacityB;
}
b -= plants[j];
}
ans += i == j && max(a, b) < plants[i];
return ans;
}
};
```

```go
func minimumRefill(plants []int, capacityA int, capacityB int) int {
func minimumRefill(plants []int, capacityA int, capacityB int) (ans int) {
a, b := capacityA, capacityB
i, j := 0, len(plants)-1
ans, a, b := 0, capacityA, capacityB
for i <= j {
if i == j {
if max(capacityA, capacityB) < plants[i] {
ans++
}
break
}
if capacityA < plants[i] {
capacityA = a - plants[i]
for ; i < j; i, j = i+1, j-1 {
if a < plants[i] {
ans++
} else {
capacityA -= plants[i]
a = capacityA
}
if capacityB < plants[j] {
capacityB = b - plants[j]
a -= plants[i]
if b < plants[j] {
ans++
} else {
capacityB -= plants[j]
b = capacityB
}
i++
j--
b -= plants[j]
}
if i == j && max(a, b) < plants[i] {
ans++
}
return ans
return
}
```

```ts
function minimumRefill(plants: number[], capacityA: number, capacityB: number): number {
let [a, b] = [capacityA, capacityB];
let ans = 0;
let [i, j] = [0, plants.length - 1];
for (; i < j; ++i, --j) {
if (a < plants[i]) {
++ans;
a = capacityA;
}
a -= plants[i];
if (b < plants[j]) {
++ans;
b = capacityB;
}
b -= plants[j];
}
ans += i === j && Math.max(a, b) < plants[i] ? 1 : 0;
return ans;
}
```

```rust
impl Solution {
pub fn minimum_refill(plants: Vec<i32>, capacity_a: i32, capacity_b: i32) -> i32 {
let mut a = capacity_a;
let mut b = capacity_b;
let mut ans = 0;
let mut i = 0;
let mut j = plants.len() - 1;

while i < j {
if a < plants[i] {
ans += 1;
a = capacity_a;
}
a -= plants[i];

if b < plants[j] {
ans += 1;
b = capacity_b;
}
b -= plants[j];

i += 1;
j -= 1;
}

if i == j && a.max(b) < plants[i] {
ans += 1;
}

ans
}
}
```

Expand Down
Loading
Loading