From 39bb8ffc3639a71bc19362e83ac43c231ca7570f Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Fri, 16 Dec 2022 15:45:21 +0800 Subject: [PATCH] s0496 --- include/s0496_next_greater_element_i.hpp | 15 ++++++++ notes/src/stack_and_queue.md | 4 +++ src/s0496_next_greater_element_i.cpp | 45 ++++++++++++++++++++++++ tests/s0496_next_greater_element_i.cpp | 19 ++++++++++ 4 files changed, 83 insertions(+) create mode 100644 include/s0496_next_greater_element_i.hpp create mode 100644 src/s0496_next_greater_element_i.cpp create mode 100644 tests/s0496_next_greater_element_i.cpp diff --git a/include/s0496_next_greater_element_i.hpp b/include/s0496_next_greater_element_i.hpp new file mode 100644 index 0000000..affb4e5 --- /dev/null +++ b/include/s0496_next_greater_element_i.hpp @@ -0,0 +1,15 @@ +#ifndef S0496_NEXT_GREATER_ELEMENT_I_HPP +#define S0496_NEXT_GREATER_ELEMENT_I_HPP + +#include +#include +#include + +using namespace std; + +class S0496 { + public: + vector nextGreaterElement(vector& nums1, vector& nums2); +}; + +#endif diff --git a/notes/src/stack_and_queue.md b/notes/src/stack_and_queue.md index 7a0b669..4517090 100644 --- a/notes/src/stack_and_queue.md +++ b/notes/src/stack_and_queue.md @@ -19,12 +19,16 @@ 使用场景:通常是一维数组,要寻找任一个元素的右边或者左边第一个比自己大或者小的元素的位置。 +从栈底到栈顶单调递减栈用来寻找右边更大元素,单调递增栈用来寻找右边更小元素。 + 时间复杂度:O(n) 经典题目: [739. 每日温度](https://leetcode.com/problems/daily-temperatures/) +[496. 下一个更大元素 I](https://leetcode.com/problems/next-greater-element-i/) + ## 队列 使用场景: diff --git a/src/s0496_next_greater_element_i.cpp b/src/s0496_next_greater_element_i.cpp new file mode 100644 index 0000000..f2b412e --- /dev/null +++ b/src/s0496_next_greater_element_i.cpp @@ -0,0 +1,45 @@ +#include "s0496_next_greater_element_i.hpp" + +// 因为题干假设 nums1 中的元素全部在 nums2 中出现过, +// 因此可以用常规的单调栈来处理 nums2 ,只不过在更新 ans +// 的时候需要判断一下这个元素是否是 nums1 中的元素 + +vector S0496::nextGreaterElement(vector& nums1, vector& nums2) { + int len1 = nums1.size(); + int len2 = nums2.size(); + if (len1 == 0) { + return vector {}; + } else if (len1 == 1) { + return vector {-1}; + } + vector ans(len1, -1); + // 先用哈希表来存储 nums1 中出现过的元素 + // key 为元素的值,value 为元素的下标 + unordered_map map; + for (int i{0}; i < len1; ++i) { + map[nums1[i]] = i; + } + // 开始遍历 nums2 + stack 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; +} diff --git a/tests/s0496_next_greater_element_i.cpp b/tests/s0496_next_greater_element_i.cpp new file mode 100644 index 0000000..61956dd --- /dev/null +++ b/tests/s0496_next_greater_element_i.cpp @@ -0,0 +1,19 @@ +#include "s0496_next_greater_element_i.hpp" + +#include + +TEST(Problem496, Case1) { + vector nums1{4, 1, 2}; + vector nums2{1, 3, 4, 2}; + vector expected{-1, 3, -1}; + S0496 solution; + EXPECT_EQ(solution.nextGreaterElement(nums1, nums2), expected); +} + +TEST(Problem496, Case2) { + vector nums1{2, 4}; + vector nums2{1, 2, 3, 4}; + vector expected{3, -1}; + S0496 solution; + EXPECT_EQ(solution.nextGreaterElement(nums1, nums2), expected); +}