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

done Design1 #2303

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

// Time Complexity :o(1)
// Space Complexity :o(n)
// Did this code successfully run on Leetcode : it runs good in leat code
// Any problem you faced while coding this : no


// Your code here along with comments explaining your approach
class MyHashSet {
private boolean[][] storage;
private int buckets;
private int bucketItems;

private int bucket(int key) {
return key % buckets;
}

private int bucketitems(int key) {
return key / bucketItems;
}

public MyHashSet() {
this.buckets = 1000;
this.bucketItems = 1000;
this.storage = new boolean[buckets][];
}

public void add(int key) {
int bucket = bucket(key);
if (storage[bucket] == null) {
if (bucket == 0) {
storage[bucket] = new boolean[bucketItems + 1];
} else {
storage[bucket] = new boolean[bucketItems];
}
}
int bucketItem = bucketitems(key);
storage[bucket][bucketItem] = true;
}

public void remove(int key) {
int bucket = bucket(key);
if (storage[bucket] == null) return;
int bucketItem = bucketitems(key);
storage[bucket][bucketItem] = false;
}

public boolean contains(int key) {
int bucket = bucket(key);
if (storage[bucket] == null) return false;
int bucketItem = bucketitems(key);
return storage[bucket][bucketItem];
}
}

/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.add(key);
* obj.remove(key);
* boolean param_3 = obj.contains(key);
*/
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)
// Space Complexity :o(n)
// Did this code successfully run on Leetcode : it runs good in leat code
// Any problem you faced while coding this : no


// Your code here along with comments explaining your approach
class MinStack {
Stack <Integer>st;
Stack <Integer>minst;
int min;
public MinStack() {
this.min=Integer.MAX_VALUE;
this.st = new Stack<>();
this.minst=new Stack<>();
this.minst.push(this.min);
}

public void push(int val) {
min =Math.min(min,val);
st.push(val);
minst.push(min);
}

public void pop() {
st.pop();
minst.pop();
min = minst.peek();
}

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

public int getMin() {
return min;
}
}

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