diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..f9fe683b --- /dev/null +++ b/Problem1.java @@ -0,0 +1,70 @@ +// Time Complexity :O(1) +// Space Complexity : O(n) +// 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 are implementing HashSet which is 1D boolean array with primary and secondary hashing to store keys. +we have followed double hashing technique to avoid collisions. +add, remove, and contains operations ad implemented with O(1) time complexity. +time - O(1) +space - O(n)*/ + +class MyHashSet { + + //length of primary and nested arrs + private int buckets; + private int bucketItems; + private boolean [][]storage; + + //primary bucket + private int hash1(int key){ + return key%buckets; + } + + //nested array bucket + private int hash2(int key){ + return key/bucketItems; + } + + public MyHashSet() { + this.buckets=1000; + this.bucketItems=1000; + this.storage = new boolean[buckets][]; + } + + public void add(int key) { + int bucket = hash1(key); + //check for nested array + if(storage[bucket]==null){ + //only for 0 element edge case to set 1000000 value + if(bucket==0){ + storage[bucket]= new boolean[bucketItems +1]; + } + else{ + storage[bucket]=new boolean[bucketItems]; + } + } + int bucketItem = hash2(key); + storage[bucket][bucketItem]=true; + } + + public void remove(int key) { + int bucket = hash1(key); + int bucketItem = hash2(key); + //check for nested array + if(storage[bucket]==null) return; + storage[bucket][bucketItem]=false; + } + + public boolean contains(int key) { + int bucket =hash1(key); + int bucketItem = hash2(key); + //check for nested array + if(storage[bucket] == null) { + return false; + } + return storage[bucket][bucketItem]; + } +} \ No newline at end of file diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..2b111c5d --- /dev/null +++ b/Problem2.java @@ -0,0 +1,58 @@ +// Time Complexity :O(1) +// Space Complexity : O(n) +// 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 +/* +In this two arrays are used one to store values and one to track the minimum at each position. +When I push a value, I also store the smallest value seen so far +This will let me get the minimum element in constant time. +time - O(1) +space - O(n) +*/ + +class MinStack { + int[] stack; + int[] minStack; + int top; + + public MinStack() { + stack = new int[10000]; + minStack = new int[10000]; + top = -1; + } + + public void push(int val) { + top++; + stack[top] = val; + // if first element, it is the minimum by default + if (top == 0) { + minStack[top] = val; + } //else, store the smaller value between the current element and the previous minimum so far + else { + minStack[top] = Math.min(val, minStack[top - 1]); + } + } + + public void pop() { + top--; + } + + public int top() { + return stack[top]; + } + + public int getMin() { + return minStack[top]; + } +} + +/** + * 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/Sample.java b/Sample.java deleted file mode 100644 index 1739a9cb..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach