λ¬Έμ : https://school.programmers.co.kr/learn/courses/30/lessons/42587
νμ΄κ³Όμ
1. μ°μ μμκ° κ°μ₯ λμ νλ‘μΈμ€λ₯Ό μ°ΎμμΌ ν©λλ€. πμ°μ μμ νλ₯Ό νμ© O(logn)νμ¬ κ΅¬ν μ μμ΅λλ€.
2. λ¬Έμ μ 2λ² μ‘°κ±΄μ ꡬνν΄μΌ ν©λλ€.
2λ² μ‘°κ±΄μ λμμμ λ€μ νμ λ£λ μμλ€μ μμλ μ μ§λ©λλ€.
μ κ·Έλ¦Όκ³Ό κ°μ΄ (2,1)μ λ€λ‘ κ°μΌνλλ° μ΄λ λ€λ‘ κ°λ (2,1)μ μμλ μ μ§λ©λλ€.
λ°λΌμ νλ₯Ό νμ©ν΄μ ꡬνν νμ μμ΄ μ£Όμ΄μ§ priorities λ°°μ΄λ€μ μμλλ‘ λ¬΄νμν(?)νλ©΄ λ©λλ€.
3. λ¬Έμ μμ μꡬνλ μ λ΅μΈ ν΄λΉ νλ‘μΈμ€κ° λͺ λ²μ§Έλ‘ μ€νλλμ§ νμ ν΄μΌ ν©λλ€.
λ°°μ΄λ€μ 무νμν(2λ² νμ΄κ³Όμ ) νλ©° μ°μ μμκ° κ°μ₯ λμ νλ‘μΈμ€(1λ² κ³Όμ )μ μΌμΉνλ©΄ ν΄λΉ νλ‘μΈμ€λ μ€νλλ―λ‘ answerμ +1ν΄μ€λλ€.
μ΄λ locationκ³Ό μ€νλλ νλ‘μΈμ€μ μΈλ±μ€κ° κ°μΌλ©΄ λ¬Έμ μμ μꡬνλ νλ‘μΈμ€λ₯Ό μ°Ύμ κ²μ΄λ―λ‘ ν¨μλ₯Ό μ’ λ£ν©λλ€.
λ°°μ΄μ
- μ°μ μμμ λν λ¬Έμ μμ μ°μ μμνλ₯Ό μκΈ΄νκ² νμ©ν μ μμμ μκ²λμμ΅λλ€.
- νλ‘μΈμ€λ€μ νλ₯Ό νμ©νμ§ μκ³ , λ°°μ΄μ 무νμν νλ λ°©μμΌλ‘ ꡬνν μ μμμ μκ²λμμ΅λλ€.
- μ°μ μμ νμ removeν¨μλ₯Ό μ¬μ©ν μ μμ΅λλ€. μ΄λ removeν¨μμ μκ°λ³΅μ‘λλ O(n) μ λλ€.
μ 체μ½λ
import java.util.*;
class Solution {
int answer;
void solve(int[] priorities,int location){
PriorityQueue<Integer> pq = new PriorityQueue<>((o1,o2)->-(o1-o2));
for(int i=0;i<priorities.length;i++){
pq.offer(priorities[i]);
}
while(!pq.isEmpty()){
for(int i=0;i<priorities.length;i++){
if(priorities[i]==pq.peek()){
answer++;
pq.poll();
if(i==location) return;
}
}
}
}
public int solution(int[] priorities, int location) {
answer = 0;
solve(priorities,location);
return answer;
}
}
'πμ½λ©ν μ€νΈ:CodingTest' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
ν¬ν¬μΈν° μκ³ λ¦¬μ¦ (0) | 2024.04.05 |
---|---|
[λ°±μ€] λλκ³€ μ»€λΈ (ꡬν) (0) | 2024.04.05 |
[νλ‘κ·Έλλ¨Έμ€] μμ (0) | 2024.04.01 |
[νλ‘κ·Έλλ¨Έμ€] λ°©μ κ°μ (0) | 2024.03.29 |
μ΄λΆνμ (0) | 2024.03.06 |