-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonotonicIncreasingStack.cpp
50 lines (42 loc) · 1.19 KB
/
monotonicIncreasingStack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
// Function to implement monotonic increasing stack
vector<int> monotonicIncreasing(vector<int> &nums) {
int n = nums.size();
stack<int> st;
vector<int> result;
// Traverse the array
for (int i = 0; i < n; ++i) {
// While stack is not empty AND top of stack is more
// than the current element
while (!st.empty() && st.top() > nums[i]) {
// Pop the top element from the
// stack
st.pop();
}
// Push the current element into the stack
st.push(nums[i]);
}
// Construct the result array from the stack
while (!st.empty()) {
result.insert(result.begin(), st.top());
st.pop();
}
return result;
}
int main() {
// Example usage:
// vector<int> nums = {3, 1, 4, 1, 5, 9, 2, 6};
vector<int> nums = {3, 1, 4, 1, 5, 9, 2, 6};
cout << "input nums : ";
for (int elem : nums)
cout << elem << " ";
cout << endl;
vector<int> result = monotonicIncreasing(nums);
cout << "Monotonic increasing stack: ";
for (int num : result) {
cout << num << " ";
}
cout << endl;
return 0;
}