Talentd
FleetCode

1121. Divide Array Into Increasing Sequences

Hard65.4% acceptance1 companyPremium elsewhere, free here

Given an integer array nums sorted in non-decreasing order and an integer k, return true if this array can be divided into one or more disjoint increasing subsequences of length at least k, or false otherwise.

Example 1:

Input: nums = [1,2,2,3,3,4,4], k = 3
Output: true
Explanation: The array can be divided into two subsequences [1,2,3,4] and [2,3,4] with lengths at least 3 each.

Example 2:

Input: nums = [5,6,6,7,8], k = 3
Output: false
Explanation: There is no way to divide the array using the conditions required.

Constraints:

  • 1 <= k <= nums.length <= 105
  • 1 <= nums[i] <= 105
  • nums is sorted in non-decreasing order.

Asked at 1 company

Hints open one at a time

Hint 1
Think in the frequency of the numbers and how this affects the number of sequences needed.
Hint 2
What is the minimum number of sequences we need to form? Considering frequency of the numbers.
Hint 3
Think about the least number of sequences to maximize the lengths.
Hint 4
The number of sequences needed is equal to the maximum frequency of an element.
Hint 5
How to put the other elements into sequences ? Think in a greedy approach.
7:38
Still stuck?Read 2 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[1,2,2,3,3,4,4]
3
Input
[5,6,6,7,8]
3
Your code is saved on this device.Solution