Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/hacktoberfest_2023.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 85 additions & 0 deletions Coding/Java/delete-node-in-a-bst.java
Original file line number Diff line number Diff line change
@@ -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<root.val)
{
root.left=deleteNode(root.left,val);
return root;
}
else
{
root.right=deleteNode(root.right,val);
return root;
}
}

//cleft() -> 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;
}



}
65 changes: 65 additions & 0 deletions Coding/Java/largest-rectangle-in-histogram.java
Original file line number Diff line number Diff line change
@@ -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<Integer> lin=new Stack<>(); //storing index in stacks
Stack<Integer> 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;i<n;i++)
{
//before add cur elem index to stack,
//we check if its the ryt next small of elems in stack
while( rin.size()>0 && 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]<h[lin.peek()] )
{
left[lin.pop()]=i;
}
lin.push(i);
}
//if no small elem present in left, assign '-1'.
while(lin.size()>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<n;i++)
{
int w=ryt[i]-left[i] - 1; //width
ans[i]= w * h[i] ; //area
}

//4. return max of ans arr(i.e...left[])
return Arrays.stream(ans).max().getAsInt();
}
}
52 changes: 52 additions & 0 deletions Coding/Java/minimum-number-of-k-consecutive-bit-flips.java
Original file line number Diff line number Diff line change
@@ -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<n;i++)
{
//only works if, (which is flipped flipCount no of times) cur_elem's value = 0
// 0 flipped even times = 0
//1 flipped odd times = 0
if( (nums[i]==0 && flipCount%2==0) || (nums[i]==1 && flipCount%2!=0) )
{
flipCount++;
totalFlips++;

if( i+(k-1) < n )
{
//mark end of cur range as true
//so, we can rmv the flipCount of this range, after reaching this endPoint.
visited[i+(k-1)]=true;
}
else
{
//if end elem goes out of range
//hence, all of arr elems cannot be flipped to 1
return -1;
}
}

if(visited[i]==true)
{
//end point of cur_range is reached.
//so reduce the flipCount of cur_range
flipCount--;
}

}

return totalFlips;
}
}
53 changes: 53 additions & 0 deletions Coding/Java/sliding-window-maximum.java
Original file line number Diff line number Diff line change
@@ -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<pair> pque=new PriorityQueue<>((a,b)->{
return b.val-a.val;
});

//start range (1st range)
for(int i=0;i<k;i++)
{
pque.add(new pair(nums[i],i));
}

ans[0]=pque.peek().val;

//start from k
for(int i=k;i<n;i++)
{
//adds ans only in range of each subarr
//if ans's index not in range, then that elem in rmved from que

while(pque.size()>0 && 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;
}

}
48 changes: 48 additions & 0 deletions Coding/Java/trapping-rain-water.java
Original file line number Diff line number Diff line change
@@ -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]<hw)
{
int ac= hw - h[l]; //actual height(i.e.. water on top of the building)
tw+= ac; //add to total water
}
lb= Math.max(lb,h[l]);
l++;
}
else
{
int hw=rb; //tot height of water

//whether water can be stored at top of building
if(h[r]<hw)
{
int ac= hw - h[r]; //actual height(i.e.. water on top)
tw+= ac; //add to total water
}
rb= Math.max(rb,h[r]);
r--;
}
}
return tw;

}
}