You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: leetcode/hard/316_remove_duplicate_letters.md
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,8 @@ With this frame of reference we can reword the question into finding the biggest
33
33
From examples 3 and 4, we can then denote that if a letter isn't the last occurring letter, we can build a much larger word that is closer to being lexicographic.
34
34
So by using a monotonic stack, that is increasing by nature, we can achieve examples 1 and 3.
35
35
A set should be obvious to avoid adding duplicate values into the stack.
36
-
However, to achieve examples 2 an 5 with the monotonic stack, we have to add another invariant were we want all last occurring letters.
36
+
However, to achieve examples 2 an 5 with the monotonic stack, we have to add another invariant were we want last occurring letters.
37
+
We can skip letters we have already seen due to the fact that stack is monotonic, the letters in the stack are already in the best position so far, this is to achieve example 3.
37
38
38
39
In summary, we can build the word from left to right using an increasing monotonic stack.
39
40
When it comes to popping off the stack, we will continue to pop from the stack if the new letter is smaller than whats on top of the stack AND if whats on top of the stack isn't the last occurring letter.
0 commit comments