This algorithm can be used for leetcode 456.
The problem is Stack-sortable permutation. The problem is to try to sort a array of numbers using only a single stack.
The input array is stack-sortable only if it does not contain 231
pattern.
As a result, we can check if the input sequence contains 231
pattern by checking if it is stack-sortable.
Also, we can check if the input sequence contains 132
pattern by reverse the input sequence first, then, check if it contains 231
pattern by checking if the reversed sequence is stack-sortable.
The algorithm is as following:
1 | vector<int> stackSort(const vector<int>& nums) { |
If the given input has a 231
pattern, say input is 231
, before 3
is pushed onto stack, 2
will be popped and added to result, but, 1
is less than 2
so it should be added before 2
.
Another way to solve the 132 Pattern
problem is as following:
We want to find s1 < s3 < s2
. We start from the end of the input, and push the elements we get into a stack.
When the value we encounter is greater than the top of the stack, it means that we have a candidate for s2
. This is because there are some values after s2
that is less than s2
.
Then, we start popping out the numbers from stack that are less than s2
we found. And, we keep track of the max
of the numbers we popped out. This max
number is the s3
we want to use for this s2
. We want to keep the max
because we want to have more possibility for s1
.
If the value we encounter is smaller than the s3
we recorded, we can say that we have found a s1
. This is because the s3
we recorded must have a s2
corresponding to it (s3 < s2
). If we found a s1
such that s1 < s3
, we know that we have found the s1 < s3 < s2
sequence.
This solution can be found here-space-and-time-solution-(8-lines)-with-detailed-explanation.).
The implementation is as following:
1 | class Solution { |