Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update solutions to lc problem: No.2393 #3524

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: update solutions to lc problem: No.2393
No.2393.Count Strictly Increasing Subarrays
yanglbme committed Sep 13, 2024
commit ae47f178202bcc3902183ab7414ad6d94456fab1
163 changes: 23 additions & 140 deletions solution/2300-2399/2393.Count Strictly Increasing Subarrays/README.md
Original file line number Diff line number Diff line change
@@ -61,123 +61,15 @@ tags:

<!-- solution:start -->

### 方法一:双指针
### 方法一:枚举

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

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

<!-- tabs:start -->

#### Python3

```python
class Solution:
def countSubarrays(self, nums: List[int]) -> int:
ans = i = 0
while i < len(nums):
j = i + 1
while j < len(nums) and nums[j] > nums[j - 1]:
j += 1
cnt = j - i
ans += (1 + cnt) * cnt // 2
i = j
return ans
```

#### Java

```java
class Solution {
public long countSubarrays(int[] nums) {
long ans = 0;
int i = 0, n = nums.length;
while (i < n) {
int j = i + 1;
while (j < n && nums[j] > nums[j - 1]) {
++j;
}
long cnt = j - i;
ans += (1 + cnt) * cnt / 2;
i = j;
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
long long countSubarrays(vector<int>& nums) {
long long ans = 0;
int i = 0, n = nums.size();
while (i < n) {
int j = i + 1;
while (j < n && nums[j] > nums[j - 1]) {
++j;
}
int cnt = j - i;
ans += 1ll * (1 + cnt) * cnt / 2;
i = j;
}
return ans;
}
};
```
#### Go
```go
func countSubarrays(nums []int) int64 {
ans := 0
i, n := 0, len(nums)
for i < n {
j := i + 1
for j < n && nums[j] > nums[j-1] {
j++
}
cnt := j - i
ans += (1 + cnt) * cnt / 2
i = j
}
return int64(ans)
}
```

#### TypeScript

```ts
function countSubarrays(nums: number[]): number {
let ans = 0;
let i = 0;
const n = nums.length;
while (i < n) {
let j = i + 1;
while (j < n && nums[j] > nums[j - 1]) {
++j;
}
const cnt = j - i;
ans += ((1 + cnt) * cnt) / 2;
i = j;
}
return ans;
}
```

<!-- tabs:end -->
遍历结束后,返回答案即可。

<!-- solution:end -->

<!-- solution:start -->

### 方法二:枚举

我们可以枚举数组中的每一个元素,找到以该元素为结尾的严格递增子数组的个数,然后将这些个数累加到答案中。

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

<!-- tabs:start -->

@@ -186,13 +78,12 @@ function countSubarrays(nums: number[]): number {
```python
class Solution:
def countSubarrays(self, nums: List[int]) -> int:
ans = pre = cnt = 0
for x in nums:
if pre < x:
ans = cnt = 1
for x, y in pairwise(nums):
if x < y:
cnt += 1
else:
cnt = 1
pre = x
ans += cnt
return ans
```
@@ -202,15 +93,13 @@ class Solution:
```java
class Solution {
public long countSubarrays(int[] nums) {
long ans = 0;
int pre = 0, cnt = 0;
for (int x : nums) {
if (pre < x) {
long ans = 1, cnt = 1;
for (int i = 1; i < nums.length; ++i) {
if (nums[i - 1] < nums[i]) {
++cnt;
} else {
cnt = 1;
}
pre = x;
ans += cnt;
}
return ans;
@@ -224,16 +113,14 @@ class Solution {
class Solution {
public:
long long countSubarrays(vector<int>& nums) {
long long ans = 0;
int pre = 0, cnt = 0;
for (int x : nums) {
if (pre < x) {
long long ans = 1, cnt = 1;
for (int i = 1; i < nums.size(); ++i) {
if (nums[i - 1] < nums[i]) {
++cnt;
} else {
cnt = 1;
}
ans += cnt;
pre = x;
}
return ans;
}
@@ -243,36 +130,32 @@ public:
#### Go
```go
func countSubarrays(nums []int) (ans int64) {
pre, cnt := 0, 0
for _, x := range nums {
if pre < x {
func countSubarrays(nums []int) int64 {
ans, cnt := 1, 1
for i, x := range nums[1:] {
if nums[i] < x {
cnt++
} else {
cnt = 1
}
ans += int64(cnt)
pre = x
ans += cnt
}
return
return int64(ans)
}
```

#### TypeScript

```ts
function countSubarrays(nums: number[]): number {
let ans = 0;
let pre = 0;
let cnt = 0;
for (const x of nums) {
if (pre < x) {
let [ans, cnt] = [1, 1];
for (let i = 1; i < nums.length; ++i) {
if (nums[i - 1] < nums[i]) {
++cnt;
} else {
cnt = 1;
}
ans += cnt;
pre = x;
}
return ans;
}
155 changes: 23 additions & 132 deletions solution/2300-2399/2393.Count Strictly Increasing Subarrays/README_EN.md
Original file line number Diff line number Diff line change
@@ -59,115 +59,15 @@ The total number of subarrays is 6 + 3 + 1 = 10.

<!-- solution:start -->

### Solution 1
### Solution 1: Enumeration

<!-- tabs:start -->

#### Python3

```python
class Solution:
def countSubarrays(self, nums: List[int]) -> int:
ans = i = 0
while i < len(nums):
j = i + 1
while j < len(nums) and nums[j] > nums[j - 1]:
j += 1
cnt = j - i
ans += (1 + cnt) * cnt // 2
i = j
return ans
```

#### Java

```java
class Solution {
public long countSubarrays(int[] nums) {
long ans = 0;
int i = 0, n = nums.length;
while (i < n) {
int j = i + 1;
while (j < n && nums[j] > nums[j - 1]) {
++j;
}
long cnt = j - i;
ans += (1 + cnt) * cnt / 2;
i = j;
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
long long countSubarrays(vector<int>& nums) {
long long ans = 0;
int i = 0, n = nums.size();
while (i < n) {
int j = i + 1;
while (j < n && nums[j] > nums[j - 1]) {
++j;
}
int cnt = j - i;
ans += 1ll * (1 + cnt) * cnt / 2;
i = j;
}
return ans;
}
};
```
#### Go
```go
func countSubarrays(nums []int) int64 {
ans := 0
i, n := 0, len(nums)
for i < n {
j := i + 1
for j < n && nums[j] > nums[j-1] {
j++
}
cnt := j - i
ans += (1 + cnt) * cnt / 2
i = j
}
return int64(ans)
}
```

#### TypeScript
We can enumerate the number of strictly increasing subarrays ending at each element and then sum them up.

```ts
function countSubarrays(nums: number[]): number {
let ans = 0;
let i = 0;
const n = nums.length;
while (i < n) {
let j = i + 1;
while (j < n && nums[j] > nums[j - 1]) {
++j;
}
const cnt = j - i;
ans += ((1 + cnt) * cnt) / 2;
i = j;
}
return ans;
}
```
We use a variable $\textit{cnt}$ to record the number of strictly increasing subarrays ending at the current element, initially $\textit{cnt} = 1$. Then we traverse the array starting from the second element. If the current element is greater than the previous element, then $\textit{cnt}$ can be incremented by $1$. Otherwise, $\textit{cnt}$ is reset to $1$. At this point, the number of strictly increasing subarrays ending at the current element is $\textit{cnt}$, and we add it to the answer.

<!-- tabs:end -->

<!-- solution:end -->
After the traversal, return the answer.

<!-- solution:start -->

### Solution 2
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

@@ -176,13 +76,12 @@ function countSubarrays(nums: number[]): number {
```python
class Solution:
def countSubarrays(self, nums: List[int]) -> int:
ans = pre = cnt = 0
for x in nums:
if pre < x:
ans = cnt = 1
for x, y in pairwise(nums):
if x < y:
cnt += 1
else:
cnt = 1
pre = x
ans += cnt
return ans
```
@@ -192,15 +91,13 @@ class Solution:
```java
class Solution {
public long countSubarrays(int[] nums) {
long ans = 0;
int pre = 0, cnt = 0;
for (int x : nums) {
if (pre < x) {
long ans = 1, cnt = 1;
for (int i = 1; i < nums.length; ++i) {
if (nums[i - 1] < nums[i]) {
++cnt;
} else {
cnt = 1;
}
pre = x;
ans += cnt;
}
return ans;
@@ -214,16 +111,14 @@ class Solution {
class Solution {
public:
long long countSubarrays(vector<int>& nums) {
long long ans = 0;
int pre = 0, cnt = 0;
for (int x : nums) {
if (pre < x) {
long long ans = 1, cnt = 1;
for (int i = 1; i < nums.size(); ++i) {
if (nums[i - 1] < nums[i]) {
++cnt;
} else {
cnt = 1;
}
ans += cnt;
pre = x;
}
return ans;
}
@@ -233,36 +128,32 @@ public:
#### Go
```go
func countSubarrays(nums []int) (ans int64) {
pre, cnt := 0, 0
for _, x := range nums {
if pre < x {
func countSubarrays(nums []int) int64 {
ans, cnt := 1, 1
for i, x := range nums[1:] {
if nums[i] < x {
cnt++
} else {
cnt = 1
}
ans += int64(cnt)
pre = x
ans += cnt
}
return
return int64(ans)
}
```

#### TypeScript

```ts
function countSubarrays(nums: number[]): number {
let ans = 0;
let pre = 0;
let cnt = 0;
for (const x of nums) {
if (pre < x) {
let [ans, cnt] = [1, 1];
for (let i = 1; i < nums.length; ++i) {
if (nums[i - 1] < nums[i]) {
++cnt;
} else {
cnt = 1;
}
ans += cnt;
pre = x;
}
return ans;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
class Solution {
public:
long long countSubarrays(vector<int>& nums) {
long long ans = 0;
int i = 0, n = nums.size();
while (i < n) {
int j = i + 1;
while (j < n && nums[j] > nums[j - 1]) {
++j;
long long ans = 1, cnt = 1;
for (int i = 1; i < nums.size(); ++i) {
if (nums[i - 1] < nums[i]) {
++cnt;
} else {
cnt = 1;
}
int cnt = j - i;
ans += 1ll * (1 + cnt) * cnt / 2;
i = j;
ans += cnt;
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
func countSubarrays(nums []int) int64 {
ans := 0
i, n := 0, len(nums)
for i < n {
j := i + 1
for j < n && nums[j] > nums[j-1] {
j++
ans, cnt := 1, 1
for i, x := range nums[1:] {
if nums[i] < x {
cnt++
} else {
cnt = 1
}
cnt := j - i
ans += (1 + cnt) * cnt / 2
i = j
ans += cnt
}
return int64(ans)
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
class Solution {
public long countSubarrays(int[] nums) {
long ans = 0;
int i = 0, n = nums.length;
while (i < n) {
int j = i + 1;
while (j < n && nums[j] > nums[j - 1]) {
++j;
long ans = 1, cnt = 1;
for (int i = 1; i < nums.length; ++i) {
if (nums[i - 1] < nums[i]) {
++cnt;
} else {
cnt = 1;
}
long cnt = j - i;
ans += (1 + cnt) * cnt / 2;
i = j;
ans += cnt;
}
return ans;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
class Solution:
def countSubarrays(self, nums: List[int]) -> int:
ans = i = 0
while i < len(nums):
j = i + 1
while j < len(nums) and nums[j] > nums[j - 1]:
j += 1
cnt = j - i
ans += (1 + cnt) * cnt // 2
i = j
ans = cnt = 1
for x, y in pairwise(nums):
if x < y:
cnt += 1
else:
cnt = 1
ans += cnt
return ans
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
function countSubarrays(nums: number[]): number {
let ans = 0;
let i = 0;
const n = nums.length;
while (i < n) {
let j = i + 1;
while (j < n && nums[j] > nums[j - 1]) {
++j;
let [ans, cnt] = [1, 1];
for (let i = 1; i < nums.length; ++i) {
if (nums[i - 1] < nums[i]) {
++cnt;
} else {
cnt = 1;
}
const cnt = j - i;
ans += ((1 + cnt) * cnt) / 2;
i = j;
ans += cnt;
}
return ans;
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.