diff --git a/MyHashMap.java b/MyHashMap.java new file mode 100644 index 00000000..cd9c141e --- /dev/null +++ b/MyHashMap.java @@ -0,0 +1,81 @@ +// Time Complexity : Amortized time complexity for operations - put, get, remove is O(1). +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +class MyHashMap { + class Node{ + int key; + int value; + Node next; + public Node(int key, int value){ + this.key = key; + this.value = value; + } + } + private Node [] storage; + private int hash(int key){ + return key % 10000; + } + + public MyHashMap() { + this.storage = new Node[10000]; + } + + private Node find(Node head, int key){ + Node previous = head; + Node current = head.next; + while(current!=null && current.key != key){ + previous = current; + current = current.next; + } + return previous; + } + + public void put(int key, int value) { + int itemIndex = hash(key); + if(storage[itemIndex] == null){ + storage[itemIndex] = new Node(-1,-1); + } + Node previous = find(storage[itemIndex], key); + if(previous.next == null){ + previous.next = new Node(key, value); + } else{ + previous.next.value = value; + } + } + + public int get(int key) { + int itemIndex = hash(key); + if(storage[itemIndex] == null){ + return -1; + } + Node previous = find(storage[itemIndex], key); + if(previous.next == null){ + return -1; + } + return previous.next.value; + } + + public void remove(int key) { + int itemIndex = hash(key); + if(storage[itemIndex] == null){ + return; + } + Node previous = find(storage[itemIndex], key); + if(previous.next == null){ + return; + } + Node temp = previous.next; + previous.next = temp.next; + temp.next = null; + } +} + +/** + * 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..8b1bc570 100644 --- a/Sample.java +++ b/Sample.java @@ -1,7 +1,56 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : +// Time Complexity : Amortized time complexity for operations - push, pop, peek , Empty is O(1). +// Time Complexity : Worst case time complexity for operations - pop and peek is O(n) when the outStack is Empty. +// Space Complexity : 2 * O(n) = O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No // Your code here along with comments explaining your approach +class MyQueue { + private Stack inStack; + private Stack outStack; + + public MyQueue() { + this.inStack = new Stack<>(); + this.outStack = new Stack<>(); + } + + //Time Complexity - O(1) + public void push(int x) { + inStack.push(x); + } + + //Time Complexity - O(1) + public int pop() { + if(outStack.isEmpty()){ + while(!inStack.isEmpty()){ + outStack.push(inStack.pop()); + } + } + return outStack.pop(); + } + + //Time Complexity - O(1) + public int peek() { + if(outStack.isEmpty()){ + while(!inStack.isEmpty()){ + outStack.push(inStack.pop()); + } + } + return outStack.peek(); + } + + //Time Complexity - O(1) + public boolean empty() { + return inStack.isEmpty() && outStack.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(); + */ \ No newline at end of file