Skip to main content

Command Palette

Search for a command to run...

Day 31 of LeetCode Challenge

Published
2 min readView as Markdown
Day 31 of LeetCode Challenge
T

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: Maximum Number of Events That Can Be Attended

Link to the problem: https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/

class Solution {
    public int maxEvents(int[][] events) {
        Arrays.sort(events, (a, b) -> Integer.compare(a[0], b[0]));

        int day = 0, index = 0 , n = events.length ,result = 0;      

        PriorityQueue<Integer> pq = new PriorityQueue<>();
        while (!pq.isEmpty() || index < n) {
            if (pq.isEmpty()) {
                day = events[index][0];
            }
            while (index < n && events[index][0] <= day) {
                pq.offer(events[index][1]);
                index++;
            }
            pq.poll();
            result++; 
            day++;    

            while (!pq.isEmpty() && pq.peek() < day) {
                pq.poll();
            }
        }
        return result;
    }
}

Problem 2: Merge Intervals

Link to the problem: https://leetcode.com/problems/merge-intervals/

class Solution {
    public int[][] merge(int[][] intervals) {

        Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));

        List<int[]> merged = new ArrayList<>();
        int[] prev = intervals[0];

        for (int i = 1; i < intervals.length; i++) {
            int[] interval = intervals[i];
            if (interval[0] <= prev[1]) {
                prev[1] = Math.max(prev[1], interval[1]);
            } else {
                merged.add(prev);
                prev = interval;
            }
        }

        merged.add(prev);

        return merged.toArray(new int[merged.size()][]);         
    }
}

Problem 3: Subarray Sum Equals K

Link to the problem: https://leetcode.com/problems/subarray-sum-equals-k/

class Solution {
    public int subarraySum(int[] nums, int k) {
        HashMap<Integer, Integer> subNum = new HashMap<>();
        subNum.put(0, 1);
        int total = 0, count = 0;

        for(int n : nums) {
            total += n;
            if(subNum.containsKey(total - k)) count += subNum.get(total - k);
            subNum.put(total, subNum.getOrDefault(total, 0) + 1);
        }
        return count;
    }
}

Problem 4: 3Sum Closest

Link to the problem: https://leetcode.com/problems/3sum-closest/

class Solution {
    public int threeSumClosest(int[] nums, int target) {
        Arrays.sort(nums);
        int n = nums.length;
        int result = nums[0] + nums[1] + nums[2];

        for(int i = 0; i < n - 2; i++) {
            int left = i + 1, right = n - 1;
            while(left < right) {
                int sum = nums[i] + nums[left] + nums[right];
                if(Math.abs(target - sum) < Math.abs(target - result))
                    result = sum;
                if(sum == target) return target;
                else if(sum < target) left++;
                else right--;
            }
        }
        return result;
    }
}

Problem 5: Sliding Window Maximum

Link to the problem: https://leetcode.com/problems/sliding-window-maximum/

class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        List<Integer> res = new ArrayList<>();
        Deque<Integer> deque = new LinkedList<>();

        for (int idx = 0; idx < nums.length; idx++) {
            int num = nums[idx];
            while (!deque.isEmpty() && deque.getLast() < num) deque.pollLast();

            deque.addLast(num);
            if(idx >= k && nums[idx - k] == deque.getFirst()) deque.pollFirst();
            if(idx >= k - 1) res.add(deque.getFirst());
        }
        return res.stream().mapToInt(i -> i).toArray();        
    }
}

More from this blog

T

Tushar Pant's Blog

178 posts

Welcome to Tushar's Blog! Here, I share my journey in tech, covering DevOps and cloud computing. Explore tutorials, tips, and insights to fuel your learning and growth in technology.