Skip to content

Add solution and test-cases for problem 330 #908

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

Merged
merged 1 commit into from
Jun 16, 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
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() {
}
Loading