๐ŸŸฃ ์˜ค๋Š˜์˜ ๋ฌธ์ œ ํšŒ๊ณ 

๊น€ํƒœ์šฑยท2023๋…„ 3์›” 30์ผ
0
post-thumbnail

๐Ÿ˜ƒ ์˜ค๋Š˜์˜ ๋ฌธ์ œ ํ•ฅ์ง #1

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Lv1: ๋ถ€์กฑํ•œ ๊ธˆ์•ก ๊ณ„์‚ฐํ•˜๊ธฐ

๋ฌธ์ œ ์„ค๋ช…
์ƒˆ๋กœ ์ƒ๊ธด ๋†€์ด๊ธฐ๊ตฌ๋Š” ์ธ๊ธฐ๊ฐ€ ๋งค์šฐ ๋งŽ์•„ ์ค„์ด ๋Š์ด์งˆ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๋†€์ด๊ธฐ๊ตฌ์˜ ์›๋ž˜ ์ด์šฉ๋ฃŒ๋Š” price์› ์ธ๋ฐ, ๋†€์ด๊ธฐ๊ตฌ๋ฅผ N ๋ฒˆ ์งธ ์ด์šฉํ•œ๋‹ค๋ฉด ์›๋ž˜ ์ด์šฉ๋ฃŒ์˜ N๋ฐฐ๋ฅผ ๋ฐ›๊ธฐ๋กœ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์ฒ˜์Œ ์ด์šฉ๋ฃŒ๊ฐ€ 100์ด์—ˆ๋‹ค๋ฉด 2๋ฒˆ์งธ์—๋Š” 200, 3๋ฒˆ์งธ์—๋Š” 300์œผ๋กœ ์š”๊ธˆ์ด ์ธ์ƒ๋ฉ๋‹ˆ๋‹ค.
๋†€์ด๊ธฐ๊ตฌ๋ฅผ count๋ฒˆ ํƒ€๊ฒŒ ๋˜๋ฉด ํ˜„์žฌ ์ž์‹ ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ธˆ์•ก์—์„œ ์–ผ๋งˆ๊ฐ€ ๋ชจ์ž๋ผ๋Š”์ง€๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”.
๋‹จ, ๊ธˆ์•ก์ด ๋ถ€์กฑํ•˜์ง€ ์•Š์œผ๋ฉด 0์„ return ํ•˜์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • ๋†€์ด๊ธฐ๊ตฌ์˜ ์ด์šฉ๋ฃŒ price : 1 โ‰ค price โ‰ค 2,500, price๋Š” ์ž์—ฐ์ˆ˜
  • ์ฒ˜์Œ ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ๊ธˆ์•ก money : 1 โ‰ค money โ‰ค 1,000,000,000, money๋Š” ์ž์—ฐ์ˆ˜
  • ๋†€์ด๊ธฐ๊ตฌ์˜ ์ด์šฉ ํšŸ์ˆ˜ count : 1 โ‰ค count โ‰ค 2,500, count๋Š” ์ž์—ฐ์ˆ˜

์ž…์ถœ๋ ฅ ์˜ˆ

pricemoneycountresult
320410

๋‚˜์˜ ํ’€์ด

function solution(price, money, count) {
  var answer = money;
  let sumPrice = 0;
  for (let i = 1; i <= count; i++) {
    sumPrice += price * i;
  }
  return sumPrice - answer <= 0 ? 0 : sumPrice - answer;
}

๊ตฌํ˜„ ์ ‘๊ทผ
1. count ๋งŒํผ ์ฆ๊ฐ€๋˜๋Š” price์˜ ์ดํ•ฉ์„ sumPrice์— ํ• ๋‹น
2. sumPrice - answer ํ•ด์„œ ๊ธˆ์•ก์ด ๋ชจ์ž๋ผ์ง€ ์•Š๋Š”๋‹ค๋ฉด 0์„ ๋ฐ˜ํ™˜
3. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉฐ ๋ถ€์กฑํ•œ ๋งŒํผ์˜ ๋ˆ์„ ๋ฐ˜ํ™˜์„ ์‚ผํ•ญ ์—ฐ์‚ฐ์œผ๋กœ ํ’€์ด.


๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

#1

function solution(price, money, count) {
    const tmp = price * count * (count + 1) / 2 - money;
    return tmp > 0 ? tmp : 0;
}

ํ’€์ด
๊ฐ€์šฐ์Šค ๊ณต์‹์— ์˜ํ•œ ํ’€์ด ๊ณต์‹.
๊ฐ€์šฐ์Šค ๊ณต์‹|์ž‘์„ฑ์ž windrew

๊ฐ€์šฐ์Šค ๊ณต์‹์ด๋ž€ : ๋“ฑ์ฐจ์ˆ˜์—ด์˜ ํ•ฉ ๊ฐ€์šฐ์Šค๋Š” 1๋ถ€ํ„ฐ 100๊นŒ์ง€์˜ ํ•ฉ์„ ๊ตฌํ•  ๋•Œ 1~100์— 100~1๊นŒ์ง€ ์ˆœ์„œ๋กœ ๋‹ค์‹œ ๋”ํ•œ ๋‹ค์Œ 2๋กœ ๋‚˜๋ˆ„์–ด ํ•ฉ์„ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ๋‹ค.
1๋ถ€ํ„ฐ 100๊นŒ์ง€์˜ ํ•ฉ์„ S๋กœ ๋‘๋ฉด
S = 1 + 2 + 3 + 4 + 5 + 6 + ... + 99 + 100
S = 100 + 99 + 98 + 97 + 96 + 95 + 94 + ... +2 + 1
์ด๋ฏ€๋กœ
2S = 101 100์ด๋‹ค.
๊ทธ๋Ÿฌ๋ฏ€๋กœ
S = 101
50 = 5050์ด๋‹ค.
์ด๊ฑธ n์— ๋Œ€ํ•ด ํ™•์žฅํ•˜๋ฉด 1๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ํ•ฉ์€ (n+1)*n/2์ธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๊ณ ,
์ผ๋ฐ˜์ ์ธ ๋‘ ์ž์—ฐ์ˆ˜ a,b(a<=b)์— ๋Œ€ํ•ด์„œ a๋ถ€ํ„ฐ b๊นŒ์ง€์˜ ์ž์—ฐ์ˆ˜์˜ ํ•ฉ์€ (a+b)(b-a+1)/2์ธ ๊ฒƒ๋„ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
์ด์ œ ์–˜๋ฅผ ์œ ์šฉํ•˜๊ฒŒ ์จ๋จน์„ ๋ฌธ์ œ๋ฅผ ์ฐพ์•„์„œ ์จ๋จน์œผ๋ฉด ๋œ๋‹ค.

#2

function solution(price, money, count) {
    let answer = 0;

    for (let i = 1; i <= count; i++) {
        answer += price * i;
    }

    return answer > money ? answer - money : 0;
}

ํ’€์ด
๊ตณ์ด price๋ฅผ ํ•ฉํ•œ ๊ฐ’์„ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์—ฌ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•  ํ•„์š”๊ฐ€ ์—†์—ˆ๋‹ค.


๐Ÿ˜ƒ ์˜ค๋Š˜์˜ ๋ฌธ์ œ ํ•ฅ์ง #2

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Lv2: ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’

๋ฌธ์ œ ์„ค๋ช…
๋ฌธ์ž์—ด s์—๋Š” ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋œ ์ˆซ์ž๋“ค์ด ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. str์— ๋‚˜ํƒ€๋‚˜๋Š” ์ˆซ์ž ์ค‘ ์ตœ์†Œ๊ฐ’๊ณผ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ์•„ ์ด๋ฅผ "(์ตœ์†Œ๊ฐ’) (์ตœ๋Œ€๊ฐ’)"ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”.
์˜ˆ๋ฅผ๋“ค์–ด s๊ฐ€ "1 2 3 4"๋ผ๋ฉด "1 4"๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ , "-1 -2 -3 -4"๋ผ๋ฉด "-4 -1"์„ ๋ฆฌํ„ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ œํ•œ์‚ฌํ•ญ

  • s์—๋Š” ๋‘˜ ์ด์ƒ์˜ ์ •์ˆ˜๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

priceresult
"1 2 3 4""1 4"
"-1 -2 -3 -4""-4 -1"
"-1 -1""-1 -1"

๋‚˜์˜ ํ’€์ด

function solution(s) {
  var answer = s.split(" ");
  let max = Math.max(...answer);
  let min = Math.min(...answer);
  return min + " " + max;
}

๊ตฌํ˜„ ์ ‘๊ทผ
1. split ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด๋‹น ๊ณต๋ฐฑ์„ ํฌํ•จํ•œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜.
2. max ๋ณ€์ˆ˜์™€ min ๋ณ€์ˆ˜ ์„ ์–ธ ํ›„ Math.max,min ๋ฉ”์†Œ๋“œ ํ™œ์šฉ.
3. ๋ฉ”์†Œ๋“œ์˜ ์ธ์ž๋กœ ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ• ๋ฐฐ์—ด์„ ํ• ๋‹นํ•˜๋ฉด ํ•ด๋‹น ๋ฐฐ์—ด์ด ์ „๋ถ€ ์ธ์ž๋กœ ํ• ๋‹น.

  1. " ", ๊ณต๋ฐฑ ๋”ฐ์˜ดํ‘œ๋ฅผ ๋”ํ•ด์คŒ์œผ๋กœ์จ ํ•ด๋‹น min๊ฐ’๊ณผ max๊ฐ’์„ ๋”ฐ๋กœ ๋ฐ˜ํ™˜.

๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

#1

function solution(s) {
    const arr = s.split(' ');

    return Math.min(...arr)+' '+Math.max(...arr);
}

ํ’€์ด
1. return์„ ํ†ตํ•ด ํ•œ์ค„๋กœ ์ฝ”๋“œ๋ฅผ ๊ฐœ์„ ์ด ๊ฐ€๋Šฅํ•˜๋”๋ผ,,


#2

function solution(s) {
    var arr = s.split(' ');
    arr.sort((a, b) => a - b);

    var answer = arr[0] + " " + arr[arr.length-1];

    return answer;
}

ํ’€์ด
1. sort๋ฅผ ์ ์šฉํ•˜์—ฌ ํ•ด๋‹น ๊ณต๋ฐฑ์„ ํฌํ•จํ•œ ๋ฐฐ์—ด์„ ์ •๋ ฌ.
2. ๋Œ€๊ด„ํ˜ธ๋ฅผ ํ†ตํ•ด ์ •๋ ฌ ๋œ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ถˆ๋Ÿฌ์™€ " "์„ ํฌํ•จํ•œ ๊ฐ’์„ ์—ฐ์‚ฐ.


๐Ÿ˜ƒ ์˜ค๋Š˜์˜ ๋ฌธ์ œ ํ•ฅ์ง #3

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Lv1 : ํ–‰๋ ฌ์˜ ๋ง์…ˆ

๋ฌธ์ œ ์„ค๋ช…
ํ–‰๋ ฌ์˜ ๋ง์…ˆ์€ ํ–‰๊ณผ ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ๊ฐ™์€ ๋‘ ํ–‰๋ ฌ์˜ ๊ฐ™์€ ํ–‰, ๊ฐ™์€ ์—ด์˜ ๊ฐ’์„ ์„œ๋กœ ๋”ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 2๊ฐœ์˜ ํ–‰๋ ฌ arr1๊ณผ arr2๋ฅผ ์ž…๋ ฅ๋ฐ›์•„, ํ–‰๋ ฌ ๋ง์…ˆ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • ํ–‰๋ ฌ arr1, arr2์˜ ํ–‰๊ณผ ์—ด์˜ ๊ธธ์ด๋Š” 500์„ ๋„˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

arr1arr2return
[[1,2],[2,3]][[3,4],[5,6]][[4,6],[7,9]]
[[1],[2]][[3],[4]][[4],[6]]

๋‚˜์˜ ํ’€์ด

function solution(arr1, arr2) {
  var answer = [];
  for (let i = 0; i < arr1.length; i++) {
    let sum = [];
    for (let j = 0; j < arr1[i].length; j++) {
      sum.push(arr1[i][j] + arr2[i][j]);
    }
    answer.push(sum);
  }
  return answer;
}

๊ตฌํ˜„ ์ ‘๊ทผ
1. ๋‹ค์ฐจ์› ๋ฐฐ์—ด์„ ํ†ตํ•œ ๋ฐฐ์—ด๋ณ„ ์ธ๋ฑ์Šค ํ•ฉ์„ ๊ตฌํ•ด์•ผ ํ–ˆ๊ธฐ์— 2์ค‘ for๋ฌธ ์‚ฌ์šฉ
2. ๋นˆ ๋ฐฐ์—ด sum์—๋‹ค ๊ฐ ๋ฐฐ์—ด์ด ๊ฐ–๋Š” ์ธ๋ฑ์Šค ๋ณ„๋กœ ํ•ฉ์„ ์ค€ ๊ฐ’์„ push.
3. ๋˜ ๊ฐ๊ฐ ํ‘ธ์‰ฌ ๋œ ๋ฐฐ์—ด์„ answer์— ํ‘ธ์‰ฌ.

์ถœ์ฒ˜ : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ info


๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

#1

function sumMatrix(A,B){
    return A.map((arr1, idx1) => arr1.map((val, idx2) => val+B[idx1][idx2]));
}

ํ’€์ด
1. map ๋ฉ”์†Œ๋“œ๋ฅผ ๋‘๋ฒˆ ์ ์šฉํ•˜์—ฌ ๋ฐ˜๋ณต๋ฌธ์„ ๋งŒ๋“ค์—ˆ๋‹ค.
๋Œ€์ฒด ์ด๋Ÿฐ ์ฝ”๋“œ๋Š” ์–ด๋–ป๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ธ๊ฐ€,,

๊ตฌ์ฒด์ ์ธ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•ด์„œ gpt์—๊ฒŒ ๋ฌผ์–ด๋ดค๋‹ค.

์ด ๊ณผ์ •์„ ์ดํ•ดํ•˜๊ณ  ๋‚˜์„œ ๋‹ค์Œ ๊ณผ์ • ๋•Œ ์จ๋จน์„ ์ˆ˜ ์žˆ์œผ๋ฉด ์ข‹๊ฒ ๊ตฐ,

#2

function solution(arr1, arr2) {
    var answer = [[]];
    for (var i=0; i<arr1.length; i++){
        answer[i] =[];
        for(var j=0; j<arr1[i].length; j++){
            answer[i].push(arr1[i][j] + arr2[i][j]);
        }
    }
    return answer;
}

ํ’€์ด
1. ์œ„ ์ฝ”๋“œ ์—ญ์‹œ 2์ค‘ for๋ฌธ์„ ํ†ตํ•ด ์ธ๋ฑ์Šค ๋ณ„ ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ๋”ํ•ด push ํ•˜๋Š” ๊ฒƒ.
2. ๊ทธ๋Ÿฌ๋‚˜ ๋‚˜ ์ฒ˜๋Ÿผ sum ๋นˆ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์•„๋‹Œ ํ•ด๋‹น ๋‹ด์•„์•ผ ํ•  ์ธ๋ฑ์Šค ์ž์ฒด์— ๋นˆ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๊ฒƒ.

์ง€๊ธˆ์˜ ์ฝ”๋“œ๊ฐ€ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ดํ•ด๊ฐ€ ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

profile
๋ฐฉ๋ฒ•์„ ์—ฐ๊ตฌํ•  ์ค„ ์•„๋Š” ๊ฐœ๋ฐœ์ž!

0๊ฐœ์˜ ๋Œ“๊ธ€