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
98 changes: 98 additions & 0 deletions MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Time Complexity : O(1) for push, pop, top, and getMin
// Space Complexity : O(n) as we store all elements in the stack
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Faced minor difficulty in translating tuple-based approach to Java, I had done in c# before.
// Your code here along with comments explaining your approach
// As a pair we can store the value and the minimum value at that point in the stack.
// So when we push a new value, we can compare it with the current minimum and store the new minimum if necessary.
// This way, we can retrieve the minimum value in O(1) time by looking at the top of the stack.
import java.util.Stack;
class MinStack {

class Pair {
long val;
long min;

Pair(long val, long min) {
this.val = val;
this.min = min;
}
}

Stack<Pair> st;

public MinStack() {
st = new Stack<>();
}
public void push(int val) {
if(st.isEmpty())
{
st.push(new Pair(val,val));
}
else
{
long currMin=Math.min(val,st.peek().min);
st.push(new Pair(val,currMin));
}
}

public void pop() {
if (!st.isEmpty()) {
st.pop();
}
}

public int top() {
if (!st.isEmpty()) {
return (int) st.peek().val;
}
throw new RuntimeException("Stack is empty.");
}

public int getMin() {
if (!st.isEmpty()) {
return (int) st.peek().min;
}
throw new RuntimeException("Stack is empty.");
}
}

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

/**
* 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();
*/
class Main {
public static void main(String args[])
{
MinStack obj = new MinStack();
obj.push(-2);
obj.push(0);
obj.push(-3);
System.out.println(obj.getMin()); // return -3
obj.pop();
System.out.println(obj.top()); // return 0
System.out.println(obj.getMin()); // return -2
MyHashSet obj1 = new MyHashSet();
obj1.add(1);
obj1.add(2);
System.out.println(obj1.contains(1)); // returns true
System.out.println(obj1.contains(3)); // returns false (not found)
obj1.add(2);
System.out.println(obj1.contains(2)); // returns true
obj1.remove(2);
System.out.println(obj1.contains(2)); // returns false (already removed)
}
}
75 changes: 70 additions & 5 deletions Sample.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,72 @@
// Time Complexity :
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :
// Time Complexity :O(1) average, O(n) worst case
// Space Complexity :O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : I took the help of a
// video to understand the concept of hash map and how to implement
// it using separate chaining approach.
// Your code here along with comments explaining your approach
// Seperate chaining approach to handle collisions in hash map
// Linked List is used to store the key value pairs
// Rehashing is done when the load factor exceeds 0.75

import java.util.LinkedList;

// Your code here along with comments explaining your approach


class MyHashSet {
class Entry{
public int key;
public Entry(int key){
this.key = key;
}
}
LinkedList<Entry>[] set;
public static int size = 769;
public MyHashSet() {
set = new LinkedList[size];
}

public void add(int key) {
int bucket = (key % size);
if (set[bucket] == null) set[bucket] = new LinkedList<>();
for (Entry e : set[bucket]) {
if (e.key == key) return;
}
set[bucket].addLast(new Entry(key));
}

public void remove(int key) {
int bucket = (key % size);
if (set[bucket] != null) {
Entry toRemove=null;
for (Entry e : set[bucket]) {
if (e.key == key)
{
toRemove=e;
break;
}
}
if(toRemove!=null) set[bucket].remove(toRemove);
}


}

public boolean contains(int key) {
int bucket = (key % size);
if (set[bucket] != null) {
for (Entry e : set[bucket]) {
if (e.key == key) return true;
}
}
return false;
}
}

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