From ae47f178202bcc3902183ab7414ad6d94456fab1 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 13 Sep 2024 13:03:31 +0800 Subject: [PATCH] feat: update solutions to lc problem: No.2393 No.2393.Count Strictly Increasing Subarrays --- .../README.md | 163 +++--------------- .../README_EN.md | 155 +++-------------- .../Solution.cpp | 18 +- .../Solution.go | 18 +- .../Solution.java | 18 +- .../Solution.py | 15 +- .../Solution.ts | 17 +- .../Solution2.cpp | 17 -- .../Solution2.go | 13 -- .../Solution2.java | 16 -- .../Solution2.py | 11 -- .../Solution2.ts | 15 -- 12 files changed, 84 insertions(+), 392 deletions(-) delete mode 100644 solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.cpp delete mode 100644 solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.go delete mode 100644 solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.java delete mode 100644 solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.py delete mode 100644 solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.ts diff --git a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/README.md b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/README.md index 4f453f8618363..12e2fc4200353 100644 --- a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/README.md +++ b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/README.md @@ -61,123 +61,15 @@ tags: -### 方法一:双指针 +### 方法一:枚举 -利用双指针,找到每一段连续递增子数组的长度,我们记为 `cnt`,每次将 $(1+cnt)\times cnt / 2$ 累加到答案中。 +我们可以枚举以每个元素结尾的严格递增子数组的个数,然后将它们累加起来即可。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 是数组的长度。 +我们用一个变量 $\textit{cnt}$ 来记录以当前元素结尾的严格递增子数组的个数,初始时 $\textit{cnt} = 1$。然后我们从第二个元素开始遍历数组,如果当前元素大于前一个元素,那么 $\textit{cnt}$ 就可以加 $1$,否则 $\textit{cnt}$ 重置为 $1$。此时,以当前元素结尾的严格递增子数组的个数就是 $\textit{cnt}$,我们将其累加到答案中即可。 - - -#### 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& 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; -} -``` - - +遍历结束后,返回答案即可。 - - - - -### 方法二:枚举 - -我们可以枚举数组中的每一个元素,找到以该元素为结尾的严格递增子数组的个数,然后将这些个数累加到答案中。 - -时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 是数组的长度。 +时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。 @@ -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& 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,18 +130,17 @@ 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) } ``` @@ -262,17 +148,14 @@ func countSubarrays(nums []int) (ans int64) { ```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; } diff --git a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/README_EN.md b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/README_EN.md index 8b6f0b499d300..ad98e648e4a81 100644 --- a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/README_EN.md +++ b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/README_EN.md @@ -59,115 +59,15 @@ The total number of subarrays is 6 + 3 + 1 = 10. -### Solution 1 +### Solution 1: Enumeration - - -#### 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& 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. - - - +After the traversal, return the answer. - - -### Solution 2 +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. @@ -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& 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,18 +128,17 @@ 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) } ``` @@ -252,17 +146,14 @@ func countSubarrays(nums []int) (ans int64) { ```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; } diff --git a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.cpp b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.cpp index 7bd24bbd26471..85980c48b734d 100644 --- a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.cpp +++ b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.cpp @@ -1,17 +1,15 @@ class Solution { public: long long countSubarrays(vector& 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; } -}; \ No newline at end of file +}; diff --git a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.go b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.go index e2fffe72292d2..04e9c8d114864 100644 --- a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.go +++ b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.go @@ -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) -} \ No newline at end of file +} diff --git a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.java b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.java index b331725463a82..1a9304840fa1a 100644 --- a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.java +++ b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.java @@ -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; } -} \ No newline at end of file +} diff --git a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.py b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.py index 4538f6a5864ef..bae19f9ad8d39 100644 --- a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.py +++ b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.py @@ -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 diff --git a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.ts b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.ts index 9b6d1ac84c3d2..3b1a4c9c230d7 100644 --- a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.ts +++ b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution.ts @@ -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; } diff --git a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.cpp b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.cpp deleted file mode 100644 index 1184da8ebe6e0..0000000000000 --- a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.cpp +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - long long countSubarrays(vector& nums) { - long long ans = 0; - int pre = 0, cnt = 0; - for (int x : nums) { - if (pre < x) { - ++cnt; - } else { - cnt = 1; - } - ans += cnt; - pre = x; - } - return ans; - } -}; \ No newline at end of file diff --git a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.go b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.go deleted file mode 100644 index 9bb112f931827..0000000000000 --- a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.go +++ /dev/null @@ -1,13 +0,0 @@ -func countSubarrays(nums []int) (ans int64) { - pre, cnt := 0, 0 - for _, x := range nums { - if pre < x { - cnt++ - } else { - cnt = 1 - } - ans += int64(cnt) - pre = x - } - return -} \ No newline at end of file diff --git a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.java b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.java deleted file mode 100644 index bbbcf80b74a99..0000000000000 --- a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.java +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { - public long countSubarrays(int[] nums) { - long ans = 0; - int pre = 0, cnt = 0; - for (int x : nums) { - if (pre < x) { - ++cnt; - } else { - cnt = 1; - } - pre = x; - ans += cnt; - } - return ans; - } -} \ No newline at end of file diff --git a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.py b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.py deleted file mode 100644 index 1b3afbb15fa1d..0000000000000 --- a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.py +++ /dev/null @@ -1,11 +0,0 @@ -class Solution: - def countSubarrays(self, nums: List[int]) -> int: - ans = pre = cnt = 0 - for x in nums: - if pre < x: - cnt += 1 - else: - cnt = 1 - pre = x - ans += cnt - return ans diff --git a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.ts b/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.ts deleted file mode 100644 index d5107e1620cc2..0000000000000 --- a/solution/2300-2399/2393.Count Strictly Increasing Subarrays/Solution2.ts +++ /dev/null @@ -1,15 +0,0 @@ -function countSubarrays(nums: number[]): number { - let ans = 0; - let pre = 0; - let cnt = 0; - for (const x of nums) { - if (pre < x) { - ++cnt; - } else { - cnt = 1; - } - ans += cnt; - pre = x; - } - return ans; -}