Skip to content

Commit 2dce477

Browse files
committed
feat: add solutions to lc problem: No.2393
No.2393.Count Strictly Increasing Subarrays
1 parent 028b963 commit 2dce477

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

solution/2300-2399/2393.Count Strictly Increasing Subarrays/README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353

5454
时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 是数组的长度。
5555

56+
**方法二:枚举**
57+
58+
我们可以枚举数组中的每一个元素,找到以该元素为结尾的严格递增子数组的个数,然后将这些个数累加到答案中。
59+
60+
时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 是数组的长度。
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
@@ -73,6 +79,20 @@ class Solution:
7379
return ans
7480
```
7581

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+
7696
### **Java**
7797

7898
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -96,6 +116,25 @@ class Solution {
96116
}
97117
```
98118

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+
99138
### **C++**
100139

101140
```cpp
@@ -118,6 +157,26 @@ public:
118157
};
119158
```
120159

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+
121180
### **Go**
122181
123182
```go
@@ -137,6 +196,22 @@ func countSubarrays(nums []int) int64 {
137196
}
138197
```
139198

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+
140215
### **TypeScript**
141216

142217
```ts
@@ -157,6 +232,24 @@ function countSubarrays(nums: number[]): number {
157232
}
158233
```
159234

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+
160253
### **...**
161254

162255
```

solution/2300-2399/2393.Count Strictly Increasing Subarrays/README_EN.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ class Solution:
5959
return ans
6060
```
6161

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+
6276
### **Java**
6377

6478
```java
@@ -80,6 +94,25 @@ class Solution {
8094
}
8195
```
8296

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+
83116
### **C++**
84117

85118
```cpp
@@ -102,6 +135,26 @@ public:
102135
};
103136
```
104137

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+
105158
### **Go**
106159
107160
```go
@@ -121,6 +174,22 @@ func countSubarrays(nums []int) int64 {
121174
}
122175
```
123176

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+
124193
### **TypeScript**
125194

126195
```ts
@@ -141,6 +210,24 @@ function countSubarrays(nums: number[]): number {
141210
}
142211
```
143212

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+
144231
### **...**
145232

146233
```

0 commit comments

Comments
 (0)