오늘 푼 문제
3. 프린터/ Lv.2/ 시간 : 42분(타임오버)
programmers.co.kr/learn/courses/30/lessons/42587?language=javascript
1차
function solution(priorities, location) {
var answer = 0;
let keep = true;
priorities = priorities.map((v,i)=>{return {val:v, index: i}});
while(keep){
let tempMax = 0;
let tempPos = 0;
let tempStack = [];
let newStack = [];
answer++;
while(priorities.length){
const cur = priorities.shift();
if(cur.val<=tempMax){
tempStack.push(cur);
}else{
newStack.push(...tempStack);
tempMax = cur.val;
tempPos = cur.index;
tempStack = [{val:tempMax, index:tempPos}];
}
}
if(tempPos===location){
keep = false;
}else{
tempStack.shift();
priorities = [...tempStack,...newStack];
}
}
return answer;
}
2차 : every 사용
function solution(priorities, location) {
var answer = 0;
let processed = 0;
const priorityObjects = priorities.map((val,i)=>{
return {value : val, originLocation : i}
});
while(true){
const target = priorityObjects.shift();
if(priorityObjects.every(ob=> ob.value<=target.value) ){
processed++;
if(target.originLocation === location){
answer = processed;
break;
}
}else{
priorityObjects.push(target);
}
}
return answer;
}
3차 : 추가 객체 할당 없이
function solution(priorities, location) {
var answer = 0;
let order = 0;
let index = 0;
while(priorities.length!==0){
const target = priorities.shift();
const max = Math.max(...priorities);
if(target>=max){
order++;
if(index === location){
answer = order;
break;;
}
}else{
priorities.push(target);
if(location===index){
location = priorities.length-1;
index = 0;
continue;
}
}
index++;
}
return answer;
}
시간 비교
'알고리즘 문제풀이' 카테고리의 다른 글
[프로그래머스] 고득점 키트 - 힙2 (0) | 2020.11.16 |
---|---|
[프로그래머스] 고득점 키트 - 힙1 (0) | 2020.11.06 |
[프로그래머스] 고득점 키트 - 스택/큐2 (0) | 2020.10.28 |
[프로그래머스] 고득점 키트 - 스택/큐1 (0) | 2020.10.27 |
[프로그래머스] 고득점 키트 - 해시2 (0) | 2020.10.22 |