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[2024] Problems #2167

Open
wants to merge 2 commits 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
57 changes: 57 additions & 0 deletions 2024_Problem2_Design_HashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//705. Design HashSet - https://leetcode.com/problems/design-hashset/description/

//Time Complexity: for all operations is O(1)
//Space Complexity: O(1)

class MyHashSet {

int primaryIndex = 1000; //square root of the number gives the index values
int secondaryIndex = 1001; //to handle an edge case if the number to store is 1000, as the index will be 0-999,
// add an extra space to handle 1000

boolean[][] storage = new boolean[primaryIndex][];

private int hash(int key){
return key % primaryIndex;
}

private int nestedHash(int key){
return key / secondaryIndex;
}
/** Initialize data structure here. */
public MyHashSet() {
this.primaryIndex = primaryIndex;
this.secondaryIndex = secondaryIndex;
this.storage = storage;
}
/** Add key into the HashSet */
public void add(int key) {
int index = hash(key);
int nestedIndex = nestedHash(key);

if(storage[index]==null){
storage[index]=new boolean[secondaryIndex];
}
storage[index][nestedIndex]=true;
}
/** Remove key from the HashSet */
public void remove(int key) {
int index = hash(key);
int nestedIndex = nestedHash(key);
if(storage[index]!=null){
storage[index][nestedIndex]=false;
}
}

/** Returns true if this set contains the specified element */
public boolean contains(int key) {
int index = hash(key);
int nestedIndex = nestedHash(key);
// if storage is not empty -> true
if(storage[index]!= null && storage[index][nestedIndex]){
return true;
}
return false;

}
}
39 changes: 39 additions & 0 deletions 2024_Problem3_MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 155. Min Stack - https://leetcode.com/problems/min-stack/description/

//Time Complexity: O(1) for all operations

class MinStack {

Stack<Integer> stack ;
Stack<Integer> minStack;
int min ;
/** initialize your data structure here. */
public MinStack() {
this.stack = new Stack<>();
this.minStack = new Stack<>();
min = Integer.MAX_VALUE;
minStack.push(min);
}

public void push(int x) {
if(x < min){
min = x;
}
minStack.push(min);
stack.push(x);
}

public void pop() {
minStack.pop();
stack.pop();
min = minStack.peek();
}

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

public int getMin() {
return min;
}
}
59 changes: 59 additions & 0 deletions Problem1_HashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//Time Complexity: O(n/k) -> where n is number of keys; and k is the number of buckets
//Space Complexity: O(n+k) -> where n is number of keys; and k is the number of buckets

class MyHashMap {
int buckets = 1000;
int bucketItems = 1000;
Integer[][] myHashmap = new Integer[buckets][];

/** Initialize your data structure here. */
public MyHashMap() {

}
//generating hash key to locate the bucket where key is stored
private int getBucketIdx(int key){
return key%buckets;
}
//to get key
private int getBucketItemIdx(int key){
return key/buckets;
}

/** value will always be non-negative. */
public void put(int key, int value) {
int bucketIdx = getBucketIdx(key);
int bucketItemIdx = getBucketItemIdx(key);
//if no bucket index present; create
if(myHashmap[bucketIdx] == null)
myHashmap[bucketIdx] = new Integer[bucketItems];
//else assign value to the bucket
myHashmap[bucketIdx][bucketItemIdx] = value;
}

/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
public int get(int key) {
int bucketIdx = getBucketIdx(key);
int bucketItemIdx = getBucketItemIdx(key);

if(myHashmap[bucketIdx] != null && myHashmap[bucketIdx][bucketItemIdx] != null)
return myHashmap[bucketIdx][bucketItemIdx];
return -1;
}

/** Removes the mapping of the specified value key if this map contains a mapping for the key */
public void remove(int key) {
int bucketIdx = getBucketIdx(key);
int bucketItemIdx = getBucketItemIdx(key);

if(myHashmap[bucketIdx] != null && myHashmap[bucketIdx][bucketItemIdx] != null)
myHashmap[bucketIdx][bucketItemIdx] = null;
}
}

/**
* 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);
*/
43 changes: 43 additions & 0 deletions Problem2_MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//Time Complexity: O(1) -> for all operations
//Space Complexity: O(n)-> Stack space

class MinStack {

Stack<Integer> stack = new Stack<>();
int min = Integer.MAX_VALUE;
/** initialize your data structure here. */
public MinStack() {

}

public void push(int x) {
if(x <= min){
stack.push(min);
min = x;
}
stack.push(x);
}

public void pop() {
if(stack.pop() == min){
min = stack.pop();
}
}

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

public int getMin() {
return min;
}
}

/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/