Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 6cf6b47

Browse files
aQuaaQua
aQua
authored and
aQua
committed
862 added
1 parent 3e8e872 commit 6cf6b47

File tree

4 files changed

+120
-23
lines changed

4 files changed

+120
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# [862. Shortest Subarray with Sum at Least K](https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/)
2+
3+
## 题目
4+
5+
Return the length of the shortest, non-empty, contiguoussubarray of A with sum at least K.
6+
7+
If there is no non-empty subarray with sum at least K, return -1.
8+
9+
Example 1:
10+
11+
```text
12+
Input: A = [1], K = 1
13+
Output: 1
14+
```
15+
16+
Example 2:
17+
18+
```text
19+
Input: A = [1,2], K = 4
20+
Output: -1
21+
```
22+
23+
Example 3:
24+
25+
```text
26+
Input: A = [2,-1,2], K = 3
27+
Output: 3
28+
```
29+
30+
Note:
31+
32+
1. 1 <= A.length <= 50000
33+
1. -10 ^ 5<= A[i] <= 10 ^ 5
34+
1. 1 <= K <= 10 ^ 9
35+
36+
## 解题思路
37+
38+
见程序注释
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package problem0862
2+
3+
func shortestSubarray(A []int, K int) int {
4+
5+
return 0
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package problem0862
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
A []int
13+
K int
14+
ans int
15+
}{
16+
17+
{
18+
[]int{1},
19+
1,
20+
1,
21+
},
22+
23+
{
24+
[]int{1, 2},
25+
4,
26+
-1,
27+
},
28+
29+
{
30+
[]int{2, -1, 2},
31+
3,
32+
3,
33+
},
34+
35+
// 可以有多个 testcase
36+
}
37+
38+
func Test_shortestSubarray(t *testing.T) {
39+
ast := assert.New(t)
40+
41+
for _, tc := range tcs {
42+
fmt.Printf("~~%v~~\n", tc)
43+
ast.Equal(tc.ans, shortestSubarray(tc.A, tc.K), "输入:%v", tc)
44+
}
45+
}
46+
47+
func Benchmark_shortestSubarray(b *testing.B) {
48+
for i := 0; i < b.N; i++ {
49+
for _, tc := range tcs {
50+
shortestSubarray(tc.A, tc.K)
51+
}
52+
}
53+
}

leetcode.json

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
3-
"Ranking": 846,
4-
"Updated": "2018-08-06T23:01:39.949100975+08:00",
3+
"Ranking": 843,
4+
"Updated": "2018-08-07T19:19:57.66016619+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 180,
@@ -49,7 +49,7 @@
4949
"ID": 2,
5050
"Title": "Add Two Numbers",
5151
"TitleSlug": "add-two-numbers",
52-
"PassRate": "28%",
52+
"PassRate": "29%",
5353
"Difficulty": "Medium",
5454
"IsAccepted": true,
5555
"IsPaid": false,
@@ -2833,7 +2833,7 @@
28332833
"ID": 234,
28342834
"Title": "Palindrome Linked List",
28352835
"TitleSlug": "palindrome-linked-list",
2836-
"PassRate": "33%",
2836+
"PassRate": "34%",
28372837
"Difficulty": "Easy",
28382838
"IsAccepted": true,
28392839
"IsPaid": false,
@@ -3889,7 +3889,7 @@
38893889
"ID": 322,
38903890
"Title": "Coin Change",
38913891
"TitleSlug": "coin-change",
3892-
"PassRate": "26%",
3892+
"PassRate": "27%",
38933893
"Difficulty": "Medium",
38943894
"IsAccepted": true,
38953895
"IsPaid": false,
@@ -4117,7 +4117,7 @@
41174117
"ID": 341,
41184118
"Title": "Flatten Nested List Iterator",
41194119
"TitleSlug": "flatten-nested-list-iterator",
4120-
"PassRate": "43%",
4120+
"PassRate": "44%",
41214121
"Difficulty": "Medium",
41224122
"IsAccepted": false,
41234123
"IsPaid": false,
@@ -4645,7 +4645,7 @@
46454645
"ID": 385,
46464646
"Title": "Mini Parser",
46474647
"TitleSlug": "mini-parser",
4648-
"PassRate": "31%",
4648+
"PassRate": "30%",
46494649
"Difficulty": "Medium",
46504650
"IsAccepted": true,
46514651
"IsPaid": false,
@@ -5161,7 +5161,7 @@
51615161
"ID": 428,
51625162
"Title": "Serialize and Deserialize N-ary Tree",
51635163
"TitleSlug": "serialize-and-deserialize-n-ary-tree",
5164-
"PassRate": "31%",
5164+
"PassRate": "32%",
51655165
"Difficulty": "Hard",
51665166
"IsAccepted": false,
51675167
"IsPaid": true,
@@ -5197,7 +5197,7 @@
51975197
"ID": 431,
51985198
"Title": "Encode N-ary Tree to Binary Tree",
51995199
"TitleSlug": "encode-n-ary-tree-to-binary-tree",
5200-
"PassRate": "34%",
5200+
"PassRate": "35%",
52015201
"Difficulty": "Hard",
52025202
"IsAccepted": false,
52035203
"IsPaid": true,
@@ -6721,7 +6721,7 @@
67216721
"ID": 558,
67226722
"Title": "Quad Tree Intersection",
67236723
"TitleSlug": "quad-tree-intersection",
6724-
"PassRate": "28%",
6724+
"PassRate": "29%",
67256725
"Difficulty": "Easy",
67266726
"IsAccepted": false,
67276727
"IsPaid": false,
@@ -7105,7 +7105,7 @@
71057105
"ID": 590,
71067106
"Title": "N-ary Tree Postorder Traversal",
71077107
"TitleSlug": "n-ary-tree-postorder-traversal",
7108-
"PassRate": "53%",
7108+
"PassRate": "54%",
71097109
"Difficulty": "Easy",
71107110
"IsAccepted": false,
71117111
"IsPaid": false,
@@ -7717,7 +7717,7 @@
77177717
"ID": 641,
77187718
"Title": "Design Circular Deque",
77197719
"TitleSlug": "design-circular-deque",
7720-
"PassRate": "50%",
7720+
"PassRate": "49%",
77217721
"Difficulty": "Medium",
77227722
"IsAccepted": true,
77237723
"IsPaid": false,
@@ -8425,7 +8425,7 @@
84258425
"ID": 700,
84268426
"Title": "Search in a Binary Search Tree",
84278427
"TitleSlug": "search-in-a-binary-search-tree",
8428-
"PassRate": "48%",
8428+
"PassRate": "49%",
84298429
"Difficulty": "Easy",
84308430
"IsAccepted": true,
84318431
"IsPaid": false,
@@ -8437,7 +8437,7 @@
84378437
"ID": 701,
84388438
"Title": "Insert into a Binary Search Tree",
84398439
"TitleSlug": "insert-into-a-binary-search-tree",
8440-
"PassRate": "44%",
8440+
"PassRate": "45%",
84418441
"Difficulty": "Medium",
84428442
"IsAccepted": true,
84438443
"IsPaid": false,
@@ -8461,7 +8461,7 @@
84618461
"ID": 703,
84628462
"Title": "Kth Largest Element in a Stream",
84638463
"TitleSlug": "kth-largest-element-in-a-stream",
8464-
"PassRate": "26%",
8464+
"PassRate": "27%",
84658465
"Difficulty": "Easy",
84668466
"IsAccepted": false,
84678467
"IsPaid": false,
@@ -8485,7 +8485,7 @@
84858485
"ID": 705,
84868486
"Title": "Design HashSet",
84878487
"TitleSlug": "design-hashset",
8488-
"PassRate": "24%",
8488+
"PassRate": "25%",
84898489
"Difficulty": "Easy",
84908490
"IsAccepted": false,
84918491
"IsPaid": false,
@@ -9661,7 +9661,7 @@
96619661
"ID": 803,
96629662
"Title": "Bricks Falling When Hit",
96639663
"TitleSlug": "bricks-falling-when-hit",
9664-
"PassRate": "23%",
9664+
"PassRate": "22%",
96659665
"Difficulty": "Hard",
96669666
"IsAccepted": true,
96679667
"IsPaid": false,
@@ -9973,7 +9973,7 @@
99739973
"ID": 829,
99749974
"Title": "Consecutive Numbers Sum",
99759975
"TitleSlug": "consecutive-numbers-sum",
9976-
"PassRate": "26%",
9976+
"PassRate": "27%",
99779977
"Difficulty": "Medium",
99789978
"IsAccepted": true,
99799979
"IsPaid": false,
@@ -10153,7 +10153,7 @@
1015310153
"ID": 844,
1015410154
"Title": "Backspace String Compare",
1015510155
"TitleSlug": "backspace-string-compare",
10156-
"PassRate": "45%",
10156+
"PassRate": "44%",
1015710157
"Difficulty": "Easy",
1015810158
"IsAccepted": true,
1015910159
"IsPaid": false,
@@ -10297,7 +10297,7 @@
1029710297
"ID": 856,
1029810298
"Title": "Score of Parentheses",
1029910299
"TitleSlug": "score-of-parentheses",
10300-
"PassRate": "53%",
10300+
"PassRate": "54%",
1030110301
"Difficulty": "Medium",
1030210302
"IsAccepted": true,
1030310303
"IsPaid": false,
@@ -10309,7 +10309,7 @@
1030910309
"ID": 857,
1031010310
"Title": "Minimum Cost to Hire K Workers",
1031110311
"TitleSlug": "minimum-cost-to-hire-k-workers",
10312-
"PassRate": "39%",
10312+
"PassRate": "38%",
1031310313
"Difficulty": "Hard",
1031410314
"IsAccepted": true,
1031510315
"IsPaid": false,
@@ -10633,7 +10633,7 @@
1063310633
"ID": 884,
1063410634
"Title": "Decoded String at Index",
1063510635
"TitleSlug": "decoded-string-at-index",
10636-
"PassRate": "20%",
10636+
"PassRate": "21%",
1063710637
"Difficulty": "Medium",
1063810638
"IsAccepted": false,
1063910639
"IsPaid": false,
@@ -10657,7 +10657,7 @@
1065710657
"ID": 886,
1065810658
"Title": "Reachable Nodes In Subdivided Graph",
1065910659
"TitleSlug": "reachable-nodes-in-subdivided-graph",
10660-
"PassRate": "31%",
10660+
"PassRate": "33%",
1066110661
"Difficulty": "Hard",
1066210662
"IsAccepted": false,
1066310663
"IsPaid": false,

0 commit comments

Comments
 (0)