-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
85 lines (78 loc) · 2.27 KB
/
main.cpp
File metadata and controls
85 lines (78 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Source: https://leetcode.com/problems/divide-array-into-increasing-sequences
// Title: Divide Array Into Increasing Sequences
// Difficulty: Hard
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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 <= 10^5`
// - `1 <= nums[i] <= 10^5`
// - `nums` is sorted in non-decreasing order.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <vector>
using namespace std;
// Math
//
// First count the frequency of each number.
//
// Say there are total n elements.
// Say the maximum frequency if `f`; that is, we need at least `f` subsequences.
// Therefore, we need at least `f * k` total elements.
//
// Now we only need to compare `n >= f * k`.
class Solution {
public:
bool canDivideIntoSubsequences(vector<int>& nums, int k) {
int n = nums.size();
auto m = nums.back(); // max value
// Frequency
auto freqs = vector<int>(m + 1);
auto f = 0;
for (auto num : nums) {
++freqs[num];
f = max(f, freqs[num]);
}
return n >= f * k;
}
};
// Math
//
// We don't need to compute the maximum frequency.
// We can check the inequality for each frequency.
class Solution2 {
public:
bool canDivideIntoSubsequences(vector<int>& nums, int k) {
int n = nums.size();
auto pre = nums[0], cnt = 0;
for (auto num : nums) {
if (num == pre) {
++cnt;
} else {
pre = num;
cnt = 1;
}
if (n < cnt * k) return false;
}
return true;
}
};