CodingTest/Programmers Lv.3
[프로그래머스 C#] Lv.3 이중우선순위큐
주녘
2024. 2. 5. 14:06
https://school.programmers.co.kr/learn/courses/30/lessons/42628
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 정답코드 및 핵심 아이디어, 유의사항
주어진 조건에 따라 순서대로 값을 추가, 삭제하는 연산을 하여 남아있는 최대 최소 값을 반환하는 문제
직관적인 풀이 - 주석 참조
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution
{
public int[] solution(string[] operations)
{
int[] answer = new int[2] { 0, 0 };
var list = new List<int>();
foreach (string str in operations)
{
string[] strs = str.Split(' ');
// 숫자 추가
if (strs[0] == "I")
{
list.Add(int.Parse(strs[1]));
continue;
}
// 명령 무시 조건
if (list.Count == 0)
continue;
// 최대 값 or 최소 값 삭제
if (strs[1] == "1")
list.Remove(list.Max());
else // strs[1] == "-1"
list.Remove(list.Min());
}
// 값이 남아 있을 경우
if (list.Count > 0)
{
answer[0] = list.Max();
answer[1] = list.Min();
}
return answer;
}
}