Skip to content

Commit

Permalink
feat: 下一个更大元素IV
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 12, 2023
1 parent c012af6 commit 1e108c2
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

### 排序

- [下一个更大元素 IV](src/sort/next_greater_element_iv.rs) [栈, 数组, 二分查找, 排序, 单调栈, 堆(优先队列)]

- LeetCode 2454. 下一个更大元素 IV <https://leetcode.cn/problems/next-greater-element-iv>

- [出租车的最大盈利](src/sort/maximum_earnings_from_taxi.rs) [数组, 二分查找, 动态规划, 排序]

- LeetCode 2008. 出租车的最大盈利 <https://leetcode.cn/problems/maximum-earnings-from-taxi>
Expand Down
Binary file added images/sort/next_greater_element_iv.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/sort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ pub mod mice_and_cheese;
pub mod compare_strings_by_frequency_of_the_smallest_character;
pub mod greatest_sum_divisible_by_three;
pub mod maximum_earnings_from_taxi;
pub mod next_greater_element_iv;
28 changes: 28 additions & 0 deletions src/sort/next_greater_element_iv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 下一个更大元素 IV
// https://leetcode.cn/problems/next-greater-element-iv
// INLINE ../../images/sort/next_greater_element_iv.jpeg

pub struct Solution;

impl Solution {
pub fn second_greater_element(nums: Vec<i32>) -> Vec<i32> {
let mut ans = vec![-1; nums.len()];
let mut s = Vec::new();
let mut t = Vec::new();
for (i, &x) in nums.iter().enumerate() {
while !t.is_empty() && nums[*t.last().unwrap()] < x {
ans[t.pop().unwrap()] = x;
}

let mut j = s.len();

while j > 0 && nums[s[j - 1]] < x {
j -= 1;
}

t.extend(s.drain(j..));
s.push(i);
}
ans
}
}
1 change: 1 addition & 0 deletions tests/sort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ pub mod mice_and_cheese_test;
pub mod compare_strings_by_frequency_of_the_smallest_character_test;
pub mod greatest_sum_divisible_by_three_test;
pub mod maximum_earnings_from_taxi_test;
pub mod next_greater_element_iv_test;
28 changes: 28 additions & 0 deletions tests/sort/next_greater_element_iv_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use rust_practice::sort::next_greater_element_iv::Solution;

#[test]
fn second_greater_element() {
// 示例 1:
// 输入:nums = [2,4,0,9,6]
// 输出:[9,6,6,-1,-1]
// 解释:
// 下标为 0 处:2 的右边,4 是大于 2 的第一个整数,9 是第二个大于 2 的整数。
// 下标为 1 处:4 的右边,9 是大于 4 的第一个整数,6 是第二个大于 4 的整数。
// 下标为 2 处:0 的右边,9 是大于 0 的第一个整数,6 是第二个大于 0 的整数。
// 下标为 3 处:右边不存在大于 9 的整数,所以第二大整数为 -1 。
// 下标为 4 处:右边不存在大于 6 的整数,所以第二大整数为 -1 。
// 所以我们返回 [9,6,6,-1,-1] 。
let nums = [2, 4, 0, 9, 6];
assert_eq!(
Solution::second_greater_element(nums.to_vec()),
[9, 6, 6, -1, -1]
);

// 示例 2:
// 输入:nums = [3,3]
// 输出:[-1,-1]
// 解释:
// 由于每个数右边都没有更大的数,所以我们返回 [-1,-1] 。
let nums = [3, 3];
assert_eq!(Solution::second_greater_element(nums.to_vec()), [-1, -1]);
}

0 comments on commit 1e108c2

Please sign in to comment.