File tree Expand file tree Collapse file tree 6 files changed +191
-2
lines changed
solution/1100-1199/1121.Divide Array Into Increasing Sequences Expand file tree Collapse file tree 6 files changed +191
-2
lines changed Original file line number Diff line number Diff line change 40
40
41
41
<!-- 这里可写通用的实现逻辑 -->
42
42
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
+
43
49
<!-- tabs:start -->
44
50
45
51
### ** Python3**
46
52
47
53
<!-- 这里可写当前语言的特殊实现逻辑 -->
48
54
49
55
``` 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)
51
60
```
52
61
53
62
### ** Java**
54
63
55
64
<!-- 这里可写当前语言的特殊实现逻辑 -->
56
65
57
66
``` 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
+ ```
58
115
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
+ }
59
133
```
60
134
61
135
### ** ...**
Original file line number Diff line number Diff line change 39
39
### ** Python3**
40
40
41
41
``` 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)
43
46
```
44
47
45
48
### ** Java**
46
49
47
50
``` 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
+ ```
48
99
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
+ }
49
117
```
50
118
51
119
### ** ...**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments