diff --git a/2024_Problem2_Design_HashSet.java b/2024_Problem2_Design_HashSet.java new file mode 100644 index 00000000..e639cfe9 --- /dev/null +++ b/2024_Problem2_Design_HashSet.java @@ -0,0 +1,57 @@ +//705. Design HashSet - https://leetcode.com/problems/design-hashset/description/ + +//Time Complexity: for all operations is O(1) +//Space Complexity: O(1) + +class MyHashSet { + + int primaryIndex = 1000; //square root of the number gives the index values + int secondaryIndex = 1001; //to handle an edge case if the number to store is 1000, as the index will be 0-999, + // add an extra space to handle 1000 + + boolean[][] storage = new boolean[primaryIndex][]; + + private int hash(int key){ + return key % primaryIndex; + } + + private int nestedHash(int key){ + return key / secondaryIndex; + } + /** Initialize data structure here. */ + public MyHashSet() { + this.primaryIndex = primaryIndex; + this.secondaryIndex = secondaryIndex; + this.storage = storage; + } + /** Add key into the HashSet */ + public void add(int key) { + int index = hash(key); + int nestedIndex = nestedHash(key); + + if(storage[index]==null){ + storage[index]=new boolean[secondaryIndex]; + } + storage[index][nestedIndex]=true; + } + /** Remove key from the HashSet */ + public void remove(int key) { + int index = hash(key); + int nestedIndex = nestedHash(key); + if(storage[index]!=null){ + storage[index][nestedIndex]=false; + } + } + + /** Returns true if this set contains the specified element */ + public boolean contains(int key) { + int index = hash(key); + int nestedIndex = nestedHash(key); + // if storage is not empty -> true + if(storage[index]!= null && storage[index][nestedIndex]){ + return true; + } + return false; + + } +} \ No newline at end of file diff --git a/2024_Problem3_MinStack.java b/2024_Problem3_MinStack.java new file mode 100644 index 00000000..4b986e59 --- /dev/null +++ b/2024_Problem3_MinStack.java @@ -0,0 +1,39 @@ +// 155. Min Stack - https://leetcode.com/problems/min-stack/description/ + +//Time Complexity: O(1) for all operations + +class MinStack { + + Stack stack ; + Stack minStack; + int min ; + /** initialize your data structure here. */ + public MinStack() { + this.stack = new Stack<>(); + this.minStack = new Stack<>(); + min = Integer.MAX_VALUE; + minStack.push(min); + } + + public void push(int x) { + if(x < min){ + min = x; + } + minStack.push(min); + stack.push(x); + } + + public void pop() { + minStack.pop(); + stack.pop(); + min = minStack.peek(); + } + + public int top() { + return stack.peek(); + } + + public int getMin() { + return min; + } +} \ No newline at end of file diff --git a/Problem1_HashMap.java b/Problem1_HashMap.java new file mode 100644 index 00000000..fdcc3a42 --- /dev/null +++ b/Problem1_HashMap.java @@ -0,0 +1,59 @@ +//Time Complexity: O(n/k) -> where n is number of keys; and k is the number of buckets +//Space Complexity: O(n+k) -> where n is number of keys; and k is the number of buckets + +class MyHashMap { + int buckets = 1000; + int bucketItems = 1000; + Integer[][] myHashmap = new Integer[buckets][]; + + /** Initialize your data structure here. */ + public MyHashMap() { + + } + //generating hash key to locate the bucket where key is stored + private int getBucketIdx(int key){ + return key%buckets; + } + //to get key + private int getBucketItemIdx(int key){ + return key/buckets; + } + + /** value will always be non-negative. */ + public void put(int key, int value) { + int bucketIdx = getBucketIdx(key); + int bucketItemIdx = getBucketItemIdx(key); + //if no bucket index present; create + if(myHashmap[bucketIdx] == null) + myHashmap[bucketIdx] = new Integer[bucketItems]; + //else assign value to the bucket + myHashmap[bucketIdx][bucketItemIdx] = value; + } + + /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */ + public int get(int key) { + int bucketIdx = getBucketIdx(key); + int bucketItemIdx = getBucketItemIdx(key); + + if(myHashmap[bucketIdx] != null && myHashmap[bucketIdx][bucketItemIdx] != null) + return myHashmap[bucketIdx][bucketItemIdx]; + return -1; + } + + /** Removes the mapping of the specified value key if this map contains a mapping for the key */ + public void remove(int key) { + int bucketIdx = getBucketIdx(key); + int bucketItemIdx = getBucketItemIdx(key); + + if(myHashmap[bucketIdx] != null && myHashmap[bucketIdx][bucketItemIdx] != null) + myHashmap[bucketIdx][bucketItemIdx] = null; + } +} + +/** + * Your MyHashMap object will be instantiated and called as such: + * MyHashMap obj = new MyHashMap(); + * obj.put(key,value); + * int param_2 = obj.get(key); + * obj.remove(key); + */ \ No newline at end of file diff --git a/Problem2_MinStack.java b/Problem2_MinStack.java new file mode 100644 index 00000000..d8d8ccb2 --- /dev/null +++ b/Problem2_MinStack.java @@ -0,0 +1,43 @@ +//Time Complexity: O(1) -> for all operations +//Space Complexity: O(n)-> Stack space + +class MinStack { + + Stack stack = new Stack<>(); + int min = Integer.MAX_VALUE; + /** initialize your data structure here. */ + public MinStack() { + + } + + public void push(int x) { + if(x <= min){ + stack.push(min); + min = x; + } + stack.push(x); + } + + 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(x); + * obj.pop(); + * int param_3 = obj.top(); + * int param_4 = obj.getMin(); + */ \ No newline at end of file