#846

Hand of Straights

Medium Smallest card starts a group

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Decide whether cards can be split into groups of groupSize consecutive values.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: split cards into groups of consecutive values.

Why this works: the smallest remaining card must start a group; take the next values right after it.

Example: [1,2,3,6,2,3,4,7,8], size 3

[1,2,3], [2,3,4], [6,7,8] ✓

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Forgetting the quick check n % groupSize != 0.

Solve on LeetCode ↗
Complexity
  • Time: O(n log n).
  • Space: O(n).
Topic: Greedy
🎬 Episodes 69–73: video coming soon