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
83 changes: 83 additions & 0 deletions MyHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Time Complexity : All Functions - put and remove - O(n) where n is the number of elements in the bucket, get - O(n) where n is the number of elements in the bucket
// 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 :No


// Your code here along with comments explaining your approach :
// I followed the same approach which I did for hashset
// Used a Linked List to handle the collision and used a prime number as the size of the hash map to reduce the collision.
// Here instead of just key we have key-value pair, so I created a class Entry to store the key-value pair and used the same approach as hashset to handle the collision.
// We will use the hash function to get the bucket index and then we will check if the key already exists in the bucket, if it does then we will update the value, if it does not then we will add a new entry to the bucket.
// For get and remove we will use the same approach to find the entry in the bucket and return the value or remove the entry from the bucket.


import java.util.*;

class MyHashMap {

class Entry {
public int key;
public int val;

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

LinkedList<Entry>[] map;
public static int size = 769;

public MyHashMap() {
map = new LinkedList[size];
}

public void put(int key, int value) {
int bucket = key % size;

if (map[bucket] == null) {
map[bucket] = new LinkedList<>();
}
for (Entry e : map[bucket]) {
if (e.key == key) {
e.val = value;
return;
}
}
map[bucket].add(new Entry(key, value));
}

public int get(int key) {
int bucket = key % size;

if (map[bucket] != null) {
for (Entry e : map[bucket]) {
if (e.key == key) {
return e.val;
}
}
}

return -1;
}

public void remove(int key) {
int bucket = key % size;

if (map[bucket] != null) {
Entry toRemove = null;

for (Entry e : map[bucket]) {
if (e.key == key) {
toRemove = e;
break;
}
}

if (toRemove != null) {
map[bucket].remove(toRemove);
}
}
}
}
76 changes: 76 additions & 0 deletions MyHashMap2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

// Time Complexity : All Functions - put and remove - O(n) where n is the number of elements in the bucket, get - O(n) where n is the number of elements in the bucket
// 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 :No


// Your code here along with comments explaining your approach :
// I followed the same approach which we discussed in class for hash set, but here instead of just key we have key-value pair, so I created a class Entry to store the key-value pair and used the same approach as hashset to handle the collision.
// We will use the hash function to get the bucket index and then we will check if the key already exists in the bucket, if it does then we will update the value, if it does
// not then we will add a new entry to the bucket.
// For get and remove we will use the same approach to find the entry in the bucket and return the value or remove the entry from the bucket.

class MyHashMap2 {

int primaryBuckets;
int secondaryBuckets;
Integer[][] storage;

public MyHashMap2() {
this.primaryBuckets=1000;
this.secondaryBuckets=1000;

this.storage=new Integer[primaryBuckets][];

}

private int getPrimaryHash(int key)
{
return key % primaryBuckets;
}

private int getsecondaryHash(int key)
{
return key/secondaryBuckets;
}

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

if(storage[primaryIndex]==null)
{
if(primaryIndex==0)
{
storage[primaryIndex]=new Integer[secondaryBuckets+1];

}
else
{
storage[primaryIndex] = new Integer[secondaryBuckets];
}
}
int secondaryIndex=getsecondaryHash(key);
storage[primaryIndex][secondaryIndex]=value;
}

public int get(int key) {
int primaryIndex = getPrimaryHash(key);
if(storage[primaryIndex]==null)
{
return -1;
}
int secondaryIndex=getsecondaryHash(key);
return storage[primaryIndex][secondaryIndex]!=null ? storage[primaryIndex][secondaryIndex] : -1;
}

public void remove(int key) {
int primaryIndex = getPrimaryHash(key);
if(storage[primaryIndex] == null){
return;
}
int secondaryIndex = getsecondaryHash(key);
storage[primaryIndex][secondaryIndex] = null; // remove key
}

}
65 changes: 65 additions & 0 deletions QueueFromStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

// Time Complexity : All Functions - O(1) for push, amotized O(1) for pop and peek
// 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 :No


// Your code here along with comments explaining your approach : Queue is usually FIFO, But stack is LIFO
// We are given 2 stacks, we have 2 menthods,
// We will push element in stack in and when we get pop or peek we will move it to stack out and pop or peek from stack out, if stack out is empty then we will move all element from stack in to stack out and then pop or peek from stack out. This way we can maintain the order of queue using 2 stacks.
// Other wise we will push element in stack in and when next element comes we will push the element from in to out and insert the new element in stack in
// push back the element from stack out to stack in, this way we can maintain the order of queue using 2 stacks. But this approach will have O(n) time complexity for push operation, so we will go with the first approach which has O(1) time complexity for push operation and amotized O(1) time complexity for pop and peek operation.
// We will go with the first approach which has O(1) time complexity for push operation and amotized O(1) time complexity for pop and peek operation.

import java.util.Stack;

class MyQueue {
Stack<Integer> inStack;
Stack<Integer> outStack;
public MyQueue() {

this.inStack=new Stack<>();
this.outStack=new Stack<>();

}

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

public int pop() {
if(outStack.isEmpty())
{
while(!inStack.isEmpty())
{
outStack.push(inStack.pop());
}
}
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();
}
}

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