|
| 1 | +--- |
| 2 | +id: hand-of-straights |
| 3 | +title: 846. Hand of Straights |
| 4 | +sidebar_label: 846. Hand of Straights |
| 5 | +tags: |
| 6 | +- Array |
| 7 | +- Hash Table |
| 8 | +- Greedy |
| 9 | +- Sorting |
| 10 | + |
| 11 | +description: "This is a solution to the 846. Hand of Straights." |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | +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. |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | + ### Examples |
| 20 | +**Example 1:** |
| 21 | +``` |
| 22 | +Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3 |
| 23 | +
|
| 24 | +Output: true |
| 25 | +
|
| 26 | +Explanation: |
| 27 | +Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8] |
| 28 | +``` |
| 29 | + |
| 30 | +**Example 2:** |
| 31 | +``` |
| 32 | +Input: [1,2,3,4,5], groupSize = 4 |
| 33 | +
|
| 34 | +Output: false |
| 35 | +
|
| 36 | +Explanation: |
| 37 | +Alice's hand can not be rearranged into groups of 4. |
| 38 | +``` |
| 39 | + |
| 40 | +### Constraints |
| 41 | +- `1 <= hand.length <= 10^4` |
| 42 | +- `0 <= hand[i] <= 10^9` |
| 43 | +- `1 <= groupSize <= hand.length` |
| 44 | +## Solution for 846. Hand of Straights |
| 45 | + |
| 46 | +The problem is about arranging cards into groups of consecutive sequences. The key observation is that, to form valid groups, each smallest number in the group should lead to groupSize - 1 consecutive numbers following it. If we can always find such sequences starting from the smallest number, then it is possible to arrange the cards into the desired groups. |
| 47 | + |
| 48 | +## Approach |
| 49 | + |
| 50 | +1. **Sort the Cards**: The first step is to sort the cards. Sorting helps us to easily identify and form consecutive sequences. |
| 51 | + |
| 52 | +2. **Find Successors**: For each number in the sorted array, if it hasn't already been used in a group (indicated by a value of -1), try to form a group starting with that number. Use a helper function findSuccessors to check if it is possible to form a valid group starting from the current number. |
| 53 | + |
| 54 | +3. **Mark Used Cards**: Once a card is used in a group, mark it as `-1` to indicate it has been processed. |
| 55 | + |
| 56 | +4. **Check Group Formation**: For each number, use the helper function to see if a group of groupSize consecutive numbers can be formed. If at any point, it is not possible to form such a group, return false. |
| 57 | + |
| 58 | +5. **Completion Check**: If all numbers can be grouped correctly, return true. |
| 59 | + |
| 60 | +### Code in Different Languages |
| 61 | + |
| 62 | +<Tabs> |
| 63 | +<TabItem value="C++" label="C++" default> |
| 64 | +<SolutionAuthor name="@nagalakshmi08"/> |
| 65 | + |
| 66 | +```cpp |
| 67 | +class Solution { |
| 68 | +public: |
| 69 | + bool findSuccessors(vector<int>& hand, int groupSize, int i, int n) { |
| 70 | + int next = hand[i] + 1; |
| 71 | + hand[i] = -1; // Mark as used |
| 72 | + int count = 1; |
| 73 | + i += 1; |
| 74 | + while (i < n && count < groupSize) { |
| 75 | + if (hand[i] == next) { |
| 76 | + next = hand[i] + 1; |
| 77 | + hand[i] = -1; |
| 78 | + count++; |
| 79 | + } |
| 80 | + i++; |
| 81 | + } |
| 82 | + return count == groupSize; |
| 83 | + } |
| 84 | + |
| 85 | + bool isNStraightHand(vector<int>& hand, int groupSize) { |
| 86 | + int n = hand.size(); |
| 87 | + if (n % groupSize != 0) return false; |
| 88 | + std::sort(hand.begin(), hand.end()); |
| 89 | + for (int i = 0; i < n; i++) { |
| 90 | + if (hand[i] >= 0) { |
| 91 | + if (!findSuccessors(hand, groupSize, i, n)) return false; |
| 92 | + } |
| 93 | + } |
| 94 | + return true; |
| 95 | + } |
| 96 | +}; |
| 97 | +``` |
| 98 | +</TabItem> |
| 99 | +<TabItem value="Java" label="Java"> |
| 100 | +<SolutionAuthor name="@nagalakshmi08"/> |
| 101 | +```java |
| 102 | +class Solution { |
| 103 | + public boolean findsucessors(int[] hand, int groupSize, int i, int n) { |
| 104 | + int f = hand[i] + 1; |
| 105 | + hand[i] = -1; |
| 106 | + int count = 1; |
| 107 | + i += 1; |
| 108 | + while (i < n && count < groupSize) { |
| 109 | + if (hand[i] == f) { |
| 110 | + f = hand[i] + 1; |
| 111 | + hand[i] = -1; |
| 112 | + count++; |
| 113 | + } |
| 114 | + i++; |
| 115 | + } |
| 116 | + if (count != groupSize) |
| 117 | + return false; |
| 118 | + else |
| 119 | + return true; |
| 120 | + } |
| 121 | + |
| 122 | + public boolean isNStraightHand(int[] hand, int groupSize) { |
| 123 | + int n = hand.length; |
| 124 | + if (n % groupSize != 0) |
| 125 | + return false; |
| 126 | + Arrays.sort(hand); |
| 127 | + int i = 0; |
| 128 | + for (; i < n; i++) { |
| 129 | + if (hand[i] >= 0) { |
| 130 | + if (!findsucessors(hand, groupSize, i, n)) |
| 131 | + return false; |
| 132 | + } |
| 133 | + } |
| 134 | + return true; |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +</TabItem> |
| 140 | + |
| 141 | +<TabItem value="Python" label="Python"> |
| 142 | +<SolutionAuthor name="@nagalakshmi08"/> |
| 143 | + |
| 144 | +```python |
| 145 | +class Solution(object): |
| 146 | + def find_successors(self, hand, groupSize, i, n): |
| 147 | + next_val = hand[i] + 1 |
| 148 | + hand[i] = -1 # Mark as used |
| 149 | + count = 1 |
| 150 | + i += 1 |
| 151 | + while i < n and count < groupSize: |
| 152 | + if hand[i] == next_val: |
| 153 | + next_val = hand[i] + 1 |
| 154 | + hand[i] = -1 |
| 155 | + count += 1 |
| 156 | + i += 1 |
| 157 | + return count == groupSize |
| 158 | + |
| 159 | + def isNStraightHand(self, hand, groupSize): |
| 160 | + n = len(hand) |
| 161 | + if n % groupSize != 0: |
| 162 | + return False |
| 163 | + hand.sort() |
| 164 | + for i in range(n): |
| 165 | + if hand[i] >= 0: |
| 166 | + if not self.find_successors(hand, groupSize, i, n): |
| 167 | + return False |
| 168 | + return True |
| 169 | +``` |
| 170 | + |
| 171 | +</TabItem> |
| 172 | +</Tabs> |
| 173 | + |
| 174 | +#### Complexity Analysis |
| 175 | + |
| 176 | +- **Time Complexity**: Sorting the array takes $(O(n \log n))$. The subsequent grouping operation in the worst case can take $(O(n \times groupSize))$, leading to an overall time complexity of $(O(n \log n + n \times groupSize))$. |
| 177 | +- **Space Complexity**: IThe space complexity is $(O(1))$ for the in-place operations (aside from the input array). |
| 178 | + |
| 179 | +--- |
| 180 | + |
| 181 | +<h2>Authors:</h2> |
| 182 | + |
| 183 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 184 | +{['nagalakshmi08'].map(username => ( |
| 185 | + <Author key={username} username={username} /> |
| 186 | +))} |
| 187 | +</div> |
0 commit comments