diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..ab1f4164 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/Design-1.iml b/.idea/Design-1.iml new file mode 100644 index 00000000..d6ebd480 --- /dev/null +++ b/.idea/Design-1.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..2bfdeda2 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..cc065d00 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MinStack.java b/MinStack.java new file mode 100644 index 00000000..429b927f --- /dev/null +++ b/MinStack.java @@ -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 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) + } +} \ No newline at end of file diff --git a/Sample.java b/Sample.java index 1739a9cb..57c7731e 100644 --- a/Sample.java +++ b/Sample.java @@ -5,3 +5,64 @@ // Your code here along with comments explaining your approach +import java.util.LinkedList; + + + +class MyHashSet { + class Entry{ + public int key; + public Entry(int key){ + this.key = key; + } + } + LinkedList[] 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); + */ \ No newline at end of file