diff --git a/ImplementQueueUsingStacks.java b/ImplementQueueUsingStacks.java new file mode 100644 index 00000000..22927992 --- /dev/null +++ b/ImplementQueueUsingStacks.java @@ -0,0 +1,49 @@ +// Time Complexity : O(1) +// 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 +// I use two stacks: mainStack for inputs and outStack for outputs. New elements are always pushed onto mainStack. For pop or peek operations, I only shift elements from mainStack to outStack if outStack is currently empty. This reversal correctly orders the elements for FIFO (First-In-First-Out) access and ensures an amortized O(1) time complexity, as each element is moved between stacks only once. + +import java.util.Stack; + +class ImplementQueueUsingStacks { + + private Stack mainStack; + private Stack outStack; + + public ImplementQueueUsingStacks() { + mainStack = new Stack<>(); + outStack = new Stack<>(); + } + + public void push(int x) { + mainStack.push(x); + } + + public int pop() { + shiftStacks(); + if (outStack.isEmpty()) return -1; // Or throw error + return outStack.pop(); + } + + public int peek() { + shiftStacks(); + if (outStack.isEmpty()) return -1; + return outStack.peek(); + } + + public boolean empty() { + return mainStack.isEmpty() && outStack.isEmpty(); + } + + private void shiftStacks() { + if (outStack.isEmpty()) { + while (!mainStack.isEmpty()) { + outStack.push(mainStack.pop()); + } + } + } +} \ No newline at end of file diff --git a/MyHashMap.java b/MyHashMap.java new file mode 100644 index 00000000..ba0183eb --- /dev/null +++ b/MyHashMap.java @@ -0,0 +1,91 @@ +// Time Complexity : Time complexity is O(1) on average and O(n) in 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 +// This custom HashMap uses an array of 1000 buckets where each bucket is a linked list to store key-value pairs, and it uses a simple hash function (key % 1000) to decide which bucket to put data in. When you add, get, or remove items, it finds the right bucket and then searches through the linked list to handle the operations. +class MyHashMap { + + static class Node { + private int key; + private int value; + private Node next; + + Node(int key, int value) { + this.key = key; + this.value = value; + this.next = null; + } + } + + private static final int SIZE = 1000; + private Node[] buckets; + + public MyHashMap() { + buckets = new Node[SIZE]; + } + + private int getHash(int key) { + return key % SIZE; + } + + public void put(int key, int value) { + int index = getHash(key); + if (buckets[index] == null) { + buckets[index] = new Node(-1, -1); + } + + Node previousNode = buckets[index]; + Node currentNode = previousNode.next; + + while (currentNode != null) { + if (currentNode.key == key) { + currentNode.value = value; + return; + } + previousNode = currentNode; + currentNode = currentNode.next; + } + + previousNode.next = new Node(key, value); + } + + public int get(int key) { + int index = getHash(key); + + if (buckets[index] == null) { + return -1; + } + + Node currentNode = buckets[index].next; + while (currentNode != null ){ + if (currentNode.key == key) { + return currentNode.value; + } + currentNode = currentNode.next; + } + return -1; + } + + public void remove(int key) { + int index = getHash(key); + + if (buckets[index] == null) { + return; + } + + Node previousNode = buckets[index]; + Node currentNode = previousNode.next; + + while (currentNode != null) { + if (currentNode.key == key) { + previousNode.next = currentNode.next; + return; + } + previousNode = currentNode; + currentNode = currentNode.next; + } + } +} \ 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