Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/week06/gahyun/p1/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package week06.gahyun.p1;

import java.util.*;

/**
* https://school.programmers.co.kr/learn/courses/30/lessons/42587
* ํ…Œ์ŠคํŠธ 1 ใ€‰ ํ†ต๊ณผ (2.69ms, 80MB)
* ํ…Œ์ŠคํŠธ 2 ใ€‰ ํ†ต๊ณผ (3.57ms, 74MB)
* ํ…Œ์ŠคํŠธ 3 ใ€‰ ํ†ต๊ณผ (3.15ms, 73.6MB)
* ํ…Œ์ŠคํŠธ 4 ใ€‰ ํ†ต๊ณผ (3.58ms, 76.4MB)
* ํ…Œ์ŠคํŠธ 5 ใ€‰ ํ†ต๊ณผ (5.11ms, 90.4MB)
*/

/**
* V์— ์ธ๋ฑ์Šค์™€ ์šฐ์„ ์ˆœ์œ„ ํ•จ๊ป˜ ์ €์žฅ -> queue์— ์‚ฝ์ž…
* ์šฐ์„ ์ˆœ์œ„ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
* ์šฐ์„ ์ˆœ์œ„ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด๋ฉด์„œ queue์— ์ €์žฅ๋œ v.pri์™€ ๋น„๊ต
* ๊ฐ™์Œ -> queue์—์„œ ๊บผ๋‚ด๊ณ  v.ind ์œ„์น˜์— ์‹คํ–‰ ์ˆœ์„œ ์ €์žฅ
* ๋‹ค๋ฆ„ -> queue์— ๋‹ค์‹œ ์‚ฝ์ž…
*/
class Solution {
public int solution(int[] priorities, int location) {
int[] order = new int[priorities.length]; // ํ”„๋กœ์„ธ์Šค ์‹คํ–‰ ์ˆœ์„œ ์ €์žฅ
Queue<V> que = new LinkedList<>();

for (int i=0;i<priorities.length;i++){
que.offer(new V(i,priorities[i]));
}

Integer[] sortedPri = Arrays.stream(priorities)
.boxed().toArray(Integer[]::new);
Arrays.sort(sortedPri, Collections.reverseOrder()); // ์šฐ์„ ์ˆœ์œ„ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ

int runCnt = 0;
while(!que.isEmpty()){
V v = que.poll();
if (v.pri==sortedPri[runCnt]){
order[v.ind] = ++runCnt;
} else {
que.offer(v);
}

}
return order[location];
}
}

class V {
int ind; // ํ”„๋กœ์„ธ์Šค ์ธ๋ฑ์Šค
int pri; // ํ”„๋กœ์„ธ์Šค ์šฐ์„ ์ˆœ์œ„

public V(int ind, int pri){
this.ind = ind;
this.pri = pri;
}
}