Day 16 of LeetCode Challenge

Cloud and DevOps Engineer with hands-on expertise in AWS, CI/CD pipelines, Docker, Kubernetes, and Monitoring tools. Adept at building and automating scalable, fault-tolerant cloud infrastructures, and consistently improving system performance, security, and reliability in dynamic environments.
Problem 1: Balanced Binary Tree
Link to the problem: https://leetcode.com/problems/balanced-binary-tree/description/

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if(root==null)
return true;
return isHeightBalanced(root, 0)==-1?false:true;
}
private int isHeightBalanced(TreeNode root, int height){
if(height==-1)
return -1;
if(root==null)
return 0;
int left = isHeightBalanced(root.left, height);
int right = isHeightBalanced(root.right, height);
if(left-right>1 || left-right<-1 || left==-1 || right==-1)
return -1;
return left>right?left+1:right+1;
}
}
Problem 2: 2 Sum IV - Input is a BST
Link to the problem: https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean findTarget(TreeNode root, int k) {
List<Integer> list = new ArrayList<>();
Traverse(root, list, k);
boolean bool = false;
for(int i=0; i<list.size()-1; i++){
for(int j=i+1; j<list.size(); j++){
if(list.get(i)+list.get(j)==k)
bool = true;
}
}
return bool;
}
private int Traverse(TreeNode root, List<Integer> list, int k){
if(root==null)
return 0;
list.add(root.val);
Traverse(root.left, list, k);
Traverse(root.right, list, k);
return 0;
}
}
Problem 3: Minimum Distance between BST Nodes
Link to the problem: https://leetcode.com/problems/minimum-distance-between-bst-nodes/description/

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int minDiffInBST(TreeNode root) {
int ans = Integer.MAX_VALUE;
List<Integer> list = new ArrayList<>();
Traverse(root, list);
for(int i=0; i<list.size()-1; i++){
for(int j=i+1; j<list.size(); j++){
int diff = list.get(i)-list.get(j)>0?list.get(i)-list.get(j):list.get(j)-list.get(i);
ans = ans<diff?ans:diff;
}
}
return ans==Integer.MAX_VALUE?0:ans;
}
private int Traverse(TreeNode root, List<Integer> list){
if(root==null)
return 0;
list.add(root.val);
Traverse(root.left, list);
Traverse(root.right, list);
return 0;
}
}
Problem 4: Maximize Distance to Closest Person
Link to the problem: https://leetcode.com/problems/maximize-distance-to-closest-person/description/

class Solution {
public int maxDistToClosest(int[] seats) {
int max = 0;
int temp = 0;
int first = 0;
boolean bool = false;
for(int i:seats){
if(i==0)
temp++;
if(i==1 && bool){
max = max>temp?max:temp;
temp=0;
}
if(i==1 && !bool){
first = temp;
max = max>temp?max:temp;
temp=0;
bool = true;
}
}
if(temp>=max/2+1 && temp>=first)
return temp;
else if(first>=max/2+1 && first>=temp)
return first;
return max%2==0?max/2:(max/2)+1;
}
}
Problem 5: Minimum Rectangles to Cover Points
Link to the problem: https://leetcode.com/problems/minimum-rectangles-to-cover-points/description/

class Solution {
public int minRectanglesToCoverPoints(int[][] points, int w) {
Arrays.sort(points, (a, b) -> a[0] - b[0]);
int n = points.length;
int count = 0;
int i = 0;
while(i < n) {
int start = points[i][0];
int end = start + w;
while(i < n && points[i][0] <= end) {
i++;
}
count++;
}
return count;
}
}




