[LC] 846. Hand of Straights

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].

class Solution {
    public boolean isNStraightHand(int[] hand, int W) {
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for (int num : hand) {
            pq.offer(num);
        }
        while (!pq.isEmpty()) {
            int num = pq.poll();
            for (int i = 1; i < W; i++) {
                if (pq.remove(i + num)) {
                    continue;
                } else {
                    return false;
                }
            }
        }
        return true;
    }
}

 

上一篇:30 Day Challenge Day 16 | Leetcode 692. Top K Frequent Words


下一篇:767. 重构字符串