All problems
0597MediumArrayHash TableGreedySorting

Filing Journal Issues Into Display Runs

Tracked in this browser only
Write code

Trains the technique from

LeetCode 846Hand of Straights

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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