-
Notifications
You must be signed in to change notification settings - Fork 2
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
5 changed files
with
57 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// 队列中可以看到的人数 | ||
// https://leetcode.cn/problems/number-of-visible-people-in-a-queue | ||
// INLINE ../../images/stack/number_of_visible_people_in_a_queue.jpeg | ||
|
||
#include <headers.hpp> | ||
|
||
class Solution { | ||
public: | ||
vector<int> canSeePersonsCount(vector<int> &heights) { | ||
int n = heights.size(); | ||
vector<int> res(n, 0); | ||
vector<int> stack; | ||
|
||
for (int i = n - 1; i >= 0; i--) { | ||
int h = heights[i]; | ||
while (!stack.empty() && stack.back() < h) { | ||
stack.pop_back(); | ||
res[i] += 1; | ||
} | ||
if (!stack.empty()) { | ||
res[i] += 1; | ||
} | ||
stack.push_back(h); | ||
} | ||
return res; | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// 执行编译时间:2024-01-04 09:37:38 | ||
// 执行编译时间:2024-01-05 14:42:19 | ||
#include <gtest/gtest.h> | ||
#include <lib.hpp> | ||
|
||
|
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,25 @@ | ||
#include <stack/number_of_visible_people_in_a_queue.cpp> | ||
|
||
TEST(队列中可以看到的人数, canSeePersonsCount) { | ||
Solution solution; | ||
// 示例 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 个人谁也看不到因为他右边没人。 | ||
vector heights = {10, 6, 8, 5, 11, 9}; | ||
vector result = {3, 1, 2, 1, 1, 0}; | ||
EXPECT_EQ(solution.canSeePersonsCount(heights), result); | ||
|
||
// 示例 2: | ||
// 输入:heights = [5,1,2,3,10] | ||
// 输出:[4,1,1,1,0] | ||
vector heights2 = {5, 1, 2, 3, 10}; | ||
vector result2 = {4, 1, 1, 1, 0}; | ||
EXPECT_EQ(solution.canSeePersonsCount(heights2), result2); | ||
} |