체육복
https://school.programmers.co.kr/learn/courses/30/lessons/42862
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
처음에 틀렸을 때는 배열을 정렬하지 않고 그냥 찾는 것으로 진행했다가 틀리게 되었다.
이후 배열을 정렬한 후 찾는 방식으로 해결하게 되었다.
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int solution(int n, int[] lost, int[] reserve) {
int count = n - lost.Length;
Array.Sort(lost);
Array.Sort(reserve);
for (int i = 0; i < lost.Length; i++)
{
if (reserve.Contains(lost[i]))
{
int index = Array.IndexOf(reserve, lost[i]);
reserve[index] = -1;
++count;
}
else if (reserve.Contains(lost[i] - 1))
{
if (!lost.Contains(lost[i] - 1))
{
int index = Array.IndexOf(reserve, lost[i] - 1);
reserve[index] = -1;
++count;
}
}
else if (reserve.Contains(lost[i] + 1))
{
if (!lost.Contains(lost[i] + 1))
{
int index = Array.IndexOf(reserve, lost[i] + 1);
reserve[index] = -1;
++count;
}
}
}
return count;
}
}
'알고리즘' 카테고리의 다른 글
프로그래머스 - 대충 만든 자판 (0) | 2023.09.06 |
---|---|
프로그래머스 - 옹알이 (2) (0) | 2023.09.06 |
프로그래머스 - 숫자 짝궁 (0) | 2023.09.06 |
프로그래머스 - 로또의 최고 순위와 최저 순위 (0) | 2023.09.06 |
프로그래머스 - 시저 암호 (0) | 2023.08.30 |