코딩/알고리즘
006. 입국심사
Hoooon22_코딩거북이_
2021. 4. 14. 22:15
728x90

- 사용 언어 : C#
- 체감 난이도 : 중간
- 활용 : 이분탐색
<작성한 코드>
public long solution(int n, int[] times)
{
long answer = -1;
int wait = n; // 대기자
int[] screen = new int[times.Length]; // 심사장
for (int i = 0; i < screen.Length; i++) // 초기화
{
screen[i] = 1;
}
bool delay = false;
if (wait < times.Length)
{
Array.Sort(times);
return times[wait - 1];
}
while (wait > 0)
{
answer++; // 1분 증가
// 대기자 넣어주기
for (int i = 0; i < times.Length; i++)
{
screen[i]--; // 심사 시간 소요
if (screen[i] == 0) // 심사 완료
{
if (wait >= times.Length)
{
wait--;
screen[i] = times[i];
}
else
{
for (int j = 0; j < times.Length; j++)
{
if (times[i] > times[j] + screen[j])
{
delay = true;
break;
}
}
if (!delay)
{
wait--;
screen[i] = times[i];
}
delay = false;
}
}
}
}
// 남은 심사 진행
answer += screen.Max();
return answer;
}
해당 알고리즘으로하면 예시 케이스는 됐는데 나머지는 오버플로우로 멈춘다...