LeetCode 0846. Hand of Straights Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
0846. Hand of Straights

Description

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/

Solutions

Solution 1: Hash Table + Sorting

We first check whether the length of the array hand is divisible by groupSize. If it is not, this means that the array cannot be partitioned into multiple subarrays of length groupSize, so we return false.

Next, we use a hash table cnt to count the occurrences of each number in the array hand, and then we sort the array hand.

After that, we iterate over the sorted array hand. For each number x, if cnt[x] ≠ 0, we enumerate every number y from x to x + groupSize - 1. If cnt[y] = 0, it means that we cannot partition the array into multiple subarrays of length groupSize, so we return false. Otherwise, we decrement cnt[y] by 1.

If the iteration completes successfully, it means that the array can be partitioned into multiple valid subarrays, so we return true.

The time complexity is O(n × log n), and the space complexity is O(n), where n is the length of the array hand.

PythonJavaC++GoTypeScript
class Solution: def isNStraightHand(self, hand: List[int], groupSize: int) -> bool: if len(hand) % groupSize: return False cnt = Counter(hand) for x in sorted(hand): if cnt[x]: for y in range(x, x + groupSize): if cnt[y] == 0: return False cnt[y] -= 1 return True(code-box)

Solution 2: Ordered Set

Similar to Solution 1, we first check whether the length of the array hand is divisible by groupSize. If it is not, this means that the array cannot be partitioned into multiple subarrays of length groupSize, so we return false.

Next, we use an ordered set sd to count the occurrences of each number in the array hand.

Then, we repeatedly take the smallest value x from the ordered set and enumerate each number y from x to x + groupSize - 1. If all these numbers appear at least once in the ordered set, we decrement their occurrence count by 1. If any count reaches 0, we remove that number from the ordered set. Otherwise, if we encounter a number that does not exist in the ordered set, it means that the array cannot be partitioned into valid subarrays, so we return false. If the iteration completes successfully, it means that the array can be partitioned into multiple valid subarrays, so we return true.

The time complexity is O(n × log n), and the space complexity is O(n), where n is the length of the array hand.

PythonJavaC++GoTypeScript
class Solution: def isNStraightHand(self, hand: List[int], groupSize: int) -> bool: if len(hand) % groupSize: return False cnt = Counter(hand) sd = SortedDict(cnt) while sd: x = next(iter(sd)) for y in range(x, x + groupSize): if y not in sd: return False if sd[y] == 1: del sd[y] else: sd[y] -= 1 return True(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !