Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from wisdompeak:master #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 13 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Oct 31, 2022
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
3fbfafd
Update 2448.Minimum-Cost-to-Make-Array-Equal_v1.cpp
wisdompeak Oct 31, 2022
389bf20
Create 907.Sum-of-Subarray-Minimums_v2.cpp
wisdompeak Oct 31, 2022
2b247e3
Update Readme.md
wisdompeak Oct 31, 2022
d24a8b1
Create 2453.Destroy-Sequential-Targets.cpp
wisdompeak Oct 31, 2022
fd03047
Update Readme.md
wisdompeak Oct 31, 2022
72e4e89
Create Readme.md
wisdompeak Oct 31, 2022
2c2ef32
Create 2454.Next-Greater-Element-IV.cpp
wisdompeak Oct 31, 2022
3115ac5
Update Readme.md
wisdompeak Oct 31, 2022
4286e56
Create Readme.md
wisdompeak Oct 31, 2022
93ef9c0
Create 2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp
wisdompeak Oct 31, 2022
f912ebf
Update Readme.md
wisdompeak Oct 31, 2022
d2994af
Create Readme.md
wisdompeak Oct 31, 2022
ad0e85c
Update 2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp
wisdompeak Oct 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create Readme.md
  • Loading branch information
wisdompeak authored Oct 31, 2022
commit 4286e56b23e610dd086328ed1b327a8c2021c74b
9 changes: 9 additions & 0 deletions Stack/2454.Next-Greater-Element-IV/Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### 2454.Next-Greater-Element-IV

我们已经知道,常规的Next Greater Element可以用单调栈实现o(n)的解法。我们维护一个单调递减的栈,如果遇到新元素大于栈顶元素,就意味着栈顶元素遇到了next greater element,于是就可以退栈。

在此题里,栈顶元素遇到了next greater selement,并不意味着它就可以一劳永逸地舍弃。我们需要的是the second greater element,于是我们应该对这些元素进行标记,表示他们已经看到了一次next greater。当它们再次遇到greater element的时候,才能记录答案。

那么如何标记呢?如果把常规的单调栈记做stk1,那么我们可以把遇到过next greater的元素拿出来,放入另外一个单调栈里,记做stk2。每次新来一个元素nums[i],先看stk2的栈顶元素是否小于num[i],是的话就意味着这些栈顶元素遇到了the second greater element,就可以记录答案并退栈了。接下来看stk1的栈顶元素是否小于nums[i],同理,是的话就意味着这些栈顶元素遇到过了next greater element,并将其移至stk2中。

这里要注意一定,将stk1的元素移至stk2的过程中,是否会干扰stk2的单调顺序?是不会的。stk2经过退栈之后,栈顶元素一定是大于nums[i]的;而从stk1转移至stk2的这些元素都是小于nums[i]的,所以我们可以放心将转移的元素都堆在stk1的栈顶,依然能保持stk2的递减性质。

AltStyle によって変換されたページ (->オリジナル) /