-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} |