forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[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
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 389bf20
Create 907.Sum-of-Subarray-Minimums_v2.cpp
wisdompeak 2b247e3
Update Readme.md
wisdompeak d24a8b1
Create 2453.Destroy-Sequential-Targets.cpp
wisdompeak fd03047
Update Readme.md
wisdompeak 72e4e89
Create Readme.md
wisdompeak 2c2ef32
Create 2454.Next-Greater-Element-IV.cpp
wisdompeak 3115ac5
Update Readme.md
wisdompeak 4286e56
Create Readme.md
wisdompeak 93ef9c0
Create 2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp
wisdompeak f912ebf
Update Readme.md
wisdompeak d2994af
Create Readme.md
wisdompeak ad0e85c
Update 2457.Minimum-Addition-to-Make-Integer-Beautiful.cpp
wisdompeak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create Readme.md
- Loading branch information
commit 4286e56b23e610dd086328ed1b327a8c2021c74b
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
Stack/2454.Next-Greater-Element-IV/Readme.md
This file contains hidden or 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,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的递减性质。 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.