Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Design-1 #2239

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
78 changes: 78 additions & 0 deletions DesignHashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Time Complexity: O(1)
// Space Complexity: O(n) where n is the number of items stored in the set.
// 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 in three sentences only
/**
* Approach:
* This a problem of Design a HashSet without using any built-in hash table libraries. Here I used 2D boolean array,
* where the first dimension represents primary buckets and the second dimension represents items within each bucket.
* The add(), remove(), and contains() methods use the modulo and division operations to determine the correct bucket
* and position for each key, allowing for fast and efficient storage and search.
*
*/
class MyHashSet {


// Indexes in primary array
private int bucket;
// Indexes in secondary array
private int bucketItems;
// 2D array to store boolean values
private boolean storage[][];

// Constructor initializes the storage array
public MyHashSet() {
bucket = 1000;
bucketItems = 1000;
storage = new boolean[bucket][];
}

// Get the primary bucket index
private int getBucket(int key) {
return key % bucket;
}

// Get the index in the secondary array
private int getBucketItem(int key) {
return key / bucketItems;
}

// Adds the key to the set
public void add(int key) {
int bucket = getBucket(key);
int bucketItem = getBucketItem(key);

if (storage[bucket] == null) {
// initialize an array
if (bucket == 0) {
storage[bucket] = new boolean[bucketItems + 1];
} else {
storage[bucket] = new boolean[bucketItems];
}
}

storage[bucket][bucketItem] = true;
}

// Removes the key from the set
public void remove(int key) {
int bucket = getBucket(key);
int bucketItem = getBucketItem(key);

if (storage[bucket] == null) return;

storage[bucket][bucketItem] = false;
}

// Checks if the key exists in the set
public boolean contains(int key) {
int bucket = getBucket(key);
int bucketItem = getBucketItem(key);

if (storage[bucket] == null) return false;

return storage[bucket][bucketItem] == true;
}
}
58 changes: 58 additions & 0 deletions MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Time Complexity: O(1)
// Space Complexity: O(n)
// 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 in three sentences only
/**
* Approach:
* In this problem I used two stacks, one to store the elements and another to track the minimum value at each level.
* Every time a new element is pushed, the minimum value is updated and stored in the second stack.
* In the pop method, Popped the top element from the stack and updated the minimum from minStack.
*/
class MinStack {

// Main stack to hold the elements
Stack<Integer> st;
// Secondary stack to track the minimum value at each level of the stack
Stack<Integer> minStack;
// Variable to hold the current minimum value
int min;

// Constructor to initialize the stacks and set the initial minimum value to Integer.MAX_VALUE
public MinStack() {
st = new Stack<>();
minStack = new Stack<>();
min = Integer.MAX_VALUE;
minStack.push(min);
}

// Push a new value onto the stack and update the minStack with the current minimum
public void push(int val) {
// If the new value is smaller than or equal to the current minimum, update min
if(val <= min) {
min = val;
}
st.push(val); // Push the value onto the main stack
minStack.push(min); // Push the current minimum onto the minStack

}

// Pop the top element from the stack and update the minimum from minStack
public void pop() {
st.pop();
minStack.pop();
min = minStack.peek();
}

// Return the top element of the stack without removing it
public int top() {
return st.peek();
}

// Return the current minimum value from the stack
public int getMin() {
return min;
}
}