diff --git a/Problem5.java b/Problem5.java new file mode 100644 index 00000000..3839dcb9 --- /dev/null +++ b/Problem5.java @@ -0,0 +1,39 @@ +// Time Complexity : O(1) +// Space Complexity : O(N) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +class MyHashMap { + int[] map; + boolean[] exists; + + public MyHashMap() { + map = new int[1000001]; + exists = new boolean[1000001]; + } + + public void put(int key, int value) { + map[key] = value; + exists[key] = true; + } + + public int get(int key) { + if (exists[key]) { + return map[key]; + } + return -1; + + } + + public void remove(int key) { + exists[key] = false; + } +} + +/** + * Your MyHashMap object will be instantiated and called as such: + * MyHashMap obj = new MyHashMap(); + * obj.put(key,value); + * int param_2 = obj.get(key); + * obj.remove(key); + */ \ No newline at end of file diff --git a/Sample.java b/Sample.java index 1739a9cb..6424f251 100644 --- a/Sample.java +++ b/Sample.java @@ -1,7 +1,48 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : +// Time Complexity : Amortized O(1) +// Space Complexity : O(N) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +class MyQueue { + private Stack in; + private Stack out; + + public MyQueue() { + this.in = new Stack<>(); + this.out = new Stack<>(); + } + + public void push(int x) { + in.push(x); + } + + public int pop() { + peek(); + return out.pop(); + } + + public int peek() { + if (out.isEmpty()) { + while(!in.isEmpty()) { + out.push(in.pop()); + } + } + return out.peek(); + } + + public boolean empty() { + return in.isEmpty() && out.isEmpty(); + } +} + +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue obj = new MyQueue(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.peek(); + * boolean param_4 = obj.empty(); + */ // Your code here along with comments explaining your approach