[LeetCode] Divide Array Into Increasing Sequences

1121. Divide Array Into Increasing Sequences

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.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
bool canDivideIntoSubsequences(vector<int>& nums, int k) {
int mafreq = 0, l = 0, r = 0, n = nums.size();
while(r < n) {
while(r < n and nums[r] == nums[l]) r++;
mafreq = max(mafreq, r - l);
l = r;
}
return mafreq * k <= n;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/25/PS/LeetCode/divide-array-into-increasing-sequences/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.