Hand of Straights
Real-world version: Filing Journal Issues Into Display RunsThe statement here is our own: the same inputs, outputs and technique.
A trolley holds loose journal booklets. You are given hand, where hand[i] is the issue number printed on the i-th booklet; the trolley may well hold several booklets bearing the same issue number.
The booklets have to go out on display in runs. A run is a shelf holding exactly groupSize booklets whose issue numbers are groupSize consecutive whole numbers, with each of those numbers appearing on exactly one booklet of that run.
Return true if every booklet on the trolley can be placed on some run, each booklet used once and no booklet left over, and false otherwise.
Examples
Example 1
- Input
- hand = [8, 6, 7, 9, 10], groupSize = 5
- Output
- true
The five booklets carry the consecutive issue numbers 6 through 10, which is exactly one run of five.
Example 2
- Input
- hand = [4, 4, 5, 5, 6, 6], groupSize = 3
- Output
- true
Two runs of three can be filled, each holding issues 4, 5 and 6, and that uses all six booklets.
Example 3
- Input
- hand = [4, 4, 5, 6], groupSize = 2
- Output
- false
Two runs of two are needed. One of them must hold the pair 4 and 5, which leaves the booklets 4 and 6, and those issue numbers are not consecutive.
Constraints
- 1 <= hand.length <= 10^4
- 0 <= hand[i] <= 10^9
- 1 <= groupSize <= hand.length
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def is_n_straight_hand(hand: list[int], groupSize: int) -> bool:public boolean isNStraightHand(int[] hand, int groupSize)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Make Array Zero by Subtracting Equal AmountsReal-world version: Rounds Needed to Clear Every ReadingEasy
- Task SchedulerReal-world version: Press Die CooldownMedium
- Maximum Palindromes After OperationsReal-world version: Noticeboard Word StripsMedium
- Find Original Array From Doubled ArrayReal-world version: Recover the Base ReadingsMedium