mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
go: add «330. Patching Array»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
d2ef757754
commit
898040fcad
2 changed files with 41 additions and 0 deletions
19
go/patching-array.go
Normal file
19
go/patching-array.go
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func minPatches(nums []int, n int) int {
|
||||||
|
missing := 1
|
||||||
|
patches := 0
|
||||||
|
|
||||||
|
i := 0
|
||||||
|
for missing <= n {
|
||||||
|
if i < len(nums) && nums[i] <= missing {
|
||||||
|
missing += nums[i]
|
||||||
|
i++
|
||||||
|
} else {
|
||||||
|
missing += missing
|
||||||
|
patches++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return patches
|
||||||
|
}
|
22
go/patching-array_test.go
Normal file
22
go/patching-array_test.go
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_PatchingArray_Example1(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
assert.Equal(1, minPatches([]int{1, 3}, 6))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_PatchingArray_Example2(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
assert.Equal(2, minPatches([]int{1, 5, 10}, 20))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_PatchingArray_Example3(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
assert.Equal(0, minPatches([]int{1, 2, 2}, 5))
|
||||||
|
}
|
Loading…
Reference in a new issue