Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions ImplementQueueUsingStacks.java
Original file line number Diff line number Diff line change
@@ -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<Integer> mainStack;
private Stack<Integer> 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());
}
}
}
}
91 changes: 91 additions & 0 deletions MyHashMap.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.