-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackPractice.java
More file actions
115 lines (96 loc) · 2.75 KB
/
StackPractice.java
File metadata and controls
115 lines (96 loc) · 2.75 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
package Stack;
import java.util.HashMap;
import java.util.Objects;
import java.util.Stack;
public class StackPractice {
// Time Complexity: O(n)
// Space: O(n)
public boolean isValid(String s) {
// Write your code here
HashMap<String, String> brackets = new HashMap<>();
brackets.put("(", ")");
brackets.put("{", "}");
brackets.put("[", "]");
if(s.length()%2!=0){
return false;
}
Stack<String> stack2 = new Stack<String>();
for (int i=0; i < s.length();i++){
if(brackets.containsKey(String.valueOf(s.charAt(i)))){
stack2.push(String.valueOf(s.charAt(i)));
}
else {
if(stack2.isEmpty()){
return false;
}
if(!Objects.equals(brackets.get(stack2.peek()), String.valueOf(s.charAt(i)))){
return false;
}
else {
stack2.pop();
}
}
}
return stack2.isEmpty();
}
public static int calPoints(String[] operations) {
Stack<Integer> stack = new Stack<>();
int temp;
int res = 0;
for (String operation : operations) {
if (Character.isDigit(operation.charAt(0)) || operation.charAt(0)=='-') {
stack.push(Integer.parseInt(operation));
}
else if(operation.equals("D")){
temp = stack.peek();
temp = temp *2;
stack.push(temp);
}
else if(operation.equals("C")){
stack.pop();
}
else if(operation.equals("+")) {
int temp1 = stack.pop();
int temp2 = stack.pop();
temp = temp1+temp2;
stack.push(temp2);
stack.push(temp1);
stack.push(temp);
}
}
while(!stack.isEmpty()){
res+= stack.pop();
}
return res;
}
}
// Time Complexity: O(1)
// Space: O(n)
// Trick: have 2 stacks 1 for values and one for keeping track of minimum value.
class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minStack;
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
public void push(int val) {
stack.push(val);
if(minStack.isEmpty()){
minStack.push(val);
}
else {
minStack.push(Math.min(val,minStack.peek()));
}
}
public void pop() {
minStack.pop();
stack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}