-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandOfStraights.go
62 lines (54 loc) · 1.2 KB
/
handOfStraights.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//go:build ignore
package main
import (
"container/heap"
"fmt"
)
type MinHeap []int
func (h MinHeap) Len() int { return len(h) }
func (h MinHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *MinHeap) Push(x interface{}) {
*h = append(*h, x.(int))
}
func (h *MinHeap) Pop() interface{} {
x := (*h)[len(*h)-1]
*h = (*h)[:len(*h)-1]
return x
}
func isNStraightHand(hand []int, groupSize int) bool {
if len(hand)%groupSize != 0 {
return false
}
count := make(map[int]int)
for _, card := range hand {
count[card]++
}
minHeap := &MinHeap{}
heap.Init(minHeap)
for key := range count {
heap.Push(minHeap, key)
}
for minHeap.Len() > 0 {
firstCard := (*minHeap)[0]
for i := firstCard; i < firstCard+groupSize; i++ {
if count[i] == 0 {
return false
}
count[i]--
if count[i] == 0 {
if i != (*minHeap)[0] {
return false
}
heap.Pop(minHeap)
}
}
}
return true
}
func main() {
hand1, groupSize1 := []int{1, 2, 4, 2, 3, 5, 3, 4}, 4
fmt.Println(isNStraightHand(hand1, groupSize1))
hand2, groupSize2 := []int{1, 2, 3, 3, 4, 5, 6, 7}, 4
fmt.Println(isNStraightHand(hand2, groupSize2))
}