Skip to content

Commit f226f16

Browse files
committed
一刷846
1 parent 0556dc4 commit f226f16

5 files changed

Lines changed: 102 additions & 38 deletions

File tree

README.adoc

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

5950-
//|{counter:codes}
5951-
//|{leetcode_base_url}/hand-of-straights/[846. Hand of Straights^]
5952-
//|{source_base_url}/_0846_HandOfStraights.java[Java]
5953-
//|{doc_base_url}/0846-hand-of-straights.adoc[题解]
5954-
//|Medium
5955-
//|
5956-
//
5950+
|{counter:codes}
5951+
|{leetcode_base_url}/hand-of-straights/[846. Hand of Straights^]
5952+
|{source_base_url}/_0846_HandOfStraights.java[Java]
5953+
|{doc_base_url}/0846-hand-of-straights.adoc[题解]
5954+
|Medium
5955+
|
5956+
59575957
//|{counter:codes}
59585958
//|{leetcode_base_url}/shortest-path-visiting-all-nodes/[847. Shortest Path Visiting All Nodes^]
59595959
//|{source_base_url}/_0847_ShortestPathVisitingAllNodes.java[Java]

docs/0846-hand-of-straights.adoc

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,68 @@
11
[#0846-hand-of-straights]
2-
= 846. Hand of Straights
2+
= 846. 一手顺子
33

4-
{leetcode}/problems/hand-of-straights/[LeetCode - Hand of Straights^]
4+
https://leetcode.cn/problems/hand-of-straights/[LeetCode - 846. 一手顺子^]
55

6-
Alice has a `hand` of cards, given as an array of integers.
6+
Alice 手中有一把牌,她想要重新排列这些牌,分成若干组,使每一组的牌数都是 `groupSize`,并且由 `groupSize` 张连续的牌组成。
77

8-
Now she wants to rearrange the cards into groups so that each group is size `W`, and consists of `W` consecutive cards.
8+
给你一个整数数组 `hand` 其中 `hand[i]` 是写在第 `i` 张牌上的**数值**。如果她可能重新排列这些牌,返回 `true` ;否则,返回 `false`
99

10-
Return `true` if and only if she can.
10+
*示例 1:*
1111

12-
12+
....
13+
输入:hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
14+
输出:true
15+
解释:Alice 手中的牌可以被重新排列为 [1,2,3],[2,3,4],[6,7,8]。
16+
....
1317

18+
*示例 2:*
1419

20+
....
21+
输入:hand = [1,2,3,4,5], groupSize = 4
22+
输出:false
23+
解释:Alice 手中的牌无法被重新排列成几个大小为 4 的组。
24+
....
1525

26+
*提示:*
1627

17-
*Example 1:*
28+
* `1 \<= hand.length \<= 10^4^`
29+
* `0 \<= hand[i] \<= 10^9^`
30+
* `1 \<= groupSize \<= hand.length`
1831
19-
[subs="verbatim,quotes,macros"]
20-
----
21-
*Input:* hand = [1,2,3,6,2,3,4,7,8], W = 3
22-
*Output:* true
23-
*Explanation:* Alice's `hand` can be rearranged as `[1,2,3],[2,3,4],[6,7,8]`.
24-
----
25-
26-
*Example 2:*
27-
28-
[subs="verbatim,quotes,macros"]
29-
----
30-
*Input:* hand = [1,2,3,4,5], W = 4
31-
*Output:* false
32-
*Explanation:* Alice's `hand` can't be rearranged into groups of `4`.
33-
----
32+
**注意:**此题目与 1296 重复:https://leetcode-cn.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
3433

35-
36-
37-
*Note:*
38-
39-
40-
. `1 <= hand.length <= 10000`
41-
. `0 <= hand[i] <= 10^9`
42-
. `1 <= W <= hand.length`
4334

35+
== 思路分析
4436

37+
贪心:将数字根据大小放入到 `TreeMap` 中并计数,每次从 `TreeMap` 中取最小的,然后内循环取从最小开始依次 `groupSize` 个元素,取不到就返回 `false`。
4538

39+
TIP: 感觉更像是一个哈希题,不像贪心题。
4640

4741
[[src-0846]]
42+
[tabs]
43+
====
44+
一刷::
45+
+
46+
--
4847
[{java_src_attr}]
4948
----
5049
include::{sourcedir}/_0846_HandOfStraights.java[tag=answer]
5150
----
51+
--
52+
53+
// 二刷::
54+
// +
55+
// --
56+
// [{java_src_attr}]
57+
// ----
58+
// include::{sourcedir}/_0846_HandOfStraights_2.java[tag=answer]
59+
// ----
60+
// --
61+
====
62+
63+
64+
== 参考资料
5265

66+
. https://leetcode.cn/problems/hand-of-straights/solutions/1179042/yi-shou-shun-zi-by-leetcode-solution-4lwn/[846. 一手顺子 - 官方题解^]
67+
. https://leetcode.cn/problems/hand-of-straights/solutions/1183248/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-4hxw/[846. 一手顺子 - 数据结构模拟题^]
68+
. https://leetcode.cn/problems/hand-of-straights/solutions/1183376/wei-rao-li-lun-mo-ni-dui-ha-xi-ji-shu-by-5qhn/[846. 一手顺子 - 模拟|堆 or 排序 +哈希计数^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ include::0844-backspace-string-compare.adoc[leveloffset=+1]
17841784

17851785
include::0845-longest-mountain-in-array.adoc[leveloffset=+1]
17861786

1787-
// include::0846-hand-of-straights.adoc[leveloffset=+1]
1787+
include::0846-hand-of-straights.adoc[leveloffset=+1]
17881788

17891789
// include::0847-shortest-path-visiting-all-nodes.adoc[leveloffset=+1]
17901790

logbook/202601.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,12 @@ endif::[]
11111111
|{doc_base_url}/0845-longest-mountain-in-array.adoc[题解]
11121112
|✅ 双指针。从前向后遍历,统计递增长度,拐点后,统计递降长度。如果遇到相等或者递降拐点成递增,则从头开始。这个过程,即可统计长度,也可以记录两端山脚的索引。
11131113

1114+
|{counter:codes}
1115+
|{leetcode_base_url}/hand-of-straights/[846. Hand of Straights^]
1116+
|{doc_base_url}/0846-hand-of-straights.adoc[题解]
1117+
|✅ 贪心。将数字根据大小放入到 `TreeMap` 中并计数,每次从 `TreeMap` 中取最小的,然后内循环取从最小开始依次 `groupSize` 个元素。
1118+
1119+
11141120

11151121
|===
11161122

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.Objects;
4+
import java.util.TreeMap;
5+
6+
public class _0846_HandOfStraights {
7+
// tag::answer[]
8+
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2026-08-03 21:37:17
12+
*/
13+
public boolean isNStraightHand(int[] hand, int groupSize) {
14+
if (hand.length % groupSize != 0) {
15+
return false;
16+
}
17+
TreeMap<Integer, Integer> map = new TreeMap<>();
18+
for (int i : hand) {
19+
map.put(i, map.getOrDefault(i, 0) + 1);
20+
}
21+
while (!map.isEmpty()) {
22+
Integer start = map.firstKey();
23+
for (int i = 0; i < groupSize; i++) {
24+
int curr = start + i;
25+
Integer cnt = map.get(curr);
26+
if (Objects.isNull(cnt)) {
27+
return false;
28+
} else if (cnt == 1) {
29+
map.remove(curr);
30+
} else {
31+
map.put(curr, cnt - 1);
32+
}
33+
}
34+
}
35+
return true;
36+
}
37+
38+
// end::answer[]
39+
static void main() {
40+
new _0846_HandOfStraights().isNStraightHand(new int[]{1, 2, 3, 6, 2, 3, 4, 7, 8}, 3);
41+
}
42+
}

0 commit comments

Comments
 (0)