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
57 changes: 57 additions & 0 deletions MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.example;

import java.util.*;

// Time Complexity : O(1)
// Space Complexity : O(1)
// 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 basic idea is to use a single stack and initially the min is assumed to be infinity. When a value less the current minimum is identified,
// then push the previous minimum followed by the new value. And when popping, pop the top of the stack and the next element below it if the
// popped value is currently the minimum, then the lastly popped element will be the new minimum

// Using one stack
class MinStack {
Stack<Integer> stack;
int min;

public MinStack() {
this.min = Integer.MAX_VALUE;
this.stack = new Stack<>();
}

public void push(int val) {
if(min>= val){
stack.push(min);
min = val;
}
stack.push(val);
}

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(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
77 changes: 77 additions & 0 deletions MyHashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.example;

// Time Complexity : O(1)
// Space Complexity : O(1)
// 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 values are mapped to the array by using double hashing to resolve collisions here(hash1 and hash2). The primary array storage will save
// the address to the secondary array which in turn will save the elements in it post computing the hash value using hash2. The index range for
// both primary and the secondary array are taken based on square root method on the upper bound provided.


class MyHashSet {
boolean [][] storage;
int buckets; // size of the primary array
int bucketItems; // size of the secondary array

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

private int hash1 (int key){
return key % 1000;
}
private int hash2 (int key){
return key / 1000;
}
public void add(int key) {
// Time Complexity : O(1)
// Space Complexity : O(1)
int bucket = hash1(key);
int bucketItem = hash2(key);

if(storage[bucket] == null){
if(bucket == 0){ // condition to handle 10^6
storage[bucket] = new boolean[bucketItems + 1];
}else{
storage[bucket] = new boolean[bucketItems];
}

}
storage[bucket][bucketItem] = true;
}

public void remove(int key) {
// Time Complexity : O(1)
// Space Complexity : O(1)
int bucket = hash1(key);
int bucketItem = hash2(key);
if(storage[bucket] == null) return;
storage[bucket][bucketItem] = false;

}

public boolean contains(int key) {
// Time Complexity : O(1)
// Space Complexity : O(1)
int bucket = hash1(key);
int bucketItem = hash2(key);
if(storage[bucket] == null) return false;
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);
*/