Day 30 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: Finding Pairs with a Certain Sum
Link to the problem: https://leetcode.com/problems/finding-pairs-with-a-certain-sum/

class FindSumPairs {
int[] n1, n2;
Map<Integer, Integer> map = new HashMap<>();
public FindSumPairs(int[] nums1, int[] nums2) {
n1 = nums1;
n2 = nums2;
for(int i:n2) map.put(i, map.getOrDefault(i, 0)+1);
}
public void add(int index, int val) {
map.put(n2[index], map.get(n2[index])-1);
n2[index] += val;
map.put(n2[index], map.getOrDefault(n2[index], 0)+1);
}
public int count(int tot) {
int res = 0;
for(int i:n1) res += map.getOrDefault(tot-i, 0);
return res;
}
}
/**
* Your FindSumPairs object will be instantiated and called as such:
* FindSumPairs obj = new FindSumPairs(nums1, nums2);
* obj.add(index,val);
* int param_2 = obj.count(tot);
*/
Problem 2: Contiguous Array
Link to the problem: https://leetcode.com/problems/contiguous-array/

class Solution {
public int findMaxLength(int[] nums) {
int n = nums.length;
Map<Integer, Integer> mp = new HashMap<>();
int sum = 0;
int subArrayLength = 0;
for (int i = 0; i < n; i++) {
sum += nums[i] == 0 ? -1 : 1;
if(sum == 0)
subArrayLength = i + 1;
else if(mp.containsKey(sum))
subArrayLength = Math.max(subArrayLength, i - mp.get(sum));
else
mp.put(sum, i);
}
return subArrayLength;
}
}
Problem 3: Minimum Moves to Reach Target in Grid
Link to the problem: https://leetcode.com/problems/minimum-moves-to-reach-target-in-grid/

class Solution {
public int minMoves(int sx, int sy, int tx, int ty) {
if (sx == 0 && sy == 0) return (tx == 0 && ty == 0) ? 0 : -1;
int res = 0;
while (sx != tx || sy != ty) {
if (sx > tx || sy > ty) return -1;
res++;
if(tx > ty){
if(tx > ty*2){
if (tx % 2 != 0) return -1;
tx /= 2;
}else tx -= ty;
}else if(tx < ty){
if(ty > tx * 2){
if(ty % 2 != 0) return -1;
ty /= 2;
}else ty -= tx;
}else{
if(sx == 0) tx = 0;
else if (sy == 0) ty = 0;
else return -1;
}
}
return res;
}
}
Problem 4: Coupon Code Validator
Link to the problem: https://leetcode.com/problems/coupon-code-validator/

import java.util.regex.*;
class Solution {
public List<String> validateCoupons(String[] code, String[] businessLine, boolean[] isActive) {
Map<String, ArrayList<String>> map = new HashMap<>();
ArrayList<String> electronics = new ArrayList<>();
ArrayList<String> grocery = new ArrayList<>();
ArrayList<String> pharmacy = new ArrayList<>();
ArrayList<String> restaurant = new ArrayList<>();
map.put("restaurant", restaurant);
map.put("grocery", grocery);
map.put("pharmacy", pharmacy);
map.put("electronics", electronics);
for(int i=0; i<code.length; i++){
if(!isActive[i] || !map.containsKey(businessLine[i]) || !Pattern.matches("[a-zA-Z0-9_]+", code[i])) continue;
map.get(businessLine[i]).add(code[i]);
}
electronics.sort(Comparator.naturalOrder());
grocery.sort(Comparator.naturalOrder());
pharmacy.sort(Comparator.naturalOrder());
restaurant.sort(Comparator.naturalOrder());
electronics.addAll(grocery);
electronics.addAll(pharmacy);
electronics.addAll(restaurant);
return electronics;
}
}
Problem 5: Non-Overlapping intervals
Link to the problem: https://leetcode.com/problems/non-overlapping-intervals/

class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
int res = 0;
Arrays.sort(intervals, (a, b) -> a[1] - b[1]);
int prev_end = intervals[0][1];
for (int i = 1; i < intervals.length; i++) {
if (prev_end > intervals[i][0]) {
res++;
} else {
prev_end = intervals[i][1];
}
}
return res;
}
}




