diff --git a/Hashmap.java b/Hashmap.java new file mode 100644 index 00000000..ced421c8 --- /dev/null +++ b/Hashmap.java @@ -0,0 +1,94 @@ +// Time Complexity : O(1)-avg , O(n)-worst case +// Space Complexity :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 +//Initialized a node structure and to avoid collisions i initialized a linked list as a sub array to each index and Uses a dummy head node and a custom getPrev method to simplify operations. +// for put i checked if that element is present or not and replaced the value +//for remove and get, i checked whether that element is there or not if not there i return -1 or none +class MyHashMap { + Node[] storage; + int buckets; + + class Node { + int key; + int val; + Node next; + public Node (int key, int value){ + this.key = key; + this.val = value; + } + } + + public MyHashMap() { + this.buckets = 1000; + this.storage = new Node[buckets]; + } + public int getHash(int key){ + return key % 1000; + } + private Node getPrev(Node head, int key){ + Node curr = head; + Node prev = null; + while (curr!= null && curr.key != key){ + prev = curr; + curr = curr.next; + } + return prev; + } + + public void put(int key, int value) { + int hash_1 = getHash(key); + if (storage[hash_1] == null){ + storage[hash_1] = new Node(-1,-1); + storage[hash_1].next = new Node(key, value); + return; + } + Node prev = getPrev(storage[hash_1],key); + if (prev.next == null){ + prev.next = new Node(key,value); + }else{ + prev.next.val = value; + } + } + + public int get(int key) { + int hash_1 = getHash(key); + if(storage[hash_1] == null){ + return -1; + } + Node prev = getPrev(storage[hash_1],key); + if (prev.next == null){ + return -1; + }else{ + return prev.next.val; + } + + } + + public void remove(int key) { + int hash_1 = getHash(key); + if(storage[hash_1] == null){ + return; + } + Node prev = getPrev(storage[hash_1],key); + if (prev.next == null){ + return; + }else{ + Node temp = prev.next; + prev.next = prev.next.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/Hashmap.py b/Hashmap.py new file mode 100644 index 00000000..2e5ca284 --- /dev/null +++ b/Hashmap.py @@ -0,0 +1,72 @@ +# Time Complexity : O(1)-avg , O(n)-worst case +# Space Complexity :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 +# Initialized a node structure and to avoid collisions i initialized a linked list as a sub array to each index and Uses a dummy head node and a custom getPrev method to simplify operations. +# for put i checked if that element is present or not and replaced the value +#for remove and get, i checked whether that element is there or not if not there i return -1 or none + +class MyHashMap: + class Node: + def __init__(self,key,value): + self.key = key + self.value = value + self.next = None + + def __init__(self): + self.buckets = 1000 + self.storage = [None]*self.buckets + def getHash(self,key): + return key % 1000 + def getPrev(self,head,key): + prev = None + curr = head + while(curr!= None and curr.key != key): + prev = curr + curr = curr.next + return prev + + def put(self, key: int, value: int) -> None: + hash_1 = self.getHash(key) + if(self.storage[hash_1] == None): + self.storage[hash_1] = self.Node(-1,-1) + self.storage[hash_1].next = self.Node(key,value) + return + prev = self.getPrev(self.storage[hash_1],key) + if(prev.next == None): + prev.next = self.Node(key,value) + else: + prev.next.value = value + + def get(self, key: int) -> int: + hash_1 = self.getHash(key) + if(self.storage[hash_1]==None): + return -1 + prev = self.getPrev(self.storage[hash_1],key) + if (prev.next == None): + return -1 + return prev.next.value + + + def remove(self, key: int) -> None: + hash_1 = self.getHash(key) + if(self.storage[hash_1]==None): + return + prev = self.getPrev(self.storage[hash_1],key) + if (prev.next == None): + return + else: + temp = prev.next + prev.next = prev.next.next + temp.next = None + + + +# Your MyHashMap object will be instantiated and called as such: +# obj = MyHashMap() +# obj.put(key,value) +# param_2 = obj.get(key) +# obj.remove(key) \ No newline at end of file diff --git a/Queue.java b/Queue.java new file mode 100644 index 00000000..52ee5206 --- /dev/null +++ b/Queue.java @@ -0,0 +1,59 @@ +// Time Complexity : push, isEmpty-O(1), pop, peek-O(n)(worstcase),-O(1)(avg case) +// Space Complexity :O(2n) +// 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 +//Initialized two arrays one to store the original order and the other to extract the 1st element when needed for pop and peek + + + +class MyQueue { + Stack inSt; + Stack outSt; + + public MyQueue() { + this.inSt = new Stack<>(); + this.outSt = new Stack<>(); + } + + public void push(int x) { + inSt.push(x); + + } + + public int pop() { + if(outSt.isEmpty()){ + while(!inSt.isEmpty()){ + outSt.push(inSt.pop()); + } + } + return outSt.pop(); + + } + + public int peek() { + if(outSt.isEmpty()){ + while(!inSt.isEmpty()){ + outSt.push(inSt.pop()); + } + } + return outSt.peek(); + + } + + public boolean empty() { + return inSt.isEmpty() && outSt.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 diff --git a/Queue.py b/Queue.py new file mode 100644 index 00000000..1a5c9751 --- /dev/null +++ b/Queue.py @@ -0,0 +1,45 @@ +# Time Complexity : push, isEmpty-O(1), pop, peek-O(n)(worstcase),-O(1)(avg case) +# Space Complexity :O(2n) +# 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 +#Initialized two arrays one to store the original order and the other to extract the 1st element when needed for pop and peek + +class MyQueue: + + def __init__(self): + self.inSt = [] + self.outSt = [] + + + def push(self, x: int) -> None: + self.inSt.append(x) + + + def pop(self) -> int: + if len(self.outSt) == 0: + while(self.inSt): + self.outSt.append(self.inSt.pop()) + return self.outSt.pop() + + + def peek(self) -> int: + if len(self.outSt) == 0: + while(self.inSt): + self.outSt.append(self.inSt.pop()) + return self.outSt[-1] + + + def empty(self) -> bool: + return len(self.outSt) == 0 and len(self.inSt) == 0 + + + +# Your MyQueue object will be instantiated and called as such: +# obj = MyQueue() +# obj.push(x) +# param_2 = obj.pop() +# param_3 = obj.peek() +# 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