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 #2292

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
89 changes: 89 additions & 0 deletions MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Time Complexity : O(1)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


class MinStack {
Stack s1;
Stack s2;

/**
* Initialise 2 stacks in constructor.
* First stack inputs all elements.
* Second stack inputs minimum elements.
*/
public MinStack() {
s1=new Stack();
s2=new Stack();
}

/**
* Push value in first stack. If this value is less than top value of stack2 then push into stack2 as well.
*
* @param val
*/
public void push(int val) {
s1.push(val);
if(s2.isEmpty() || s2.peek()>=val){
s2.push(val);
}
}

/**
* removes top element of first stack. If this element is equal to top element of stack2 then remove that as well.
*/
public void pop() {
if(!s1.isEmpty()){
int top=s1.peek();
s1.pop();
if(s2.peek()==top){
s2.pop();
}
}

}

/**
* returns top element of stack1
* @return
*/
public int top() {
if(s1.isEmpty()) {
return -1;
}
return s1.peek();
}

/**
* returns top element of stack2
* @return
*/
public int getMin() {
if(s2.isEmpty()) {
return -1;
}
return s2.peek();
}

// Driver code
public static void main(String[] args)
{

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

/**
* 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();
*/
92 changes: 92 additions & 0 deletions MyHashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

// Time Complexity : O(1)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


class MyHashSet {

private boolean[][] storage;
//primary array size
private int buckets;
//secondary array size
private int bucketitems;

//logic for first hash function
private int hash1(int key){
return key%buckets;
}

//logic for second hash function
private int hash2(int key){
return key/bucketitems;
}

//constructor to initialise bucket size, and 2D array. Since the range of values is from 0-10^6, we assign buckets size as sqrt(10^6)=1000
public MyHashSet() {
this.buckets = 1000;
this.bucketitems=1000;
//initiate primary array of nulls
this.storage =new boolean[buckets][];
}

//Find the index in primary array using hash1 method. If the secondary array is not initialised at the index then
// initialise the array. Find the index in secondary array using hash2 function. Mark that index as true.
public void add(int key) {
int bucket=hash1(key);
if(storage[bucket]==null){
if(bucket==0){
//in 0th index we have 1 extra collsion as 1million is included.
storage[bucket]=new boolean[bucketitems+1];
}else{
storage[bucket]=new boolean[bucketitems];
}
}
int bucketinside = hash2(key);
storage[bucket][bucketinside]=true;
}

// Find the first index. If there is no array on that index then the key is not present, else find the second index.
// Mark this location as false.
public void remove(int key) {
int bucket=hash1(key);
if(storage[bucket]==null){
return;
}else{
int bucketitem=hash2(key);
storage[bucket][bucketitem]=false;
}
}

// Find the first index. If there is no array on that index then the key is not present so return false, else find the second index.
// Return the boolean value present at that location.
public boolean contains(int key) {
int bucket=hash1(key);
if(storage[bucket]==null){
return false;
}else{
int bucketitem=hash2(key);
return storage[bucket][bucketitem];
}
}

// Driver code
public static void main(String[] args)
{
// MyHashSet object will be instantiated and called as such:
MyHashSet obj = new MyHashSet();
obj.add(1);
obj.remove(1);
boolean param_3 = obj.contains(1);
System.out.print(param_3);
}
}

/**
* 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);
*/