Skip to content

Commit 1bc46b6

Browse files
committed
- Add: Java solutions
1 parent 006af5e commit 1bc46b6

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

LeetCode/155. Min Stack.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class MinStack {
2+
LinkedList<Node> st = new LinkedList<>();
3+
4+
public MinStack() {} //SC: O(count of elements)
5+
6+
public void push(int val) {
7+
if (!st.isEmpty()) {
8+
st.push(new Node(val, st.peek().minVal));
9+
} else {
10+
st.push(new Node(val, Integer.MAX_VALUE));
11+
}
12+
} //TC: O(1)
13+
14+
public void pop() {
15+
st.poll();
16+
} //TC: O(1)
17+
18+
public int top() {
19+
return st.peek().val;
20+
} //TC: O(1)
21+
22+
public int getMin() {
23+
return st.peek().minVal;
24+
} //TC: O(1)
25+
}
26+
class Node {
27+
int minVal = Integer.MAX_VALUE;
28+
int val = 0;
29+
Node(int val, int lastMin) {
30+
this.val = val;
31+
this.minVal = Math.min(lastMin, this.val);
32+
}
33+
}
34+
35+
/**
36+
* Your MinStack object will be instantiated and called as such:
37+
* MinStack obj = new MinStack();
38+
* obj.push(val);
39+
* obj.pop();
40+
* int param_3 = obj.top();
41+
* int param_4 = obj.getMin();
42+
*/

Pramp/Flatten a Dictionary.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class Solution {
5+
6+
static HashMap<String, String> flattenDictionary(HashMap<String, Object> dict) {
7+
HashMap<String, String> res = new HashMap<>();
8+
class Helper {
9+
void dfs(String prefix, Object o) {
10+
if (o instanceof Map) {
11+
for (Map.Entry<String, Object> e : ((Map<String, Object>) o).entrySet()) {
12+
String newPrefix = prefix;
13+
if (e.getKey() != "" && prefix != "") {
14+
newPrefix += ".";
15+
}
16+
dfs(newPrefix + e.getKey(), e.getValue());
17+
}
18+
} else {
19+
res.put(prefix, (String)o);
20+
}
21+
}
22+
}
23+
Helper h = new Helper();
24+
for (Map.Entry<String, Object> e : dict.entrySet()) {
25+
h.dfs(e.getKey(), e.getValue());
26+
}
27+
28+
return res;
29+
}
30+
31+
public static void main(String[] args) {
32+
33+
}
34+
35+
}

0 commit comments

Comments
 (0)