Minimum Number of Arrows to Burst Balloons

ㅋㅋ·2023년 1월 5일
0

알고리즘-leetcode

목록 보기
85/135

가로 방향으로 긴 풍선 시작점과 끝점들이 담긴 2차원 정수형 벡터를 받는다

이 때 화살을 세로 방향으로 쏴서 모든 풍선을 터뜨릴 때,

화살을 가장 적게 쏘는 횟수를 구하는 문제

class Solution {
public:
    static const int START = 0;
    static const int END = 1;

    int findMinArrowShots(vector<vector<int>>& points) {
        
        sort(points.begin(), points.end(), 
            [] (const auto &lhs, const auto &rhs)
            {
                return (lhs[START] < rhs[START]);
            });

        int result{1};
        int target{0};
        for (int i = 1; i < points.size(); i++)
        {
            if (points[i][START] <= points[target][END])
            {
                if (points[i][END] < points[target][END])
                {
                    points[target][END] = points[i][END];
                }
                continue;
            }

            target = i;
            result++;
        }

        return result;
    }
};

0개의 댓글