-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackQueueHashing.java
More file actions
167 lines (141 loc) · 5.08 KB
/
StackQueueHashing.java
File metadata and controls
167 lines (141 loc) · 5.08 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import java.util.Arrays;
import java.util.HashMap;
import java.util.Stack;
public class StackQueueHashing {
public static void longestSubarrayWithSumZero(int[] A) {
long sum = 0;
int n = A.length;
int length = 0;
HashMap<Long, Integer> map = new HashMap<>();
map.put(sum, -1);
for (int i = 0; i < n; i++) {
sum += A[i];
if (map.containsKey(sum)) {
length = Math.max(length, i - map.get(sum));
} else {
map.put(sum, i);
}
}
System.out.println(length);
}
public static void balancedParenthesis(String s) {
Stack<Character> stack = new Stack<>();
int n = s.length();
for (int i = 0; i < n; i++) {
char el = s.charAt(i);
if (stack.isEmpty() || el == '[' || el == '{' || el == '(') {
stack.push(el);
} else if (!stack.isEmpty() && stack.peek() == '[' && el == ']') {
stack.pop();
} else if (!stack.isEmpty() && stack.peek() == '{' && el == '}') {
stack.pop();
} else if (!stack.isEmpty() && stack.peek() == '(' && el == ')') {
stack.pop();
}
}
System.out.println(stack.isEmpty());
}
public static void finalPrices(int[] prices) {
Stack<Integer> stack = new Stack<>();
int n = prices.length;
int[] ans = new int[n];
int idx = n - 1;
for (int i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && stack.peek() > prices[i]) {
stack.pop();
}
if (stack.isEmpty()) {
ans[idx] = prices[i];
stack.add(prices[i]);
idx--;
} else {
// System.out.println(prices[i]+" peek: "+ stack.peek());
ans[idx] = prices[i] - stack.peek();
stack.add(prices[i]);
idx--;
}
}
System.out.println(Arrays.toString(ans));
}
public static int largestRectangleArea(int[] heights) {
int n = heights.length;
int[] smallL = new int[n];
int[] smallR = new int[n];
Stack<Integer> stack = new Stack<>();
int idx = 0;
for (int i = 0; i < n; i++) {
while (!stack.isEmpty() && heights[i] <= heights[stack.peek()]) {
stack.pop();
}
if (stack.isEmpty()) {
smallL[idx] = -1;
idx++;
stack.push(i);
} else if (heights[i] > heights[stack.peek()]) {
smallL[idx] = stack.peek();
idx++;
stack.push(i);
}
}
while (!stack.isEmpty()) {
stack.pop();
}
idx = n - 1;
for (int i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && heights[i] <= heights[stack.peek()]) {
stack.pop();
}
if (stack.isEmpty()) {
smallR[idx] = n;
idx--;
stack.push(i);
} else if (heights[i] > heights[stack.peek()]) {
smallR[idx] = stack.peek();
idx--;
stack.push(i);
}
}
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
int h = heights[i];
int dist = smallR[i] - smallL[i] - 1;
max = Math.max(max, h * dist);
}
return max;
}
public static void main(String[] args) {
/*
* Problem: Given an array having both positive and negative integers.Find
* length of the
* largest subarray with sum 0.
*/
int[] A = { 1, -2, 1, 2 };
longestSubarrayWithSumZero(A);
/*
* Problem: Given an array having both positive and negative integers.Find
* length of the largest subarray with sum 0.
*/
String s = "[()]{}{[()()]()}";
balancedParenthesis(s);
/*
* You are given an integer array prices where prices[i] is the price of the ith
* item in a shop.
* There is a special discount for items in the shop. If you buy the ith item,
* then you will receive a discount equivalent to prices[j] where j is the
* minimum index such that j > i and prices[j] <= prices[i]. Otherwise, you will
* not receive any discount at all.
* Return an integer array answer where answer[i] is the final price you will
* pay for the ith item of the shop, considering the special discount.
*/
int[] prices = { 8, 4, 6, 2, 3 };
finalPrices(prices);
/*
* Given an array of integers heights representing the histogram's bar height
* where the width of each bar is 1, return the area of the largest rectangle in
* the histogram.
*/
int[] heights = { 2, 1, 5, 6, 2, 3 };
int finalAns = largestRectangleArea(heights);
System.out.println(finalAns);
}
}