๋ฌธ์ œ

ํ˜„์žฌ ๋Œ€๊ธฐ๋ชฉ๋ก์— ์žˆ๋Š” ๋ฌธ์„œ์˜ ์ค‘์š”๋„๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด๊ธด ๋ฐฐ์—ด 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;
    }
}

 

๋ฌธ์ œ

Finn์€ ์š”์ฆ˜ ์ˆ˜ํ•™๊ณต๋ถ€์— ๋น ์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆ˜ํ•™ ๊ณต๋ถ€๋ฅผ ํ•˜๋˜ Finn์€ "์ž์—ฐ์ˆ˜ n์„ ์—ฐ์†ํ•œ ์ž์—ฐ์ˆ˜๋“ค๋กœ ํ‘œํ˜„ ํ•˜๋Š” ๋ฐฉ๋ฒ•"์ด ์—ฌ๋Ÿฌ๊ฐœ๋ผ๋Š” ์‚ฌ์‹ค์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด 15๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด 4๊ฐ€์ง€๋กœ ํ‘œํ˜„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1 + 2 + 3 + 4 + 5 = 15

4 + 5 + 6 = 15

7 + 8 = 15

15 = 15

์ž์—ฐ์ˆ˜ n์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์—ฐ์†๋œ ์ž์—ฐ์ˆ˜๋“ค๋กœ n์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ returnํ•˜๋Š” solution๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

ํ’€์ด

์ฒ˜์Œ์— ์ฐพ์€ ๊ทœ์น™์€ ์•ฝ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์•˜๋‹ค.

15์˜ ์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜ [1,3,5,15] -> 4๊ฐœ

 

14์˜ ์•ฝ์ˆ˜๋กœ ๊ณ„์‚ฐํ•ด๋ณด์•˜์„ ๋•Œ 

์—ฐ์†๋œ ๊ฒฝ์šฐ๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ 2๊ฐ€์ง€์ธ๋ฐ,

2+3+4+5 = 14

14 = 14 

์•ฝ์ˆ˜๋Š” [1,2,7,14] 4๊ฐœ์ด๋‹ค. 

 

์™„์ „ํƒ์ƒ‰์œผ๋กœ ํ’€์–ด์•ผํ•˜๋‚˜ ์‹ถ์–ด์„œ Array.from์œผ๋กœ 1๋ถ€ํ„ฐ n๊นŒ์ง€ ๋‹ด๊ธด ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , ๋ˆ„์ ํ•ด์„œ ๋”ํ•˜๋‹ค๊ฐ€ 15๊ฐ€ ๋˜๋ฉด ์นด์šดํŒ…์„ ํ•œ๋‹ค. ๋˜๊ธดํ•  ๊ฒƒ ๊ฐ™์€๋ฐ ์‹œ๊ฐ„๋ณต์žก๋„์—์„œ ๋ณ„๋กœ์ธ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๊ณ , ์•ฝ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

ํ•ด๋‹น ๋ฌธ์ œ๋Š”

"์ฃผ์–ด์ง„ ์ˆ˜๋ฅผ ์—ฐ์†๋œ ์ž์—ฐ์ˆ˜์˜ ํ•ฉ์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜"์™€

"์ฃผ์–ด์ง„ ์ˆ˜์˜ ํ™€์ˆ˜์ธ ์•ฝ์ˆ˜์˜ ๊ฐฏ์ˆ˜"๋Š” ๊ฐ™๋‹ค.

์ฃผ์–ด์ง„ ์ˆ˜ ๋ฅผ ์—ฐ์†๋œ ์ž์—ฐ์ˆ˜์˜ ํ•ฉ์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ• ์ˆ˜ = ์ฃผ์–ด์ง„ ์ˆ˜์˜ ์•ฝ์ˆ˜ ์ค‘์— ํ™€์ˆ˜์˜ ๊ฐœ์ˆ˜

๋Š” ๊ณต์‹์„ ์ด์šฉํ•œ ํ’€์ด๋ผ๊ณ ํ•œ๋‹ค. 

 

์•ฝ์ˆ˜ ์ค‘์— ํ™€์ˆ˜์ธ ๊ฒƒ์˜ ๊ฐฏ์ˆ˜๋ฅผ ์ฐพ์•„์•ผํ–ˆ๋‹ค. 

*์•ฝ์ˆ˜ : ์–ด๋–ค ์ˆ˜๋ฅผ ๋‚˜๋ˆ„์–ด ๋‚˜๋จธ์ง€ ์—†์ด ๋–จ์–ด์ง€๋Š” ์ˆ˜

15์˜ ์•ฝ์ˆ˜๋Š” 1, 3, 5, 15 ์ด๊ณ  ์ด ์ค‘ ํ™€์ˆ˜๋Š” 4๊ฐœ์ด๋‹ค. 

• ์•ฝ์ˆ˜ 1 => ์—ฐ์†ํ•˜๋Š” 1๊ฐœ ์ž์—ฐ์ˆ˜์˜ ํ•ฉ์œผ๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅ
   15 = 15
• ์•ฝ์ˆ˜ 3 => ์—ฐ์†ํ•˜๋Š” 3๊ฐœ ์ž์—ฐ์ˆ˜์˜ ํ•ฉ์œผ๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅ15๋ฅผ 3์œผ๋กœ ๋‚˜๋ˆˆ๊ฐ’์ธ 5๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
   5 + 5 + 5 = 15 => 3 + 4 + 5 = 15
• ์•ฝ์ˆ˜ 5 => ์—ฐ์†ํ•˜๋Š” 5๊ฐœ ์ž์—ฐ์ˆ˜์˜ ํ•ฉ์œผ๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅ15๋ฅผ 5๋กœ ๋‚˜๋ˆˆ๊ฐ’์ธ 3์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
   3 + 3 + 3 + 3 + 3 = 15 => 1 + 2 + 3 + 4 + 5 = 15
• ์•ฝ์ˆ˜ 15 => ๋ชจ๋“  ํ™€์ˆ˜(2n+1)๋Š” n๊ณผ n+1๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅ
   7 + 8 = 15

 

 

์ฝ”๋“œ

const solution = (n) => {
  let count = 0;

  for (let i = 0; i <= n; i++) {
    if (n % i === 0 && i % 2 !== 0) {
      count++;
    }
  }

  return count;
}

๋ฌธ์ œ

๊ธธ์ด๊ฐ€ ๊ฐ™์€ ๋ฐฐ์—ด A, B

๋ฐฐ์—ด A, B์—์„œ ๊ฐ๊ฐ ํ•œ ๊ฐœ ์ˆซ์ž ๋ฝ‘์•„ ๋‘ ์ˆ˜๋ฅผ ๊ณฑํ•œ๋‹ค. -> ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณต. 

๋‘ ์ˆ˜๋ฅผ ๊ณฑํ•œ ๊ฐ’์„ ๋ˆ„์ ํ•˜์—ฌ ๋”ํ•œ๋‹ค.

 

์ตœ์ข…์ ์œผ๋กœ ๋ˆ„์ ๋œ ๊ฐ’์ด ์ตœ์†Œ๊ฐ€ ๋˜๋„๋ก

(๊ฐ ๋ฐฐ์—ด์—์„œ k๋ฒˆ์งธ ์ˆซ์ž ๋ฝ‘์•˜๋‹ค๋ฉด ๋‹ค์Œ๋ฒˆ์— k๋ฒˆ์งธ ์ˆซ์ž๋Š” ๋ฝ‘์„ ์ˆ˜ ์—†๋‹ค)

 

๋ฐฐ์—ด A, B๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ์ตœ์ข…์ €๊ธ๋กœ ๋ˆ„์ ๋œ ์ตœ์†Ÿ๊ฐ’์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

- ๋ฐฐ์—ด A, B์˜ ํฌ๊ธฐ : 1,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜

- ๋ฐฐ์—ด A, B์˜ ์›์†Œ์˜ ํฌ๊ธฐ : 1,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜

 

ํ’€์ด

A์—์„œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’๊ณผ B์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ณฑํ•˜๋ฉด์„œ ๋ˆ„์ ํ•˜๋ฉด ์ตœ์†Ÿ๊ฐ’์ด ๋งŒ๋“ค์–ด์ง„๋‹ค. 

A๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ , B๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ™์€ ์ธ๋ฑ์Šค ๊ฐ’๋ผ๋ฆฌ ๊ณฑํ•˜๋ฉด์„œ ๋ˆ„์ ํ•œ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค. 

 

์ฒ˜์Œ์—๋Š” ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ•ด์„œ ์™„์ „ํƒ์ƒ‰์œผ๋กœ ํ’€์–ด์•ผํ•˜๋‚˜ ๊ณ ๋ฏผํ–ˆ๋‹ค. 

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ฃผ์–ด์ง„ 2๊ฐœ์˜ ์ž…์ถœ๋ ฅ์„ ๋ณด๋ฉด์„œ ๊ทœ์น™์„ ๋ณด๋ฉด

๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ์™€ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๊ณฑํ•ด์„œ ์ตœ์†Ÿ๊ฐ’์„ ๋งŒ๋“ค๊ณ ์žˆ์—ˆ๋‹ค. (์—ญ์‹œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์ž˜ ๋ณด๋ฉด์„œ ๊ทœ์น™์„ ์ฐพ์•„์•ผํ–ˆ๋‹ค.)

 

์ฝ”๋“œ

const solution = (A, B) => {
  let answer = 0;

  A.sort((a, b) => a - b);
  B.sort((a, b) => b - a);

  for (let i = 0; i < A.length; i++) {
    answer += A[i] * B[i];
  }

  return answer;
}

 

 

๋ฌธ์ œ

์ฃผ์–ด์ง„ ์กฐ๊ฑด์— ๋งž๋Š” n์˜ ๋‹ค์Œ ํฐ ์ˆซ์ž๋ฅผ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

- ์กฐ๊ฑด 1. n์˜ ๋‹ค์Œ ํฐ ์ˆซ์ž๋Š” n๋ณด๋‹ค ํฐ ์ž์—ฐ์ˆ˜ ์ž…๋‹ˆ๋‹ค.

- ์กฐ๊ฑด 2. n์˜ ๋‹ค์Œ ํฐ ์ˆซ์ž์™€ n์€ 2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ–ˆ์„ ๋•Œ 1์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

- ์กฐ๊ฑด 3. n์˜ ๋‹ค์Œ ํฐ ์ˆซ์ž๋Š” ์กฐ๊ฑด 1, 2๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜ ์ž…๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ

n result
78 83
15 23

 

ํ’€์ด

n๋ณด๋‹ค ํฌ๊ณ , 2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ–ˆ์„๋•Œ n๊ณผ 1์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค -> n๊ณผ n๋ณด๋‹ค ํฐ์ˆ˜๋ฅผ 2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

์กฐ๊ฑด 2๊ฐ€์ง€๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜ ์ค‘์—์„œ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜

 

2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ -> toString() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

n์˜ 1์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฐพ์•„๋†“๊ณ ,

while๋ฌธ์œผ๋กœ n์„ ํ•˜๋‚˜์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด์„œ ๋‹ค์Œ ํฐ ์ˆ˜์˜ 2์ง„์ˆ˜์˜ 1์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค.

๋น„๊ตํ•ด์„œ ๊ฐ™์œผ๋ฉด ๋ฉˆ์ถ”๊ณ , n์„ ๋ฆฌํ„ดํ•œ๋‹ค.

 

 

์ฝ”๋“œ

const solution = (n) => {
  let binary = n.toString(2);
  let count = 0;

  // count : n์˜ ์ด์ง„์ˆ˜์—์„œ 1์˜ ๊ฐœ์ˆ˜
  for (let x of binary) {
    if (x === '1') count++;
  }

  while (true) {
    n += 1;
    let bigNumBinary = n.toString(2);
    let bigNumCount = 0;

    // ๋‹ค์Œ ํฐ ์ˆซ์ž์˜ count
    for (let x of bigNumBinary) {
      if (x === '1') bigNumCount++;
    }

    if (count === bigNumCount) break;
  }
  return n;
}

 

๋ฌธ์ œ

๋ฌธ์ž์—ด s๋Š” ํ•œ ๊ฐœ ์ด์ƒ์˜ ๋‹จ์–ด๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ณต๋ฐฑ๋ฌธ์ž๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์˜ ์ง์ˆ˜๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์€ ๋Œ€๋ฌธ์ž๋กœ, ํ™€์ˆ˜๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์€ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • ๋ฌธ์ž์—ด ์ „์ฒด์˜ ์ง/ํ™€์ˆ˜ ์ธ๋ฑ์Šค๊ฐ€ ์•„๋‹ˆ๋ผ, ๋‹จ์–ด(๊ณต๋ฐฑ์„ ๊ธฐ์ค€)๋ณ„๋กœ ์ง/ํ™€์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ํŒ๋‹จํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
  • ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž๋Š” 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋กœ ๋ณด์•„ ์ง์ˆ˜๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

ํ’€์ด

๋ฌธ์ž์—ด์˜ ์ง์ˆ˜ ์ธ๋ฑ์Šค๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ , ํ™€์ˆ˜ ์ธ๋ฑ์Šค๋Š” ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€๋‹ค. 

 

๊ผผ๊ผผํ•˜๊ฒŒ ์ง์ˆ˜ ํ™€์ˆ˜ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผํ–ˆ๋‹ค. ์˜ˆ์‹œ์— ์žˆ๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ†ต๊ณผ์ธ๋ฐ

์ œ์ถœํ•ด๋ณด๋ฉด ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๊ฐ€ 16๊ฐœ์ค‘์— 4๊ฐœ๋ฐ–์— ํ†ต๊ณผ๊ฐ€ ๋˜์ง€์•Š์•˜๋‹ค.

๋ฌธ์ œ๋ฅผ ๊ผผ๊ผผํ•˜๊ฒŒ ์ฝ์–ด์•ผํ–ˆ๋‹ค. 

try 

hello

world

๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ ์„œ ๋‹ค์‹œ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•„์ค˜์•ผํ–ˆ๋‹ค. 

 

 

์ฒ˜์Œ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

const solution = (s) => {
  let answer = '';
  let string = s.split('');

  for (let i = 0; i < string.length; i++) {

      if (i % 2 === 0) {
        answer += string[i].toUpperCase();
      } else {
        answer += string[i].toLowerCase();
      }
  }
  return answer;
}

const s = "try hello world";
console.log(solution(s))

 

์ตœ์ข… ์ฝ”๋“œ

const solution = (s) => {
  let answer = '';
  let index = 0;

  for (let i = 0; i < s.length; i++) {
    if (s[i] === ' ') {
      index = 0; // ๊ณต๋ฐฑ์ด๋ฉด ์ธ๋ฑ์Šค ๋‹ค์‹œ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
      answer += ' ';
    } else {
      if (index % 2 === 0) {
        answer += s[i].toUpperCase();
      } else {
        answer += s[i].toLowerCase();
      }
      index++; // ๊ณต๋ฐฑ์ด ์•„๋‹๋•Œ ์ธ๋ฑ์Šค ์ฆ๊ฐ€
    }
  }
  return answer;
}

const s = "try hello world";
console.log(solution(s));

 

ํ†ต๊ณผ๊ฐ€ ์•ˆ๋˜๋ฉด ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ์ฝ์–ด๋ณด์ž. 

๊ทธ ์ „์— ๋ฌธ์ œ๋ฅผ ๊ผผ๊ผผํ•˜๊ฒŒ ์ฝ์ž.

๋ฌธ์ œ

๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด lottos, ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด win_nums๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋•Œ, ๋‹น์ฒจ ๊ฐ€๋Šฅํ•œ ์ตœ๊ณ  ์ˆœ์œ„์™€ ์ตœ์ € ์ˆœ์œ„๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„์„œ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

 

์ฝ”๋“œ

const solution = (lottos, win_nums) => {
  let answer = [];

  let zeroCount = lottos.filter((el) => el === 0).length;
  let sameNums = lottos.filter((el) => win_nums.includes(el)).length;

  let min = 7 - sameNums >= 6 ? 6 : 7 - sameNums; // ์ตœ์ €์ˆœ์œ„
  let max = min - zeroCount < 1 ? 1 : min - zeroCount// ์ตœ๊ณ ์ˆœ์œ„

  answer = [max, min];
  return answer;
}

const lottos = [44, 1, 0, 0, 31, 25];
const win_nums = [31, 10, 45, 1, 6, 19];
console.log(solution(lottos, win_nums));

๋ฌธ์ œ

์›ํ•˜๋Š” ์ƒํ’ˆ์„ ๊ณจ๋ผ ๊ฐ€๊ฒฉ๊ณผ ๋ฐฐ์†ก๋น„๋ฅผ ์ œ์ถœํ•˜๋ผ

ํ˜„์žฌ ์˜ˆ์‚ฐ์œผ๋กœ ์ตœ๋Œ€ ๋ช‡ ๋ช…์˜ ํ•™์ƒ์—์„œ ์„ ๋ฌผ์„ ์‚ฌ์ค„ ์ˆ˜ ์žˆ๋Š”์ง€

์ƒํ’ˆ ํ•˜๋‚˜๋ฅผ 50% ํ• ์ธํ•ด์„œ ์‚ด ์ˆ˜ ์žˆ๋Š” ์ฟ ํฐ ์žˆ๋‹ค. ๋ฐฐ์†ก๋น„๋Š” ํ• ์ธ์— ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

์ž…๋ ฅ์˜ˆ์ œ |

์˜ˆ์‚ฐ 28

์ œ์ถœํ•œ ๊ฐ€๊ฒฉ๊ณผ ๋ฐฐ์†ก๋น„ [6, 6], [2, 2], [4, 3], [4, 5], [10, 3]

์ถœ๋ ฅ์˜ˆ์ œ |

4

 

ํ’€์ด

์ด ๋น„์šฉ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ์„ ํ•œ๋‹ค.

์ฟ ํฐ์„ ์–ด๋–ค ๊ฐ€๊ฒฉ์— ์ ์šฉ์‹œ์ผœ์•ผํ•˜๋Š”๊ฐ€ -> ๊ธฐ์ค€์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋‹ค ์ฐพ์•„๋ด์•ผํ•œ๋‹ค. 

 

์ฒซ ๋ฒˆ์งธ ์ƒํ’ˆ์„ ํ• ์ธ๋ฐ›๋Š”๋‹ค ์ƒ๊ฐํ•˜๊ณ , ์ด ๋น„์šฉ์—์„œ ์ œ์™ธํ•˜๊ณ  ๊ณ„์‚ฐ

๋‘ ๋ฒˆ์งธ ์ƒํ’ˆ์„ ํ• ์ธ๋ฐ›๋Š”๋‹ค ์ƒ๊ฐํ•˜๊ณ  ์ด ๋น„์šฉ์—์„œ ์ œ์™ธํ•˜๊ณ  ๊ณ„์‚ฐ

.

.

.

๋‹ค ํ•ด๋ณด๋Š” ๊ฒƒ์ด ์™„์ „ํƒ์ƒ‰

 

a[0]+a[1]  ์ƒํ’ˆ๊ฐ€๊ฒฉ+๋ฐฐ์†ก๋น„

 

์ „์ฒด ๋น„์šฉํ•ด์„œ ์ฒซ ๋ฒˆ์งธ ๊ธˆ์•ก์„ ํ• ์ธํ•ด์„œ ๋บ€๋‹ค. → ์นด์šดํŒ…์€ 1๋ถ€ํ„ฐ ์‹œ์ž‘

 

 

์ฝ”๋“œ

const solution = (budget, product) => {
  let answer = 0;
  let n = product.length;

  product.sort((a, b) => (a[0] + a[1]) - (b[0] + b[1])); // ์ดํ•ฉ ์ˆœ์„œ๋Œ€๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ๋จผ์ €ํ•ด์ฃผ๊ธฐ

  // for๋ฌธ ๋Œ๋ฉด์„œ ๋ชจ๋“  ์ƒํ’ˆ์ด ํ•œ ๋ฒˆ์”ฉ ํ• ์ธ๋ฐ›๋Š” ๊ฒฝ์šฐ ํ•ด๋ณด๊ธฐ
  for (let i = 0; i < n; i++) {
    // ์ „์ฒด ์˜ˆ์‚ฐ ๊ตฌํ•˜๊ธฐ
    let money = budget - (product[i][0] / 2 + product[i][1]); // ํ• ์ธ๋ฐ›์€ ๊ธˆ์•ก์„ ์ „์ฒด ์˜ˆ์‚ฐ์—์„œ ๋บ€๋‹ค.(์‚ฐ ๊ฒฝ์šฐ๋กœ ๊ณ„์‚ฐ)
    let count = 1; // ์œ„์˜ ๊ณ„์‚ฐ์œผ๋กœ 1๊ฐœ๋Š” ๊ณ„์‚ฐํ–ˆ๋‹ค๋Š” ๊ฐ€์ •์œผ๋กœ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•จ
    for (let j = 0; j < n; j++) {
      // i๋Š” ์ด๋ฏธ ํ• ์ธ๋œ ์ƒํ’ˆ์ด๋ฏ€๋กœ j๊ฐ€ i๋ž‘ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์•ˆ๋˜๋„๋ก ์ฒ˜๋ฆฌ
      if (j !== i && (product[j][0] + product[j][1]) > money) break; // ์‚ฌ๋ ค๊ณ ํ•˜๋Š” ๊ฒƒ์ด ๋‚จ์€ ์˜ˆ์‚ฐ๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ break;
      if (j !== i && (product[j][0] + product[j][1]) <= money) { // ๋‚จ์€์˜ˆ์‚ฐ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์•„์•ผํ•œ๋‹ค.
        money -= (product[j][0] + product[j][1]); // ์ „์ฒด์—์„œ ๋น„์šฉ ๋นผ์ฃผ๊ธฐ
        count++; // ์ƒํ’ˆ ๊ฐœ์ˆ˜ ์นด์šดํŒ…
      }
    }
    answer = Math.max(answer, count);
  }
  return answer;
}

const budget = 28;
const product = [[6, 6], [2, 2], [4, 3], [4, 5], [10, 3]];
console.log(solution(budget, product));

์ฃผ์„ ์—†๋Š” ์ „์ฒด์ฝ”๋“œ

const solution = (budget, product) => {
  let answer = 0;
  let n = product.length;

  product.sort((a, b) => (a[0] + a[1]) - (b[0] + b[1]));

  for (let i = 0; i < n; i++) {
    let money = budget - (product[i][0] / 2 + product[i][1]); 
    let count = 1;
    for (let j = 0; j < n; j++) {
      if (j !== i && (product[j][0] + product[j][1]) > money) break;
      if (j !== i && (product[j][0] + product[j][1]) <= money) {
        money -= (product[j][0] + product[j][1]);
        count++;
      }
    }
    answer = Math.max(answer, count);
  }
  return answer;
}

const budget = 28;
const product = [[6, 6], [2, 2], [4, 3], [4, 5], [10, 3]];
console.log(solution(budget, product));

๋ฌธ์ œ

๊ฐ€์žฅ ๊ธด ๊ฐ€๋กœ ๊ธธ์ด์™€ ์„ธ๋กœ ๊ธธ์ด๊ฐ€ ๊ฐ๊ฐ 80, 70์ด๊ธฐ ๋•Œ๋ฌธ์— 80(๊ฐ€๋กœ) x 70(์„ธ๋กœ) ํฌ๊ธฐ์˜ ์ง€๊ฐ‘์„ ๋งŒ๋“ค๋ฉด ๋ชจ๋“  ๋ช…ํ•จ๋“ค์„ ์ˆ˜๋‚ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ 2๋ฒˆ ๋ช…ํ•จ์„ ๊ฐ€๋กœ๋กœ ๋ˆ•ํ˜€ ์ˆ˜๋‚ฉํ•œ๋‹ค๋ฉด 80(๊ฐ€๋กœ) x 50(์„ธ๋กœ) ํฌ๊ธฐ์˜ ์ง€๊ฐ‘์œผ๋กœ ๋ชจ๋“  ๋ช…ํ•จ๋“ค์„ ์ˆ˜๋‚ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ์˜ ์ง€๊ฐ‘ ํฌ๊ธฐ๋Š” 4000(=80 x 50)์ž…๋‹ˆ๋‹ค.

 

๋ชจ๋“  ๋ช…ํ•จ์˜ ๊ฐ€๋กœ ๊ธธ์ด์™€ ์„ธ๋กœ ๊ธธ์ด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” 2์ฐจ์› ๋ฐฐ์—ด sizes๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ช…ํ•จ์„ ์ˆ˜๋‚ฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ์ž‘์€ ์ง€๊ฐ‘์„ ๋งŒ๋“ค ๋•Œ, ์ง€๊ฐ‘์˜ ํฌ๊ธฐ๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

ํ’€์ด

๊ฐ€๋กœ ๊ธธ์ด ์ค‘์— ๊ฐ€์žฅ ๊ธด ๊ฒƒ, ์„ธ๋กœ ๊ธธ์ด ์ค‘์— ๊ฐ€์žฅ ๊ธด ๊ฒƒ์„ ๊ณฑํ•œ๊ฒŒ ์ตœ์†Œ ์ง€๊ฐ‘ํฌ๊ธฐ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ์„ธ๋กœ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ํฐ ๋ช…ํ•จ์ด ๊ฐ€๋กœ๋กœ ๋ˆ•ํ˜€์„œ ์ˆ˜๋‚ฉํ•  ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค๋ฉด, ๋” ์ž‘์€ ์‚ฌ์ด์ฆˆ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. 

 

๊ฐ ๋ฐฐ์—ด์—์„œ ์›์†Œ๋ผ๋ฆฌ ๋น„๊ตํ•ด์„œ ํฐ ๊ฐ’์€ 0๋ฒˆ์งธ, ์ž‘์€ ๊ฐ’์€ 1๋ฒˆ์งธ ์ž๋ฆฌ์— ๋ฐฐ์น˜ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํฐ ๊ฐ’์„ ๋ชจ์•„์„œ big์ด๋ผ๋Š” ๋ฐฐ์—ด์— pushํ•˜๊ณ , ์ž‘์€ ๊ฐ’์„ small์ด๋ผ๋Š” ๋ฐฐ์—ด์— pushํ•œ๋‹ค. ๊ฐ™์„ ๊ฒฝ์šฐ ๊ทธ๋Œ€๋กœ ๋„ฃ๋Š”๋‹ค. ๊ฐ ๋ฐฐ์—ด์—์„œ Math.max() ๋ฉ”์†Œ๋“œ๋กœ ์ฐพ์•„์„œ ๊ณฑํ•˜๊ณ  answer๋กœ ๋ฆฌํ„ดํ•œ๋‹ค. 

 

์ฝ”๋“œ

const solution = (sizes) => {
  let answer;
  let temp;
  let small = [];
  let big = [];

  for (let i = 0; i < sizes.length; i++) {
    if (sizes[i][0] < sizes[i][1]) {
      temp = sizes[i][1];
      sizes[i][1] = sizes[i][0];
      sizes[i][0] = temp;
    }
    small.push(sizes[i][1]);
    big.push(sizes[i][0]);
  }
  
  answer = Math.max(...big) * Math.max(...small);
  return answer;
}

const sizes = [[60, 50], [30, 70], [60, 30], [80, 40]];
console.log(solution(sizes));

 

 

๋‹ค๋ฅธ ํ’€์ด

๋งคํ•‘์„ ํ•ด์„œ ์ •๋ ฌ๋œ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , ๋ฐ”๋กœ 0๋ฒˆ์งธ ์š”์†Œ๋“ค๋งŒ ๋งคํ•‘ํ•ด์„œ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ๊ณ , 1๋ฒˆ์งธ ์š”์†Œ๋“ค๋งŒ ๋งคํ•‘ํ•ด์„œ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ์•„์„œ ๊ณฑํ•ด์„œ ๋ฆฌํ„ดํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์—ˆ๋‹ค. 

const solution = (sizes) => {
  const newSizes = sizes.map(e => e.sort((a, b) => a - b));
  return Math.max(...newSizes.map(e => e[0])) * Math.max(...newSizes.map(e => e[1]));
}

const sizes = [[60, 50], [30, 70], [60, 30], [80, 40]];
console.log(solution(sizes));

+ Recent posts