From c664a16db3765cf08a8f054d7ec055cafdde7954 Mon Sep 17 00:00:00 2001 From: afrinc Date: Sun, 1 Oct 2023 11:43:55 +0530 Subject: [PATCH 1/3] Added Solution for Median of stream Data --- .../Hard/\302\226MedianFromDataStream.java" | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 "Worktech/Hard/\302\226MedianFromDataStream.java" diff --git "a/Worktech/Hard/\302\226MedianFromDataStream.java" "b/Worktech/Hard/\302\226MedianFromDataStream.java" new file mode 100644 index 0000000..977413c --- /dev/null +++ "b/Worktech/Hard/\302\226MedianFromDataStream.java" @@ -0,0 +1,41 @@ +package Hard; + +import java.util.*; +class MedianCalculator { + /** initialize your data structure here. */ + PriorityQueue minHeap = new PriorityQueue<>((a,b) -> a - b); + PriorityQueue maxHeap = new PriorityQueue<>((a,b) -> b - a); + boolean isEven = true; // for zero elements + + public MedianCalculator() { + + + } + + public void addNum(int num) { + if(isEven){ + maxHeap.offer(num); + minHeap.offer(maxHeap.poll()); + }else{ + minHeap.offer(num); + maxHeap.offer(minHeap.poll()); + } + isEven = !isEven; + } + + public float getMedian() { + if(isEven){ + return (minHeap.peek() + maxHeap.peek()) / 2f; + }else{ + return minHeap.peek(); + } + } +} + +/** + * Your MedianCalculator object will be instantiated and called as such: + * MedianCalculator obj = new MedianCalculator(); + * obj.addNum(num); + * float median = obj.getMedian(); + */ + From 4449207e2cefbf4249df6e1f13e5d9b6a83b85aa Mon Sep 17 00:00:00 2001 From: afrinc Date: Sun, 1 Oct 2023 11:52:32 +0530 Subject: [PATCH 2/3] Added Regular Expression matching --- Worktech/Hard/RegularExpressionMatching.java | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Worktech/Hard/RegularExpressionMatching.java diff --git a/Worktech/Hard/RegularExpressionMatching.java b/Worktech/Hard/RegularExpressionMatching.java new file mode 100644 index 0000000..91e28c4 --- /dev/null +++ b/Worktech/Hard/RegularExpressionMatching.java @@ -0,0 +1,33 @@ +class Solution { + boolean isMatch (String s, String p) { + boolean dp[][] = new boolean[s.length() + 1][p.length() + 1]; + for(int i = 0; i <= s.length(); i++){ + for(int j = 0; j <= p.length(); j++){ + if(j == 0 && j == i) { + dp[i][j] = true; + } + else if(j == 0) { + dp[i][j] = false; + } + else if(i == 0 && p.charAt(j - 1) != '*') { + dp[i][j] = false; + } + else if(i == 0) { + dp[i][j] = dp[i][j - 2]; + } + else { + if(p.charAt(j - 1) == '.' || s.charAt(i - 1) == p.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1]; + } + if(p.charAt(j - 1) == '*'){ + dp[i][j] = dp[i][j - 2]; + boolean match = ((p.charAt(j - 2) == '.' || p.charAt(j - 2) == s.charAt(i - 1)) && dp[i - 1][j]); + dp[i][j] = dp[i][j] || match; + } + } + } + } + return dp[s.length()][p.length()]; + } +} + From b75b59538de2f2212cedcb9b348c3c7780f60153 Mon Sep 17 00:00:00 2001 From: afrinc Date: Sun, 1 Oct 2023 11:57:44 +0530 Subject: [PATCH 3/3] Revert "Added Regular Expression matching" This reverts commit 4449207e2cefbf4249df6e1f13e5d9b6a83b85aa. --- Worktech/Hard/RegularExpressionMatching.java | 33 -------------------- 1 file changed, 33 deletions(-) delete mode 100644 Worktech/Hard/RegularExpressionMatching.java diff --git a/Worktech/Hard/RegularExpressionMatching.java b/Worktech/Hard/RegularExpressionMatching.java deleted file mode 100644 index 91e28c4..0000000 --- a/Worktech/Hard/RegularExpressionMatching.java +++ /dev/null @@ -1,33 +0,0 @@ -class Solution { - boolean isMatch (String s, String p) { - boolean dp[][] = new boolean[s.length() + 1][p.length() + 1]; - for(int i = 0; i <= s.length(); i++){ - for(int j = 0; j <= p.length(); j++){ - if(j == 0 && j == i) { - dp[i][j] = true; - } - else if(j == 0) { - dp[i][j] = false; - } - else if(i == 0 && p.charAt(j - 1) != '*') { - dp[i][j] = false; - } - else if(i == 0) { - dp[i][j] = dp[i][j - 2]; - } - else { - if(p.charAt(j - 1) == '.' || s.charAt(i - 1) == p.charAt(j - 1)) { - dp[i][j] = dp[i - 1][j - 1]; - } - if(p.charAt(j - 1) == '*'){ - dp[i][j] = dp[i][j - 2]; - boolean match = ((p.charAt(j - 2) == '.' || p.charAt(j - 2) == s.charAt(i - 1)) && dp[i - 1][j]); - dp[i][j] = dp[i][j] || match; - } - } - } - } - return dp[s.length()][p.length()]; - } -} -