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
77 changes: 77 additions & 0 deletions HashMapImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* 1. Implementing Hashmap using Linear chaining for resolving collisions.
* 2. We use Linked list for underling data structure.
* 3. For put/get/remove we will first find previous node.
* 4. Time Complexity for put/get/remove operations is amortized O(1)
*/
class MyHashMap2 {

private int bucketSize = 10000;
private Node[] nodes;

class Node {
int val;
int key;
Node next;

public Node(int key, int val) {
this.key = key;
this.val = val;
}
}

public MyHashMap2() {
this.nodes = new Node[bucketSize];
}

private int hash1(int key) {
return key % bucketSize;
}

private Node find(int key, Node head) {
Node previous = head;
Node current = head.next;
while (current != null && current.key != key) {
previous = current;
current = current.next;
}
return previous;
}

//Time Complexity : Amortized O(1)
public void put(int key, int value) {
int idx = hash1(key);
if (nodes[idx] == null) {
//create a dummy node at the head
nodes[idx] = new Node(-1, -1);
}
Node previous = find(key, nodes[idx]);
if (previous.next == null) {
previous.next = new Node(key, value);
} else {
previous.next.val = value;
}

}

//Time Complexity : Amortized O(1)
public int get(int key) {
int idx = hash1(key);
if (nodes[idx] == null) return -1;
Node previous = find(key, nodes[idx]);
if(previous.next == null) return -1;

return previous.next.val;
}

//Time Complexity : Amortized O(1)
public void remove(int key) {
int idx = hash1(key);
if (nodes[idx] == null) return;
Node previous = find(key, nodes[idx]);
if(previous.next == null) return;

previous.next = previous.next.next;
}

}
46 changes: 46 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 1. Creating HashSet and use double hashing for resolving collisions.
* 2. Use 3D square matrix/array for storing keys and values.
*/
class MyHashMap {

private int bucketSize = 1000;
private int[][][] buckets = new int[bucketSize][][];

private int hash1(int key) {
return key % bucketSize;
}

private int hash2(int key) {
return key / bucketSize;
}

public MyHashMap() {
}

public void put(int key, int value) {
if (buckets[hash1(key)] == null) {
if (hash1(key) == 0)
buckets[hash1(key)] = new int[bucketSize + 1][];
else
buckets[hash1(key)] = new int[bucketSize][];
}
if(buckets[hash1(key)][hash2(key)] == null)
buckets[hash1(key)][hash2(key)] = new int[1];

buckets[hash1(key)][hash2(key)][0] = value;

}

public int get(int key) {
if (buckets[hash1(key)] == null || buckets[hash1(key)][hash2(key)] == null)
return -1;
return buckets[hash1(key)][hash2(key)][0];
}

public void remove(int key) {
if (buckets[hash1(key)] == null || buckets[hash1(key)][hash2(key)] == null)
return;
buckets[hash1(key)][hash2(key)] = null;
}
}
47 changes: 47 additions & 0 deletions QueueUsingStacks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.Stack;

/**
* Implementing Queue using 2 stack, IN stack and OUT stack.
* We push values in IN Stack and Pop from OUT stack.
* When Pop is called, check if OUT stack is empty
* a) If emtpy, push all value from IN to OUT stack, and call pop() on OUT stack
* b) If Not empty, call pop() on OUT Stack
* Push Time Complexity : O(n)
* Pop Time Complexity : Amortized O(1)
*/
class MyQueue2 {
private Stack<Integer> in = new Stack<>();
private Stack<Integer> out = new Stack<>();

public MyQueue2() {
}

// Time Complexity : O(n)
public void push(int x) {
in.push(x);
}

// Time Complexity : Amortized O(1)
public int pop() {
if(out.isEmpty()){
while (!in.isEmpty()){
out.push(in.pop());
}
}
return out.pop();
}

// Time Complexity : O(1)
public int peek() {
if(out.isEmpty()){
while (!in.isEmpty()){
out.push(in.pop());
}
}
return out.peek();
}

public boolean empty() {
return in.isEmpty() && out.empty();
}
}
54 changes: 49 additions & 5 deletions Sample.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
// Time Complexity :
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :
/**
* Implementing Queue using 2 stack.
* When pushing value
* a) If stack is empty push the value to the stack.
* b) If Stack is not empty
* i) Pop all the values to temp stack
* ii) Push the current value
* iii) Push all the values from temp stack back to the stack.
* Push Time Complexity : O(n)
* Pop Time Complexity : O(1)
*/

import java.util.Stack;

// Your code here along with comments explaining your approach
class MyQueue {
private Stack<Integer> stack = new Stack<>();
private Stack<Integer> tempStack = new Stack<>();

public MyQueue() {
}

// Time Complexity : O(n)
public void push(int x) {
if(stack.isEmpty()) stack.push(x);
else {
while (!stack.isEmpty()){
tempStack.push(stack.pop());
}
stack.push(x);
while (!tempStack.isEmpty()){
stack.push(tempStack.pop());
}
}
}

// Time Complexity : O(1)
public int pop() {
return stack.pop();
}

// Time Complexity : O(1)
public int peek() {
// Placeholder return value
return stack.peek();
}

public boolean empty() {
// Placeholder return value
return stack.isEmpty();
}
}