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
82 changes: 82 additions & 0 deletions CustomHashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Time Complexity : O(N)
// Space Complexity : O(M + N) Where M is your fixed array of 1,000 buckets, N is the actual number of nodes created to store data
// 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
// The CustomHashSet uses an array of 1,000 buckets, where each bucket acts as a Linked List to store keys that share the same hash index.
// The add, remove, and contains methods calculate the index using the modulo operator and then traverse the corresponding list to perform the operation.
// By using a dummy node at the start of every bucket, the code simplifies deletion logic and ensures unique keys by checking for duplicates before every insertion.

public class CustomHashSet {
class Node {
int key;
Node next;

public Node(int key) {
this.key = key;
this.next = null;
}
}
public static final int size = 1000;
public Node[] buckets;


public CustomHashSet() {
buckets = new Node[size];
}

public int getHash(int key) {
return key % size;
}

public void add(int key) {
int index = getHash(key);

if (buckets[index] == null) {
buckets[index] = new Node(-1);
}
Node previousNode = this.buckets[index];
Node currentNode = previousNode.next;

while (currentNode != null) {
if (key == currentNode.key) {
currentNode.key = key;
return;
}
previousNode = currentNode;
currentNode = currentNode.next;
}
Node newNode = new Node(key);
previousNode.next = newNode;

}

public void remove(int key) {
int index = getHash(key);
if (buckets[index] == null) return;
Node previousNode = buckets[index];
Node currentNode = previousNode.next;

while(currentNode != null) {
if (key == currentNode.key) {
previousNode.next = currentNode.next;
}
previousNode = currentNode;
currentNode = currentNode.next;
}
}

public boolean contains(int key) {
int index = getHash(key);
if (buckets[index] == null) return false;
Node currentNode = buckets[index].next;
while(currentNode != null) {
if (key == currentNode.key) {
return true;
}
currentNode = currentNode.next;
}
return false;
}
}
42 changes: 42 additions & 0 deletions MinStackUsingTwoStacks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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
// The mainStack stores all values normally, while the minStack tracks the minimum element by only pushing values that are less than or equal to its current top.
// When you pop, if the removed value is the current minimum, it is popped from the minStack as well to keep the minimums synced with the remaining data.

import java.util.Stack;

class MinStackUsingTwoStacks {
Stack<Integer> mainStack;
Stack<Integer> minStack;

public MinStackUsingTwoStacks() {
mainStack = new Stack<>();
minStack = new Stack<>();
}

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

public void pop() {
int poppedValue = mainStack.pop();
if (poppedValue == minStack.peek())
minStack.pop();
}

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

public int getMin() {
return minStack.peek();
}
}


7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.