File tree Expand file tree Collapse file tree 2 files changed +77
-0
lines changed Expand file tree Collapse file tree 2 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 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+ */
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments