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
81 changes: 81 additions & 0 deletions ImplementHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//Approach: Similar to HashSet implementation try using double-hashing method
// to handle collisions

// Time Complexity: O(1)
// Space Complexity: O(1)

class MyHashMap {

int[][] arr;
int buckets;
int bucketItems;

public MyHashMap() {
buckets = 1000;
bucketItems = 1000;
arr = new int[buckets][];
}

public int getPrimaryHash(int key)
{
return key % buckets;
}

public int getSecondaryHash(int key)
{
return key / bucketItems;
}

public void put(int key, int value) {
var firstIndex = getPrimaryHash(key);
var secondIndex = getSecondaryHash(key);
if (arr[firstIndex] == null)
{
if (firstIndex == 0)
{
arr[firstIndex] = new int[bucketItems + 1];
}
else
{
arr[firstIndex] = new int[bucketItems];
}
}
if (value == 0)
{
arr[firstIndex][secondIndex] = value - 2;
}
else
{
arr[firstIndex][secondIndex] = value;
}
}

public int get(int key) {
var firstIndex = getPrimaryHash(key);
var secondIndex = getSecondaryHash(key);

if (arr[firstIndex] == null || arr[firstIndex][secondIndex] == 0)
{
return -1;
}
if (arr[firstIndex][secondIndex] == -2)
{
return 0;
}
return arr[firstIndex][secondIndex];
}

public void remove(int key) {
var firstIndex = getPrimaryHash(key);
var secondIndex = getSecondaryHash(key);
if (arr[firstIndex] == null || arr[firstIndex][secondIndex] == -1) return;
arr[firstIndex][secondIndex] = -1;
}
}
/**
* 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);
*/
50 changes: 50 additions & 0 deletions ImplementQueueUsingStacks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//Approach: The idea is to hold elements in one stack, until a pop() or peek() happens and copy the elements from first stack to another and then pop() or peek()

// Time Complexity for Pop() and Peek() is O(n) (as linear traversal is involved)
// Time Complexity for Push() and empty() is O(1)

// Space Complexity: O(n), as we have to maintain another stack to temporarily hold elements for pop() and peek() operations

class MyQueue {

Stack<Integer> firstStack;
Stack<Integer> secondStack;

public MyQueue() {
firstStack = new Stack<>();
secondStack = new Stack<>();
}

public void push(int x) {
firstStack.push(x);
}

public int pop() {
peek();
return secondStack.pop();
}

public int peek() {
if(secondStack.size() == 0){
while(firstStack.size() != 0)
{
secondStack.push(firstStack.pop());
}
}

return secondStack.peek();
}

public boolean empty() {
return firstStack.size() == 0 && secondStack.size() == 0;
}
}

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