Skip to content

Commit

Permalink
leetcode: Add index to 2574
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jul 29, 2024
1 parent 1b0b5d7 commit 8a0ee72
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@
- [2101-2200](leetcode/by-id/2101-2200.md)
- [2108. 找出数组中的第一个回文字符串 Find First Palindromic String in the Array](leetcode/2108.find-first-palindromic-string-in-the-array/index.md)
- [2119. 反转两次的数字 A Number After a Double Reversal](leetcode/2119.a-number-after-a-double-reversal/index.md)
- [2501-2600](leetcode/by-id/2501-2600.md)
- [2574. 左右元素和的差值 Left and Right Sum Differences](leetcode/2574.left-and-right-sum-differences/index.md)
- [2801-2900](leetcode/by-id/2801-2900.md)
- [2848. 与车相交的点 Points That Intersect With Cars](leetcode/2848.points-that-intersect-with-cars/index.md)
- [3001-3100](leetcode/by-id/3001-3100.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "lc-0000-name"
name = "lc-2574-left-and-right-sum-differences"
version = "0.1.0"
edition = "2021"
publish = false
Expand Down
19 changes: 17 additions & 2 deletions src/leetcode/2574.left-and-right-sum-differences/index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
# 2574. 左右元素和的差值 Left and Right Sum Differences

#
[问题描述](https://leetcode.com/problems/left-and-right-sum-differences)

[问题描述](https://leetcode.com/problems/)
这是一个简单的前缀和 prefix sum 问题.

## Prefix sum

处理思路如下:

1. 计算 `left_sum`, 从左到右遍历原数组, 并计算前缀和, `left_sum[i + 1] = left_sum[i] + nums[i]`
2. 计算 `right_sum` 从左到右遍历原数组, 并计算前缀和, `right_sum[i - 1] = right_sum[i] + nums[i]`
3. 计算遍历两个数组, 并计算 `(left_sum[i] - right_sum[i]).abs()`, 就得到了答案

```rust
{{#include src/main.rs:5:31}}
```

该算法的时间复杂度是 `O(n)`, 空间复杂度是 `O(n)`.
8 changes: 5 additions & 3 deletions src/leetcode/2574.left-and-right-sum-differences/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ pub fn left_right_difference1(nums: Vec<i32>) -> Vec<i32> {
let len = nums.len();
let mut left_sum = vec![0; len];
left_sum[0] = 0;
// 从左向右遍历
for i in 0..(len - 1) {
left_sum[i + 1] = left_sum[i] + nums[i];
}
//println!("left sum: {left_sum:?}");

let mut right_sum = vec![0; len];
right_sum[0] = 0;
for i in 0..(len - 1) {
right_sum[i + 1] = right_sum[i] + nums[len - i - 1];
// 从右向左遍历
for i in (1..=(len - 1)).rev() {
right_sum[i - 1] = right_sum[i] + nums[i];
}
//println!("right sum: {right_sum:?}");

left_sum
.into_iter()
.zip(right_sum.into_iter().rev())
.zip(right_sum)
.map(|(left, right)| (left - right).abs())
.collect()
}
Expand Down
1 change: 1 addition & 0 deletions src/leetcode/by-id/2501-2600.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 2501-2600
5 changes: 3 additions & 2 deletions src/leetcode/tags/prefix-sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
4. [1480. 一维数组的动态和 Running Sum of 1d Array](../1480.running-sum-of-1d-array/index.md)
5. [1732. 找到最高海拔 Find the Highest Altitude](../1732.find-the-highest-altitude/index.md)
6. [1991. 找到数组的中间位置 Find the Middle Index in Array](../1991.find-the-middle-index-in-array/index.md)
7. [2848. 与车相交的点 Points That Intersect With Cars](../2848.points-that-intersect-with-cars/index.md)
8. [3028. 边界上的蚂蚁 Ant on the Boundary](../3028.ant-on-the-boundary/index.md)
7. [2574. 左右元素和的差值 Left and Right Sum Differences](../2574.left-and-right-sum-differences/index.md)
8. [2848. 与车相交的点 Points That Intersect With Cars](../2848.points-that-intersect-with-cars/index.md)
9. [3028. 边界上的蚂蚁 Ant on the Boundary](../3028.ant-on-the-boundary/index.md)

## 中等

Expand Down

0 comments on commit 8a0ee72

Please sign in to comment.