diff --git a/Sample.java b/Sample.java deleted file mode 100644 index 1739a9cbc..000000000 --- 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 diff --git a/Sample1.java b/Sample1.java new file mode 100644 index 000000000..52dc52006 --- /dev/null +++ b/Sample1.java @@ -0,0 +1,95 @@ +// Approach: +// Use a boolean array to represent the presence of each possible key. +// Mark a key as true when adding and false when removing. +// Check the stored value to determine whether the key exists. + +// Time Complexity: add: O(1), remove: O(1), contains: O(1) +class MyHashSet { + + private int buckets = 1000; + private int bucketItems = 1001; + private boolean[][] storage; + + public MyHashSet() { + storage = new boolean[buckets][]; + } + + private int hash1(int key) { + return key % buckets; + } + + private int hash2(int key) { + return key / buckets; + } + + public void add(int key) { + int bucket = hash1(key); + int bucketItem = hash2(key); + + if (storage[bucket] == null) { + if (bucket == 0) { + storage[bucket] = new boolean[bucketItems + 1]; + } else { + storage[bucket] = new boolean[bucketItems]; + } + } + + storage[bucket][bucketItem] = true; + } + + public void remove(int key) { + int bucket = hash1(key); + int bucketItem = hash2(key); + + if (storage[bucket] != null) { + storage[bucket][bucketItem] = false; + } + } + + public boolean contains(int key) { + int bucket = hash1(key); + int bucketItem = hash2(key); + + return storage[bucket] != null && + storage[bucket][bucketItem]; + } +} +//problem 2 +// Approach: +// Use one stack to store all elements and another stack to track the minimum values. +// Push the current minimum into the min stack whenever a new minimum is encountered. +// During pop operations, update both stacks to maintain the correct minimum. + +// Time Complexity: push: O(1), pop: O(1), top: O(1), getMin: O(1) +// Space Complexity: O(N) +class MinStack { + Stackst; + Stack minst; + int min; + public MinStack() { + this.st=new Stack<>(); + this.minst=new Stack<>(); + this.min=Integer.MAX_VALUE; + this.minst.push(min); + } + + public void push(int val) { + min=Math.min(min,val); + st.push(val); + minst.push(min); + } + + public void pop() { + st.pop(); + minst.pop(); + min=minst.peek(); + } + + public int top() { + return st.peek(); + } + + public int getMin() { + return min; + } +} diff --git a/min stack problem b/min stack problem new file mode 100644 index 000000000..caecbad64 --- /dev/null +++ b/min stack problem @@ -0,0 +1,31 @@ +class MinStack { + Stackst; + Stack minst; + int min; + public MinStack() { + this.st=new Stack<>(); + this.minst=new Stack<>(); + this.min=Integer.MAX_VALUE; + this.minst.push(min); + } + + public void push(int val) { + min=Math.min(min,val); + st.push(val); + minst.push(min); + } + + public void pop() { + st.pop(); + minst.pop(); + min=minst.peek(); + } + + public int top() { + return st.peek(); + } + + public int getMin() { + return min; + } +}