๐Ÿค ์€์ง€log ๐Ÿค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ๋ฐฐ์—ด ๋น„๊ตํ•˜๊ธฐ ๋ณธ๋ฌธ

๐ŸŒˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ๋ฐฐ์—ด ๋น„๊ตํ•˜๊ธฐ

Eun_zii 2024. 3. 26. 16:47

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.0

- ๋ฐฐ์—ด ๋น„๊ตํ•˜๊ธฐ

 

๐Ÿ’š ๋ฌธ์ œ์„ค๋ช…

  • ์ด ๋ฌธ์ œ์—์„œ ๋‘ ์ •์ˆ˜ ๋ฐฐ์—ด์˜ ๋Œ€์†Œ๊ด€๊ณ„๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • ๋‘ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด, ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๊ธด ์ชฝ์ด ๋” ํฝ๋‹ˆ๋‹ค.
  • ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๊ฐ ๋ฐฐ์—ด์— ์žˆ๋Š” ๋ชจ๋“  ์›์†Œ์˜ ํ•ฉ์„ ๋น„๊ตํ•˜์—ฌ ๋‹ค๋ฅด๋‹ค๋ฉด ๋” ํฐ ์ชฝ์ด ํฌ๊ณ , ๊ฐ™๋‹ค๋ฉด ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • ๋‘ ์ •์ˆ˜ ๋ฐฐ์—ด arr1๊ณผ arr2๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ์œ„์—์„œ ์ •์˜ํ•œ ๋ฐฐ์—ด์˜ ๋Œ€์†Œ๊ด€๊ณ„์— ๋Œ€ํ•˜์—ฌ arr2๊ฐ€ ํฌ๋‹ค๋ฉด -1, arr1์ด ํฌ๋‹ค๋ฉด 1, ๋‘ ๋ฐฐ์—ด์ด ๊ฐ™๋‹ค๋ฉด 0์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

 

๐Ÿ’š ์ œํ•œ์กฐ๊ฑด

  • 1 ≤ arr1์˜ ๊ธธ์ด ≤ 100
  • 1 ≤ arr2์˜ ๊ธธ์ด ≤ 100
  • 1 ≤ arr1์˜ ์›์†Œ ≤ 100
  • 1 ≤ arr2์˜ ์›์†Œ ≤ 100
  • ๋ฌธ์ œ์—์„œ ์ •์˜ํ•œ ๋ฐฐ์—ด์˜ ๋Œ€์†Œ๊ด€๊ณ„๊ฐ€ ์ผ๋ฐ˜์ ์ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ์ •์˜๋œ ๋ฐฐ์—ด์˜ ๋Œ€์†Œ๊ด€๊ณ„์™€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋Š” ์ ์— ์œ ์˜ํ•ด์ฃผ์„ธ์š”.

๐Ÿ’š ํ’€์ด

function solution(arr1, arr2) {
    let answer = 0;

    if (arr1.length !== arr2.length) {
        answer = arr2.length > arr1.length ? -1 : 1;
    } else {
        const sum1 = arr1.reduce((a, c) => a + c, 0);
        const sum2 = arr2.reduce((a, c) => a + c, 0);

        if (sum1 !== sum2) {
            answer = sum2 > sum1 ? -1 : 1;
        }
    }

    return answer;
}

 

โœ… 

๋ฌธ์ œ์˜ " ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๊ฐ ๋ฐฐ์—ด์— ์žˆ๋Š” ๋ชจ๋“  ์›์†Œ์˜ ํ•ฉ์„ ๋น„๊ตํ•˜์—ฌ ๋‹ค๋ฅด๋‹ค๋ฉด ๋” ํฐ ์ชฝ์ด ํฌ๊ณ , ๊ฐ™๋‹ค๋ฉด ๊ฐ™์Šต๋‹ˆ๋‹ค. " ์ด๋ถ€๋ถ„์„ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ์ง€ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ๊ฒ€์ƒ‰ํ•ด๋ณด์•˜๋‹น ..

 

์šฐ์„  answer ๋ณ€์ˆ˜๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๊ณ ,

arr1๊ณผ arr2์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๊ณ , arr2์˜ ๊ธธ์ด๊ฐ€ ๋” ํฌ๋‹ค๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 1์„ answer๋กœ ๊ฒฐ์ •ํ•ด์ค€๋‹ค.

๋‘ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™๋‹ค๋ฉด, ๊ฐ ๋ฐฐ์—ด์˜ ํ•ฉ์„ ๊ตฌํ•˜์—ฌ ๋น„๊ตํ•œ๋‹ค. ํ•ฉ์ด ๋‹ค๋ฅด๋‹ค๋ฉด, ํ•ฉ์ด ํฐ ๋ฐฐ์—ด์— ๋”ฐ๋ผ answer๋ฅผ ๊ฒฐ์ •ํ•ด์ค€๋‹ค.

 

reduce( ) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋Š”๋ฐ sum1๊ณผ sum2 ๋ณ€์ˆ˜๋Š” ๊ฐ๊ฐ arr1๊ณผ arr2 ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ํ•ฉํ•œ ๊ฐ’์ด๊ณ ,

์ด๋•Œ a๋Š” ๋ˆ„์ ๊ฐ’(accumulator)์ด๊ณ  c๋Š” ํ˜„์žฌ ์š”์†Œ์˜ ๊ฐ’(currentValue)์ด๋‹ค. 

 

์•„์ง reduce( ) ์‚ฌ์šฉ์ด ์–ด๋ ต๋‹น .. ๐Ÿ˜–๐Ÿ’จ

 

๐Ÿ’ก reduce( ) ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ œ๊ณต๋œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ๊ฐ ์š”์†Œ์˜ ๊ฐ’์„ ๋ˆ„์ ํ•˜์—ฌ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ๊ฐ ์š”์†Œ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด์„œ ๋ˆ„์ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. 

array.reduce(callback(accumulator, currentValue, currentIndex, array), initialValue)

 

  • callback: ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์‹คํ–‰ํ•  ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋„ค ๊ฐœ์˜ ์ธ์ˆ˜๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
    • accumulator: ์ฝœ๋ฐฑ ํ•จ์ˆ˜์—์„œ ๋ˆ„์ ๋œ ๊ฐ’์ž…๋‹ˆ๋‹ค.
    • currentValue: ํ˜„์žฌ ์ฒ˜๋ฆฌ ์ค‘์ธ ์š”์†Œ์˜ ๊ฐ’์ž…๋‹ˆ๋‹ค.
    • currentIndex: ํ˜„์žฌ ์ฒ˜๋ฆฌ ์ค‘์ธ ์š”์†Œ์˜ ์ธ๋ฑ์Šค์ž…๋‹ˆ๋‹ค. (์„ ํƒ ์‚ฌํ•ญ)
    • array: reduce()๋ฅผ ํ˜ธ์ถœํ•œ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. (์„ ํƒ ์‚ฌํ•ญ)
  • initialValue: ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์— ๋Œ€ํ•œ ์ดˆ๊ธฐ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ด ๊ฐ’์€ ์ฒซ ๋ฒˆ์งธ ํ˜ธ์ถœ์—์„œ accumulator์˜ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. (์„ ํƒ ์‚ฌํ•ญ)
728x90