diff --git a/daily/June24.cpp b/daily/June24.cpp new file mode 100644 index 0000000..ddb0a3b --- /dev/null +++ b/daily/June24.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; + +class Solution { + public: + /** + * @brief LC: 503: Next Greater Element II + * Use stack + * Time: O(N), Space: O(N) + * + * @param nums + * @return vector + */ + vector nextGreaterElements(vector& nums) { + int len = nums.size(); + vector ans(len, -1); + + stack stk; + for (int i = 2 * len - 1; i >= 0; i--) { + int x = nums[i % len]; + while (!stk.empty() && x >= stk.top()) { + stk.pop(); + } + if (i < len && !stk.empty()) { + ans[i] = stk.top(); + } + stk.push(x); + } + return ans; + } + + /** + * @brief nextGreaterElementsII, but from left to right + * Still Stack + * Time: O(N), Space: O(N) + * + * @param nums + * @return vector + */ + vector nextGreaterElementsII(vector& nums) { + int len = nums.size(); + vector ans(len, -1); + stack stk; + for (int i = 0; i < 2 * len; i++) { + int x = nums[i % len]; + while (!stk.empty() && x > nums[stk.top()]) { + ans[stk.top()] = x; + stk.pop(); + } + if (i < len) { + stk.push(i); + } + } + return ans; + } +}; \ No newline at end of file