From 009596a4ed52e185a89c0e5dcf9b94889168428a Mon Sep 17 00:00:00 2001 From: Pranav Ananth Date: Sat, 21 Oct 2023 10:10:20 +0530 Subject: [PATCH 1/5] added sliding-window-maximum.java code --- .idea/.gitignore | 3 ++ .idea/hacktoberfest_2023.iml | 9 +++++ .idea/misc.xml | 6 +++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ Coding/Java/sliding-window-maximum.java | 53 +++++++++++++++++++++++++ 6 files changed, 85 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/hacktoberfest_2023.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 Coding/Java/sliding-window-maximum.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/hacktoberfest_2023.iml b/.idea/hacktoberfest_2023.iml new file mode 100644 index 00000000..d6ebd480 --- /dev/null +++ b/.idea/hacktoberfest_2023.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..639900d1 --- /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..dae0f5b6 --- /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/Coding/Java/sliding-window-maximum.java b/Coding/Java/sliding-window-maximum.java new file mode 100644 index 00000000..1b2dbd25 --- /dev/null +++ b/Coding/Java/sliding-window-maximum.java @@ -0,0 +1,53 @@ +//Question : https://leetcode.com/problems/sliding-window-maximum/description/ + +class Solution { + + //helper class to combine cur_index's value & cur_index + public class pair + { + int val; //value + int ind; //index + + pair(int val,int ind) + { + this.val=val; + this.ind=ind; + } + } + + public int[] maxSlidingWindow(int[] nums, int k) { + + int n=nums.length; + + //stores ans + int ans[]=new int[n-(k-1)]; + + //queue of descending order by cur_index's value (max heap) + PriorityQueue pque=new PriorityQueue<>((a,b)->{ + return b.val-a.val; + }); + + //start range (1st range) + for(int i=0;i0 && pque.peek().ind <= (i-k) ) + pque.remove(); + + pque.add(new pair(nums[i],i)); + ans[i-k+1]=pque.peek().val; //storing every k window's max + } + return ans; + } + +} \ No newline at end of file From c805e414b853e0fffc39f731e152708116752ec2 Mon Sep 17 00:00:00 2001 From: Pranav Ananth Date: Sat, 21 Oct 2023 10:17:48 +0530 Subject: [PATCH 2/5] added largest-rectangle-in-histogram.java code --- .../Java/largest-rectangle-in-histogram.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Coding/Java/largest-rectangle-in-histogram.java diff --git a/Coding/Java/largest-rectangle-in-histogram.java b/Coding/Java/largest-rectangle-in-histogram.java new file mode 100644 index 00000000..ca4a4d76 --- /dev/null +++ b/Coding/Java/largest-rectangle-in-histogram.java @@ -0,0 +1,65 @@ +//Question : https://leetcode.com/problems/largest-rectangle-in-histogram/description/ + +class Solution { + public int largestRectangleArea(int[] h) { + + //monotonic stack + + int n=h.length; + int left[]=new int[n]; //left small elem's ind' + int ryt[]=new int[n]; //ryt smaller elem's ind' + + Stack lin=new Stack<>(); //storing index in stacks + Stack rin=new Stack<>(); + + int ans[]=new int[n]; //ans array + + //1.store each elem's ryt side next small elem index in ryt[] + for(int i=0;i0 && h[i]< h[rin.peek()] ) + { + ryt[rin.peek()]=i; + rin.pop(); + } + rin.push(i); + } + //if no small elem present in ryt, assign n. + while(rin.size()>0) + { + ryt[rin.pop()]=n; + } + + + //2.store each elem's left side next small elem index in left[] + for(int i=n-1;i>=0;i--) + { + //before add cur elem index to stack, + //we check if its the left next small of elems in stack + while( lin.size()>0 && h[i]0) + { + left[lin.pop()]=-1; + } + + + //3. calculate area of rectangle(i.e area=width*height) + //store ans arr in left[] //height=value at an ind + for(int i=0;i Date: Sat, 21 Oct 2023 10:26:40 +0530 Subject: [PATCH 3/5] added minimum-number-of-k-consecutive-bit-flips.java code --- ...mum-number-of-k-consecutive-bit-flips.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Coding/Java/minimum-number-of-k-consecutive-bit-flips.java diff --git a/Coding/Java/minimum-number-of-k-consecutive-bit-flips.java b/Coding/Java/minimum-number-of-k-consecutive-bit-flips.java new file mode 100644 index 00000000..9d0c8b54 --- /dev/null +++ b/Coding/Java/minimum-number-of-k-consecutive-bit-flips.java @@ -0,0 +1,52 @@ +//Question : https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/description/ + +//Question exp : convert alll 0 -> 1 by doing k bit flip + +//Ans approach : greedy approach +class Solution { + public int minKBitFlips(int[] nums, int k) { + int n=nums.length; + int flipCount=0; // range [cur - (k-1)elem] will be flipped this much times + int totalFlips=0 ; //final ans + + //true will be marked for all range's(cur indx - k-1th indx) end elem. + //While traversing, if visited[cur elem]=true, + //then we reduce the flipCount of cur_range ,as we move towards nxt range + boolean[] visited=new boolean[n]; + + for(int i=0;i Date: Sat, 21 Oct 2023 10:33:07 +0530 Subject: [PATCH 4/5] added trapping-rain-water.java code --- Coding/Java/trapping-rain-water.java | 48 ++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Coding/Java/trapping-rain-water.java diff --git a/Coding/Java/trapping-rain-water.java b/Coding/Java/trapping-rain-water.java new file mode 100644 index 00000000..9f2882db --- /dev/null +++ b/Coding/Java/trapping-rain-water.java @@ -0,0 +1,48 @@ +//Question : https://leetcode.com/problems/trapping-rain-water/description/ + +class Solution { + public int trap(int[] h) { + int n=h.length; + + //left and ryt boundary + int lb=h[0], rb=h[n-1]; + + //traversing range ,start and end within boundary + int l=1, r= n-2; + + int tw=0; //total_water + + //traverse + while(l<=r) + { + if(lb<=rb) + { + int hw=lb; //theight of water (smallest of(left and ryt boundary)) + + //whether water can be stored at top of building(h[l]) + if(h[l] Date: Sat, 21 Oct 2023 10:38:18 +0530 Subject: [PATCH 5/5] added delete-node-in-a-bst.java code --- Coding/Java/delete-node-in-a-bst.java | 85 +++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Coding/Java/delete-node-in-a-bst.java diff --git a/Coding/Java/delete-node-in-a-bst.java b/Coding/Java/delete-node-in-a-bst.java new file mode 100644 index 00000000..99bf9f6b --- /dev/null +++ b/Coding/Java/delete-node-in-a-bst.java @@ -0,0 +1,85 @@ +// question link : https://leetcode.com/problems/delete-node-in-a-bst/description/ +class Solution { + + //1.reach the given node using recursion,if not found just return + public TreeNode deleteNode(TreeNode root, int val) { + if(root==null) + return root; + + + if(val==root.val) + {//2. After found, there are 4 conditions + + //2.1 cur node have both left & ryt nodes. + if(root.left!=null && root.right!=null) + { + + /*concept : take ryt node's left subtree, + connect it to left node's ryt most node + connect the updated left node to ryt node's left (i.e.. ryt.left=left) + and return the updated ryt node. (deleting the cur node) + */ + + TreeNode ryt=cleft(root.left,root.right); + return ryt; + } + + //2.2 have only ryt node + else if(root.left==null && root.right!=null) + { + return root.right; + } + + //2.3 have only left node + else if(root.left!=null && root.right==null) + { + return root.left; + } + + //2.4 have no child nodes. + else + { + return null; + } + } + + if(val does insert() func and connect the updated left to (ryt.left) + TreeNode cleft(TreeNode left,TreeNode ryt) + { + if(ryt.left==null) + { + ryt.left=left; + return ryt; + } + TreeNode temp=ryt.left; + ryt.left=insert(left,temp); + return ryt; + + } + + //insert() -> connect ryt node's left subtree(temp) to rytmost of left node and return the updated left node + TreeNode insert(TreeNode root,TreeNode temp) + { + if(root==null) + return temp; + + root.right=insert(root.right,temp); + + return root; + } + + + +} \ No newline at end of file