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
75 changes: 75 additions & 0 deletions solution1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Time Complexity :
// push(x) -> O(1)
// pop() -> Amortized O(1)
// peek() -> Amortized O(1)
// empty() -> O(1)
//
// Space Complexity :
// O(N) where N is number of elements stored in the queue
//
// Did this code successfully run on Leetcode :
// Yes
//
// Any problem you faced while coding this :
// No major issues. The only thing to be careful about is moving elements
// from one stack to another only when needed, otherwise operations become slow.


// Your code here along with comments explaining your approach

import java.util.Stack;

class MyQueue {

// This stack is used for push operation
private Stack<Integer> inst;

// This stack is used for pop and peek operations
private Stack<Integer> outst;

public MyQueue() {
this.inst = new Stack<>();
this.outst = new Stack<>();
}

public void push(int x) {
// Always push element into input stack
inst.push(x);
}

public int pop() {
// If output stack is empty, move all elements from input stack to output stack
if (outst.isEmpty()) {
while (!inst.isEmpty()) {
outst.push(inst.pop());
}
}
// Now top of outst is the front of the queue
return outst.pop();
}

public int peek() {
// If output stack is empty, move elements first
if (outst.isEmpty()) {
while (!inst.isEmpty()) {
outst.push(inst.pop());
}
}
// Return the front element
return outst.peek();
}

public boolean empty() {
// Queue is empty only if both stacks are 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();
*/
106 changes: 106 additions & 0 deletions solution2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Time Complexity :
// put(key, value) -> Average: O(1), Worst: O(n) (if many keys fall in same bucket)
// get(key) -> Average: O(1), Worst: O(n)
// remove(key) -> Average: O(1), Worst: O(n)
// (n = number of nodes in that bucket)
//
// Space Complexity :
// O(N + B) where N = total pairs stored, B = number of buckets
//
// Did this code successfully run on Leetcode :
// Yes
//
// Any problem you faced while coding this :
// No major issues. Just need to handle collisions using a LinkedList,
// and update value if key already exists.


// Your code here along with comments explaining your approach

import java.util.LinkedList;

class MyHashMap {

// total buckets size
int parentbuckets;

// each index has a linkedlist of nodes (to handle collisions)
LinkedList<Node>[] storage;

// simple node class to store key-value
static class Node {
int key;
int value;
Node(int key, int value) {
this.key = key;
this.value = value;
}
}

public MyHashMap() {
this.parentbuckets = 1000;
storage = new LinkedList[parentbuckets];
}

// hash function to find bucket index
private int getPrimaryHash(int key) {
return Math.floorMod(key, parentbuckets);
}

public void put(int key, int value) {
int index = getPrimaryHash(key);

// if bucket not created, create it
if (storage[index] == null) {
storage[index] = new LinkedList<>();
}

// check if key already exists -> update
for (Node n : storage[index]) {
if (n.key == key) {
n.value = value;
return;
}
}

// if key not found, add new node
storage[index].add(new Node(key, value));
}

public int get(int key) {
int index = getPrimaryHash(key);

// if bucket empty, key not present
if (storage[index] == null) return -1;

// search in that bucket
for (Node n : storage[index]) {
if (n.key == key) return n.value;
}

return -1;
}

public void remove(int key) {
int index = getPrimaryHash(key);

// if bucket doesn't exist, nothing to remove
if (storage[index] == null) return;

// find and remove the node
for (Node n : storage[index]) {
if (n.key == key) {
storage[index].remove(n);
break;
}
}
}
}

/**
* 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);
*/