Skip to content

Commit b56e2ac

Browse files
committed
feat: add solutions to lc problem: No.1121
No.1121.Divide Array Into Increasing Sequences
1 parent 2cf8472 commit b56e2ac

File tree

6 files changed

+191
-2
lines changed

6 files changed

+191
-2
lines changed

solution/1100-1199/1121.Divide Array Into Increasing Sequences/README.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,96 @@
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43+
**方法一:脑筋急转弯**
44+
45+
我们假设可以将数组分成 $m$ 个长度至少为 $k$ 的严格递增子序列,如果数组中出现次数最多的数字的个数为 $cnt$,那么这 $cnt$ 个数字必须在不同的子序列中,所以 $m \geq cnt$,又因为 $m$ 个子序列的长度至少为 $k$,因此,子序列的个数越少越好,所以 $m = cnt$。那么 $cnt \times k \leq n$,才能满足题意。因此,我们只需要统计数组中出现次数最多的数字的个数 $cnt$,然后判断 $cnt \times k \leq n$ 即可。如果是,返回 `true`,否则返回 `false`
46+
47+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
48+
4349
<!-- tabs:start -->
4450

4551
### **Python3**
4652

4753
<!-- 这里可写当前语言的特殊实现逻辑 -->
4854

4955
```python
50-
56+
class Solution:
57+
def canDivideIntoSubsequences(self, nums: List[int], k: int) -> bool:
58+
mx = max(len(list(x)) for _, x in groupby(nums))
59+
return mx * k <= len(nums)
5160
```
5261

5362
### **Java**
5463

5564
<!-- 这里可写当前语言的特殊实现逻辑 -->
5665

5766
```java
67+
class Solution {
68+
public boolean canDivideIntoSubsequences(int[] nums, int k) {
69+
Map<Integer, Integer> cnt = new HashMap<>();
70+
int mx = 0;
71+
for (int x : nums) {
72+
mx = Math.max(mx, cnt.merge(x, 1, Integer::sum));
73+
}
74+
return mx * k <= nums.length;
75+
}
76+
}
77+
```
78+
79+
```java
80+
class Solution {
81+
public boolean canDivideIntoSubsequences(int[] nums, int k) {
82+
int cnt = 0;
83+
int a = 0;
84+
for (int b : nums) {
85+
cnt = a == b ? cnt + 1 : 1;
86+
if (cnt * k > nums.length) {
87+
return false;
88+
}
89+
a = b;
90+
}
91+
return true;
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
bool canDivideIntoSubsequences(vector<int>& nums, int k) {
102+
int cnt = 0;
103+
int a = 0;
104+
for (int& b : nums) {
105+
cnt = a == b ? cnt + 1 : 1;
106+
if (cnt * k > nums.size()) {
107+
return false;
108+
}
109+
a = b;
110+
}
111+
return true;
112+
}
113+
};
114+
```
58115
116+
### **Go**
117+
118+
```go
119+
func canDivideIntoSubsequences(nums []int, k int) bool {
120+
cnt, a := 0, 0
121+
for _, b := range nums {
122+
cnt++
123+
if a != b {
124+
cnt = 1
125+
}
126+
if cnt*k > len(nums) {
127+
return false
128+
}
129+
a = b
130+
}
131+
return true
132+
}
59133
```
60134

61135
### **...**

solution/1100-1199/1121.Divide Array Into Increasing Sequences/README_EN.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,81 @@
3939
### **Python3**
4040

4141
```python
42-
42+
class Solution:
43+
def canDivideIntoSubsequences(self, nums: List[int], k: int) -> bool:
44+
mx = max(len(list(x)) for _, x in groupby(nums))
45+
return mx * k <= len(nums)
4346
```
4447

4548
### **Java**
4649

4750
```java
51+
class Solution {
52+
public boolean canDivideIntoSubsequences(int[] nums, int k) {
53+
Map<Integer, Integer> cnt = new HashMap<>();
54+
int mx = 0;
55+
for (int x : nums) {
56+
mx = Math.max(mx, cnt.merge(x, 1, Integer::sum));
57+
}
58+
return mx * k <= nums.length;
59+
}
60+
}
61+
```
62+
63+
```java
64+
class Solution {
65+
public boolean canDivideIntoSubsequences(int[] nums, int k) {
66+
int cnt = 0;
67+
int a = 0;
68+
for (int b : nums) {
69+
cnt = a == b ? cnt + 1 : 1;
70+
if (cnt * k > nums.length) {
71+
return false;
72+
}
73+
a = b;
74+
}
75+
return true;
76+
}
77+
}
78+
```
79+
80+
### **C++**
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
bool canDivideIntoSubsequences(vector<int>& nums, int k) {
86+
int cnt = 0;
87+
int a = 0;
88+
for (int& b : nums) {
89+
cnt = a == b ? cnt + 1 : 1;
90+
if (cnt * k > nums.size()) {
91+
return false;
92+
}
93+
a = b;
94+
}
95+
return true;
96+
}
97+
};
98+
```
4899
100+
### **Go**
101+
102+
```go
103+
func canDivideIntoSubsequences(nums []int, k int) bool {
104+
cnt, a := 0, 0
105+
for _, b := range nums {
106+
cnt++
107+
if a != b {
108+
cnt = 1
109+
}
110+
if cnt*k > len(nums) {
111+
return false
112+
}
113+
a = b
114+
}
115+
return true
116+
}
49117
```
50118

51119
### **...**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
bool canDivideIntoSubsequences(vector<int>& nums, int k) {
4+
int cnt = 0;
5+
int a = 0;
6+
for (int& b : nums) {
7+
cnt = a == b ? cnt + 1 : 1;
8+
if (cnt * k > nums.size()) {
9+
return false;
10+
}
11+
a = b;
12+
}
13+
return true;
14+
}
15+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func canDivideIntoSubsequences(nums []int, k int) bool {
2+
cnt, a := 0, 0
3+
for _, b := range nums {
4+
cnt++
5+
if a != b {
6+
cnt = 1
7+
}
8+
if cnt*k > len(nums) {
9+
return false
10+
}
11+
a = b
12+
}
13+
return true
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public boolean canDivideIntoSubsequences(int[] nums, int k) {
3+
int cnt = 0;
4+
int a = 0;
5+
for (int b : nums) {
6+
cnt = a == b ? cnt + 1 : 1;
7+
if (cnt * k > nums.length) {
8+
return false;
9+
}
10+
a = b;
11+
}
12+
return true;
13+
}
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def canDivideIntoSubsequences(self, nums: List[int], k: int) -> bool:
3+
mx = max(len(list(x)) for _, x in groupby(nums))
4+
return mx * k <= len(nums)

0 commit comments

Comments
 (0)