Skip to content

Commit 8221bdb

Browse files
authored
feat: update solutions to lc problem: No.2393 (#3524)
No.2393.Count Strictly Increasing Subarrays
1 parent 0159755 commit 8221bdb

File tree

12 files changed

+84
-392
lines changed

12 files changed

+84
-392
lines changed

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

+23-140
Original file line numberDiff line numberDiff line change
@@ -61,123 +61,15 @@ tags:
6161

6262
<!-- solution:start -->
6363

64-
### 方法一:双指针
64+
### 方法一:枚举
6565

66-
利用双指针,找到每一段连续递增子数组的长度,我们记为 `cnt`,每次将 $(1+cnt)\times cnt / 2$ 累加到答案中
66+
我们可以枚举以每个元素结尾的严格递增子数组的个数,然后将它们累加起来即可
6767

68-
时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 是数组的长度
68+
我们用一个变量 $\textit{cnt}$ 来记录以当前元素结尾的严格递增子数组的个数,初始时 $\textit{cnt} = 1$。然后我们从第二个元素开始遍历数组,如果当前元素大于前一个元素,那么 $\textit{cnt}$ 就可以加 $1$,否则 $\textit{cnt}$ 重置为 $1$。此时,以当前元素结尾的严格递增子数组的个数就是 $\textit{cnt}$,我们将其累加到答案中即可
6969

70-
<!-- tabs:start -->
71-
72-
#### Python3
73-
74-
```python
75-
class Solution:
76-
def countSubarrays(self, nums: List[int]) -> int:
77-
ans = i = 0
78-
while i < len(nums):
79-
j = i + 1
80-
while j < len(nums) and nums[j] > nums[j - 1]:
81-
j += 1
82-
cnt = j - i
83-
ans += (1 + cnt) * cnt // 2
84-
i = j
85-
return ans
86-
```
87-
88-
#### Java
89-
90-
```java
91-
class Solution {
92-
public long countSubarrays(int[] nums) {
93-
long ans = 0;
94-
int i = 0, n = nums.length;
95-
while (i < n) {
96-
int j = i + 1;
97-
while (j < n && nums[j] > nums[j - 1]) {
98-
++j;
99-
}
100-
long cnt = j - i;
101-
ans += (1 + cnt) * cnt / 2;
102-
i = j;
103-
}
104-
return ans;
105-
}
106-
}
107-
```
108-
109-
#### C++
110-
111-
```cpp
112-
class Solution {
113-
public:
114-
long long countSubarrays(vector<int>& nums) {
115-
long long ans = 0;
116-
int i = 0, n = nums.size();
117-
while (i < n) {
118-
int j = i + 1;
119-
while (j < n && nums[j] > nums[j - 1]) {
120-
++j;
121-
}
122-
int cnt = j - i;
123-
ans += 1ll * (1 + cnt) * cnt / 2;
124-
i = j;
125-
}
126-
return ans;
127-
}
128-
};
129-
```
130-
131-
#### Go
132-
133-
```go
134-
func countSubarrays(nums []int) int64 {
135-
ans := 0
136-
i, n := 0, len(nums)
137-
for i < n {
138-
j := i + 1
139-
for j < n && nums[j] > nums[j-1] {
140-
j++
141-
}
142-
cnt := j - i
143-
ans += (1 + cnt) * cnt / 2
144-
i = j
145-
}
146-
return int64(ans)
147-
}
148-
```
149-
150-
#### TypeScript
151-
152-
```ts
153-
function countSubarrays(nums: number[]): number {
154-
let ans = 0;
155-
let i = 0;
156-
const n = nums.length;
157-
while (i < n) {
158-
let j = i + 1;
159-
while (j < n && nums[j] > nums[j - 1]) {
160-
++j;
161-
}
162-
const cnt = j - i;
163-
ans += ((1 + cnt) * cnt) / 2;
164-
i = j;
165-
}
166-
return ans;
167-
}
168-
```
169-
170-
<!-- tabs:end -->
70+
遍历结束后,返回答案即可。
17171

172-
<!-- solution:end -->
173-
174-
<!-- solution:start -->
175-
176-
### 方法二:枚举
177-
178-
我们可以枚举数组中的每一个元素,找到以该元素为结尾的严格递增子数组的个数,然后将这些个数累加到答案中。
179-
180-
时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 是数组的长度。
72+
时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。
18173

18274
<!-- tabs:start -->
18375

@@ -186,13 +78,12 @@ function countSubarrays(nums: number[]): number {
18678
```python
18779
class Solution:
18880
def countSubarrays(self, nums: List[int]) -> int:
189-
ans = pre = cnt = 0
190-
for x in nums:
191-
if pre < x:
81+
ans = cnt = 1
82+
for x, y in pairwise(nums):
83+
if x < y:
19284
cnt += 1
19385
else:
19486
cnt = 1
195-
pre = x
19687
ans += cnt
19788
return ans
19889
```
@@ -202,15 +93,13 @@ class Solution:
20293
```java
20394
class Solution {
20495
public long countSubarrays(int[] nums) {
205-
long ans = 0;
206-
int pre = 0, cnt = 0;
207-
for (int x : nums) {
208-
if (pre < x) {
96+
long ans = 1, cnt = 1;
97+
for (int i = 1; i < nums.length; ++i) {
98+
if (nums[i - 1] < nums[i]) {
20999
++cnt;
210100
} else {
211101
cnt = 1;
212102
}
213-
pre = x;
214103
ans += cnt;
215104
}
216105
return ans;
@@ -224,16 +113,14 @@ class Solution {
224113
class Solution {
225114
public:
226115
long long countSubarrays(vector<int>& nums) {
227-
long long ans = 0;
228-
int pre = 0, cnt = 0;
229-
for (int x : nums) {
230-
if (pre < x) {
116+
long long ans = 1, cnt = 1;
117+
for (int i = 1; i < nums.size(); ++i) {
118+
if (nums[i - 1] < nums[i]) {
231119
++cnt;
232120
} else {
233121
cnt = 1;
234122
}
235123
ans += cnt;
236-
pre = x;
237124
}
238125
return ans;
239126
}
@@ -243,36 +130,32 @@ public:
243130
#### Go
244131
245132
```go
246-
func countSubarrays(nums []int) (ans int64) {
247-
pre, cnt := 0, 0
248-
for _, x := range nums {
249-
if pre < x {
133+
func countSubarrays(nums []int) int64 {
134+
ans, cnt := 1, 1
135+
for i, x := range nums[1:] {
136+
if nums[i] < x {
250137
cnt++
251138
} else {
252139
cnt = 1
253140
}
254-
ans += int64(cnt)
255-
pre = x
141+
ans += cnt
256142
}
257-
return
143+
return int64(ans)
258144
}
259145
```
260146

261147
#### TypeScript
262148

263149
```ts
264150
function countSubarrays(nums: number[]): number {
265-
let ans = 0;
266-
let pre = 0;
267-
let cnt = 0;
268-
for (const x of nums) {
269-
if (pre < x) {
151+
let [ans, cnt] = [1, 1];
152+
for (let i = 1; i < nums.length; ++i) {
153+
if (nums[i - 1] < nums[i]) {
270154
++cnt;
271155
} else {
272156
cnt = 1;
273157
}
274158
ans += cnt;
275-
pre = x;
276159
}
277160
return ans;
278161
}

0 commit comments

Comments
 (0)