@@ -830,31 +830,30 @@ class TreeMultiSet<T = number> {
830830
831831<!-- solution: end -->
832832
833- <!-- problem: end -->
834-
835833### Solution 2: Double Stacks
834+
836835We maintain two decreasing monotonic stacks:
837836
838- ``` stackOne `` ` : stores elements that have not yet encountered any greater element to their right.
837+ ` stackOne ` : stores elements that have not yet encountered any greater element to their right.
839838
840- ``` stackTwo `` ` : stores elements that have encountered exactly one greater element to their right.
839+ ` stackTwo ` : stores elements that have encountered exactly one greater element to their right.
841840
842841Algorithm:
843842
844- As we iterate through the array ``` nums `` ` , for current element $nums[ k] $, have these steps:
843+ As we iterate through the array ` nums ` , for current element $nums[ k] $, have these steps:
845844
846- 1 . While ``` stackTwo `` ` is not empty and its top element is less than $nums[ k] $,
845+ 1 . While ` stackTwo ` is not empty and its top element is less than $nums[ k] $,
847846these top elements have found their second next greater element.
848847Pop them and record $nums[ k] $ as the answer for their respective indices.
849848
850- 2 . While ``` stackOne `` ` is not empty and its top element is less than $nums[ k] $,
849+ 2 . While ` stackOne ` is not empty and its top element is less than $nums[ k] $,
851850these top elements have found their first next greater element.
852- Pop them to move them to a temporary list/vector called ``` transporter `` ` .
851+ Pop them to move them to a temporary list/vector called ` transporter ` .
853852
854- 3 . Pop all elements from the back of ``` transporter ``` and push them onto ``` stackTwo `` ` .
855- These elements will naturally maintain the decreasing order in ``` stackTwo `` ` .
853+ 3 . Pop all elements from the back of ` transporter ` and push them onto ` stackTwo ` .
854+ These elements will naturally maintain the decreasing order in ` stackTwo ` .
856855
857- 4 . Push current element $nums[ k] $ into ``` stackOne `` ` for future comparisons.
856+ 4 . Push current element $nums[ k] $ into ` stackOne ` for future comparisons.
858857
859858Our time complexity is $O(n)$, where $n$ is the length of the array nums.
860859
0 commit comments