Alice has a hand of cards, each card has a card number printed on it. Alice wants to rearrange the cards into groups so that each group is of size W, and consists of W consecutive cards.
Given an integer array hand of size n where hand[i] is the value written on the i-th card and an integer W, return true if and only if she can rearrange the cards into groups of size W, or false otherwise.
n is not a multiple of W?
W to form W groups. If not, return false.hand is empty, the result is trivially true. If W is 1, the result is trivially true because each individual card can be its own group.n is not a multiple of W, return false immediately.hand array will allow us to easily check for consecutive sequences.hand array takes (O(n \log n)).Overall, the time complexity is (O(n \log n + n \cdot W)).
from collections import Counter
def isNStraightHand(hand, W):
if len(hand) % W != 0:
return False
count = Counter(hand)
for num in sorted(count):
if count[num] > 0: # Only if the card needs to be part of a group
card_count = count[num]
for i in range(W):
if count[num + i] < card_count:
return False
count[num + i] -= card_count
return True
# Example usage
print(isNStraightHand([1,2,3,6,2,3,4,7,8], 3)) # Expected output: True
print(isNStraightHand([1,2,3,4,5], 4)) # Expected output: False
This code uses a Counter to track the frequency of each card. It then sorts the cards and tries to form groups starting with the smallest card available. If at any point it can’t form a required group, it returns false. If it successfully forms all required groups, it returns true.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?