문제를 풀었는데, 테스트 결과에서는 3개 다 성공이지만.... 채점하기로 가게되면...난리가 난다..... 어떻게 더 고쳐야하는거지...
드디어 성공이다 ㅡ_ㅡ 아이고 이거 푸는데 한참 걸렸네......
혹시 몰라 예전풀이도 남겨놓는다.
나의 풀이
import java.util.*;
class Solution {
public long solution(int n, int[] works) {
/*
* PriorityQuene << 우선순위 큐 정렬하기
*
* >>>>> 우선순위 큐 오름차순 정렬
* PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
* priorityQueue.add(4);
* priorityQueue.add(6);
* priorityQueue.add(5);
* priorityQueue.add(1);
*
*
* while(!priorityQueue.isEmpty()) {
* System.out.println(priorityQueue.poll()); //remove()를 사용하셔도 됩니다.
* }
*
* 출력
* 1 4 5 6
*
* >>>>> 우선순위 큐 내림차순 정렬
* new PriorityQueue<>(Collections.reverseOrder())
*
* priorityQueue.add(4);
* priorityQueue.add(6);
* priorityQueue.add(5);
* priorityQueue.add(1);
*
*
* while(!priorityQueue.isEmpty()) {
* System.out.println(priorityQueue.poll()); //remove()를 사용하셔도 됩니다.
* }
*
* 출력
* 6 5 4 1
*
*/
long answer = 0;
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Collections.reverseOrder());
for(int work:works){
priorityQueue.add(work);
}
while(n>0){
if(priorityQueue.isEmpty()){
break;
}
int num = priorityQueue.poll();
if(num>1){
priorityQueue.add(num-1);
}
n--;
}
while(!priorityQueue.isEmpty()){
int num = priorityQueue.poll();
answer+=num*num;
}
return answer;
}
}
나의 예전 풀이
class Solution {
public long solution(int n, int[] works) {
long answer = 0;
// n에서 1시간씩 각 배열 순서대로 빼줘야함.
for(int i=0; i<n; i++){
int temp_i = 0;
// i 가 배열의 길이를 넘어간다면 다시 처음으로 돌아옴
if(i >= works.length){
temp_i = i % works.length;
}else{
temp_i = i;
}
// i번째 배열의 값에서 1씩 빼준 후 다시 배열에 집어넣음
int temp = works[temp_i];
if(temp!=0) {
temp --;
}
works[temp_i] = temp;
}
for(int k=0; k<works.length; k++){
answer += Math.pow(works[k],2);
}
return answer;
}
}
테스트결과
채점결과
.
.
.
.
.
.
.
.
에라이!!
'Algorithm' 카테고리의 다른 글
[Algorithm] 평균구하기 (0) | 2022.10.11 |
---|---|
[Algorithm] 로또의 최고 순위와 최저 순위 (0) | 2022.08.31 |
[Algorithm] 같은 숫자는 싫어 (0) | 2022.08.30 |
[Algorithm] 가장 큰 수 (0) | 2022.08.29 |
[Algorithm] 알고리즘 공부 시작 방법 및 순서 (0) | 2022.08.26 |