Skip to content

Commit

Permalink
add: 队列中可以看到的人数
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 5, 2024
1 parent a82833d commit 339fdff
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

###

- [队列中可以看到的人数](src/stack/number_of_visible_people_in_a_queue.rs) [栈, 数组, 单调栈]

- LeetCode 1944. 队列中可以看到的人数 <https://leetcode.cn/problems/number-of-visible-people-in-a-queue>

- [从链表中移除节点](src/stack/remove_nodes_from_linked_list.rs) [栈, 递归, 链表, 单调栈]

- LeetCode 2487. 从链表中移除节点 <https://leetcode.cn/problems/remove-nodes-from-linked-list>
Expand Down
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/stack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pub mod shortest_subarray_to_be_removed_to_make_array_sorted;
pub mod valid_parentheses;
pub mod validate_stack_sequences;
pub mod remove_nodes_from_linked_list;
pub mod number_of_visible_people_in_a_queue;
26 changes: 26 additions & 0 deletions src/stack/number_of_visible_people_in_a_queue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 队列中可以看到的人数
// https://leetcode.cn/problems/number-of-visible-people-in-a-queue
// INLINE ../../images/stack/number_of_visible_people_in_a_queue.jpeg

pub struct Solution;

impl Solution {
pub fn can_see_persons_count(heights: Vec<i32>) -> Vec<i32> {
let n = heights.len();
let mut res = vec![0; n];
let mut stack = Vec::new();

for i in (0..n).rev() {
let h = heights[i];
while !stack.is_empty() && *stack.last().unwrap() < h {
stack.pop();
res[i] += 1;
}
if !stack.is_empty() {
res[i] += 1;
}
stack.push(h);
}
res
}
}
1 change: 1 addition & 0 deletions tests/stack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pub mod dinner_plate_stacks_test;
pub mod check_if_word_is_valid_after_substitutions_test;
pub mod minimum_cost_tree_from_leaf_values_test;
pub mod remove_nodes_from_linked_list_test;
pub mod number_of_visible_people_in_a_queue_test;
25 changes: 25 additions & 0 deletions tests/stack/number_of_visible_people_in_a_queue_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use rust_practice::stack::number_of_visible_people_in_a_queue::Solution;

#[test]
fn can_see_persons_count() {
// 示例 1:
// 输入:heights = [10,6,8,5,11,9]
// 输出:[3,1,2,1,1,0]
// 解释:
// 第 0 个人能看到编号为 1 ,2 和 4 的人。
// 第 1 个人能看到编号为 2 的人。
// 第 2 个人能看到编号为 3 和 4 的人。
// 第 3 个人能看到编号为 4 的人。
// 第 4 个人能看到编号为 5 的人。
// 第 5 个人谁也看不到因为他右边没人。
let heights = vec![10, 6, 8, 5, 11, 9];
let result = Solution::can_see_persons_count(heights);
assert_eq!(result, vec![3, 1, 2, 1, 1, 0]);

// 示例 2:
// 输入:heights = [5,1,2,3,10]
// 输出:[4,1,1,1,0]
let heights = vec![5, 1, 2, 3, 10];
let result = Solution::can_see_persons_count(heights);
assert_eq!(result, vec![4, 1, 1, 1, 0]);
}

0 comments on commit 339fdff

Please sign in to comment.