File tree 11 files changed +272
-22
lines changed
0100-0199/0128.Longest Consecutive Sequence
1200-1299/1248.Count Number of Nice Subarrays
1700-1799/1799.Maximize Score After N Operations
11 files changed +272
-22
lines changed Original file line number Diff line number Diff line change @@ -180,15 +180,14 @@ public:
180
180
class Solution {
181
181
public:
182
182
int longestConsecutive(vector<int>& nums) {
183
- unordered_set<int> s;
184
- for (int num : nums)
185
- s.insert(num);
183
+ unordered_set<int> s(nums.begin(), nums.end());
186
184
int res = 0;
187
- for (int num : nums) {
185
+ for (int& num : nums) {
188
186
if (!s.count(num - 1)) {
189
187
int t = 1, next = num + 1;
190
- while (s.count(next++))
188
+ while (s.count(next++)) {
191
189
++t;
190
+ }
192
191
res = max(res, t);
193
192
}
194
193
}
@@ -255,6 +254,30 @@ func max(a, b int) int {
255
254
}
256
255
```
257
256
257
+ ### ** JavaScript**
258
+
259
+ ``` js
260
+ /**
261
+ * @param {number[]} nums
262
+ * @return {number}
263
+ */
264
+ var longestConsecutive = function (nums ) {
265
+ const s = new Set (nums);
266
+ let res = 0 ;
267
+ for (const num of nums) {
268
+ if (! s .has (num - 1 )) {
269
+ let t = 1 ;
270
+ let next = num + 1 ;
271
+ while (s .has (next++ )) {
272
+ t++ ;
273
+ }
274
+ res = Math .max (res, t);
275
+ }
276
+ }
277
+ return res;
278
+ };
279
+ ```
280
+
258
281
### ** ...**
259
282
260
283
```
Original file line number Diff line number Diff line change @@ -157,15 +157,14 @@ public:
157
157
class Solution {
158
158
public:
159
159
int longestConsecutive(vector<int>& nums) {
160
- unordered_set<int> s;
161
- for (int num : nums)
162
- s.insert(num);
160
+ unordered_set<int> s(nums.begin(), nums.end());
163
161
int res = 0;
164
- for (int num : nums) {
162
+ for (int& num : nums) {
165
163
if (!s.count(num - 1)) {
166
164
int t = 1, next = num + 1;
167
- while (s.count(next++))
165
+ while (s.count(next++)) {
168
166
++t;
167
+ }
169
168
res = max(res, t);
170
169
}
171
170
}
@@ -232,6 +231,30 @@ func max(a, b int) int {
232
231
}
233
232
```
234
233
234
+ ### ** JavaScript**
235
+
236
+ ``` js
237
+ /**
238
+ * @param {number[]} nums
239
+ * @return {number}
240
+ */
241
+ var longestConsecutive = function (nums ) {
242
+ const s = new Set (nums);
243
+ let res = 0 ;
244
+ for (const num of nums) {
245
+ if (! s .has (num - 1 )) {
246
+ let t = 1 ;
247
+ let next = num + 1 ;
248
+ while (s .has (next++ )) {
249
+ t++ ;
250
+ }
251
+ res = Math .max (res, t);
252
+ }
253
+ }
254
+ return res;
255
+ };
256
+ ```
257
+
235
258
### ** ...**
236
259
237
260
```
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
int longestConsecutive (vector<int >& nums) {
4
- unordered_set<int > s;
5
- for (int num : nums)
6
- s.insert (num);
4
+ unordered_set<int > s (nums.begin (), nums.end ());
7
5
int res = 0 ;
8
- for (int num : nums) {
6
+ for (int & num : nums) {
9
7
if (!s.count (num - 1 )) {
10
8
int t = 1 , next = num + 1 ;
11
- while (s.count (next++))
9
+ while (s.count (next++)) {
12
10
++t;
11
+ }
13
12
res = max (res, t);
14
13
}
15
14
}
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {number }
4
+ */
5
+ var longestConsecutive = function ( nums ) {
6
+ const s = new Set ( nums ) ;
7
+ let res = 0 ;
8
+ for ( const num of nums ) {
9
+ if ( ! s . has ( num - 1 ) ) {
10
+ let t = 1 ;
11
+ let next = num + 1 ;
12
+ while ( s . has ( next ++ ) ) {
13
+ t ++ ;
14
+ }
15
+ res = Math . max ( res , t ) ;
16
+ }
17
+ }
18
+ return res ;
19
+ } ;
Original file line number Diff line number Diff line change 49
49
50
50
<!-- 这里可写通用的实现逻辑 -->
51
51
52
+ ** 方法一:前缀和 + 数组/哈希表**
53
+
54
+ 题目求子数组中恰好有 $k$ 个奇数的子数组个数,我们可以求出每个前缀数组中奇数的个数 $t$,记录在数组或哈希表 ` cnt ` 中。对于每个前缀数组,我们只需要求出前缀数组中奇数个数为 $t-k$ 的前缀数组个数,即为以当前前缀数组结尾的子数组个数。
55
+
56
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 ` nums ` 的长度。
57
+
52
58
<!-- tabs:start -->
53
59
54
60
### ** Python3**
55
61
56
62
<!-- 这里可写当前语言的特殊实现逻辑 -->
57
63
58
64
``` python
59
-
65
+ class Solution :
66
+ def numberOfSubarrays (self , nums : List[int ], k : int ) -> int :
67
+ cnt = Counter({0 : 1 })
68
+ ans = t = 0
69
+ for v in nums:
70
+ t += v & 1
71
+ ans += cnt[t - k]
72
+ cnt[t] += 1
73
+ return ans
60
74
```
61
75
62
76
### ** Java**
63
77
64
78
<!-- 这里可写当前语言的特殊实现逻辑 -->
65
79
66
80
``` java
81
+ class Solution {
82
+ public int numberOfSubarrays (int [] nums , int k ) {
83
+ int n = nums. length;
84
+ int [] cnt = new int [n + 1 ];
85
+ cnt[0 ] = 1 ;
86
+ int ans = 0 , t = 0 ;
87
+ for (int v : nums) {
88
+ t += v & 1 ;
89
+ if (t - k >= 0 ) {
90
+ ans += cnt[t - k];
91
+ }
92
+ cnt[t]++ ;
93
+ }
94
+ return ans;
95
+ }
96
+ }
97
+ ```
98
+
99
+ ### ** C++**
100
+
101
+ ``` cpp
102
+ class Solution {
103
+ public:
104
+ int numberOfSubarrays(vector<int >& nums, int k) {
105
+ int n = nums.size();
106
+ vector<int > cnt(n + 1);
107
+ cnt[ 0] = 1;
108
+ int ans = 0, t = 0;
109
+ for (int& v : nums) {
110
+ t += v & 1;
111
+ if (t - k >= 0) {
112
+ ans += cnt[ t - k] ;
113
+ }
114
+ cnt[ t] ++;
115
+ }
116
+ return ans;
117
+ }
118
+ };
119
+ ```
67
120
121
+ ### **Go**
122
+
123
+ ```go
124
+ func numberOfSubarrays(nums []int, k int) (ans int) {
125
+ n := len(nums)
126
+ cnt := make([]int, n+1)
127
+ cnt[0] = 1
128
+ t := 0
129
+ for _, v := range nums {
130
+ t += v & 1
131
+ if t >= k {
132
+ ans += cnt[t-k]
133
+ }
134
+ cnt[t]++
135
+ }
136
+ return
137
+ }
68
138
```
69
139
70
140
### ** ...**
Original file line number Diff line number Diff line change 49
49
<p ><strong >Constraints:</strong ></p >
50
50
51
51
<ul >
52
-
53
52
<li><code>1 <= nums.length <= 50000</code></li>
54
-
55
53
<li><code>1 <= nums[i] <= 10^5</code></li>
56
-
57
54
<li><code>1 <= k <= nums.length</code></li>
58
-
59
55
</ul >
60
56
61
57
## Solutions
65
61
### ** Python3**
66
62
67
63
``` python
68
-
64
+ class Solution :
65
+ def numberOfSubarrays (self , nums : List[int ], k : int ) -> int :
66
+ cnt = Counter({0 : 1 })
67
+ ans = t = 0
68
+ for v in nums:
69
+ t += v & 1
70
+ ans += cnt[t - k]
71
+ cnt[t] += 1
72
+ return ans
69
73
```
70
74
71
75
### ** Java**
72
76
73
77
``` java
78
+ class Solution {
79
+ public int numberOfSubarrays (int [] nums , int k ) {
80
+ int n = nums. length;
81
+ int [] cnt = new int [n + 1 ];
82
+ cnt[0 ] = 1 ;
83
+ int ans = 0 , t = 0 ;
84
+ for (int v : nums) {
85
+ t += v & 1 ;
86
+ if (t - k >= 0 ) {
87
+ ans += cnt[t - k];
88
+ }
89
+ cnt[t]++ ;
90
+ }
91
+ return ans;
92
+ }
93
+ }
94
+ ```
95
+
96
+ ### ** C++**
97
+
98
+ ``` cpp
99
+ class Solution {
100
+ public:
101
+ int numberOfSubarrays(vector<int >& nums, int k) {
102
+ int n = nums.size();
103
+ vector<int > cnt(n + 1);
104
+ cnt[ 0] = 1;
105
+ int ans = 0, t = 0;
106
+ for (int& v : nums) {
107
+ t += v & 1;
108
+ if (t - k >= 0) {
109
+ ans += cnt[ t - k] ;
110
+ }
111
+ cnt[ t] ++;
112
+ }
113
+ return ans;
114
+ }
115
+ };
116
+ ```
74
117
118
+ ### **Go**
119
+
120
+ ```go
121
+ func numberOfSubarrays(nums []int, k int) (ans int) {
122
+ n := len(nums)
123
+ cnt := make([]int, n+1)
124
+ cnt[0] = 1
125
+ t := 0
126
+ for _, v := range nums {
127
+ t += v & 1
128
+ if t >= k {
129
+ ans += cnt[t-k]
130
+ }
131
+ cnt[t]++
132
+ }
133
+ return
134
+ }
75
135
```
76
136
77
137
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int numberOfSubarrays (vector<int >& nums, int k) {
4
+ int n = nums.size ();
5
+ vector<int > cnt (n + 1 );
6
+ cnt[0 ] = 1 ;
7
+ int ans = 0 , t = 0 ;
8
+ for (int & v : nums) {
9
+ t += v & 1 ;
10
+ if (t - k >= 0 ) {
11
+ ans += cnt[t - k];
12
+ }
13
+ cnt[t]++;
14
+ }
15
+ return ans;
16
+ }
17
+ };
Original file line number Diff line number Diff line change
1
+ func numberOfSubarrays (nums []int , k int ) (ans int ) {
2
+ n := len (nums )
3
+ cnt := make ([]int , n + 1 )
4
+ cnt [0 ] = 1
5
+ t := 0
6
+ for _ , v := range nums {
7
+ t += v & 1
8
+ if t >= k {
9
+ ans += cnt [t - k ]
10
+ }
11
+ cnt [t ]++
12
+ }
13
+ return
14
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int numberOfSubarrays (int [] nums , int k ) {
3
+ int n = nums .length ;
4
+ int [] cnt = new int [n + 1 ];
5
+ cnt [0 ] = 1 ;
6
+ int ans = 0 , t = 0 ;
7
+ for (int v : nums ) {
8
+ t += v & 1 ;
9
+ if (t - k >= 0 ) {
10
+ ans += cnt [t - k ];
11
+ }
12
+ cnt [t ]++;
13
+ }
14
+ return ans ;
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def numberOfSubarrays (self , nums : List [int ], k : int ) -> int :
3
+ cnt = Counter ({0 : 1 })
4
+ ans = t = 0
5
+ for v in nums :
6
+ t += v & 1
7
+ ans += cnt [t - k ]
8
+ cnt [t ] += 1
9
+ return ans
You can’t perform that action at this time.
0 commit comments