Skip to content

Commit

Permalink
leetcode: Add index to 1854
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jul 30, 2024
1 parent 65c8432 commit 6e166f8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@
- [1732. 找到最高海拔 Find the Highest Altitude](leetcode/1732.find-the-highest-altitude/index.md)
- [1780. 判断一个数字是否可以表示成三的幂的和 Check if Number is a Sum of Powers of Three](leetcode/1780.check-if-number-is-a-sum-of-powers-of-three/index.md)
- [1801-1900](leetcode/by-id/1801-1900.md)
- [1854. 人口最多的年份 Maximum Population Year](leetcode/1854.maximum-population-year/index.md)
- [1893. 检查是否区域内所有整数都被覆盖 Check if All the Integers in a Range Are Covered](leetcode/1893.check-if-all-the-integers-in-a-range-are-covered/index.md)
- [1901-2000](leetcode/by-id/1901-2000.md)
- [1991. 找到数组的中间位置 Find the Middle Index in Array](leetcode/1991.find-the-middle-index-in-array/index.md)
Expand Down
45 changes: 45 additions & 0 deletions src/leetcode/1854.maximum-population-year/index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
# 1854. 人口最多的年份 Maximum Population Year

[问题描述](https://leetcode.com/problems/maximum-population-year)

这是一个计数的问题, 首先想到的就是字典计数.

## BTreeMap

计数步骤如下:

1. 创建字典, 为了找到相同生存人数的最小年份, 我们使用 BTreeMap 来保证年份的有序.
2. 遍历 `logs` 中的所有记录, 并遍历每条记录的出生到去世间的所有年份, 将本年度加入到字典中
3. 遍历有序字典, 找到有最大生存人数的那一年

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

这个算法的时间复杂度是 `O(n * m)`, 其中 `n` 是人数, `m` 是生存的年份.
空间复杂度是 `O(n)`, 其中 `n``logs` 的年份范围.

## 计数数组

因为年份跨度比较小, 只有100年, 我们可以用栈上的数组来代替 BTreeMap, 其它步骤没有太多变化.

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

这个算法的时间复杂度是 `O(n * m)`, 其中 `n` 是人数, `m` 是生存的年份.
空间复杂度是 `O(n)`, 其中 `n``logs` 的年份范围.

## 前缀和

这个有些不好考虑, 在构造前缀和数组之前, 我们先构造一个辅助数组. 整个解决步骤如下:

1. 创建 `alive` 辅助数组
2. 遍历 `logs` 数组
1. 当一个人出生时, 将 alive 中出生所在年份计数加1, `alive[start_year] += 1`
2. 当一个人去世时, 将 alive 中去世所在年份计数减1, `alive[end_year] -= 1`
3. 创建 `prefix_sum` 前缀和数组, 通过遍历 `alive` 数组
4. 最后遍历 `prefix_sum` 数组, 找到生存人数最多的那个年份

```rust
{{#include src/main.rs:56:89}}
```

这个算法的时间复杂度是 `O(n)`, 空间复杂度是 `O(n)`, 其中 `n``logs` 的年份范围.
14 changes: 7 additions & 7 deletions src/leetcode/1854.maximum-population-year/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ pub fn maximum_population3(logs: Vec<Vec<i32>>) -> i32 {

// 构造数组, 用于记录每年增减的人数,
// 为构造前缀和数组做准备
let mut arr = vec![0; no_years as usize];
let mut alive = vec![0; no_years as usize];
for log in logs {
let start_year_index = (log[0] - start_year) as usize;
let end_year_index = (log[1] - start_year) as usize;
arr[start_year_index] += 1;
arr[end_year_index] -= 1;
alive[start_year_index] += 1;
alive[end_year_index] -= 1;
}

// 构造前缀和数组
let mut prefix_sum = vec![0; arr.len()];
prefix_sum[0] = arr[0];
for i in 1..arr.len() {
prefix_sum[i] = prefix_sum[i - 1] + arr[i];
let mut prefix_sum = vec![0; alive.len()];
prefix_sum[0] = alive[0];
for i in 1..alive.len() {
prefix_sum[i] = prefix_sum[i - 1] + alive[i];
}

// 遍历前缀和数组, 找到最多的人所在的年份
Expand Down
13 changes: 7 additions & 6 deletions src/leetcode/tags/prefix-sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
3. [1422. 分割字符串的最大得分 Maximum Score After Splitting a String](../1422.maximum-score-after-splitting-a-string/index.md)
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. [1893. 检查是否区域内所有整数都被覆盖 Check if All the Integers in a Range Are Covered](../1893.check-if-all-the-integers-in-a-range-are-covered/index.md)
7. [1991. 找到数组的中间位置 Find the Middle Index in Array](../1991.find-the-middle-index-in-array/index.md)
8. [2485. 找出中枢整数 Find the Pivot Integer](../2485.find-the-pivot-integer/index.md)
9. [2574. 左右元素和的差值 Left and Right Sum Differences](../2574.left-and-right-sum-differences/index.md)
10. [2848. 与车相交的点 Points That Intersect With Cars](../2848.points-that-intersect-with-cars/index.md)
11. [3028. 边界上的蚂蚁 Ant on the Boundary](../3028.ant-on-the-boundary/index.md)
6. [1854. 人口最多的年份 Maximum Population Year](../1854.maximum-population-year/index.md)
7. [1893. 检查是否区域内所有整数都被覆盖 Check if All the Integers in a Range Are Covered](../1893.check-if-all-the-integers-in-a-range-are-covered/index.md)
8. [1991. 找到数组的中间位置 Find the Middle Index in Array](../1991.find-the-middle-index-in-array/index.md)
9. [2485. 找出中枢整数 Find the Pivot Integer](../2485.find-the-pivot-integer/index.md)
10. [2574. 左右元素和的差值 Left and Right Sum Differences](../2574.left-and-right-sum-differences/index.md)
11. [2848. 与车相交的点 Points That Intersect With Cars](../2848.points-that-intersect-with-cars/index.md)
12. [3028. 边界上的蚂蚁 Ant on the Boundary](../3028.ant-on-the-boundary/index.md)

## 中等

Expand Down

0 comments on commit 6e166f8

Please sign in to comment.