Back to all solutions

#1121 - Divide Array Into Increasing Sequences

Problem Description

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.

Solution

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {boolean}
 */
var canDivideIntoSubsequences = function(nums, k) {
  let maxFrequency = 0;
  let currentFrequency = 1;

  for (let i = 1; i < nums.length; i++) {
    currentFrequency = nums[i] === nums[i - 1] ? currentFrequency + 1 : 1;
    maxFrequency = Math.max(maxFrequency, currentFrequency);
  }

  return k * maxFrequency <= nums.length;
};