From 7106861aa11829e73cf16ea75339deeb9291ec23 Mon Sep 17 00:00:00 2001 From: yashhh-23 Date: Fri, 5 Jun 2026 23:47:26 +0530 Subject: [PATCH] completed design-1 --- MinStack.java | 47 ++++++++++++++++++++++++++++++++++++++++ README.md | 3 +-- design_hashset.java | 53 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 MinStack.java create mode 100644 design_hashset.java diff --git a/MinStack.java b/MinStack.java new file mode 100644 index 00000000..01bc06d8 --- /dev/null +++ b/MinStack.java @@ -0,0 +1,47 @@ +// Time Complexity : O(1) for all push pop top 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 : none + +// Your code here along with comments explaining your approach +//used two stacks, one for storing elements and one for storing the minimum element at each position. +import java.util.Stack; +class MinStack +{ + private Stack stack; + private Stack minStack; + + public MinStack() + { + stack = new Stack<>(); + minStack = new Stack<>(); + } + + public void push(int val) + { + stack.push(val); + if (minStack.isEmpty() || val <= minStack.peek()) + { + minStack.push(val); + } else + { + minStack.push(minStack.peek()); + } + } + + public void pop() + { + stack.pop(); + minStack.pop(); + } + + public int top() + { + return stack.peek(); + } + + public int getMin() + { + return minStack.peek(); + } +} \ No newline at end of file diff --git a/README.md b/README.md index 6907f832..001d6310 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,7 @@ Explain your approach in **three sentences only** at top of your code -## Problem 2: -Design MinStack (https://leetcode.com/problems/min-stack/) +## Problem 2: Design MinStack (https://leetcode.com/problems/min-stack/) diff --git a/design_hashset.java b/design_hashset.java new file mode 100644 index 00000000..4e71dd5a --- /dev/null +++ b/design_hashset.java @@ -0,0 +1,53 @@ +// Time Complexity : O(1) for add, remove and contains operations +// Space Complexity : O(n) where n is the number of elements in the hash set +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : none + +// Your code here along with comments explaining your approach +//used bucketing to split each key in two parts: key % 1000 gives the bucket index, and key / 1000 gives the position inside that bucket. + +class MyHashSet { + private boolean[][] storage; + private int buckets; + private int bucketItems; + + public MyHashSet() { + this.buckets = 1000; + this.bucketItems = 1001; + this.storage = new boolean[buckets][]; + } + + private int hash(int key) { + return key % buckets; + } + + private int pos(int key) { + return key / buckets; + } + + public void add(int key) { + int bucket = hash(key); + int bucketItem = pos(key); + + if (storage[bucket] == null) { + storage[bucket] = new boolean[bucketItems]; + } + storage[bucket][bucketItem] = true; + } + + public void remove(int key) { + int bucket = hash(key); + int bucketItem = pos(key); + + if (storage[bucket] != null) { + storage[bucket][bucketItem] = false; + } + } + + public boolean contains(int key) { + int bucket = hash(key); + int bucketItem = pos(key); + + return storage[bucket] != null && storage[bucket][bucketItem]; + } +} \ No newline at end of file