diff --git a/Design-Hashset.java b/Design-Hashset.java new file mode 100644 index 00000000..20bfb451 --- /dev/null +++ b/Design-Hashset.java @@ -0,0 +1,42 @@ +// Time Complexity : O(1) for add, remove and contains +// Space Complexity : O(n) where n is the number of elements in the hashset +// 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 +/* +We can use a boolean array to implement the hashset. +The index of the array will represent the key and when we add the key to the array, we will set the value true +When the remove function is called, we will mark the array[key] as false +When the contains function is called, we will return the value of the array[key] + +*/ +class MyHashSet { + boolean[] array = new boolean[1000001]; + + public MyHashSet() { + + } + + public void add(int key) { + array[key] = true; + + } + + public void remove(int key) { + array[key]=false; + } + + public boolean contains(int key) { + return array[key]; + } +} + +/** + * 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 diff --git a/Hashset.java b/Hashset.java new file mode 100644 index 00000000..3b3d6e89 --- /dev/null +++ b/Hashset.java @@ -0,0 +1,77 @@ +// Time Complexity :o(1) for add, remove and contains +// Space Complexity : O(n) where n is the number of elements in the hashset +// 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 + +/* +We can use a 2D boolean array to implement the hashset. +The index of the first dimension will represent the row and the index of the second dimension will represent +the column. When we add the key to the array, we will set the value true at the index of the row and column. +When the remove function is called, we will mark the array[row][column] as false +When the contains function is called, we will return the value of the array[row][column] +*/ + +class MyHashSet { + boolean array[][] = new boolean[1000][]; + + + public MyHashSet() { + + + } + public int row(int key){ + return key%1000; + + } + + public int column(int key){ + return key/1000; + + } + + + public void add(int key) { + int row = row(key); + int column = column(key); + if(array[row]==null){ + array[row] = new boolean[1001]; + } + array[row][column] = true; + + } + + public void remove(int key) { + int row = row(key); + int column = column(key); + if( array[row] !=null){ + array[row][column] = false; + } + + + } + + public boolean contains(int key) { + boolean value = false; + int row = row(key); + int column = column(key); + if( array[row] !=null){ + value = array[row][column]; + } + if(value){ + 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); + */ diff --git a/Min-Stack-155.java b/Min-Stack-155.java new file mode 100644 index 00000000..22dc7407 --- /dev/null +++ b/Min-Stack-155.java @@ -0,0 +1,63 @@ + + +// Time Complexity : O(1) for push, pop and top, O(n) for getMin where n is the number of elements in the stack +// Space Complexity : O(n) where n is the number of elements in the stack +// 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 +/* +We can use an ArrayList to implement the min stack. +When the push function is called, we will add the element to the end of the arraylist. +When the pop function is called, we will remove the last element from the arraylist. +When the top function is called, we will return the last element from the arraylist. +When the getMin function is called, we will iterate through the arraylist and find the minimum element and return it. + +*/ + +class MinStack { + ArrayList al = new ArrayList(); + + public MinStack() { + + } + + public void push(int val) { + al.add(al.size(), val); + + + } + + public void pop() { + al.remove(al.size()-1); + + + } + + public int top() { + int result = al.get(al.size()-1); + return result; + + } + + public int getMin() { + int min = al.get(0); + for(int i =1 ; i al.get(i)){ + min = al.get(i); + } + + } + 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(); + */ \ No newline at end of file diff --git a/Minstack.java b/Minstack.java new file mode 100644 index 00000000..b78f2df4 --- /dev/null +++ b/Minstack.java @@ -0,0 +1,59 @@ +// Time Complexity : O(1) for push, pop, top and getMin +// Space Complexity : O(n) where n is the number of elements in the stack +// 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 +/** + * This is a min stack that supports push, pop, top, and retrieving the minimum element in constant time. + * We use two stacks to achieve this. The first stack is used to store the elements of the stack and the second stack is used to store the minimum elements of the stack. + */ +class MinStack { + Stack stack = new Stack<>(); + Stack minStack = new Stack<>(); + + public MinStack() { + + + } + } + + public void push(int val) { + stack.push(val); + if(!(minStack.isEmpty())){ + minStack.push(Math.min(val,minStack.peek())); + + }else{ + minStack.push(val); + } + + } + + public void pop() { + stack.pop(); + minStack.pop(); + + } + + public int top() { + return stack.peek(); + } + + public int getMin() { + return minStack.peek(); + + + + + } +} + +/** + * 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(); + */ \ No newline at end of file