Data Structures & Algorithms Greedy Algorithms
💡
Exercise 140

Hand of Straights 20 XP Medium

LeetCode Ctrl+Enter Run Ctrl+S Save

Given an array of integers and a group size W, determine if the array can be divided into groups of W consecutive numbers.

Greedy: sort, use a Counter. For each smallest remaining number, try to form a group of W consecutive numbers.

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Check if hand can be rearranged into groups of W consecutive cards.
Sort unique values. For each smallest card, try to use card, card+1, ..., card+W-1. Decrement counts.
🧪 Test Cases
Input
isNStraightHand([1,2,3,6,2,3,4,7,8], 3)
Expected
True
Boolean check
Input
isNStraightHand([1,2,3,4,5], 4)
Expected
False
Boolean check
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.