Show problems as
Classic problem #846MediumArrayHash TableGreedySorting

Hand of Straights

Real-world version: Filing Journal Issues Into Display Runs
Tracked in this browser only
Read Hand of Straights on LeetCode(opens in a new tab)

The 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.

Python
def is_n_straight_hand(hand: list[int], groupSize: int) -> bool:
Java
public boolean isNStraightHand(int[] hand, int groupSize)