프로그래머스 레벨1 C# 체육복(list Remove)
C#
using System;
public class Solution {
public int solution(int n, int[] lost, int[] reserve) {
int answer = 0;
int[] student = new int[n];
int a = 0;
for(int i=0;i<n;i++)
{
student[i] = i+1;
}
//학생 번호를 정의한다.
for(int i=0;i<n;i++)
{
for(int j=0;j<reserve.Length;j++)
{
if(student[i] == reserve[j])
{
student[i] = 0;
}
}
//reserve에 기록된 학생들은 0으로 바꿔서 0의 개수를 리턴하게
if(i>=1 && student[i-1] != 0)
{
student[i-1] = 0;
}
if(i+1 < n && student[i+1] != 0 && i>=1 && student[i-1] != 0 && student[i] == 0)
{
student[i+1] = 0;
}
}
for(int i=0;i<n;i++)
{
if(student[i] == 0)
{
answer++;
}
}
return answer;
}
}
// 이렇게 했는데 안돼서 걍 다른 사람 거를 봤다.
using System;
using System.Collections;
using System.Collections.Generic;
public class Solution {
public int solution(int n, int[] lost, int[] reserve) {
List<int> lostList = new List<int>(lost);
List<int> reserveList = new List<int>(reserve);
//소팅 안되어 있음
lostList.Sort();
reserveList.Sort();
List<int> tempList = new List<int>(reserveList);
// 소팅 되어 있음
for (int i = 0; i < tempList.Count; i++)
{
if (lostList.Contains(tempList[i]))
{
lostList.Remove(tempList[i]);
reserveList.Remove(tempList[i]);
}
}
for (int i = 0; i < reserveList.Count; i++)
{
if (lostList.Contains(reserveList[i] - 1))
{
lostList.Remove(reserveList[i] - 1);
continue;
}
if (lostList.Contains(reserveList[i] + 1))
{
lostList.Remove(reserveList[i] + 1);
continue;
}
}
return n - lostList.Count;
}
}
////
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int solution(int n, int[] lost, int[] reserve)
{
int answer = 0;
Queue<int> que = new Queue<int>();
for (int i = 1; i <= n; i++)
{
if (lost.Contains(i))
{
if (reserve.Contains(i))
{
answer++;
que.Enqueue(i);
}
else if (reserve.Contains(i - 1) && lost.Contains(i - 1) == false && que.Contains(i - 1) == false)
{
answer++;
que.Enqueue(i - 1);
}
else if (reserve.Contains(i + 1) && lost.Contains(i + 1) == false && que.Contains(i + 1) == false)
{
answer++;
que.Enqueue(i + 1);
}
}
else answer++;
}
return answer;
}
}