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; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/72539.html
摘要:題意分出組連續的個元素的數組思路比較簡單,直接循環刪除連續的數組,如此循環反復。 題意:分出n組連續的W個元素的數組 思路:比較簡單,直接循環刪除連續的數組,如此while循環反復。 class Solution(object): def isNStraightHand(self, hand, W): # c = collections.Counter(han...
摘要:如果當前數字代表的整數值已經是所有排列組合中的最大值,則返回當前數字組成的最小值??墒沁@意味著大量無用的數字的生成和比較。一個數字中的各個位上的數如何調整順序才能獲得一個最小的更大值。其次,要保證移動之后,高位以后的值為最小值。 題目要求 Implement next permutation, which rearranges numbers into the lexicographi...
摘要:因為增加高位會帶來更大的增益。所以對于一個長為的序列,我們增加第位的前提是,前位已經達到了最大排列方法。因為是找下一個數,所以我們要找一個比小卻盡可能大的數,所以找到。把換到的位置后,后三位仍然是個降序的排列。 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] ...
閱讀 1307·2021-11-04 16:09
閱讀 3504·2021-10-19 11:45
閱讀 2401·2021-10-11 10:59
閱讀 1015·2021-09-23 11:21
閱讀 2768·2021-09-22 10:54
閱讀 1140·2019-08-30 15:53
閱讀 2610·2019-08-30 15:53
閱讀 3482·2019-08-30 12:57