프로그래머스
2023 현대모비스 알고리즘 경진대회 예선 lv.3 - 상담원 인원
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 전체 문제 로직
2. 코드
public int solution(int k, int n, int[][] reqs) {
// 각 유형마다 멘토가 배치됨에 따라서 대기시간이 어떻게 되는지 구한다.
int[][] waitingTime = new int[n - k + 1][k];
// i명 배치할 때, 각 유형의 기다리는 시간 구하기
for (int i = 0; i < n - k + 1; i++) {
// mentos : i+1명의 mento가 있을 때 각자 상담 끝나는 시간 기록
// timeRecord : 각 유형당 기다린 시간 기록
int[][] mentos = new int[k][i + 1];
int[] timeRecord = new int[k];
// 참가자 배열을 돌려서 timeRecord를 구한다.
for (int j = 0; j < reqs.length; j++) {
boolean hasFreeMento = false;
boolean hasEmptyMento = false;
int freeMentoIndex = 0;
int emptyMentoIndex = 0;
int minTime = Integer.MAX_VALUE;
int minTimeIndex = 0;
int type = reqs[j][2];
for (int m = 0; m < i + 1; m++) {
if (mentos[type - 1][m] <= reqs[j][0]) {
hasFreeMento = true;
freeMentoIndex = m;
break;
} else if (mentos[type - 1][m] == 0) {
hasEmptyMento = true;
emptyMentoIndex = m;
break;
} else if (minTime > mentos[type - 1][m]) {
minTime = mentos[type - 1][m];
minTimeIndex = m;
}
}
if (hasFreeMento) {
mentos[type - 1][freeMentoIndex] = reqs[j][0] + reqs[j][1];
} else if (hasEmptyMento) {
mentos[type - 1][emptyMentoIndex] += reqs[j][0] + reqs[j][1];
} else {
mentos[type - 1][minTimeIndex] += reqs[j][1];
timeRecord[type - 1] += minTime - reqs[j][0];
}
}
// timeRecord를 waitingTime에 기록.
for (int j = 0; j < k; j++) {
waitingTime[i][j] = timeRecord[j] < 0 ? 0 : timeRecord[j];
}
}
// 비교할 인덱스 저장
int[] compareIndex = new int[k];
// 각 유형의 기다리는 시간(최대)
int[] answerArr = waitingTime[0].clone();
// 멘토 한 명을 더 추가했을 때, 줄어드는 시간이 최대인 유형에 멘토 배정
for (int i = 1; i <= n - k; i++) {
int diffTimeMin = Integer.MIN_VALUE;
int minIndex = 0;
for (int j = 0; j < k; j++) {
if (waitingTime[compareIndex[j]][j] - waitingTime[compareIndex[j] + 1][j] > diffTimeMin) {
diffTimeMin = waitingTime[compareIndex[j]][j] - waitingTime[compareIndex[j] + 1][j];
minIndex = j;
}
}
compareIndex[minIndex]++;
answerArr[minIndex] = waitingTime[compareIndex[minIndex]][minIndex];
}
// 각 유형의 기다리는 시간을 더해서 총 기다리는 시간 return
int answer = 0;
for (int i = 0; i < answerArr.length; i++) {
answer += answerArr[i];
}
return answer;
}
첫 lv.3 도전! 꽤 여러 날을 애먹인 문제.. 결국 힌트를 듣기는 했지만 힌트를 듣고 내가 구현하는 것도 상당히 힘들었다!
구현을 한 것에 의의를 두는 걸로~
'STUDY > JAVA' 카테고리의 다른 글
[TIL] 프로그래머스 - 같은 숫자는 싫어 (GPT 첫 개시!) (0) | 2023.06.27 |
---|---|
[TIL] java.lang.IllegalStateException 해결하기 (0) | 2023.06.26 |
[TIL] 피보나치 수열 구하기 - 메모이제이션 (0) | 2023.06.23 |
[TIL] 프로그래머스 lv.2 다음 큰 숫자 (비트연산자) (0) | 2023.06.22 |
[TIL] 예외 되던지기(Exception re-throwing)는 언제 사용될까? (0) | 2023.06.21 |