코딩공부/프로그래머스

※[프로그래머스]Lv.2 요격 시스템 C# (List<(int, int)>)

usingsystem 2023. 6. 20. 12:31
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/181188

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

소스코드

마지막 answer ++ 하는 이유는 마지막 미사일은 요격을 안항 상태로 반복문을 빠져나오기 때문이다.

using System;
using System.Collections.Generic;
using System.Linq;
public class Solution
{
    public int solution(int[,] targets)
    {
        int answer = 0;

        List<(int, int)> list = new List<(int, int)>();

        for (int i = 0; i < targets.GetLength(0); i++)
            list.Add((targets[i, 0], targets[i, 1]));

        list = list.OrderBy(o => o.Item1).ToList();

        int x = int.MaxValue;
        foreach (var point in list)
        {
            if (point.Item2 < x)
            {
                x = point.Item2;
                continue;
            }

            if (point.Item1 >= x)
            {
                answer++;
                x = point.Item2;
            }
        }

        if (list.Count > 0)
            answer++;

        return answer;
    }
}
728x90