Skip to content

Commit 62e50d3

Browse files
committed
Add solution and test-cases for problem 846
1 parent 09a1dcb commit 62e50d3

File tree

3 files changed

+93
-27
lines changed

3 files changed

+93
-27
lines changed

leetcode/801-900/0846.Hand-of-Straights/README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
# [846.Hand of Straights][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
Given an integer array `hand` where `hand[i]` is the value written on the i<sup>th</sup> card and an integer `groupSize`, return `true` if she can rearrange the cards, or `false` otherwise.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
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]
1314
```
1415

15-
## 题意
16-
> ...
17-
18-
## 题解
16+
**Example 2:**
1917

20-
### 思路1
21-
> ...
22-
Hand of Straights
23-
```go
2418
```
25-
19+
Input: hand = [1,2,3,4,5], groupSize = 4
20+
Output: false
21+
Explanation: Alice's hand can not be rearranged into groups of 4.
22+
```
2623

2724
## 结语
2825

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,73 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"sort"
5+
)
6+
7+
func Solution(hand []int, groupSize int) bool {
8+
l := len(hand)
9+
if l%groupSize != 0 {
10+
return false
11+
}
12+
if groupSize == 1 {
13+
return true
14+
}
15+
16+
keys := make([]int, 0)
17+
keyCount := make(map[int]int)
18+
for _, n := range hand {
19+
keyCount[n]++
20+
if keyCount[n] == 1 {
21+
keys = append(keys, n)
22+
}
23+
}
24+
sort.Ints(keys)
25+
26+
idx := 1
27+
count := keyCount[keys[0]]
28+
nextIdx := -1
29+
keyCount[keys[0]] = 0
30+
used := 1
31+
l -= count
32+
33+
for idx < len(keys) {
34+
if keys[idx]-keys[idx-1] != 1 {
35+
return false
36+
}
37+
keyCount[keys[idx]] -= count
38+
l -= count
39+
used++
40+
if keyCount[keys[idx]] < 0 {
41+
return false
42+
}
43+
if keyCount[keys[idx]] > 0 && nextIdx == -1 {
44+
nextIdx = idx
45+
}
46+
47+
if used != groupSize {
48+
idx++
49+
continue
50+
}
51+
if nextIdx == -1 {
52+
if idx == len(keys)-1 {
53+
break
54+
}
55+
idx++
56+
count = keyCount[keys[idx]]
57+
nextIdx = -1
58+
keyCount[keys[idx]] = 0
59+
l -= count
60+
used = 1
61+
idx++
62+
continue
63+
}
64+
65+
idx = nextIdx + 1
66+
count = keyCount[keys[nextIdx]]
67+
keyCount[keys[nextIdx]] = 0
68+
nextIdx = -1
69+
l -= count
70+
used = 1
71+
}
72+
return l == 0 && used == groupSize
573
}

leetcode/801-900/0846.Hand-of-Straights/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
hand []int
14+
groupSize int
15+
expect bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 2, 3, 6, 2, 3, 4, 7, 8}, 3, true},
18+
{"TestCase2", []int{1, 2, 3, 4, 5}, 4, false},
19+
{"TestCase3", []int{1, 1, 2, 2, 3, 3}, 2, false},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.hand, c.groupSize)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.hand, c.groupSize)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)