[LeetCode] 846. 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 <= 104
  • 0 <= hand[i] <= 109
  • 1 <= groupSize <= hand.length

Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/

一手顺子。

Alice 手中有一把牌,她想要重新排列这些牌,分成若干组,使每一组的牌数都是 groupSize ,并且由 groupSize 张连续的牌组成。

给你一个整数数组 hand 其中 hand[i] 是写在第 i 张牌上的数值。如果她可能重新排列这些牌,返回 true ;否则,返回 false 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/hand-of-straights
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这个题的思路别想复杂了。首先需要对数组排序,同时需要对数组的所有元素用hashmap进行统计。比如第一个例子,排序之后长这样,

[1,2,3,6,2,3,4,7,8] ->[1,2,2,3,3,4,6,7,8]

然后对于每一个数字 N,我们要找的是是否存在数字 N + 1,同时 N + 1 出现的次数仍然大于 0,如果不满足则直接返回 false。如果满足,则接着去检查 N + 2, N + 3,直到 N + W。

时间O(nlogn)

空间O(n)

Java实现

 1 class Solution {
 2     public boolean isNStraightHand(int[] hand, int W) {
 3         // corner case
 4         if (hand.length % W != 0) {
 5             return false;
 6         }
 7 
 8         // normal case
 9         HashMap<Integer, Integer> map = new HashMap<>();
10         for (int num : hand) {
11             map.put(num, map.getOrDefault(num, 0) + 1);
12         }
13         Arrays.sort(hand);
14         for (int i = 0; i < hand.length; i++) {
15             if (map.get(hand[i]) == 0) {
16                 continue;
17             }
18             int cur = hand[i];
19             int end = cur + W;
20             while (cur < end) {
21                 int count = map.getOrDefault(cur, 0);
22                 if (count == 0) {
23                     return false;
24                 }
25                 map.put(cur, count - 1);
26                 cur++;
27             }
28         }
29         return true;
30     }
31 }

 

JavaScript实现

 1 /**
 2  * @param {number[]} hand
 3  * @param {number} W
 4  * @return {boolean}
 5  */
 6 var isNStraightHand = function (hand, W) {
 7     if (hand.length % W !== 0) {
 8         return false;
 9     }
10 
11     let cards = {};
12     hand.forEach((card) => {
13         cards[card] ? cards[card]++ : (cards[card] = 1);
14     });
15 
16     for (let i = 0; i < hand.length; i++) {
17         if (cards[hand[i] - 1] > 0 || !cards[hand[i]]) {
18             continue;
19         }
20         let min = hand[i];
21         let j = 0;
22         while (j < W) {
23             if (cards[min] > 0) {
24                 cards[min]--;
25                 min++;
26             } else {
27                 return false;
28             }
29             j++;
30         }
31     }
32     return true;
33 };

 

相关题目

659. Split Array into Consecutive Subsequences

846. Hand of Straights

1296. Divide Array in Sets of K Consecutive Numbers

LeetCode 题目总结

posted @ 2020-08-31 14:44  CNoodle  阅读(236)  评论(0编辑  收藏  举报