Problem
Alice has a hand of cards, given as an array of integers.
Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards.
Return true if and only if she can.
Example 1:
Input: hand = [1,2,3,6,2,3,4,7,8], W = 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], W = 4
Output: false
Explanation: Alice"s hand can"t be rearranged into groups of 4.
Note:
1 <= hand.length <= 10000
0 <= hand[i] <= 10^9
1 <= W <= hand.length
class Solution { public boolean isNStraightHand(int[] hand, int W) { int n = hand.length; if (n < W || n%W != 0) return false; int groups = n/W; PriorityQueuepq = new PriorityQueue<>(); for (int card: hand) pq.offer(card); for (int i = 0; i < groups; i++) { int first = pq.poll(); for (int j = 1; j < W; j++) { // if (pq.remove(first+j)) continue; if (pq.contains(first+j)) pq.remove(first+j); else return false; } } return true; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/72539.html
摘要:題意分出組連續(xù)的個元素的數(shù)組思路比較簡單,直接循環(huán)刪除連續(xù)的數(shù)組,如此循環(huán)反復。 題意:分出n組連續(xù)的W個元素的數(shù)組 思路:比較簡單,直接循環(huán)刪除連續(xù)的數(shù)組,如此while循環(huán)反復。 class Solution(object): def isNStraightHand(self, hand, W): # c = collections.Counter(han...
摘要:如果當前數(shù)字代表的整數(shù)值已經(jīng)是所有排列組合中的最大值,則返回當前數(shù)字組成的最小值。可是這意味著大量無用的數(shù)字的生成和比較。一個數(shù)字中的各個位上的數(shù)如何調(diào)整順序才能獲得一個最小的更大值。其次,要保證移動之后,高位以后的值為最小值。 題目要求 Implement next permutation, which rearranges numbers into the lexicographi...
摘要:因為增加高位會帶來更大的增益。所以對于一個長為的序列,我們增加第位的前提是,前位已經(jīng)達到了最大排列方法。因為是找下一個數(shù),所以我們要找一個比小卻盡可能大的數(shù),所以找到。把換到的位置后,后三位仍然是個降序的排列。 Next Permutation Implement next permutation, which rearranges numbers into the lexicogr...
Problem Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especially, this path can be either increasing or decreasing. For example, [1,2,3,4] and [4,3,2,1] ...
閱讀 1314·2021-11-04 16:09
閱讀 3517·2021-10-19 11:45
閱讀 2408·2021-10-11 10:59
閱讀 1022·2021-09-23 11:21
閱讀 2774·2021-09-22 10:54
閱讀 1149·2019-08-30 15:53
閱讀 2618·2019-08-30 15:53
閱讀 3490·2019-08-30 12:57