Skip to content
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

Add solution and test-cases for problem 862 #1035

Merged
merged 1 commit into from
Nov 17, 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
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# [862.Shortest Subarray with Sum at Least K][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 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`.

A **subarray** is a **contiguous** part of an array.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [1], k = 1
Output: 1
```

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

## 题解

### 思路1
> ...
Shortest Subarray with Sum at Least K
```go
```
Input: nums = [1,2], k = 4
Output: -1
```

**Example 3:**

```
Input: nums = [2,-1,2], k = 3
Output: 3
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,80 @@
package Solution

func Solution(x bool) bool {
import (
"container/heap"
"container/list"
"math"
)

func Solution(nums []int, k int) int {
n := len(nums)
prefixSum := make([]int, n+1)
for i := 0; i < n; i++ {
prefixSum[i+1] = prefixSum[i] + nums[i]
}

minLength := math.MaxInt32
deque := list.New()

for i := 0; i <= n; i++ {
for deque.Len() > 0 && prefixSum[i]-prefixSum[deque.Front().Value.(int)] >= k {
minLength = min(minLength, i-deque.Remove(deque.Front()).(int))
}

for deque.Len() > 0 && prefixSum[i] <= prefixSum[deque.Back().Value.(int)] {
deque.Remove(deque.Back())
}

deque.PushBack(i)
}

if minLength == math.MaxInt32 {
return -1
}
return minLength
}

type heap862 struct {
v, i int
}
type heap862list []heap862

func (h *heap862list) Len() int {
return len(*h)
}
func (h *heap862list) Swap(i, j int) {
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
}

func (h *heap862list) Less(i, j int) bool {
return (*h)[i].v < (*h)[j].v
}
func (h *heap862list) Push(x any) {
*h = append(*h, x.(heap862))
}
func (h *heap862list) Pop() any {
old := *h
l := len(old)
x := old[l-1]
*h = old[:l-1]
return x
}

func Solution1(nums []int, k int) int {
h := heap862list{{0, -1}}
cur := 0
l := len(nums)
ans := -1
i := 0
for ; i < l; i++ {
cur += nums[i]
for len(h) > 0 && cur-h[0].v >= k {
top := heap.Pop(&h).(heap862)
if ans == -1 || ans > i-top.i {
ans = i - top.i
}
}
heap.Push(&h, heap862{cur, i})
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,56 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
nums []int
k int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1}, 1, 1},
{"TestCase2", []int{1, 2}, 4, -1},
{"TestCase3", []int{2, -1, 2}, 3, 3},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.nums, c.k)
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.nums, c.k)
}
})
}
}

// 压力测试
func TestSolution1(t *testing.T) {
// 测试用例
cases := []struct {
name string
nums []int
k int
expect int
}{
{"TestCase1", []int{1}, 1, 1},
{"TestCase2", []int{1, 2}, 4, -1},
{"TestCase3", []int{2, -1, 2}, 3, 3},
}

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

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

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