-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackk.java
More file actions
112 lines (89 loc) · 3.13 KB
/
Stackk.java
File metadata and controls
112 lines (89 loc) · 3.13 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.util.*;
public class Stackk {
// (1) Given a string. Remove all consecutive pair of duplicate characters recursively.
// TC: O(N)
public static String removeDuplicates(String s) {
Stack<Character> st = new Stack<>();
for(int i=0; i<s.length(); i++) {
char ch = s.charAt(i);
if(st.isEmpty() || st.peek() != ch) st.push(ch);
else st.pop();
}
StringBuilder sb = new StringBuilder();
while(!st.isEmpty()) {
sb.append(st.pop());
}
return sb.reverse().toString();
}
// (2) Given an array. For every index i find the nearest (wrt index) smaller element on the left side of i. [0, i-1] (smaller than A[i])
// TC: O(N) SC: O(N)
public static int[] prevSmallNum(int[] A) {
int N = A.length;
int[] arr = new int[N];
Stack<Integer> st = new Stack<>();
for(int i=0; i<N; i++) {
while(!st.empty() && st.peek() >= A[i]) {
st.pop();
}
if(st.isEmpty()) {
arr[i] = -1;
}
else if(!st.isEmpty() && st.peek() < A[i]) {
arr[i] = st.peek();
}
st.push(A[i]);
}
return arr;
}
// (3) Given an array. For every index i find the index of the nearest (wrt index) smaller element on the left side of i. [0, i-1] (smaller than A[i])
public static int[] prevSmallIdx(int[] A) {
int N = A.length;
int[] arr = new int[N];
Stack<Integer> st = new Stack<>();
for(int i=0; i<N; i++) {
while(!st.empty() && A[st.peek()] >= A[i]) {
st.pop();
}
if(st.isEmpty()) {
arr[i] = -1;
}
else if(!st.isEmpty() && A[st.peek()] < A[i]) {
arr[i] = st.peek();
}
st.push(i);
}
return arr;
}
// (4) Given an array with lengths of the bars of a histogram.
// TC: O(N)
public static int maxArHist(int[] A) {
int N = A.length;
int[] NSL = new int[N];
Stack<Integer> st = new Stack<>();
for(int i=0; i<N; i++) {
while(!st.isEmpty() && A[st.peek()] >= A[i]) {
st.pop();
}
NSL[i] = st.isEmpty() ? -1 : st.peek();
st.push(i);
}
st.clear();
int[] NSR = new int[N];
for(int i=N-1; i>=0; i--) {
while(!st.isEmpty() && A[st.peek()] >= A[i]) {
st.pop();
}
NSR[i] = st.isEmpty() ? N : st.peek();
st.push(i);
}
int maxArea = 0;
for(int i=0; i<N; i++) {
int width = NSR[i] - NSL[i] - 1;
int area = A[i] * width;
maxArea = Math.max(area, maxArea);
}
return maxArea;
}
public static void main(String[] args) {
}
}