๋ฌธ์ œ

ํ˜„์žฌ ๋Œ€๊ธฐ๋ชฉ๋ก์— ์žˆ๋Š” ๋ฌธ์„œ์˜ ์ค‘์š”๋„๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด๊ธด ๋ฐฐ์—ด priorities์™€ ๋‚ด๊ฐ€ ์ธ์‡„๋ฅผ ์š”์ฒญํ•œ ๋ฌธ์„œ๊ฐ€ ํ˜„์žฌ ๋Œ€๊ธฐ๋ชฉ๋ก์˜ ์–ด๋–ค ์œ„์น˜์— ์žˆ๋Š”์ง€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” location์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋‚ด๊ฐ€ ์ธ์‡„๋ฅผ ์š”์ฒญํ•œ ๋ฌธ์„œ๊ฐ€ ๋ช‡ ๋ฒˆ์งธ๋กœ ์ธ์‡„๋˜๋Š”์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

1. ์ธ์‡„ ๋Œ€๊ธฐ๋ชฉ๋ก์˜ ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š” ๋ฌธ์„œ(J)๋ฅผ ๋Œ€๊ธฐ๋ชฉ๋ก์—์„œ ๊บผ๋ƒ…๋‹ˆ๋‹ค.
2. ๋‚˜๋จธ์ง€ ์ธ์‡„ ๋Œ€๊ธฐ๋ชฉ๋ก์—์„œ J๋ณด๋‹ค ์ค‘์š”๋„๊ฐ€ ๋†’์€ ๋ฌธ์„œ๊ฐ€ ํ•œ ๊ฐœ๋ผ๋„ ์กด์žฌํ•˜๋ฉด J๋ฅผ ๋Œ€๊ธฐ๋ชฉ๋ก์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋„ฃ์Šต๋‹ˆ๋‹ค.
3. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด J๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ์‚ฌํ•ญ

• ํ˜„์žฌ ๋Œ€๊ธฐ๋ชฉ๋ก์—๋Š” 1๊ฐœ ์ด์ƒ 100๊ฐœ ์ดํ•˜์˜ ๋ฌธ์„œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
• ์ธ์‡„ ์ž‘์—…์˜ ์ค‘์š”๋„๋Š” 1~9๋กœ ํ‘œํ˜„ํ•˜๋ฉฐ ์ˆซ์ž๊ฐ€ ํด์ˆ˜๋ก ์ค‘์š”ํ•˜๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.
• location์€ 0 ์ด์ƒ (ํ˜„์žฌ ๋Œ€๊ธฐ๋ชฉ๋ก์— ์žˆ๋Š” ์ž‘์—… ์ˆ˜ - 1) ์ดํ•˜์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ ๋Œ€๊ธฐ๋ชฉ๋ก์˜ ๊ฐ€์žฅ ์•ž์— ์žˆ์œผ๋ฉด 0, ๋‘ ๋ฒˆ์งธ์— ์žˆ์œผ๋ฉด 1๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.

 

ํ•„์š” ๊ฐœ๋…

๋ฐฐ์—ด์— ๊ฐ’ ์ถ”๊ฐ€ 

unshift() : ๋ฐฐ์—ด์˜ ๋งจ ์•ž์— ๊ฐ’ ์ถ”๊ฐ€

push() : ๋ฐฐ์—ด์˜ ๋งจ ๋์— ๊ฐ’ ์ถ”๊ฐ€

๋ฐฐ์—ด์— ๊ฐ’ ์ œ๊ฑฐ

shift() : ๋ฐฐ์—ด์˜ ๋งจ ์•ž์— ๊ฐ’ ์ œ๊ฑฐ

pop() : ๋ฐฐ์—ด์˜ ๋งจ ๋’ค์˜ ๊ฐ’ ์ œ๊ฑฐ

 

ํ’€์ด

์ถœ๋ ฅํšŸ์ˆ˜๋ฅผ ์นด์šดํŠธํ•  ๋ณ€์ˆ˜ count

ํ˜„์žฌ ์œ„์น˜ ์ธ๋ฑ์Šค๋ฅผ ๋‹ด๊ณ  ๋ณ€๊ฒฝํ•ด์ค„ ๋ณ€์ˆ˜ myDoc

 

while๋ฌธ์œผ๋กœ ๋ฐ˜๋ณตํ•ด์„œ ๋ฐฐ์—ด์„ ๋Œ๋ฉด์„œ 

ํ˜„์žฌ ์œ„์น˜์˜ ์š”์†Œ๋ฅผ ๊บผ๋‚ธ ํ›„(shift),

๋‚˜๋จธ์ง€ ์š”์†Œ๋“ค๊ณผ ๋น„๊ตํ•ด์„œ ์ œ์ผ ๋†’์€ ์ˆซ์ž๋ผ๋ฉด ์ถœ๋ ฅํ•œ๋‹ค(์นด์šดํŒ…). ๋งŒ์•ฝ ๋‚ด ์ธ๋ฑ์Šค ๋ณ€์ˆ˜๊ฐ€ 0๋ฒˆ์งธ๋ผ๋ฉด answer์— ๋‹ด์•„์„œ ๋ฆฌํ„ดํ•œ๋‹ค. 

๋‚˜๋จธ์ง€ ์š”์†Œ๋“ค๊ณผ ๋น„๊ตํ•ด์„œ ํ˜„์žฌ ์œ„์น˜ ์š”์†Œ๋ณด๋‹ค ๋†’์€ ์ˆซ์ž๊ฐ€ ์žˆ๋‹ค๋ฉด, ๊บผ๋‚ธ ์š”์†Œ๋ฅผ ๋ฐฐ์—ด์˜ ๋งจ ๋’ค์— ๋‹ค์‹œ ๋„ฃ๋Š”๋‹ค(push)

(์ดํ›„ ๊ณตํ†ต์œผ๋กœ)

๋‚ด ์ธ๋ฑ์Šค๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฐ์—ด ๋‚ด ์ด๋™์ด ์žˆ์—ˆ์œผ๋ฏ€๋กœ -1์”ฉํ•ด์ค€๋‹ค. 

๋งŒ์•ฝ ๋‚ด ๋ฌธ์„œ๊ฐ€ ๋งจ ๋’ค๋กœ ๊ฐ„ ๊ฒฝ์šฐ, ๋‚ด ๋ฌธ์„œ์˜ ์œ„์น˜๋„ ๋งจ ๋’ค๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค. 

 

์ฝ”๋“œ

const solution = (priorities, location) => {
    let answer;
    let count= 0;
    let myDoc = location;
    
    while(priorities.length>0){
        let item = priorities.shift();
        if(priorities.filter((el) => el > item).length>0){
            priorities.push(item);
        } else {
            count++;
            if(myDoc === 0) {
                return answer = count;
            }
        }
        myDoc--;
        if(myDoc === -1){
            myDoc = priorities.length -1;
        }
    }
    
    return answer;
}

 

๋‹ค๋ฅธ ํ’€์ด ์ฝ”๋“œ

function solution(priorities, location) {
    let temp = [...priorities];
    let max = 0;
    let answer = 0;
    while(temp.length>0) {
        max = Math.max(...temp);
        if(temp[0] !== max) {
            temp.push(temp.shift());
        } else {
            temp.shift();
            answer++;
            if(location===0) return answer;
        }
        location>0 ? location-- : location = temp.length - 1;
    }
}

 

+ Recent posts