Skip to content

feat: add solutions to lc problem: No.3284 #3515

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

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions lcof2/剑指 Offer II 098. 路径的数目/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class Solution {
func uniquePaths(_ m: Int, _ n: Int) -> Int {
var dp = Array(repeating: Array(repeating: 0, count: n), count: m)
dp[0][0] = 1

for i in 0..<m {
for j in 0..<n {
if i > 0 {
Expand All @@ -265,7 +265,7 @@ class Solution {
}
}
}

return dp[m - 1][n - 1]
}
}
Expand Down
6 changes: 3 additions & 3 deletions lcof2/剑指 Offer II 099. 最小路径之和/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,17 @@ class Solution {
for i in 1..<m {
dp[i][0] += dp[i-1][0]
}

for j in 1..<n {
dp[0][j] += dp[0][j-1]
}

for i in 1..<m {
for j in 1..<n {
dp[i][j] += min(dp[i-1][j], dp[i][j-1])
}
}

return dp[m-1][n-1]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ tags:
<b>输出:</b>[[2,1],[1,2]]
<b>解释:</b>起点为 (2,3) 。
价格范围为 [2,3] ,我们可以选择的物品坐标为 (0,1),(1,1),(1,2) 和 (2,1) 。
这些物品的排名为:
这些物品的排名为:
- (2,1) 距离为 2 ,价格为 2
- (1,2) 距离为 2 ,价格为 3
- (1,1) 距离为 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ Thus, the 2 highest ranked items in the price range are (2,1) and (1,2).
<strong>Input:</strong> grid = [[1,1,1],[0,0,1],[2,3,4]], pricing = [2,3], start = [0,0], k = 3
<strong>Output:</strong> [[2,1],[2,0]]
<strong>Explanation:</strong> You start at (0,0).
With a price range of [2,3], we can take items from (2,0) and (2,1).
The ranks of these items are:
With a price range of [2,3], we can take items from (2,0) and (2,1).
The ranks of these items are:
- (2,1) with distance 5
- (2,0) with distance 6
Thus, the 2 highest ranked items in the price range are (2,1) and (2,0).
Thus, the 2 highest ranked items in the price range are (2,1) and (2,0).
Note that k = 3 but there are only 2 reachable items within the price range.
</pre>

Expand Down
298 changes: 298 additions & 0 deletions solution/3200-3299/3284.Sum of Consecutive Subarrays/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
---
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3284.Sum%20of%20Consecutive%20Subarrays/README.md
---

<!-- problem:start -->

# [3284. Sum of Consecutive Subarrays 🔒](https://leetcode.cn/problems/sum-of-consecutive-subarrays)

[English Version](/solution/3200-3299/3284.Sum%20of%20Consecutive%20Subarrays/README_EN.md)

## 题目描述

<!-- description:start -->

<p>We call an array <code>arr</code> of length <code>n</code> <strong>consecutive</strong> if one of the following holds:</p>

<ul>
<li><code>arr[i] - arr[i - 1] == 1</code> for <em>all</em> <code>1 &lt;= i &lt; n</code>.</li>
<li><code>arr[i] - arr[i - 1] == -1</code> for <em>all</em> <code>1 &lt;= i &lt; n</code>.</li>
</ul>

<p>The <strong>value</strong> of an array is the sum of its elements.</p>

<p>For example, <code>[3, 4, 5]</code> is a consecutive array of value 12 and <code>[9, 8]</code> is another of value 17. While <code>[3, 4, 3]</code> and <code>[8, 6]</code> are not consecutive.</p>

<p>Given an array of integers <code>nums</code>, return the <em>sum</em> of the <strong>values</strong> of all <strong>consecutive </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>

<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9 </sup>+ 7.</code></p>

<p><strong>Note</strong> that an array of length 1 is also considered consecutive.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3]</span></p>

<p><strong>Output:</strong> <span class="example-io">20</span></p>

<p><strong>Explanation:</strong></p>

<p>The consecutive subarrays are: <code>[1]</code>, <code>[2]</code>, <code>[3]</code>, <code>[1, 2]</code>, <code>[2, 3]</code>, <code>[1, 2, 3]</code>.<br />
Sum of their values would be: <code>1 + 2 + 3 + 3 + 5 + 6 = 20</code>.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,5,7]</span></p>

<p><strong>Output:</strong> <span class="example-io">16</span></p>

<p><strong>Explanation:</strong></p>

<p>The consecutive subarrays are: <code>[1]</code>, <code>[3]</code>, <code>[5]</code>, <code>[7]</code>.<br />
Sum of their values would be: <code>1 + 3 + 5 + 7 = 16</code>.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [7,6,1,2]</span></p>

<p><strong>Output:</strong> <span class="example-io">32</span></p>

<p><strong>Explanation:</strong></p>

<p>The consecutive subarrays are: <code>[7]</code>, <code>[6]</code>, <code>[1]</code>, <code>[2]</code>, <code>[7, 6]</code>, <code>[1, 2]</code>.<br />
Sum of their values would be: <code>7 + 6 + 1 + 2 + 13 + 3 = 32</code>.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:递推

我们定义两个变量 $f$ 和 $g$,分别表示以当前元素结尾的递增子数组的长度和以当前元素结尾的递减子数组的长度,用另外两个变量 $s$ 和 $t$ 分别表示以当前元素结尾的递增子数组的和和以当前元素结尾的递减子数组的和。初始时 $f = g = 1$,而 $s = t = \textit{nums}[0]$。

接下来,我们从第二个元素开始遍历数组,对于当前元素 $\textit{nums}[i]$,我们分别考虑以 $\textit{nums}[i]$ 结尾的递增子数组和递减子数组。

如果 $\textit{nums}[i] - \textit{nums}[i - 1] = 1$,那么 $\textit{nums}[i]$ 可以加入到以 $\textit{nums}[i - 1]$ 结尾的递增子数组中,此时我们更新 $f$ 和 $s$,并将 $s$ 加到答案中;

如果 $\textit{nums}[i] - \textit{nums}[i - 1] = -1$,那么 $\textit{nums}[i]$ 可以加入到以 $\textit{nums}[i - 1]$ 结尾的递减子数组中,此时我们更新 $g$ 和 $t$,并将 $t$ 加到答案中。

否则,$\textit{nums}[i]$ 无法加入到以 $\textit{nums}[i - 1]$ 结尾的递增子数组或递减子数组中,我们将 $\textit{nums}[i]$ 加到答案中。

遍历结束后,返回答案即可。

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

<!-- tabs:start -->

#### Python3

```python
class Solution:
def getSum(self, nums: List[int]) -> int:
mod = 10**9 + 7
f = g = 1
s = t = nums[0]
ans = nums[0]
for x, y in pairwise(nums):
if y - x == 1:
f += 1
s += f * y
ans = (ans + s) % mod
else:
f = 1
s = y
if y - x == -1:
g += 1
t += g * y
ans = (ans + t) % mod
else:
g = 1
t = y
if abs(y - x) != 1:
ans = (ans + y) % mod
return ans
```

#### Java

```java
class Solution {
public int getSum(int[] nums) {
final int mod = (int) 1e9 + 7;
long s = nums[0], t = nums[0], ans = nums[0];
int f = 1, g = 1;
for (int i = 1; i < nums.length; ++i) {
int x = nums[i - 1], y = nums[i];
if (y - x == 1) {
++f;
s += 1L * f * y;
ans = (ans + s) % mod;
} else {
f = 1;
s = y;
}
if (y - x == -1) {
++g;
t += 1L * g * y;
ans = (ans + t) % mod;
} else {
g = 1;
t = y;
}
if (Math.abs(y - x) != 1) {
ans = (ans + y) % mod;
}
}
return (int) ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int getSum(vector<int>& nums) {
const int mod = 1e9 + 7;
long long s = nums[0], t = nums[0], ans = nums[0];
int f = 1, g = 1;
for (int i = 1; i < nums.size(); ++i) {
int x = nums[i - 1], y = nums[i];
if (y - x == 1) {
++f;
s += 1LL * f * y;
ans = (ans + s) % mod;
} else {
f = 1;
s = y;
}
if (y - x == -1) {
++g;
t += 1LL * g * y;
ans = (ans + t) % mod;
} else {
g = 1;
t = y;
}
if (abs(y - x) != 1) {
ans = (ans + y) % mod;
}
}
return ans;
}
};
```

#### Go

```go
func getSum(nums []int) int {
const mod int = 1e9 + 7
f, g := 1, 1
s, t := nums[0], nums[0]
ans := nums[0]

for i := 1; i < len(nums); i++ {
x, y := nums[i-1], nums[i]

if y-x == 1 {
f++
s += f * y
ans = (ans + s) % mod
} else {
f = 1
s = y
}

if y-x == -1 {
g++
t += g * y
ans = (ans + t) % mod
} else {
g = 1
t = y
}

if abs(y-x) != 1 {
ans = (ans + y) % mod
}
}

return ans
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}
```

#### TypeScript

```ts
function getSum(nums: number[]): number {
const mod = 10 ** 9 + 7;
let f = 1,
g = 1;
let s = nums[0],
t = nums[0];
let ans = nums[0];

for (let i = 1; i < nums.length; i++) {
const x = nums[i - 1];
const y = nums[i];

if (y - x === 1) {
f++;
s += f * y;
ans = (ans + s) % mod;
} else {
f = 1;
s = y;
}

if (y - x === -1) {
g++;
t += g * y;
ans = (ans + t) % mod;
} else {
g = 1;
t = y;
}

if (Math.abs(y - x) !== 1) {
ans = (ans + y) % mod;
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading
Loading