티스토리 뷰
title: "프린터"
category: 프로그래머스[Level-2]
tags: [C++, JavaScript, 프로그래머스]
date: "2021-01-21"
문제 링크
C++
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int solution(vector<int> priorities, int location) {
int answer = 0;
vector<pair<int, int>> v; // 저장용
queue<pair<int, int>> q; // 순서목록
for(int i=0; i<priorities.size(); i++){
q.push(make_pair(i, priorities[i]));
}
sort(priorities.rbegin(), priorities.rend()); // 내림차순
for(int i=0; i<priorities.size(); ){
pair<int, int> cur = q.front();
q.pop();
if(cur.second != priorities[i]) q.push(cur);
else {
v.push_back(cur);
i++;
}
}
for(int i=0; i<v.size(); i++){
if(v[i].first != location) continue;
answer = i+1;
}
return answer;
}
JavaScript
function solution(priorities, location) {
var answer = 0;
let vector = []; // 저장용
const queue = priorities.map((value, index) => {
// 큐 초기화
return [index, value];
});
priorities.sort((a, b) => b - a);
for (let i = 0; i < priorities.length; ) {
const cur = queue.shift();
if (cur[1] !== priorities[i]) {
queue.push(cur);
} else {
vector.push(cur);
i++;
}
}
vector.some((value, index) => {
if (value[0] === location) {
answer = index + 1;
return true;
}
});
return answer;
}
728x90
반응형
'Programmers Solutions > Level-2' 카테고리의 다른 글
[프로그래머스] 삼각 달팽이 (0) | 2021.02.01 |
---|---|
[프로그래머스] 문자열 압축 (0) | 2021.02.01 |
[프로그래머스] 124 나라의 숫자 (0) | 2021.01.31 |
[프로그래머스] 멀쩡한 사각형 (0) | 2021.01.31 |
[프로그래머스] 스킬트리 (0) | 2021.01.31 |
댓글