From bf9b713b8a10fb238ca816cafe0978e87e48e06c Mon Sep 17 00:00:00 2001 From: pankhuri0209 Date: Tue, 22 Oct 2024 17:40:05 -0400 Subject: [PATCH] problems leetcode --- .idea/.gitignore | 8 +++++++ .idea/Design-1.iml | 11 +++++++++ .idea/misc.xml | 6 +++++ .idea/modules.xml | 8 +++++++ .idea/vcs.xml | 6 +++++ problem1.java | 59 ++++++++++++++++++++++++++++++++++++++++++++++ problem2.java | 35 +++++++++++++++++++++++++++ 7 files changed, 133 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/Design-1.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 problem1.java create mode 100644 problem2.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/Design-1.iml b/.idea/Design-1.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/.idea/Design-1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..07115cdf --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..cc065d00 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..903f45e6 --- /dev/null +++ b/problem1.java @@ -0,0 +1,59 @@ +public class problem1 { + +// explanation: In this problem to implement hashset we consume the concept of double hash operation. Starting by creating 2d array +// and initializing the buckets sizes as 10^3 for, except primarybuckets due to test case issue . Caluating primaryhash by % operation +// and to calculate secondary hash by / operation . we do so to calculate the index for the key + + + + class myHashSet{ + int primaryBuckets; + int secondaryBuckets; + boolean[][] storage; + public myHashSet(){ + this.primaryBuckets=1001; + this.secondaryBuckets=1000; + storage = new boolean[primaryBuckets][]; + } + private int getPrimaryHash(int key) + { + return key%primaryBuckets; + } + private int getSecondaryHash(int key) + { + return key/secondaryBuckets; + } + public void add(int key) + { + int primaryIndex= getPrimaryHash(key); + if (storage[primaryIndex] == null) + { + storage[primaryIndex]= new boolean[secondaryBuckets]; + } + int secondaryIndex= getSecondaryHash(key); + storage[primaryIndex][secondaryIndex]=true; + } + public void remove(int key) + { + int primaryIndex= getPrimaryHash(key); + if (storage[primaryIndex] == null) + { + return; + } + int secondaryIndex= getSecondaryHash(key); + storage[primaryIndex][secondaryIndex]=false; + } + public boolean contains(int key) + { + int primaryIndex= getPrimaryHash(key); + if (storage[primaryIndex] == null) + { + return false; + } + int secondaryIndex= getSecondaryHash(key); + return storage[primaryIndex][secondaryIndex]; + } + + } + +} diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..3cfbd72c --- /dev/null +++ b/problem2.java @@ -0,0 +1,35 @@ +import java.util.Stack; + +public class problem2 { + +class MinStack{ + Stack st; + Stack minSt; + int min; + + public MinStack(){ + this.st = new Stack<>(); + this.minSt = new Stack<>(); + min= Integer.MAX_VALUE; + minSt.push(min); + } + public void push(int x){ + min= Math.min(min, x); + st.push(x); + minSt.push(min); + } + public void pop(){ + st.pop(); + minSt.pop(); + min= st.peek(); + } + public int top(){ + return st.peek(); + } + public int getMin(){ + return min; + } +} + + +}