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
60 changes: 60 additions & 0 deletions HashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

// Time Complexity : O(1) for add (amortized), remove and contains
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this :

// this approach uses double hashing to store the keys in a 2D boolean array.
// The primary hash function determines the index of the first dimension,
// while the secondary hash function determines the index of the second dimension.
// This allows us to efficiently store and retrieve keys while minimizing space usage.

class MyHashSet {
int primaryBuckets;
int secondaryBuckets;
boolean[][] storage;

public MyHashSet() {
primaryBuckets = 1000;
secondaryBuckets = 1000;
storage = new boolean[primaryBuckets][];
}

int getPrimaryHash(int key){
return key%primaryBuckets;
}
int getSecondaryHash(int key){
return key/secondaryBuckets;
}

public void add(int key) {
int primaryIndex = getPrimaryHash(key);
if(storage[primaryIndex]==null){
if(primaryIndex == 0){
storage[primaryIndex] = new boolean[secondaryBuckets +1];
}else{
storage[primaryIndex] = new boolean[secondaryBuckets];
}
}
int secondaryIndex = getSecondaryHash(key);
storage[primaryIndex][secondaryIndex] = true;
}

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

public boolean contains(int key) {
int primaryIndex = getPrimaryHash(key);
if(storage[primaryIndex]==null){
return false;
}
int secondaryIndex = getSecondaryHash(key);
return storage[primaryIndex][secondaryIndex];
}
}
47 changes: 47 additions & 0 deletions MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Time Complexity : O(1) for push, pop, top and getMin
// Space Complexity : O(n) where n is the number of elements in the stack
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this :

// This approach uses two stacks:
// a primary stack to store all the elements and
// a min stack to keep track of the minimum element at each level of the primary stack.
// Whenever a new element is pushed onto the primary stack,
// we compare it with the current minimum element (the top of the min stack) and
// push the smaller of the two onto the min stack.

import java.util.Stack;

class MinStack {
Stack<Integer> primaryStack;
Stack<Integer> minStack;
public MinStack() {
primaryStack = new Stack<Integer>();
minStack = new Stack<Integer>();
}

public void push(int val) {
primaryStack.push(val);
if(minStack.isEmpty() || val < minStack.peek()){
minStack.push(val);
} else {
minStack.push(minStack.peek());
}
}

public void pop() {
if(primaryStack.isEmpty()){
return;
}
primaryStack.pop();
minStack.pop();
}

public int top() {
return primaryStack.peek();
}

public int getMin() {
return minStack.peek();
}
}
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.