Skip to content

Commit

Permalink
Merge pull request #833 from 0xff-dev/846
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 846
  • Loading branch information
6boris authored Jun 6, 2024
2 parents b8bd73e + 62e50d3 commit 1d8a9d2
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 27 deletions.
25 changes: 11 additions & 14 deletions leetcode/801-900/0846.Hand-of-Straights/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
# [846.Hand of Straights][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
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.

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.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
Output: true
Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]
```

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Hand of Straights
```go
```

Input: hand = [1,2,3,4,5], groupSize = 4
Output: false
Explanation: Alice's hand can not be rearranged into groups of 4.
```

## 结语

Expand Down
72 changes: 70 additions & 2 deletions leetcode/801-900/0846.Hand-of-Straights/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,73 @@
package Solution

func Solution(x bool) bool {
return x
import (
"sort"
)

func Solution(hand []int, groupSize int) bool {
l := len(hand)
if l%groupSize != 0 {
return false
}
if groupSize == 1 {
return true
}

keys := make([]int, 0)
keyCount := make(map[int]int)
for _, n := range hand {
keyCount[n]++
if keyCount[n] == 1 {
keys = append(keys, n)
}
}
sort.Ints(keys)

idx := 1
count := keyCount[keys[0]]
nextIdx := -1
keyCount[keys[0]] = 0
used := 1
l -= count

for idx < len(keys) {
if keys[idx]-keys[idx-1] != 1 {
return false
}
keyCount[keys[idx]] -= count
l -= count
used++
if keyCount[keys[idx]] < 0 {
return false
}
if keyCount[keys[idx]] > 0 && nextIdx == -1 {
nextIdx = idx
}

if used != groupSize {
idx++
continue
}
if nextIdx == -1 {
if idx == len(keys)-1 {
break
}
idx++
count = keyCount[keys[idx]]
nextIdx = -1
keyCount[keys[idx]] = 0
l -= count
used = 1
idx++
continue
}

idx = nextIdx + 1
count = keyCount[keys[nextIdx]]
keyCount[keys[nextIdx]] = 0
nextIdx = -1
l -= count
used = 1
}
return l == 0 && used == groupSize
}
23 changes: 12 additions & 11 deletions leetcode/801-900/0846.Hand-of-Straights/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
hand []int
groupSize int
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 2, 3, 6, 2, 3, 4, 7, 8}, 3, true},
{"TestCase2", []int{1, 2, 3, 4, 5}, 4, false},
{"TestCase3", []int{1, 1, 2, 2, 3, 3}, 2, false},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.hand, c.groupSize)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.hand, c.groupSize)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 1d8a9d2

Please sign in to comment.