diff --git a/HashMap.java b/HashMap.java new file mode 100644 index 00000000..ec40757c --- /dev/null +++ b/HashMap.java @@ -0,0 +1,36 @@ +// Time Complexity : O(1) for put, O(1) for get and remove +// Space Complexity : O(n) where n is the number of key-value pairs in the hash map +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : +// this approach initializes an array of size 1000001 to store the values, +// which may not be the most space-efficient solution if the number of key-value pairs is small. +// However, it provides O(1) time complexity for put, get, and remove operations. + + +// This approach uses a simple array to implement the hash map. +// We initialize an array of size 1000001 to store the values, +// and fill it with -1 to indicate that the key is not present. +// The put method updates the value at the index corresponding to the key, +// the get method returns the value at the index corresponding to the + +import java.util.Arrays; + +class MyHashMap { + int[] map; + public MyHashMap() { + map = new int[1000001]; + Arrays.fill(map, -1); + } + + public void put(int key, int value) { + map[key] = value; + } + + public int get(int key) { + return map[key]; + } + + public void remove(int key) { + map[key] = -1; + } +} \ No newline at end of file diff --git a/QueueWithStacks.java b/QueueWithStacks.java new file mode 100644 index 00000000..46084d57 --- /dev/null +++ b/QueueWithStacks.java @@ -0,0 +1,50 @@ +// Time Complexity : O(1) for push, O(n) for pop and peek in worst case when outStack is empty, O(1) for pop and peek in average case when outStack is not empty +// Space Complexity : O(n) where n is the number of elements in the queue +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : + + +// this approach uses two stacks to implement a queue. +// The inStack is used to store the elements when they are pushed into the queue, +// and the outStack is used to store the elements when they are popped from the queue. +// When we need to pop or peek an element, we check if the outStack is empty. +// If it is empty, we transfer all elements from the inStack to the outStack, +// which reverses the order of the elements and allows us to access the front of the queue. +// If the outStack is not empty, we can directly pop or peek from it. +// This way, we maintain the FIFO order of the queue using two LIFO stacks. + +import java.util.Stack; + +class MyQueue { + Stack inStack; + Stack outStack; + public MyQueue() { + inStack = new Stack(); + 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.isEmpty()){ + while(!inStack.isEmpty()){ + outStack.push(inStack.pop()); + } + } + return outStack.peek(); + } + + public boolean empty() { + return inStack.isEmpty() && outStack.isEmpty(); + } +} \ 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