programmers_야근 지수_java
2023. 2. 28. 16:25ㆍAlgorithm/Programmers
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/12927
import java.util.*;
class Solution {
public long solution(int n, int[] works) {
long answer = 0;
int[] fatigue = fatigueArr(n, works);
answer = calFatigue(fatigue);
return answer;
}
//남은피로도 계산
public int[] fatigueArr (int n, int[] works){
int size = works.length;
int[] fatigues = copyArray(works);
Arrays.sort(fatigues);
int idx = size -1;
int before = fatigues[idx];
while(n>0){
if(fatigues[idx] == 0) break; // 더이상 뺄 수 있는 값 없음
if(before >= fatigues[idx]){ // 최대값으로 이동
idx= size -1;
}
fatigues[idx]--;
before = fatigues[idx];
idx--;
n--;
if(idx == -1){ // 모든 값이 동일한 경우
idx= size -1;
}
}
return fatigues;
}
// 야근지수 계산
public long calFatigue(int[] works){
int size = works.length;
long sum = 0;
for(int i = 0; i<size; i++){
sum += works[i] * works[i];
}
return sum;
}
// 배열 복사
public int[] copyArray(int[] arr){
int size = arr.length;
int[] newArr = new int[size];
for(int i = 0; i<size; i++){
newArr[i] = arr[i];
}
return newArr;
}
}
'Algorithm > Programmers' 카테고리의 다른 글
programmers_단속카메라_java (0) | 2023.03.01 |
---|---|
programmers_등굣길_java (0) | 2023.02.28 |
programmers_최고의 집합_java (0) | 2023.02.28 |
programmers_단어 변환_java (0) | 2023.02.27 |
programmers_네트워크_java (0) | 2023.02.27 |