diff --git a/DesignHashMap.C# b/DesignHashMap.C# new file mode 100644 index 00000000..1d76db87 --- /dev/null +++ b/DesignHashMap.C# @@ -0,0 +1,103 @@ +// 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 , As I solved the problem after attending a class + + +// Your code here along with comments explaining your approach +//I implemented a HashMap using an array of buckets +//and resolved collisions using separate chaining with singly linked lists and a dummy head node. + + +public class MyHashMap { + Node[] storage; + int bucket; + private class Node + { + public int key; + public int value; + public Node next; + public Node(int key,int value) + { + this.key = key; + this.value = value; + } + } + + public MyHashMap() { + bucket = 1000; + storage = new Node[bucket]; + } + private int GetHash(int key) + { + return key % bucket; + } + private Node GetPrev(Node head,int key) + { + Node prev=null; + Node cur = head; + while(cur!=null && cur.key!=key) + { + prev=cur; + cur=cur.next; + } + return prev; + } + public void Put(int key, int value) { + int index = GetHash(key); + if(storage[index]==null) + { + storage[index] = new Node(-1, -1); // dummy head + storage[index].next = new Node(key, value); + return; + } + Node prev = GetPrev(storage[index],key); + if (prev.next == null) + prev.next = new Node(key, value); + else + prev.next.value = value; // Key found then update + + } + + public int Get(int key) { + int index = GetHash(key); + + if (storage[index] == null) + return -1; + + Node prev = GetPrev(storage[index], key); + + if (prev.next == null) + return -1; + + return prev.next.value; + } + + public void Remove(int key) { + int index = GetHash(key); + + if (storage[index] == null) + return; + + Node prev = GetPrev(storage[index], key); + + if (prev.next == null) + return; + + // Remove node + Node curr = prev.next; + prev.next = curr.next; + curr.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.C# b/Sample.C# new file mode 100644 index 00000000..afb02bca --- /dev/null +++ b/Sample.C# @@ -0,0 +1,56 @@ +// Time Complexity :Amortized - O(1) Worst Case - O(n) +// Space Complexity : O(n) number of elements in the stack +// Did this code successfully run on Leetcode :Yes +// Any problem you faced while coding this :No , As I solved the problem after attending a class + + +// Your code here along with comments explaining your approach +// I have maintained two stacks in and out , for every push elements are added in the in stack , for pop/peek if items in +// the outstack are empty then pop elements from in stack and add it to the out stack + + +public class MyQueue { + + Stack inStack; + Stack outStack; + public MyQueue() { + this.inStack = new Stack(); + this.outStack= new Stack(); + } + + public void Push(int x) { + inStack.Push(x); + } + + public int Pop() { + if(Empty()) return -1; + Peek(); + return outStack.Pop(); + } + + public int Peek() { + + if(outStack.Count == 0) + { + while(inStack.Count !=0) + { + outStack.Push(inStack.Pop()); + } + } + Console.WriteLine(outStack.Peek()); + return outStack.Peek(); + } + + public bool Empty() { + return inStack.Count == 0 && outStack.Count == 0; + } +} + +/** + * 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(); + * bool param_4 = obj.Empty(); + */ \ No newline at end of file diff --git a/Sample.java b/Sample.java deleted file mode 100644 index 1739a9cb..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach