Skip to content

Commit 2277b06

Browse files
authored
Merge pull request #1035 from 0xff-dev/862
Add solution and test-cases for problem 862
2 parents 6821fb3 + 8c22e69 commit 2277b06

File tree

3 files changed

+127
-24
lines changed

3 files changed

+127
-24
lines changed

leetcode/801-900/0862.Shortest-Subarray-with-Sum-at-Least-K/README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [862.Shortest Subarray with Sum at Least K][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 an integer array `nums` and an integer `k`, return the length of the shortest non-empty **subarray** of `nums` with a sum of at least `k`. If there is no such **subarray**, return `-1`.
5+
6+
A **subarray** is a **contiguous** part of an array.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: nums = [1], k = 1
12+
Output: 1
1313
```
1414

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

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Shortest Subarray with Sum at Least K
23-
```go
17+
```
18+
Input: nums = [1,2], k = 4
19+
Output: -1
2420
```
2521

22+
**Example 3:**
23+
24+
```
25+
Input: nums = [2,-1,2], k = 3
26+
Output: 3
27+
```
2628

2729
## 结语
2830

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,80 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import (
4+
"container/heap"
5+
"container/list"
6+
"math"
7+
)
8+
9+
func Solution(nums []int, k int) int {
10+
n := len(nums)
11+
prefixSum := make([]int, n+1)
12+
for i := 0; i < n; i++ {
13+
prefixSum[i+1] = prefixSum[i] + nums[i]
14+
}
15+
16+
minLength := math.MaxInt32
17+
deque := list.New()
18+
19+
for i := 0; i <= n; i++ {
20+
for deque.Len() > 0 && prefixSum[i]-prefixSum[deque.Front().Value.(int)] >= k {
21+
minLength = min(minLength, i-deque.Remove(deque.Front()).(int))
22+
}
23+
24+
for deque.Len() > 0 && prefixSum[i] <= prefixSum[deque.Back().Value.(int)] {
25+
deque.Remove(deque.Back())
26+
}
27+
28+
deque.PushBack(i)
29+
}
30+
31+
if minLength == math.MaxInt32 {
32+
return -1
33+
}
34+
return minLength
35+
}
36+
37+
type heap862 struct {
38+
v, i int
39+
}
40+
type heap862list []heap862
41+
42+
func (h *heap862list) Len() int {
43+
return len(*h)
44+
}
45+
func (h *heap862list) Swap(i, j int) {
46+
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
47+
}
48+
49+
func (h *heap862list) Less(i, j int) bool {
50+
return (*h)[i].v < (*h)[j].v
51+
}
52+
func (h *heap862list) Push(x any) {
53+
*h = append(*h, x.(heap862))
54+
}
55+
func (h *heap862list) Pop() any {
56+
old := *h
57+
l := len(old)
58+
x := old[l-1]
59+
*h = old[:l-1]
460
return x
561
}
62+
63+
func Solution1(nums []int, k int) int {
64+
h := heap862list{{0, -1}}
65+
cur := 0
66+
l := len(nums)
67+
ans := -1
68+
i := 0
69+
for ; i < l; i++ {
70+
cur += nums[i]
71+
for len(h) > 0 && cur-h[0].v >= k {
72+
top := heap.Pop(&h).(heap862)
73+
if ans == -1 || ans > i-top.i {
74+
ans = i - top.i
75+
}
76+
}
77+
heap.Push(&h, heap862{cur, i})
78+
}
79+
return ans
80+
}

leetcode/801-900/0862.Shortest-Subarray-with-Sum-at-Least-K/Solution_test.go

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,56 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
nums []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1}, 1, 1},
18+
{"TestCase2", []int{1, 2}, 4, -1},
19+
{"TestCase3", []int{2, -1, 2}, 3, 3},
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.nums, c.k)
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.nums, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
func TestSolution1(t *testing.T) {
35+
// 测试用例
36+
cases := []struct {
37+
name string
38+
nums []int
39+
k int
40+
expect int
41+
}{
42+
{"TestCase1", []int{1}, 1, 1},
43+
{"TestCase2", []int{1, 2}, 4, -1},
44+
{"TestCase3", []int{2, -1, 2}, 3, 3},
45+
}
46+
47+
// 开始测试
48+
for i, c := range cases {
49+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
50+
got := Solution1(c.nums, c.k)
51+
if !reflect.DeepEqual(got, c.expect) {
52+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
53+
c.expect, got, c.nums, c.k)
54+
}
55+
})
56+
}
57+
}
58+
59+
// 压力测试
3460
func BenchmarkSolution(b *testing.B) {
3561
}
3662

37-
// 使用案列
63+
// 使用案列
3864
func ExampleSolution() {
3965
}

0 commit comments

Comments
 (0)