Skip to content

Commit a75681b

Browse files
committed
feat: add solutions to lc problem: No.2393
No.2393.Count Strictly Increasing Subarrays
1 parent b89c1e6 commit a75681b

File tree

10 files changed

+249
-10
lines changed

10 files changed

+249
-10
lines changed

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

+88-2
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,114 @@ The total number of subarrays is 6 + 3 + 1 = 10.
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:双指针**
49+
50+
利用双指针,找到每一段连续递增子数组的长度,我们记为 `cnt`,每次将 $(1+cnt)\times cnt / 2$ 累加到答案中。
51+
52+
时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 是数组的长度。
53+
4854
<!-- tabs:start -->
4955

5056
### **Python3**
5157

5258
<!-- 这里可写当前语言的特殊实现逻辑 -->
5359

5460
```python
55-
61+
class Solution:
62+
def countSubarrays(self, nums: List[int]) -> int:
63+
ans = i = 0
64+
while i < len(nums):
65+
j = i + 1
66+
while j < len(nums) and nums[j] > nums[j - 1]:
67+
j += 1
68+
cnt = j - i
69+
ans += (1 + cnt) * cnt // 2
70+
i = j
71+
return ans
5672
```
5773

5874
### **Java**
5975

6076
<!-- 这里可写当前语言的特殊实现逻辑 -->
6177

6278
```java
79+
class Solution {
80+
public long countSubarrays(int[] nums) {
81+
long ans = 0;
82+
int i = 0, n = nums.length;
83+
while (i < n) {
84+
int j = i + 1;
85+
while (j < n && nums[j] > nums[j - 1]) {
86+
++j;
87+
}
88+
long cnt = j - i;
89+
ans += (1 + cnt) * cnt / 2;
90+
i = j;
91+
}
92+
return ans;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
long long countSubarrays(vector<int>& nums) {
103+
long long ans = 0;
104+
int i = 0, n = nums.size();
105+
while (i < n) {
106+
int j = i + 1;
107+
while (j < n && nums[j] > nums[j - 1]) {
108+
++j;
109+
}
110+
int cnt = j - i;
111+
ans += 1ll * (1 + cnt) * cnt / 2;
112+
i = j;
113+
}
114+
return ans;
115+
}
116+
};
117+
```
63118
119+
### **Go**
120+
121+
```go
122+
func countSubarrays(nums []int) int64 {
123+
ans := 0
124+
i, n := 0, len(nums)
125+
for i < n {
126+
j := i + 1
127+
for j < n && nums[j] > nums[j-1] {
128+
j++
129+
}
130+
cnt := j - i
131+
ans += (1 + cnt) * cnt / 2
132+
i = j
133+
}
134+
return int64(ans)
135+
}
64136
```
65137

66138
### **TypeScript**
67139

68140
```ts
69-
141+
function countSubarrays(nums: number[]): number {
142+
let ans = 0;
143+
let i = 0;
144+
const n = nums.length;
145+
while (i < n) {
146+
let j = i + 1;
147+
while (j < n && nums[j] > nums[j - 1]) {
148+
++j;
149+
}
150+
const cnt = j - i;
151+
ans += ((1 + cnt) * cnt) / 2;
152+
i = j;
153+
}
154+
return ans;
155+
}
70156
```
71157

72158
### **...**

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

+82-2
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,99 @@ The total number of subarrays is 6 + 3 + 1 = 10.
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def countSubarrays(self, nums: List[int]) -> int:
51+
ans = i = 0
52+
while i < len(nums):
53+
j = i + 1
54+
while j < len(nums) and nums[j] > nums[j - 1]:
55+
j += 1
56+
cnt = j - i
57+
ans += (1 + cnt) * cnt // 2
58+
i = j
59+
return ans
5060
```
5161

5262
### **Java**
5363

5464
```java
65+
class Solution {
66+
public long countSubarrays(int[] nums) {
67+
long ans = 0;
68+
int i = 0, n = nums.length;
69+
while (i < n) {
70+
int j = i + 1;
71+
while (j < n && nums[j] > nums[j - 1]) {
72+
++j;
73+
}
74+
long cnt = j - i;
75+
ans += (1 + cnt) * cnt / 2;
76+
i = j;
77+
}
78+
return ans;
79+
}
80+
}
81+
```
5582

83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
long long countSubarrays(vector<int>& nums) {
89+
long long ans = 0;
90+
int i = 0, n = nums.size();
91+
while (i < n) {
92+
int j = i + 1;
93+
while (j < n && nums[j] > nums[j - 1]) {
94+
++j;
95+
}
96+
int cnt = j - i;
97+
ans += 1ll * (1 + cnt) * cnt / 2;
98+
i = j;
99+
}
100+
return ans;
101+
}
102+
};
103+
```
104+
105+
### **Go**
106+
107+
```go
108+
func countSubarrays(nums []int) int64 {
109+
ans := 0
110+
i, n := 0, len(nums)
111+
for i < n {
112+
j := i + 1
113+
for j < n && nums[j] > nums[j-1] {
114+
j++
115+
}
116+
cnt := j - i
117+
ans += (1 + cnt) * cnt / 2
118+
i = j
119+
}
120+
return int64(ans)
121+
}
56122
```
57123

58124
### **TypeScript**
59125

60126
```ts
61-
127+
function countSubarrays(nums: number[]): number {
128+
let ans = 0;
129+
let i = 0;
130+
const n = nums.length;
131+
while (i < n) {
132+
let j = i + 1;
133+
while (j < n && nums[j] > nums[j - 1]) {
134+
++j;
135+
}
136+
const cnt = j - i;
137+
ans += ((1 + cnt) * cnt) / 2;
138+
i = j;
139+
}
140+
return ans;
141+
}
62142
```
63143

64144
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
long long countSubarrays(vector<int>& nums) {
4+
long long ans = 0;
5+
int i = 0, n = nums.size();
6+
while (i < n) {
7+
int j = i + 1;
8+
while (j < n && nums[j] > nums[j - 1]) {
9+
++j;
10+
}
11+
int cnt = j - i;
12+
ans += 1ll * (1 + cnt) * cnt / 2;
13+
i = j;
14+
}
15+
return ans;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func countSubarrays(nums []int) int64 {
2+
ans := 0
3+
i, n := 0, len(nums)
4+
for i < n {
5+
j := i + 1
6+
for j < n && nums[j] > nums[j-1] {
7+
j++
8+
}
9+
cnt := j - i
10+
ans += (1 + cnt) * cnt / 2
11+
i = j
12+
}
13+
return int64(ans)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public long countSubarrays(int[] nums) {
3+
long ans = 0;
4+
int i = 0, n = nums.length;
5+
while (i < n) {
6+
int j = i + 1;
7+
while (j < n && nums[j] > nums[j - 1]) {
8+
++j;
9+
}
10+
long cnt = j - i;
11+
ans += (1 + cnt) * cnt / 2;
12+
i = j;
13+
}
14+
return ans;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def countSubarrays(self, nums: List[int]) -> int:
3+
ans = i = 0
4+
while i < len(nums):
5+
j = i + 1
6+
while j < len(nums) and nums[j] > nums[j - 1]:
7+
j += 1
8+
cnt = j - i
9+
ans += (1 + cnt) * cnt // 2
10+
i = j
11+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function countSubarrays(nums: number[]): number {
2+
let ans = 0;
3+
let i = 0;
4+
const n = nums.length;
5+
while (i < n) {
6+
let j = i + 1;
7+
while (j < n && nums[j] > nums[j - 1]) {
8+
++j;
9+
}
10+
const cnt = j - i;
11+
ans += ((1 + cnt) * cnt) / 2;
12+
i = j;
13+
}
14+
return ans;
15+
}

solution/CONTEST_README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
| 段位 | 比例 | 段位名 | 国服分数线 | 勋章 |
1313
| ----- | ------ | -------- | --------- | --------------------------------------------------------------------------- |
1414
| LV3 | 5% | Guardian | &ge;2221.84 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
15-
| LV2 | 20% | Knight | &ge;1867.64 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
15+
| LV2 | 20% | Knight | &ge;1868.56 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
1616
| LV1 | 75% | - | - | - |
1717

1818
力扣竞赛 **全国排名前 10** 的用户,全站用户名展示为品牌橙色。

solution/CONTEST_README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ If you are in the top 25% of the contest rating, you’ll get the “Knight” b
1313

1414
| Level | Proportion | Badge | Rating | |
1515
| ----- | ---------- | ---------- | -------------- | ----------------------------------------------------------------------------------------------------------------------- |
16-
| LV3 | 5\% | Guardian | &ge;2173.79 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
17-
| LV2 | 20\% | Knight | &ge;1848.62 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
16+
| LV3 | 5\% | Guardian | &ge;2175.14 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
17+
| LV2 | 20\% | Knight | &ge;1849.15 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
1818
| LV1 | 75\% | - | - | - |
1919

2020
For top 10 users (excluding LCCN users), your LeetCode ID will be colored orange on the ranking board. You'll also have the honor with you when you post/comment under discuss.

solution/contest.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def generate_contest_list():
124124
| 段位 | 比例 | 段位名 | 国服分数线 | 勋章 |
125125
| ----- | ------ | -------- | --------- | --------------------------------------------------------------------------- |
126126
| LV3 | 5% | Guardian | &ge;2221.84 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
127-
| LV2 | 20% | Knight | &ge;1867.64 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
127+
| LV2 | 20% | Knight | &ge;1868.56 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
128128
| LV1 | 75% | - | - | - |
129129
130130
力扣竞赛 **全国排名前 10** 的用户,全站用户名展示为品牌橙色。
@@ -149,8 +149,8 @@ def generate_contest_list():
149149
150150
| Level | Proportion | Badge | Rating | |
151151
| ----- | ---------- | ---------- | -------------- | ----------------------------------------------------------------------------------------------------------------------- |
152-
| LV3 | 5\% | Guardian | &ge;2173.79 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
153-
| LV2 | 20\% | Knight | &ge;1848.62 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
152+
| LV3 | 5\% | Guardian | &ge;2175.14 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
153+
| LV2 | 20\% | Knight | &ge;1849.15 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
154154
| LV1 | 75\% | - | - | - |
155155
156156
For top 10 users (excluding LCCN users), your LeetCode ID will be colored orange on the ranking board. You'll also have the honor with you when you post/comment under discuss.

0 commit comments

Comments
 (0)