|
| 1 | +# [846. Hand of Straights](https://leetcode.com/problems/hand-of-straights/description) |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +title: "Intuition and Approach to Solve 'isNStraightHand' Problem" |
| 6 | +summary: "This article provides a detailed explanation and approach to solving the 'isNStraightHand' problem, including time and space complexity analysis." |
| 7 | +date: "2024-06-06" |
| 8 | +modified_date: "2024-06-06" |
| 9 | +tags: ["algorithm", "javascript", "coding problem", "leetcode"] |
| 10 | +slug: "intuition-and-approach-to-solve-isnstraighthand-problem" |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +# Intuition |
| 17 | + |
| 18 | +To determine if a given hand can be rearranged into groups of consecutive cards of a specified size, we need to ensure that each group contains exactly `groupSize` number of cards and all cards within the group form a straight sequence. The problem can be likened to checking if we can partition the list into several continuous subsequences. |
| 19 | + |
| 20 | +# Approach |
| 21 | + |
| 22 | +1. **Initial Check:** If the total number of cards in hand is not a multiple of `groupSize`, return false immediately as it is impossible to partition the cards into groups of the desired size. |
| 23 | +2. **Frequency Count:** Use a hashmap to count the occurrences of each card. |
| 24 | +3. **Sorting:** Extract the unique cards from the hashmap and sort them. |
| 25 | +4. **Group Formation:** Iterate through the sorted unique cards. For each card, if its frequency is greater than zero, attempt to form a group starting from this card and extending for `groupSize` consecutive cards. If at any point, a required card is not available in the needed quantity, return false. |
| 26 | +5. **Frequency Update:** Decrease the frequency of cards used to form each group. |
| 27 | + |
| 28 | +# Complexity |
| 29 | + |
| 30 | +- **Time complexity:** $$(O(n log n))$$, where \(n\) is the number of cards in the hand. The primary contributors to this complexity are sorting the unique cards and iterating through the cards to form groups. |
| 31 | +- **Space complexity:** $$(O(n))$$ due to the additional storage required for the frequency hashmap. |
| 32 | + |
| 33 | +# Code |
| 34 | + |
| 35 | +```javascript |
| 36 | +/** |
| 37 | + * @param {number[]} hand |
| 38 | + * @param {number} groupSize |
| 39 | + * @return {boolean} |
| 40 | + */ |
| 41 | +function isNStraightHand(hand, groupSize) { |
| 42 | + if (hand.length % groupSize !== 0) return false; |
| 43 | + |
| 44 | + const count = new Map(); |
| 45 | + |
| 46 | + // Count the frequency of each card |
| 47 | + for (let card of hand) { |
| 48 | + count.set(card, (count.get(card) || 0) + 1); |
| 49 | + } |
| 50 | + |
| 51 | + const uniqueCards = Array.from(count.keys()).sort((a, b) => a - b); |
| 52 | + |
| 53 | + for (let card of uniqueCards) { |
| 54 | + if (count.get(card) > 0) { |
| 55 | + const freq = count.get(card); |
| 56 | + for (let i = 0; i < groupSize; i++) { |
| 57 | + const currentCard = card + i; |
| 58 | + if ((count.get(currentCard) || 0) < freq) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + count.set(currentCard, count.get(currentCard) - freq); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return true; |
| 67 | +} |
| 68 | +``` |
0 commit comments