mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-24 19:04:32 +08:00
Add solution 846
This commit is contained in:
@ -0,0 +1,75 @@
|
||||
# [846. Hand of Straights](https://leetcode.com/problems/hand-of-straights/)
|
||||
|
||||
## 题目
|
||||
|
||||
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 ith card and an integer groupSize, return true if she can rearrange the cards, or false otherwise.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
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**:
|
||||
|
||||
Input: hand = [1,2,3,4,5], groupSize = 4
|
||||
Output: false
|
||||
Explanation: Alice's hand can not be rearranged into groups of 4.
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- 1 <= hand.length <= 10000
|
||||
- 0 <= hand[i] <= 1000000000
|
||||
- 1 <= groupSize <= hand.length
|
||||
|
||||
## 题目大意
|
||||
|
||||
Alice 手中有一把牌,她想要重新排列这些牌,分成若干组,使每一组的牌数都是 groupSize ,并且由 groupSize 张连续的牌组成。
|
||||
|
||||
给你一个整数数组 hand 其中 hand[i] 是写在第 i 张牌,和一个整数 groupSize 。如果她可能重新排列这些牌,返回 true ;否则,返回 false 。
|
||||
|
||||
## 解题思路
|
||||
|
||||
贪心算法
|
||||
|
||||
- 对hand升序排序
|
||||
- 对hand内数字进行哈希计数(key:数字,value:数量)
|
||||
- 遍历hand中的数字,以数量大于1的数字作为顺子开头,寻找顺子后续元素,若无法找到完整顺子则返回false
|
||||
- 所有数字都能找到完整顺子返回true
|
||||
|
||||
##代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
func isNStraightHand(hand []int, groupSize int) bool {
|
||||
mp := make(map[int]int)
|
||||
for _, v := range hand {
|
||||
mp[v] += 1
|
||||
}
|
||||
sort.Ints(hand)
|
||||
for _, num := range hand {
|
||||
if mp[num] == 0 {
|
||||
continue
|
||||
}
|
||||
for diff := 0; diff < groupSize; diff++ {
|
||||
if mp[num+diff] == 0 {
|
||||
return false
|
||||
}
|
||||
mp[num+diff] -= 1
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0845.Longest-Mountain-in-Array/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0850.Rectangle-Area-II/">下一页➡️</a></p>
|
||||
</div>
|
@ -295,6 +295,6 @@ func (sat *SegmentAreaTree) updateInTree(treeIndex, left, right, updateLeft, upd
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0845.Longest-Mountain-in-Array/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0846.Hand-of-Straights/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0851.Loud-and-Rich/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -78,5 +78,5 @@ func clumsy(N int) int {
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1005.Maximize-Sum-Of-Array-After-K-Negations/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1011.Capacity-To-Ship-Packages-Within-D-Days/">下一页➡️</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1009.Complement-of-Base-10-Integer/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
Reference in New Issue
Block a user