Skip to content

Commit 6e5ea10

Browse files
authored
Create Next Smaller Left.java
1 parent 47688da commit 6e5ea10

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
https://practice.geeksforgeeks.org/problems/help-classmates--141631/1?utm_source=gfg&utm_medium=article&utm_campaign=bottom_sticky_on_article
2+
3+
class Solution{
4+
static List<Integer> leftSmaller(int n, int arr[])
5+
{
6+
//Lets Play with Stack::
7+
List<Integer> res = new ArrayList<>();
8+
Stack<Integer> st = new Stack<>();
9+
for(int i=0;i<n;i++){
10+
if(st.isEmpty()){
11+
res.add(-1);
12+
}else if(st.peek()<arr[i]){
13+
res.add(st.peek());
14+
}else{
15+
while(!st.isEmpty() && st.peek()>=arr[i]){
16+
st.pop();
17+
}
18+
if(st.isEmpty()){
19+
res.add(-1);
20+
}else{
21+
res.add(st.peek());
22+
}
23+
}
24+
st.push(arr[i]);
25+
}
26+
return res;
27+
}
28+
}

0 commit comments

Comments
 (0)