File tree Expand file tree Collapse file tree 2 files changed +180
-0
lines changed
solution/2300-2399/2393.Count Strictly Increasing Subarrays Expand file tree Collapse file tree 2 files changed +180
-0
lines changed Original file line number Diff line number Diff line change 53
53
54
54
时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 是数组的长度。
55
55
56
+ ** 方法二:枚举**
57
+
58
+ 我们可以枚举数组中的每一个元素,找到以该元素为结尾的严格递增子数组的个数,然后将这些个数累加到答案中。
59
+
60
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 是数组的长度。
61
+
56
62
<!-- tabs:start -->
57
63
58
64
### ** Python3**
@@ -73,6 +79,20 @@ class Solution:
73
79
return ans
74
80
```
75
81
82
+ ``` python
83
+ class Solution :
84
+ def countSubarrays (self , nums : List[int ]) -> int :
85
+ ans = pre = cnt = 0
86
+ for x in nums:
87
+ if pre < x:
88
+ cnt += 1
89
+ else :
90
+ cnt = 1
91
+ pre = x
92
+ ans += cnt
93
+ return ans
94
+ ```
95
+
76
96
### ** Java**
77
97
78
98
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -96,6 +116,25 @@ class Solution {
96
116
}
97
117
```
98
118
119
+ ``` java
120
+ class Solution {
121
+ public long countSubarrays (int [] nums ) {
122
+ long ans = 0 ;
123
+ int pre = 0 , cnt = 0 ;
124
+ for (int x : nums) {
125
+ if (pre < x) {
126
+ ++ cnt;
127
+ } else {
128
+ cnt = 1 ;
129
+ }
130
+ pre = x;
131
+ ans += cnt;
132
+ }
133
+ return ans;
134
+ }
135
+ }
136
+ ``
137
+
99
138
### ** C ++ **
100
139
101
140
```cpp
@@ -118,6 +157,26 @@ public:
118
157
};
119
158
```
120
159
160
+ ``` cpp
161
+ class Solution {
162
+ public:
163
+ long long countSubarrays(vector<int >& nums) {
164
+ long long ans = 0;
165
+ int pre = 0, cnt = 0;
166
+ for (int x : nums) {
167
+ if (pre < x) {
168
+ ++cnt;
169
+ } else {
170
+ cnt = 1;
171
+ }
172
+ ans += cnt;
173
+ pre = x;
174
+ }
175
+ return ans;
176
+ }
177
+ };
178
+ ```
179
+
121
180
### **Go**
122
181
123
182
```go
@@ -137,6 +196,22 @@ func countSubarrays(nums []int) int64 {
137
196
}
138
197
```
139
198
199
+ ``` go
200
+ func countSubarrays (nums []int ) (ans int64 ) {
201
+ pre , cnt := 0 , 0
202
+ for _ , x := range nums {
203
+ if pre < x {
204
+ cnt++
205
+ } else {
206
+ cnt = 1
207
+ }
208
+ ans += int64 (cnt)
209
+ pre = x
210
+ }
211
+ return
212
+ }
213
+ ```
214
+
140
215
### ** TypeScript**
141
216
142
217
``` ts
@@ -157,6 +232,24 @@ function countSubarrays(nums: number[]): number {
157
232
}
158
233
```
159
234
235
+ ``` ts
236
+ function countSubarrays(nums : number []): number {
237
+ let ans = 0 ;
238
+ let pre = 0 ;
239
+ let cnt = 0 ;
240
+ for (const x of nums ) {
241
+ if (pre < x ) {
242
+ ++ cnt ;
243
+ } else {
244
+ cnt = 1 ;
245
+ }
246
+ ans += cnt ;
247
+ pre = x ;
248
+ }
249
+ return ans ;
250
+ }
251
+ ```
252
+
160
253
### ** ...**
161
254
162
255
```
Original file line number Diff line number Diff line change @@ -59,6 +59,20 @@ class Solution:
59
59
return ans
60
60
```
61
61
62
+ ``` python
63
+ class Solution :
64
+ def countSubarrays (self , nums : List[int ]) -> int :
65
+ ans = pre = cnt = 0
66
+ for x in nums:
67
+ if pre < x:
68
+ cnt += 1
69
+ else :
70
+ cnt = 1
71
+ pre = x
72
+ ans += cnt
73
+ return ans
74
+ ```
75
+
62
76
### ** Java**
63
77
64
78
``` java
@@ -80,6 +94,25 @@ class Solution {
80
94
}
81
95
```
82
96
97
+ ``` java
98
+ class Solution {
99
+ public long countSubarrays (int [] nums ) {
100
+ long ans = 0 ;
101
+ int pre = 0 , cnt = 0 ;
102
+ for (int x : nums) {
103
+ if (pre < x) {
104
+ ++ cnt;
105
+ } else {
106
+ cnt = 1 ;
107
+ }
108
+ pre = x;
109
+ ans += cnt;
110
+ }
111
+ return ans;
112
+ }
113
+ }
114
+ ``
115
+
83
116
### ** C ++ **
84
117
85
118
```cpp
@@ -102,6 +135,26 @@ public:
102
135
};
103
136
```
104
137
138
+ ``` cpp
139
+ class Solution {
140
+ public:
141
+ long long countSubarrays(vector<int >& nums) {
142
+ long long ans = 0;
143
+ int pre = 0, cnt = 0;
144
+ for (int x : nums) {
145
+ if (pre < x) {
146
+ ++cnt;
147
+ } else {
148
+ cnt = 1;
149
+ }
150
+ ans += cnt;
151
+ pre = x;
152
+ }
153
+ return ans;
154
+ }
155
+ };
156
+ ```
157
+
105
158
### **Go**
106
159
107
160
```go
@@ -121,6 +174,22 @@ func countSubarrays(nums []int) int64 {
121
174
}
122
175
```
123
176
177
+ ``` go
178
+ func countSubarrays (nums []int ) (ans int64 ) {
179
+ pre , cnt := 0 , 0
180
+ for _ , x := range nums {
181
+ if pre < x {
182
+ cnt++
183
+ } else {
184
+ cnt = 1
185
+ }
186
+ ans += int64 (cnt)
187
+ pre = x
188
+ }
189
+ return
190
+ }
191
+ ```
192
+
124
193
### ** TypeScript**
125
194
126
195
``` ts
@@ -141,6 +210,24 @@ function countSubarrays(nums: number[]): number {
141
210
}
142
211
```
143
212
213
+ ``` ts
214
+ function countSubarrays(nums : number []): number {
215
+ let ans = 0 ;
216
+ let pre = 0 ;
217
+ let cnt = 0 ;
218
+ for (const x of nums ) {
219
+ if (pre < x ) {
220
+ ++ cnt ;
221
+ } else {
222
+ cnt = 1 ;
223
+ }
224
+ ans += cnt ;
225
+ pre = x ;
226
+ }
227
+ return ans ;
228
+ }
229
+ ```
230
+
144
231
### ** ...**
145
232
146
233
```
You can’t perform that action at this time.
0 commit comments