This commit is contained in:
parent
ac57b13edb
commit
39bb8ffc36
15
include/s0496_next_greater_element_i.hpp
Normal file
15
include/s0496_next_greater_element_i.hpp
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef S0496_NEXT_GREATER_ELEMENT_I_HPP
|
||||
#define S0496_NEXT_GREATER_ELEMENT_I_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <unordered_map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class S0496 {
|
||||
public:
|
||||
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2);
|
||||
};
|
||||
|
||||
#endif
|
@ -19,12 +19,16 @@
|
||||
|
||||
使用场景:通常是一维数组,要寻找任一个元素的右边或者左边第一个比自己大或者小的元素的位置。
|
||||
|
||||
从栈底到栈顶单调递减栈用来寻找右边更大元素,单调递增栈用来寻找右边更小元素。
|
||||
|
||||
时间复杂度:O(n)
|
||||
|
||||
经典题目:
|
||||
|
||||
[739. 每日温度](https://leetcode.com/problems/daily-temperatures/)
|
||||
|
||||
[496. 下一个更大元素 I](https://leetcode.com/problems/next-greater-element-i/)
|
||||
|
||||
## 队列
|
||||
|
||||
使用场景:
|
||||
|
45
src/s0496_next_greater_element_i.cpp
Normal file
45
src/s0496_next_greater_element_i.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include "s0496_next_greater_element_i.hpp"
|
||||
|
||||
// 因为题干假设 nums1 中的元素全部在 nums2 中出现过,
|
||||
// 因此可以用常规的单调栈来处理 nums2 ,只不过在更新 ans
|
||||
// 的时候需要判断一下这个元素是否是 nums1 中的元素
|
||||
|
||||
vector<int> S0496::nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
|
||||
int len1 = nums1.size();
|
||||
int len2 = nums2.size();
|
||||
if (len1 == 0) {
|
||||
return vector<int> {};
|
||||
} else if (len1 == 1) {
|
||||
return vector<int> {-1};
|
||||
}
|
||||
vector<int> ans(len1, -1);
|
||||
// 先用哈希表来存储 nums1 中出现过的元素
|
||||
// key 为元素的值,value 为元素的下标
|
||||
unordered_map<int, int> map;
|
||||
for (int i{0}; i < len1; ++i) {
|
||||
map[nums1[i]] = i;
|
||||
}
|
||||
// 开始遍历 nums2
|
||||
stack<int> s;
|
||||
for (int i{0}; i < len2; ++i) {
|
||||
// 如果栈为空则入栈并 continue
|
||||
if (s.empty()) {
|
||||
s.push(i);
|
||||
continue;
|
||||
}
|
||||
// 循环弹出,不是 if 弹出
|
||||
while (nums2[i] > nums2[s.top()]) {
|
||||
// 如果元素存在于 nums1 中,则更新 ans
|
||||
if (map.count(nums2[s.top()]) > 0) {
|
||||
ans[map[nums2[s.top()]]] = nums2[i];
|
||||
}
|
||||
s.pop();
|
||||
// 栈为空则结束循环
|
||||
if (s.empty()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
s.push(i);
|
||||
}
|
||||
return ans;
|
||||
}
|
19
tests/s0496_next_greater_element_i.cpp
Normal file
19
tests/s0496_next_greater_element_i.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include "s0496_next_greater_element_i.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(Problem496, Case1) {
|
||||
vector<int> nums1{4, 1, 2};
|
||||
vector<int> nums2{1, 3, 4, 2};
|
||||
vector<int> expected{-1, 3, -1};
|
||||
S0496 solution;
|
||||
EXPECT_EQ(solution.nextGreaterElement(nums1, nums2), expected);
|
||||
}
|
||||
|
||||
TEST(Problem496, Case2) {
|
||||
vector<int> nums1{2, 4};
|
||||
vector<int> nums2{1, 2, 3, 4};
|
||||
vector<int> expected{3, -1};
|
||||
S0496 solution;
|
||||
EXPECT_EQ(solution.nextGreaterElement(nums1, nums2), expected);
|
||||
}
|
Loading…
Reference in New Issue
Block a user