414. Partition Linked List into K Consecutive Groups

HardLinked ListGreedyHashLinked List

Given a linked list of integers and an integer k, determine whether the list can be partitioned into groups of exactly k consecutive increasing numbers (each group is a run like x, x+1, ..., x+k-1). Return true if such a partition exists, otherwise false. Input: '[list], k'.

Input: '[list], k'.

Output: Boolean — true or false.

Examples

Example 1
Input: [1,2,3,3,4,4,5,6], 4
Output: true
Explanation: [1,2,3,4] and [3,4,5,6].
Example 2
Input: [1,2,3,4], 3
Output: false
Explanation: Length not divisible by 3.
Example 3
Input: [1,1,2,2,3,3], 3
Output: true
Explanation: [1,2,3] twice.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →