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
36 changes: 36 additions & 0 deletions HashMap.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
50 changes: 50 additions & 0 deletions QueueWithStacks.java
Original file line number Diff line number Diff line change
@@ -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<Integer> inStack;
Stack<Integer> outStack;
public MyQueue() {
inStack = new Stack<Integer>();
outStack = new Stack<Integer>();
}

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();
}
}
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.