-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathNearestSmallerElement.cpp
53 lines (35 loc) · 981 Bytes
/
NearestSmallerElement.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
51
/*
https://www.interviewbit.com/problems/nearest-smaller-element/
Given an array, find the nearest smaller element G[i] for every element A[i] in the array such that the element has an index smaller than i.
More formally,
G[i] for an element A[i] = an element A[j] such that
j is maximum possible AND
j < i AND
A[j] < A[i]
Elements for which no smaller element exist, consider next smaller element as -1.
Example:
Input : A : [4, 5, 2, 10, 8]
Return : [-1, 4, -1, 2, 2]
Example 2:
Input : A : [3, 2, 1]
Return : [-1, -1, -1]
*/
vector<int> Solution::prevSmaller(vector<int> &A)
{
vector<int> v(A.size());
stack<int> st;
for(auto i=0; i<A.size(); i++)
{
while(!st.empty() && A[i] <= A[st.top()])
{
//v[st.top()] = A[i];
st.pop();
}
if(st.empty())
v[i] = -1;
else
v[i] = A[st.top()];
st.push(i);
}
return v;
}