1
0
Fork 0
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:
Matej Focko 2024-06-16 11:46:41 +02:00
parent d2ef757754
commit 898040fcad
Signed by: mfocko
GPG key ID: 7C47D46246790496
2 changed files with 41 additions and 0 deletions

19
go/patching-array.go Normal file
View 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
View 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))
}