Skip to content

Commit 67444c4

Browse files
committed
一刷862
1 parent 27a6822 commit 67444c4

8 files changed

Lines changed: 108 additions & 48 deletions

File tree

README.adoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6059,13 +6059,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
60596059
|Medium
60606060
|
60616061

6062-
//|{counter:codes}
6063-
//|{leetcode_base_url}/shortest-subarray-with-sum-at-least-k/[862. Shortest Subarray with Sum at Least K^]
6064-
//|{source_base_url}/_0862_ShortestSubarrayWithSumAtLeastK.java[Java]
6065-
//|{doc_base_url}/0862-shortest-subarray-with-sum-at-least-k.adoc[题解]
6066-
//|Hard
6067-
//|
6068-
//
6062+
|{counter:codes}
6063+
|{leetcode_base_url}/shortest-subarray-with-sum-at-least-k/[862. Shortest Subarray with Sum at Least K^]
6064+
|{source_base_url}/_0862_ShortestSubarrayWithSumAtLeastK.java[Java]
6065+
|{doc_base_url}/0862-shortest-subarray-with-sum-at-least-k.adoc[题解]
6066+
|Hard
6067+
|
6068+
60696069
//|{counter:codes}
60706070
//|{leetcode_base_url}/all-nodes-distance-k-in-binary-tree/[863. All Nodes Distance K in Binary Tree^]
60716071
//|{source_base_url}/_0863_AllNodesDistanceKInBinaryTree.java[Java]
Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,74 @@
11
[#0862-shortest-subarray-with-sum-at-least-k]
2-
= 862. Shortest Subarray with Sum at Least K
2+
= 862. 和至少为 K 的最短子数组
33

4-
{leetcode}/problems/shortest-subarray-with-sum-at-least-k/[LeetCode - Shortest Subarray with Sum at Least K^]
4+
https://leetcode.cn/problems/shortest-subarray-with-sum-at-least-k/[LeetCode - 862. 和至少为 K 的最短子数组^]
55

6-
Return the *length* of the shortest, non-empty, contiguous subarray of `A` with sum at least `K`.
6+
给你一个整数数组 `nums` 和一个整数 `k` ,找出 `nums` 中和至少为 `k`*最短非空子数组* ,并返回该子数组的长度。如果不存在这样的 *子数组* ,返回 `-1`
77

8-
If there is no non-empty subarray with sum at least `K`, return `-1`.
8+
*子数组* 是数组中 *连续* 的一部分。
99

10-
10+
*示例 1:*
1111

12+
....
13+
输入:nums = [1], k = 1
14+
输出:1
15+
....
1216

17+
*示例 2:*
1318

19+
....
20+
输入:nums = [1,2], k = 4
21+
输出:-1
22+
....
1423

24+
*示例 3:*
1525

16-
*Example 1:*
17-
18-
[subs="verbatim,quotes,macros"]
19-
----
20-
*Input:* A = [1], K = 1
21-
*Output:* 1
22-
----
23-
24-
25-
*Example 2:*
26-
27-
[subs="verbatim,quotes,macros"]
28-
----
29-
*Input:* A = [1,2], K = 4
30-
*Output:* -1
31-
----
26+
....
27+
输入:nums = [2,-1,2], k = 3
28+
输出:3
29+
....
3230

31+
*提示:*
3332

34-
*Example 3:*
35-
36-
[subs="verbatim,quotes,macros"]
37-
----
38-
*Input:* A = [2,-1,2], K = 3
39-
*Output:* 3
40-
----
41-
42-
43-
44-
*Note:*
45-
46-
47-
. `1 <= A.length <= 50000`
48-
. `-10 ^ 5 <= A[i] <= 10 ^ 5`
49-
. `1 <= K <= 10 ^ 9`
33+
* `1 \<= nums.length \<= 10^5^`
34+
* `-10^5^ \<= nums[i] \<= 10^5^`
35+
* `1 \<= k \<= 10^9^`
5036
5137
5238
39+
== 思路分析
5340

41+
一脸懵逼!
5442

43+
image::images/0862-10.png[{image_attr}]
5544

45+
image::images/0862-11.png[{image_attr}]
5646

5747
[[src-0862]]
48+
[tabs]
49+
====
50+
一刷::
51+
+
52+
--
5853
[{java_src_attr}]
5954
----
6055
include::{sourcedir}/_0862_ShortestSubarrayWithSumAtLeastK.java[tag=answer]
6156
----
57+
--
58+
59+
// 二刷::
60+
// +
61+
// --
62+
// [{java_src_attr}]
63+
// ----
64+
// include::{sourcedir}/_0862_ShortestSubarrayWithSumAtLeastK_2.java[tag=answer]
65+
// ----
66+
// --
67+
====
68+
69+
70+
== 参考资料
71+
72+
. https://leetcode.cn/problems/shortest-subarray-with-sum-at-least-k/solutions/1925036/liang-zhang-tu-miao-dong-dan-diao-dui-li-9fvh/[862. 和至少为 K 的最短子数组 - 两张图秒懂单调队列^]
73+
. https://leetcode.cn/problems/shortest-subarray-with-sum-at-least-k/solutions/1923445/he-zhi-shao-wei-k-de-zui-duan-zi-shu-zu-57ffq/[862. 和至少为 K 的最短子数组 - 官方题解^]
6274

docs/images/0862-10.png

52.2 KB
Loading

docs/images/0862-11.png

51.6 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1816,7 +1816,7 @@ include::0859-buddy-strings.adoc[leveloffset=+1]
18161816

18171817
include::0861-score-after-flipping-matrix.adoc[leveloffset=+1]
18181818

1819-
// include::0862-shortest-subarray-with-sum-at-least-k.adoc[leveloffset=+1]
1819+
include::0862-shortest-subarray-with-sum-at-least-k.adoc[leveloffset=+1]
18201820

18211821
// include::0863-all-nodes-distance-k-in-binary-tree.adoc[leveloffset=+1]
18221822

logbook/202601.adoc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,22 +1141,26 @@ endif::[]
11411141
|{doc_base_url}/0853-car-fleet.adoc[题解]
11421142
|✅ 排序或单调栈。将车按照位置排序,计算耗时,合并车队。注意:从左向右计算,左边的快车会被右边的慢车挡住路,所以从右向左计算更简单。
11431143

1144-
11451144
|{counter:codes}
11461145
|{leetcode_base_url}/exam-room/[855. 考场就座^]
11471146
|{doc_base_url}/0855-exam-room.adoc[题解]
11481147
|⭕️ 有序集合+优先队列。有序集合保存已选位置,优先队列保存下一个符合要求的位置。思路是OK的,细节还需要再琢磨。
11491148

11501149
|{counter:codes}
1151-
|{leetcode_base_url}/buddy-strings/[859. Buddy Strings^]
1150+
|{leetcode_base_url}/buddy-strings/[859. 亲密字符串^]
11521151
|{doc_base_url}/0859-buddy-strings.adoc[题解]
11531152
|✅ 哈希。注意处理字符串相同的情况。
11541153

11551154
|{counter:codes}
1156-
|{leetcode_base_url}/score-after-flipping-matrix/[861. Score After Flipping Matrix^]
1155+
|{leetcode_base_url}/score-after-flipping-matrix/[861. 翻转矩阵后的得分^]
11571156
|{doc_base_url}/0861-score-after-flipping-matrix.adoc[题解]
11581157
|✅ 贪心。确保最高位为 `1`,确保每一列的 `1` 占多数。
11591158

1159+
|{counter:codes}
1160+
|{leetcode_base_url}/shortest-subarray-with-sum-at-least-k/[862. 和至少为 K 的最短子数组^]
1161+
|{doc_base_url}/0862-shortest-subarray-with-sum-at-least-k.adoc[题解]
1162+
|❌ 前缀和+单调队列。一脸懵逼!
1163+
11601164

11611165
|===
11621166

logbook/corrections.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,11 @@ endif::[]
12731273
|{doc_base_url}/0855-exam-room.adoc[题解]
12741274
|⭕️
12751275

1276+
|{counter:codes}
1277+
|{leetcode_base_url}/shortest-subarray-with-sum-at-least-k/[862. 和至少为 K 的最短子数组^]
1278+
|{doc_base_url}/0862-shortest-subarray-with-sum-at-least-k.adoc[题解]
1279+
|❌
1280+
12761281
|===
12771282

12781283
截止目前,本轮练习一共完成 {corrections} 道题。
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
public class _0862_ShortestSubarrayWithSumAtLeastK {
7+
// tag::answer[]
8+
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2026-08-13 21:00:28
12+
*/
13+
public int shortestSubarray(int[] nums, int k) {
14+
int n = nums.length;
15+
int[] presum = new int[n + 1];
16+
for (int i = 0; i < n; i++) {
17+
presum[i + 1] = presum[i] + nums[i];
18+
}
19+
int result = n + 1;
20+
Deque<Integer> queue = new ArrayDeque<>();
21+
for (int i = 0; i <= n; i++) {
22+
int sum = presum[i];
23+
while (!queue.isEmpty() && sum - presum[queue.peekFirst()] >= k) {
24+
result = Math.min(result, i - queue.pollFirst());
25+
}
26+
while (!queue.isEmpty() && presum[queue.peekLast()] >= sum) {
27+
queue.pollLast();
28+
}
29+
queue.addLast(i);
30+
}
31+
return result > n ? -1 : result;
32+
}
33+
// end::answer[]
34+
static void main() {
35+
new _0862_ShortestSubarrayWithSumAtLeastK()
36+
// .shortestSubarray(new int[]{0, 0, 69, 56, -34}, 91);
37+
.shortestSubarray(new int[]{2, -1, 2}, 3);
38+
}
39+
}

0 commit comments

Comments
 (0)