Skip to content

Commit

Permalink
Create 0846-hand-of-straights.go
Browse files Browse the repository at this point in the history
  • Loading branch information
kkr2 committed Jan 7, 2023
1 parent c1f03e5 commit ec779c5
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions go/0846-hand-of-straights.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
func isNStraightHand(hand []int, groupSize int) bool {

if len(hand)%groupSize != 0 {
return false
}

countMap := make(map[int]int)

for _, num := range hand {
countMap[num]++
}

var minHeap []int

for uniqueNum, _ := range countMap {
minHeap = append(minHeap, uniqueNum)
}

sort.Ints(minHeap)

for len(minHeap) != 0 {
first := minHeap[0]

for i := first; i < groupSize+first; i++ {
if _, ok := countMap[i]; !ok {
return false
}

countMap[i]--
val := countMap[i]

if val == 0 {
if i != minHeap[0] {
return false
}

minHeap = minHeap[1:]
}

}

}
return true

}

0 comments on commit ec779c5

Please sign in to comment.