Skip to content

Commit 3e60f0b

Browse files
authored
Merge pull request halfrost#220 from gostool/leetcode0846
Leetcode0846
2 parents bf40f42 + e81b962 commit 3e60f0b

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package leetcode
2+
3+
import "sort"
4+
5+
func isNStraightHand(hand []int, groupSize int) bool {
6+
mp := make(map[int]int)
7+
for _, v := range hand {
8+
mp[v] += 1
9+
}
10+
sort.Ints(hand)
11+
for _, num := range hand {
12+
if mp[num] == 0 {
13+
continue
14+
}
15+
for diff := 0; diff < groupSize; diff++ {
16+
if mp[num+diff] == 0 {
17+
return false
18+
}
19+
mp[num+diff] -= 1
20+
}
21+
}
22+
return true
23+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question846 struct {
9+
para846
10+
ans846
11+
}
12+
13+
// para 是参数
14+
type para846 struct {
15+
hand []int
16+
groupSize int
17+
}
18+
19+
// ans 是答案
20+
type ans846 struct {
21+
ans bool
22+
}
23+
24+
func Test_Problem846(t *testing.T) {
25+
26+
qs := []question846{
27+
28+
{
29+
para846{[]int{1, 2, 3, 6, 2, 3, 4, 7, 8}, 3},
30+
ans846{true},
31+
},
32+
33+
{
34+
para846{[]int{1, 2, 3, 4, 5}, 4},
35+
ans846{false},
36+
},
37+
}
38+
39+
fmt.Printf("------------------------Leetcode Problem 846------------------------\n")
40+
41+
for _, q := range qs {
42+
_, p := q.ans846, q.para846
43+
fmt.Printf("【input】:%v 【output】:%v\n", p, isNStraightHand(p.hand, p.groupSize))
44+
}
45+
fmt.Printf("\n\n\n")
46+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# [846. Hand of Straights](https://leetcode-cn.com/problems/hand-of-straights/)
2+
3+
## 题目
4+
5+
Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards.
6+
7+
Given an integer array hand where hand[i] is the value written on the ith card and an integer groupSize, return true if she can rearrange the cards, or false otherwise.
8+
9+
**Example 1**:
10+
11+
Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
12+
Output: true
13+
Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]
14+
15+
**Example 2**:
16+
17+
Input: hand = [1,2,3,4,5], groupSize = 4
18+
Output: false
19+
Explanation: Alice's hand can not be rearranged into groups of 4.
20+
21+
**Constraints:**
22+
23+
- 1 <= hand.length <= 10000
24+
- 0 <= hand[i] <= 1000000000
25+
- 1 <= groupSize <= hand.length
26+
27+
## 题目大意
28+
29+
Alice 手中有一把牌,她想要重新排列这些牌,分成若干组,使每一组的牌数都是 groupSize ,并且由 groupSize 张连续的牌组成。
30+
31+
给你一个整数数组 hand 其中 hand[i] 是写在第 i 张牌,和一个整数 groupSize 。如果她可能重新排列这些牌,返回 true ;否则,返回 false 。
32+
33+
## 解题思路
34+
35+
贪心算法
36+
37+
- 对hand升序排序
38+
- 对hand内数字进行哈希计数(key:数字,value:数量)
39+
- 遍历hand中的数字,以数量大于1的数字作为顺子开头,寻找顺子后续元素,若无法找到完整顺子则返回false
40+
- 所有数字都能找到完整顺子返回true
41+
42+
##代码
43+
44+
```go
45+
package leetcode
46+
47+
import "sort"
48+
49+
func isNStraightHand(hand []int, groupSize int) bool {
50+
mp := make(map[int]int)
51+
for _, v := range hand {
52+
mp[v] += 1
53+
}
54+
sort.Ints(hand)
55+
for _, num := range hand {
56+
if mp[num] == 0 {
57+
continue
58+
}
59+
for diff := 0; diff < groupSize; diff++ {
60+
if mp[num+diff] == 0 {
61+
return false
62+
}
63+
mp[num+diff] -= 1
64+
}
65+
}
66+
return true
67+
}
68+
```

0 commit comments

Comments
 (0)