「每日LeetCode」2021年12月30日

本文最后更新于:2023年3月19日 晚上

  1. 一手顺子

846. 一手顺子

Alice 手中有一把牌,她想要重新排列这些牌,分成若干组,使每一组的牌数都是 groupSize ,并且由 groupSize 张连续的牌组成。
给你一个整数数组 hand 其中 hand[i] 是写在第 i 张牌,和一个整数 groupSize 。如果她可能重新排列这些牌,返回 true ;否则,返回 false 。

示例 1:
输入:hand = [1,2,3,6,2,3,4,7,8], groupSize = 3 输出:true 解释:Alice 手中的牌可以被重新排列为 [1,2,3],[2,3,4],[6,7,8]。
示例 2:
输入:hand = [1,2,3,4,5], groupSize = 4 输出:false 解释:Alice 手中的牌无法被重新排列成几个大小为 4 的组。

提示:

  • 1 <= hand.length <= 104
  • 0 <= hand[i] <= 109
  • 1 <= groupSize <= hand.length

注意:此题目与 1296 重复:https://leetcode-cn.com/problems/divide-array-in-sets-of-k-consecutive-numbers/

思路

如果不能除 groupSize,返回 false。用哈希表记录对应的数组和出现的数量。升序排序数组,从小到大,连续的判断哈希表中是否有对应的值,计算以后减 1,没有的时候删掉对应的可以。如果哈希表中没有对应的 key,说明无法构成组,返回 false。

解答

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
var isNStraightHand = function (hand, groupSize) {
const n = hand.length;
if (n % groupSize !== 0) {
return false;
}
hand.sort((a, b) => a - b);
const cnt = new Map();
for (const x of hand) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
for (const x of hand) {
if (!cnt.has(x)) {
continue;
}
for (let j = 0; j < groupSize; j++) {
const num = x + j;
if (!cnt.has(num)) {
return false;
}
cnt.set(num, cnt.get(num) - 1);
if (cnt.get(num) == 0) {
cnt.delete(num);
}
}
}
return true;
};