Skip to content

Commit 4ba0cee

Browse files
authored
Merge pull request #276 from pranav-3005/pranav5
added delete-node-in-a-bst.java
2 parents c770dab + f9e5439 commit 4ba0cee

10 files changed

+335
-0
lines changed

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/hacktoberfest_2023.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// question link : https://leetcode.com/problems/delete-node-in-a-bst/description/
2+
class Solution {
3+
4+
//1.reach the given node using recursion,if not found just return
5+
public TreeNode deleteNode(TreeNode root, int val) {
6+
if(root==null)
7+
return root;
8+
9+
10+
if(val==root.val)
11+
{//2. After found, there are 4 conditions
12+
13+
//2.1 cur node have both left & ryt nodes.
14+
if(root.left!=null && root.right!=null)
15+
{
16+
17+
/*concept : take ryt node's left subtree,
18+
connect it to left node's ryt most node
19+
connect the updated left node to ryt node's left (i.e.. ryt.left=left)
20+
and return the updated ryt node. (deleting the cur node)
21+
*/
22+
23+
TreeNode ryt=cleft(root.left,root.right);
24+
return ryt;
25+
}
26+
27+
//2.2 have only ryt node
28+
else if(root.left==null && root.right!=null)
29+
{
30+
return root.right;
31+
}
32+
33+
//2.3 have only left node
34+
else if(root.left!=null && root.right==null)
35+
{
36+
return root.left;
37+
}
38+
39+
//2.4 have no child nodes.
40+
else
41+
{
42+
return null;
43+
}
44+
}
45+
46+
if(val<root.val)
47+
{
48+
root.left=deleteNode(root.left,val);
49+
return root;
50+
}
51+
else
52+
{
53+
root.right=deleteNode(root.right,val);
54+
return root;
55+
}
56+
}
57+
58+
//cleft() -> does insert() func and connect the updated left to (ryt.left)
59+
TreeNode cleft(TreeNode left,TreeNode ryt)
60+
{
61+
if(ryt.left==null)
62+
{
63+
ryt.left=left;
64+
return ryt;
65+
}
66+
TreeNode temp=ryt.left;
67+
ryt.left=insert(left,temp);
68+
return ryt;
69+
70+
}
71+
72+
//insert() -> connect ryt node's left subtree(temp) to rytmost of left node and return the updated left node
73+
TreeNode insert(TreeNode root,TreeNode temp)
74+
{
75+
if(root==null)
76+
return temp;
77+
78+
root.right=insert(root.right,temp);
79+
80+
return root;
81+
}
82+
83+
84+
85+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//Question : https://leetcode.com/problems/largest-rectangle-in-histogram/description/
2+
3+
class Solution {
4+
public int largestRectangleArea(int[] h) {
5+
6+
//monotonic stack
7+
8+
int n=h.length;
9+
int left[]=new int[n]; //left small elem's ind'
10+
int ryt[]=new int[n]; //ryt smaller elem's ind'
11+
12+
Stack<Integer> lin=new Stack<>(); //storing index in stacks
13+
Stack<Integer> rin=new Stack<>();
14+
15+
int ans[]=new int[n]; //ans array
16+
17+
//1.store each elem's ryt side next small elem index in ryt[]
18+
for(int i=0;i<n;i++)
19+
{
20+
//before add cur elem index to stack,
21+
//we check if its the ryt next small of elems in stack
22+
while( rin.size()>0 && h[i]< h[rin.peek()] )
23+
{
24+
ryt[rin.peek()]=i;
25+
rin.pop();
26+
}
27+
rin.push(i);
28+
}
29+
//if no small elem present in ryt, assign n.
30+
while(rin.size()>0)
31+
{
32+
ryt[rin.pop()]=n;
33+
}
34+
35+
36+
//2.store each elem's left side next small elem index in left[]
37+
for(int i=n-1;i>=0;i--)
38+
{
39+
//before add cur elem index to stack,
40+
//we check if its the left next small of elems in stack
41+
while( lin.size()>0 && h[i]<h[lin.peek()] )
42+
{
43+
left[lin.pop()]=i;
44+
}
45+
lin.push(i);
46+
}
47+
//if no small elem present in left, assign '-1'.
48+
while(lin.size()>0)
49+
{
50+
left[lin.pop()]=-1;
51+
}
52+
53+
54+
//3. calculate area of rectangle(i.e area=width*height)
55+
//store ans arr in left[] //height=value at an ind
56+
for(int i=0;i<n;i++)
57+
{
58+
int w=ryt[i]-left[i] - 1; //width
59+
ans[i]= w * h[i] ; //area
60+
}
61+
62+
//4. return max of ans arr(i.e...left[])
63+
return Arrays.stream(ans).max().getAsInt();
64+
}
65+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//Question : https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/description/
2+
3+
//Question exp : convert alll 0 -> 1 by doing k bit flip
4+
5+
//Ans approach : greedy approach
6+
class Solution {
7+
public int minKBitFlips(int[] nums, int k) {
8+
int n=nums.length;
9+
int flipCount=0; // range [cur - (k-1)elem] will be flipped this much times
10+
int totalFlips=0 ; //final ans
11+
12+
//true will be marked for all range's(cur indx - k-1th indx) end elem.
13+
//While traversing, if visited[cur elem]=true,
14+
//then we reduce the flipCount of cur_range ,as we move towards nxt range
15+
boolean[] visited=new boolean[n];
16+
17+
for(int i=0;i<n;i++)
18+
{
19+
//only works if, (which is flipped flipCount no of times) cur_elem's value = 0
20+
// 0 flipped even times = 0
21+
//1 flipped odd times = 0
22+
if( (nums[i]==0 && flipCount%2==0) || (nums[i]==1 && flipCount%2!=0) )
23+
{
24+
flipCount++;
25+
totalFlips++;
26+
27+
if( i+(k-1) < n )
28+
{
29+
//mark end of cur range as true
30+
//so, we can rmv the flipCount of this range, after reaching this endPoint.
31+
visited[i+(k-1)]=true;
32+
}
33+
else
34+
{
35+
//if end elem goes out of range
36+
//hence, all of arr elems cannot be flipped to 1
37+
return -1;
38+
}
39+
}
40+
41+
if(visited[i]==true)
42+
{
43+
//end point of cur_range is reached.
44+
//so reduce the flipCount of cur_range
45+
flipCount--;
46+
}
47+
48+
}
49+
50+
return totalFlips;
51+
}
52+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//Question : https://leetcode.com/problems/sliding-window-maximum/description/
2+
3+
class Solution {
4+
5+
//helper class to combine cur_index's value & cur_index
6+
public class pair
7+
{
8+
int val; //value
9+
int ind; //index
10+
11+
pair(int val,int ind)
12+
{
13+
this.val=val;
14+
this.ind=ind;
15+
}
16+
}
17+
18+
public int[] maxSlidingWindow(int[] nums, int k) {
19+
20+
int n=nums.length;
21+
22+
//stores ans
23+
int ans[]=new int[n-(k-1)];
24+
25+
//queue of descending order by cur_index's value (max heap)
26+
PriorityQueue<pair> pque=new PriorityQueue<>((a,b)->{
27+
return b.val-a.val;
28+
});
29+
30+
//start range (1st range)
31+
for(int i=0;i<k;i++)
32+
{
33+
pque.add(new pair(nums[i],i));
34+
}
35+
36+
ans[0]=pque.peek().val;
37+
38+
//start from k
39+
for(int i=k;i<n;i++)
40+
{
41+
//adds ans only in range of each subarr
42+
//if ans's index not in range, then that elem in rmved from que
43+
44+
while(pque.size()>0 && pque.peek().ind <= (i-k) )
45+
pque.remove();
46+
47+
pque.add(new pair(nums[i],i));
48+
ans[i-k+1]=pque.peek().val; //storing every k window's max
49+
}
50+
return ans;
51+
}
52+
53+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//Question : https://leetcode.com/problems/trapping-rain-water/description/
2+
3+
class Solution {
4+
public int trap(int[] h) {
5+
int n=h.length;
6+
7+
//left and ryt boundary
8+
int lb=h[0], rb=h[n-1];
9+
10+
//traversing range ,start and end within boundary
11+
int l=1, r= n-2;
12+
13+
int tw=0; //total_water
14+
15+
//traverse
16+
while(l<=r)
17+
{
18+
if(lb<=rb)
19+
{
20+
int hw=lb; //theight of water (smallest of(left and ryt boundary))
21+
22+
//whether water can be stored at top of building(h[l])
23+
if(h[l]<hw)
24+
{
25+
int ac= hw - h[l]; //actual height(i.e.. water on top of the building)
26+
tw+= ac; //add to total water
27+
}
28+
lb= Math.max(lb,h[l]);
29+
l++;
30+
}
31+
else
32+
{
33+
int hw=rb; //tot height of water
34+
35+
//whether water can be stored at top of building
36+
if(h[r]<hw)
37+
{
38+
int ac= hw - h[r]; //actual height(i.e.. water on top)
39+
tw+= ac; //add to total water
40+
}
41+
rb= Math.max(rb,h[r]);
42+
r--;
43+
}
44+
}
45+
return tw;
46+
47+
}
48+
}

0 commit comments

Comments
 (0)