Skip to content

Commit 418ae45

Browse files
committed
Add solution and test-cases for problem 330
1 parent b342e44 commit 418ae45

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

leetcode/301-400/0330.Patching-Array/README.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [330.Patching Array][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
5+
6+
Return the minimum number of patches required.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: nums = [1,3], n = 6
12+
Output: 1
13+
Explanation:
14+
Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4.
15+
Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3].
16+
Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6].
17+
So we only need 1 patch.
1318
```
1419

15-
## 题意
16-
> ...
20+
**Example 2:**
1721

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Patching Array
23-
```go
22+
```
23+
Input: nums = [1,5,10], n = 20
24+
Output: 2
25+
Explanation: The two patches can be [2, 4].
2426
```
2527

28+
**Example 3:**
29+
30+
```
31+
Input: nums = [1,2,2], n = 5
32+
Output: 0
33+
```
2634

2735
## 结语
2836

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, n int) int {
4+
patches := 0
5+
maxReach := 0
6+
i := 0
7+
8+
for maxReach < n {
9+
if i < len(nums) && nums[i] <= maxReach+1 {
10+
maxReach += nums[i]
11+
i++
12+
} else {
13+
maxReach = 2*maxReach + 1
14+
patches++
15+
}
16+
}
17+
18+
return patches
519
}

leetcode/301-400/0330.Patching-Array/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
n int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 3}, 6, 1},
18+
{"TestCase2", []int{1, 5, 10}, 20, 2},
19+
{"TestCase3", []int{1, 2, 2}, 5, 0},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.n)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.n)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)