Leetcode Daily Problem - 5 Jan 2023

Leetcode Daily Problem - 5 Jan 2023

Minimum Number of Arrows to Burst Balloons - Javascript Algorithms

Problem Name - Minimum Number of Arrows to Burst Balloons

Problem Link - https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/

Solution 1

/**
 * @param {number[][]} points
 * @return {number}
 */
var findMinArrowShots = function(points) {
    points.sort((a,b) => a[0]!=b[0]?a[0]-b[0]:a[1]-b[1]);

    let ans = 1;
    let prevEnd = points[0][1];
    let prevStart = points[0][0];

    // simply i am taking intersection from start as much as I can
    // at any point if I can't take intersection 
    // I increase the count and update the prevStart and prevEnd with 
    // x and y 

    for(let i=1;i<points.length;i++){
        const [x, y] = points[i];
        if(x<=prevEnd && x>=prevStart){
            prevStart = Math.max(prevStart, x);
            prevEnd = Math.min(prevEnd,y);
        }else{
            ans++;
            prevEnd = y;
            prevStart = x;
        }
    }

    return ans;
};

Thanks for reading !!