Skip to content

Commit c1ce865

Browse files
committedMay 16, 2022
feat: add solutions to lc problem: No.0862
No.0862.Shortest Subarray with Sum at Least K
1 parent 819690e commit c1ce865

File tree

8 files changed

+269
-16
lines changed

8 files changed

+269
-16
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
- [每日温度](/solution/0700-0799/0739.Daily%20Temperatures/README.md) - `单调栈`
5959
- [滑动窗口最大值](/solution/0200-0299/0239.Sliding%20Window%20Maximum/README.md) - `单调队列`
6060
- [满足不等式的最大值](/solution/1400-1499/1499.Max%20Value%20of%20Equation/README.md) - `单调队列`
61+
- [和至少为 K 的最短子数组](/solution/0800-0899/0862.Shortest%20Subarray%20with%20Sum%20at%20Least%20K/README.md) - `单调队列`
6162

6263
### 3. 搜索
6364

‎README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
5656
- [Daily Temperatures](/solution/0700-0799/0739.Daily%20Temperatures/README_EN.md) - `Monotonic Stack`
5757
- [Sliding Window Maximum](/solution/0200-0299/0239.Sliding%20Window%20Maximum/README_EN.md) - `Monotonic Queue`
5858
- [Max Value of Equation](/solution/1400-1499/1499.Max%20Value%20of%20Equation/README_EN.md) - `Monotonic Queue`
59+
- [Shortest Subarray with Sum at Least K](/solution/0800-0899/0862.Shortest%20Subarray%20with%20Sum%20at%20Least%20K/README_EN.md) - `Monotonic Queue`
5960

6061
### 3. Search
6162

‎solution/0800-0899/0862.Shortest Subarray with Sum at Least K/README.md

+96-1
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,117 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:单调队列**
54+
5355
<!-- tabs:start -->
5456

5557
### **Python3**
5658

5759
<!-- 这里可写当前语言的特殊实现逻辑 -->
5860

5961
```python
60-
62+
class Solution:
63+
def shortestSubarray(self, nums: List[int], k: int) -> int:
64+
s = [0] + list(accumulate(nums))
65+
ans = float('inf')
66+
q = deque([0])
67+
for i in range(1, len(s)):
68+
while q and s[i] - s[q[0]] >= k:
69+
ans = min(ans, i - q.popleft())
70+
while q and s[i] <= s[q[-1]]:
71+
q.pop()
72+
q.append(i)
73+
return -1 if ans == float('inf') else ans
6174
```
6275

6376
### **Java**
6477

6578
<!-- 这里可写当前语言的特殊实现逻辑 -->
6679

6780
```java
81+
class Solution {
82+
public int shortestSubarray(int[] nums, int k) {
83+
int n = nums.length;
84+
long[] s = new long[n + 1];
85+
for (int i = 0; i < n; ++i) {
86+
s[i + 1] = s[i] + nums[i];
87+
}
88+
Deque<Integer> q = new ArrayDeque<>();
89+
q.offer(0);
90+
int ans = Integer.MAX_VALUE;
91+
for (int i = 1; i <= n; ++i) {
92+
while (!q.isEmpty() && s[i] - s[q.peek()] >= k) {
93+
ans = Math.min(ans, i - q.poll());
94+
}
95+
while (!q.isEmpty() && s[i] <= s[q.peekLast()]) {
96+
q.pollLast();
97+
}
98+
q.offer(i);
99+
}
100+
return ans == Integer.MAX_VALUE ? -1 : ans;
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
int shortestSubarray(vector<int>& nums, int k) {
111+
int n = nums.size();
112+
vector<long long> s(n + 1);
113+
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i];
114+
deque<int> q{{0}};
115+
int ans = INT_MAX;
116+
for (int i = 1; i <= n; ++i)
117+
{
118+
while (!q.empty() && s[i] - s[q.front()] >= k)
119+
{
120+
ans = min(ans, i - q.front());
121+
q.pop_front();
122+
}
123+
while (!q.empty() && s[i] <= s[q.back()]) q.pop_back();
124+
q.push_back(i);
125+
}
126+
return ans == INT_MAX ? -1 : ans;
127+
}
128+
};
129+
```
68130
131+
### **Go**
132+
133+
```go
134+
func shortestSubarray(nums []int, k int) int {
135+
n := len(nums)
136+
s := make([]int, n+1)
137+
for i, v := range nums {
138+
s[i+1] = s[i] + v
139+
}
140+
q := []int{0}
141+
ans := math.MaxInt32
142+
for i := 1; i <= n; i++ {
143+
for len(q) > 0 && s[i]-s[q[0]] >= k {
144+
ans = min(ans, i-q[0])
145+
q = q[1:]
146+
}
147+
for len(q) > 0 && s[i] <= s[q[len(q)-1]] {
148+
q = q[:len(q)-1]
149+
}
150+
q = append(q, i)
151+
}
152+
if ans == math.MaxInt32 {
153+
return -1
154+
}
155+
return ans
156+
}
157+
158+
func min(a, b int) int {
159+
if a < b {
160+
return a
161+
}
162+
return b
163+
}
69164
```
70165

71166
### **...**

‎solution/0800-0899/0862.Shortest Subarray with Sum at Least K/README_EN.md

+94-1
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,106 @@
3535
### **Python3**
3636

3737
```python
38-
38+
class Solution:
39+
def shortestSubarray(self, nums: List[int], k: int) -> int:
40+
s = [0] + list(accumulate(nums))
41+
ans = float('inf')
42+
q = deque([0])
43+
for i in range(1, len(s)):
44+
while q and s[i] - s[q[0]] >= k:
45+
ans = min(ans, i - q.popleft())
46+
while q and s[i] <= s[q[-1]]:
47+
q.pop()
48+
q.append(i)
49+
return -1 if ans == float('inf') else ans
3950
```
4051

4152
### **Java**
4253

4354
```java
55+
class Solution {
56+
public int shortestSubarray(int[] nums, int k) {
57+
int n = nums.length;
58+
long[] s = new long[n + 1];
59+
for (int i = 0; i < n; ++i) {
60+
s[i + 1] = s[i] + nums[i];
61+
}
62+
Deque<Integer> q = new ArrayDeque<>();
63+
q.offer(0);
64+
int ans = Integer.MAX_VALUE;
65+
for (int i = 1; i <= n; ++i) {
66+
while (!q.isEmpty() && s[i] - s[q.peek()] >= k) {
67+
ans = Math.min(ans, i - q.poll());
68+
}
69+
while (!q.isEmpty() && s[i] <= s[q.peekLast()]) {
70+
q.pollLast();
71+
}
72+
q.offer(i);
73+
}
74+
return ans == Integer.MAX_VALUE ? -1 : ans;
75+
}
76+
}
77+
```
78+
79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
int shortestSubarray(vector<int>& nums, int k) {
85+
int n = nums.size();
86+
vector<long long> s(n + 1);
87+
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i];
88+
deque<int> q{{0}};
89+
int ans = INT_MAX;
90+
for (int i = 1; i <= n; ++i)
91+
{
92+
while (!q.empty() && s[i] - s[q.front()] >= k)
93+
{
94+
ans = min(ans, i - q.front());
95+
q.pop_front();
96+
}
97+
while (!q.empty() && s[i] <= s[q.back()]) q.pop_back();
98+
q.push_back(i);
99+
}
100+
return ans == INT_MAX ? -1 : ans;
101+
}
102+
};
103+
```
44104
105+
### **Go**
106+
107+
```go
108+
func shortestSubarray(nums []int, k int) int {
109+
n := len(nums)
110+
s := make([]int, n+1)
111+
for i, v := range nums {
112+
s[i+1] = s[i] + v
113+
}
114+
q := []int{0}
115+
ans := math.MaxInt32
116+
for i := 1; i <= n; i++ {
117+
for len(q) > 0 && s[i]-s[q[0]] >= k {
118+
ans = min(ans, i-q[0])
119+
q = q[1:]
120+
}
121+
for len(q) > 0 && s[i] <= s[q[len(q)-1]] {
122+
q = q[:len(q)-1]
123+
}
124+
q = append(q, i)
125+
}
126+
if ans == math.MaxInt32 {
127+
return -1
128+
}
129+
return ans
130+
}
131+
132+
func min(a, b int) int {
133+
if a < b {
134+
return a
135+
}
136+
return b
137+
}
45138
```
46139

47140
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int shortestSubarray(vector<int>& nums, int k) {
4+
int n = nums.size();
5+
vector<long long> s(n + 1);
6+
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i];
7+
deque<int> q{{0}};
8+
int ans = INT_MAX;
9+
for (int i = 1; i <= n; ++i)
10+
{
11+
while (!q.empty() && s[i] - s[q.front()] >= k)
12+
{
13+
ans = min(ans, i - q.front());
14+
q.pop_front();
15+
}
16+
while (!q.empty() && s[i] <= s[q.back()]) q.pop_back();
17+
q.push_back(i);
18+
}
19+
return ans == INT_MAX ? -1 : ans;
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
func shortestSubarray(nums []int, k int) int {
2+
n := len(nums)
3+
s := make([]int, n+1)
4+
for i, v := range nums {
5+
s[i+1] = s[i] + v
6+
}
7+
q := []int{0}
8+
ans := math.MaxInt32
9+
for i := 1; i <= n; i++ {
10+
for len(q) > 0 && s[i]-s[q[0]] >= k {
11+
ans = min(ans, i-q[0])
12+
q = q[1:]
13+
}
14+
for len(q) > 0 && s[i] <= s[q[len(q)-1]] {
15+
q = q[:len(q)-1]
16+
}
17+
q = append(q, i)
18+
}
19+
if ans == math.MaxInt32 {
20+
return -1
21+
}
22+
return ans
23+
}
24+
25+
func min(a, b int) int {
26+
if a < b {
27+
return a
28+
}
29+
return b
30+
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
class Solution {
2-
public int shortestSubarray(int[] A, int K) {
3-
int n = A.length;
4-
int[] s = new int[n + 1];
2+
public int shortestSubarray(int[] nums, int k) {
3+
int n = nums.length;
4+
long[] s = new long[n + 1];
55
for (int i = 0; i < n; ++i) {
6-
s[i + 1] = s[i] + A[i];
6+
s[i + 1] = s[i] + nums[i];
77
}
8-
Deque<Integer> deque = new ArrayDeque<>();
9-
deque.offer(0);
10-
int res = Integer.MAX_VALUE;
8+
Deque<Integer> q = new ArrayDeque<>();
9+
q.offer(0);
10+
int ans = Integer.MAX_VALUE;
1111
for (int i = 1; i <= n; ++i) {
12-
while (!deque.isEmpty() && s[i] - s[deque.peekFirst()] >= K) {
13-
res = Math.min(res, i - deque.pollFirst());
12+
while (!q.isEmpty() && s[i] - s[q.peek()] >= k) {
13+
ans = Math.min(ans, i - q.poll());
1414
}
15-
while (!deque.isEmpty() && s[i] <= s[deque.peekLast()]) {
16-
deque.pollLast();
15+
while (!q.isEmpty() && s[i] <= s[q.peekLast()]) {
16+
q.pollLast();
1717
}
18-
deque.offer(i);
18+
q.offer(i);
1919
}
20-
return res != Integer.MAX_VALUE ? res : -1;
20+
return ans == Integer.MAX_VALUE ? -1 : ans;
2121
}
22-
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def shortestSubarray(self, nums: List[int], k: int) -> int:
3+
s = [0] + list(accumulate(nums))
4+
ans = float('inf')
5+
q = deque([0])
6+
for i in range(1, len(s)):
7+
while q and s[i] - s[q[0]] >= k:
8+
ans = min(ans, i - q.popleft())
9+
while q and s[i] <= s[q[-1]]:
10+
q.pop()
11+
q.append(i)
12+
return -1 if ans == float('inf') else ans

0 commit comments

Comments
 (0)