← DiffPush

Divide Array in Sets of K Consecutive Numbers

Standard Bar Heaps · Medium Problems O(n log n) · O(n)

The Hotel Room Block Allocator

A hotel allocates room blocks to tour groups: each block must be k consecutive room numbers, and every room number in the inventory list must be used exactly once. The allocator walks room numbers in ascending order, and whenever it opens a new block at a room, it must be able to claim the next k-1 rooms without conflict.

Input: An integer array nums of room numbers and an integer k, the block size.

Output: Return true if the inventory can be partitioned into sets of k consecutive numbers, false otherwise.

Constraints

Examples

Example 1

Input: {"nums":[1,2,3,3,4,4,5,6],"k":4}
Output: true
Blocks [1,2,3,4] and [3,4,5,6] consume every room exactly once.

Example 2

Input: {"nums":[1,2,3,4],"k":3}
Output: false
Four rooms cannot split into blocks of three.

Solve this in your browser →

Also on LeetCode ↗