Skip to content

Commit

Permalink
Add solution and test-cases for problem 330
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Jun 16, 2024
1 parent b342e44 commit 418ae45
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
34 changes: 21 additions & 13 deletions leetcode/301-400/0330.Patching-Array/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
# [330.Patching Array][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
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.

Return the minimum number of patches required.

**Example 1:**

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

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Patching Array
```go
```
Input: nums = [1,5,10], n = 20
Output: 2
Explanation: The two patches can be [2, 4].
```

**Example 3:**

```
Input: nums = [1,2,2], n = 5
Output: 0
```

## 结语

Expand Down
18 changes: 16 additions & 2 deletions leetcode/301-400/0330.Patching-Array/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int, n int) int {
patches := 0
maxReach := 0
i := 0

for maxReach < n {
if i < len(nums) && nums[i] <= maxReach+1 {
maxReach += nums[i]
i++
} else {
maxReach = 2*maxReach + 1
patches++
}
}

return patches
}
21 changes: 11 additions & 10 deletions leetcode/301-400/0330.Patching-Array/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
n int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 3}, 6, 1},
{"TestCase2", []int{1, 5, 10}, 20, 2},
{"TestCase3", []int{1, 2, 2}, 5, 0},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.n)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.n)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 418ae45

Please sign in to comment.