728x90
#include <iostream>
#include <queue>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, num, k = 0, temp, count = 0;
pair<int, int> t;
cin >> n;
while(n--){
queue<pair<int,int>> q;
priority_queue<int> qq;
count = 0;
cin >> num >> k;
for(int i=0; i<num; i++){
cin >> temp;
if(i == k) q.push(make_pair(temp, 1));
else q.push(make_pair(temp, 0));
qq.push(temp);
}
if(q.size() == 1) cout << "1\n";
else {
while(!q.empty()){
t = q.front();
q.pop();
if(qq.top() != t.first ) q.push(t);
else{
qq.pop();
count++;
if(t.second == 1){
cout << count <<"\n";
break;
}
}
}
}
}
}
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
class Solution { public int solution(int[] priorities, int location) {
int answer = 0;
Queue<print_info> prior_Q = new LinkedList<print_info>();
Iterator<print_info> it;
// Queue 생성
for (int i = 0; i<priorities.length; i++)
if(i != location)
prior_Q.add(new print_info(priorities[i], false));
else
prior_Q.add(new print_info(priorities[i], true));
print_info A;
boolean check = true;
while(!prior_Q.isEmpty()) {
check = true;
A = prior_Q.poll();
it = prior_Q.iterator();
while (it.hasNext()) {
if (A.data < it.next().data) {
check = false;
break;
}
}
// check가 true라면 큐 안 요소 중에서 A가 크다는 것을 의미함
if (check && A.flag){
answer++;
break;
} else if(check) {
answer++;
} else {
prior_Q.add(new print_info(A.data, A.flag));
}
}
return answer;
}
static class print_info {
int data;
boolean flag;
public print_info(int data, boolean flag){
this.data = data;
this.flag = flag;
}
}
}
c++은 2020.4.24에 짠 코드이고, Java는 대략 2월 초중에 짠 코드이다.
c++의 경우 두개의 큐, queue와 우선순위 queue를 사용하여 짠 코드이다. 방금 짜서 그런지 c++이 더 간결하다고 생각이 든다.
Java로 짠 2월달 아기는 다시 읽을 때 뭐지? 이 생각을 조금 했다. ㅋㅋㅋㅋ
다시 보니 c++과 동일하게 data와 flag를 가져 내가 원하고자하는 수를 찾고자했다.
다른 점은 우선 순위 큐를 사용하지 않고 이걸 queue를 한 바퀴 돌아서 값이 제일 큰지 확인한다. 그 다음 제일 크고 flag가 true이라면 answer값을 반환한다.
사실 c++로 짜면서도 큐를 한 바퀴 돌까 생각했는데 도무지 귀찮아서 그럴 생각이 들지 않았는데 2월달에 난 귀찮아도 일단 풀었다. 대단하다 ! 굿굿
반응형
'코딩 관련 > c++' 카테고리의 다른 글
백준 1003번 피보나치 함수 C++ (0) | 2020.05.06 |
---|---|
백준 1620 나는야 포켓몬 마스터 이다솜 (0) | 2020.05.03 |
백준 수 정렬하기3 (0) | 2020.04.24 |
백준 2108번 통계학 (0) | 2020.04.24 |
백준 4949 균형잡힌 세상 (0) | 2020.04.23 |