|
1 | 1 | [#0846-hand-of-straights] |
2 | | -= 846. Hand of Straights |
| 2 | += 846. 一手顺子 |
3 | 3 |
|
4 | | -{leetcode}/problems/hand-of-straights/[LeetCode - Hand of Straights^] |
| 4 | +https://leetcode.cn/problems/hand-of-straights/[LeetCode - 846. 一手顺子^] |
5 | 5 |
|
6 | | -Alice has a `hand` of cards, given as an array of integers. |
| 6 | +Alice 手中有一把牌,她想要重新排列这些牌,分成若干组,使每一组的牌数都是 `groupSize`,并且由 `groupSize` 张连续的牌组成。 |
7 | 7 |
|
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`。 |
9 | 9 |
|
10 | | -Return `true` if and only if she can. |
| 10 | +*示例 1:* |
11 | 11 |
|
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 | +.... |
13 | 17 |
|
| 18 | +*示例 2:* |
14 | 19 |
|
| 20 | +.... |
| 21 | +输入:hand = [1,2,3,4,5], groupSize = 4 |
| 22 | +输出:false |
| 23 | +解释:Alice 手中的牌无法被重新排列成几个大小为 4 的组。 |
| 24 | +.... |
15 | 25 |
|
| 26 | +*提示:* |
16 | 27 |
|
17 | | -*Example 1:* |
| 28 | +* `1 \<= hand.length \<= 10^4^` |
| 29 | +* `0 \<= hand[i] \<= 10^9^` |
| 30 | +* `1 \<= groupSize \<= hand.length` |
18 | 31 |
|
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/ |
34 | 33 |
|
35 | | - |
36 | | - |
37 | | -*Note:* |
38 | | - |
39 | | - |
40 | | -. `1 <= hand.length <= 10000` |
41 | | -. `0 <= hand[i] <= 10^9` |
42 | | -. `1 <= W <= hand.length` |
43 | 34 |
|
| 35 | +== 思路分析 |
44 | 36 |
|
| 37 | +贪心:将数字根据大小放入到 `TreeMap` 中并计数,每次从 `TreeMap` 中取最小的,然后内循环取从最小开始依次 `groupSize` 个元素,取不到就返回 `false`。 |
45 | 38 |
|
| 39 | +TIP: 感觉更像是一个哈希题,不像贪心题。 |
46 | 40 |
|
47 | 41 | [[src-0846]] |
| 42 | +[tabs] |
| 43 | +==== |
| 44 | +一刷:: |
| 45 | ++ |
| 46 | +-- |
48 | 47 | [{java_src_attr}] |
49 | 48 | ---- |
50 | 49 | include::{sourcedir}/_0846_HandOfStraights.java[tag=answer] |
51 | 50 | ---- |
| 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 | +== 参考资料 |
52 | 65 |
|
| 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 排序 +哈希计数^] |
0 commit comments