[JavaScript] LeetCode : Find Pivot Index
728x90

๋ฌธ์ œ

Given an array of integers nums, calculate the pivot index of this array.

The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.

If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array.

Return the leftmost pivot index. If no such index exists, return -1.

 

์ •์ˆ˜ ๋ฐฐ์—ด์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„ ํ•ด๋‹น ๋ฐฐ์—ด์˜ ํ”ผ๋ด‡ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ.
ํ”ผ๋ด‡ ์ธ๋ฑ์Šค๋Š” ๋ฐฐ์—ด ๋‚ด ์ธ๋ฑ์Šค ์™ผ์ชฝ์— ์žˆ๋Š” ๋ชจ๋“  ์ˆซ์ž์˜ ํ•ฉ์ด ์ธ๋ฑ์Šค ์˜ค๋ฅธ์ชฝ์˜ ๋ชจ๋“  ์ˆซ์ž์˜ ํ•ฉ๊ณผ ๊ฐ™์€ ์ธ๋ฑ์Šค์ด๋‹ค.
๋งŒ์•ฝ ๊ฐ€์žฅ ์™ผ์ชฝ ๋์— ์žˆ๋Š” ์ธ๋ฑ์Šค๊ฐ€ ์žˆ๋‹ค๋ฉด ์™ผ์ชฝ ์ธ๋ฑ์Šค์˜ ํ•ฉ์€ 0์ด ๋œ๋‹ค. (์™ผ์ชฝ์— ๋‹ค๋ฅธ ์š”์†Œ๊ฐ€ ๋” ์—†๊ธฐ ๋•Œ๋ฌธ์—. ์˜ค๋ฅธ์ชฝ๋„ ๋™์ผ)
๊ฐ€์žฅ ์™ผ์ชฝ์— ์žˆ๋Š” ํ”ผ๋ด‡ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ์ธ๋ฑ์Šค๊ฐ€ ์—†์„ ๊ฒฝ์šฐ์—๋Š” -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 

ํ’€์ด

var pivotIndex = function(nums) {
    let pivot = 0;
    let sum = nums.reduce((prev, curr) => {
        return prev + curr;
    });

    let leftSum = 0;
    for (let i = 0; i < nums.length; i++) {
        pivot = i;

        sum -= nums[i];
        if (sum === leftSum) return pivot;
        leftSum += nums[i];
  }
    return -1;
};

 

์ƒ๊ฐํ•œ ๊ณผ์ •์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • ๋ฐฐ์—ด์˜ ์ดํ•ฉ ๊ตฌํ•˜๊ธฐ
  • ์™ผ์ชฝ๋ถ€ํ„ฐ ์ธ๋ฑ์Šค ํ•˜๋‚˜๋‹น ์ดํ•ฉ์—์„œ ๋บ€ ๊ฐ’๊ณผ ์™ผ์ชฝ ์ธ๋ฑ์Šค ๋ˆ„์ ๊ฐ’ ๊ตฌํ•˜๊ธฐ
  • ์ดํ•ฉ์—์„œ ๋บ€ ๊ฐ’๊ณผ ์™ผ์ชฝ ์ธ๋ฑ์Šค๊ฐ€ ์ผ์น˜ํ•˜๋ฉด ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜ํ•˜๊ธฐ

๋จผ์ € ๋ฐฐ์—ด์˜ ์ดํ•ฉ์€ for๋ฌธ์œผ๋กœ ๊ตฌํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ข€ ๋” ์ฝ”๋“œ๋ฅผ ์ง๊ด€์ ์ด๊ณ  ๊ฐ„์†Œํ™” ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋„๋ก,

์ง€๋‚œ ๋ฒˆ์— ์‚ฌ์šฉํ•ด๋ณด์•˜๋˜ reduce ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด๋ดค๋‹ค.

๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋”ฐ์ ธ๋ด์•ผ ํ•˜๋ฏ€๋กœ ํ•˜๋‚˜์˜ for๋ฌธ์„ ํ†ตํ•ด ์ธ๋ฑ์Šค๋ฅผ ๊ตฌํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

ํ”ผ๋ด‡ ์ธ๋ฑ์Šค๋Š” i๊ฐ€ ์ฆ๊ฐ€ํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กญ๊ฒŒ ์ง€์ •ํ•˜๊ณ  ์กฐ๊ฑด์— ์ผ์น˜ํ•˜๋ฉด ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

ํ•˜๋‚˜์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ดํ•ฉ์—์„œ ํ˜„์žฌ ์ธ๋ฑ์Šค ๊ฐ’์„ ๋บ์„ ๋•Œ ๋ˆ„์ ๊ฐ’๊ณผ ๊ฐ™์€์ง€ ๋น„๊ตํ•˜๊ณ ,

์ดํ•ฉ์ด ๋” ํฌ๋‹ค๋ฉด ๋ˆ„์ ๊ฐ’์— ์ธ๋ฑ์Šค๊ฐ’์„ ๋”ํ•ด์ฃผ๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

 

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

LeetCode์—์„œ ๋ณธ ๋‹ค๋ฅธ ํ’€์ด ๋ฐฉ์‹ ์ค‘ ํ•˜๋‚˜์ด๋‹ค.

var pivotIndex = function(nums) {
    let sum = nums.reduce((a,b)=>a+b, 0);
    let sumL = 0;
    for(let i=0, len=nums.length; i<len; i++){
        if(sumL === (sum-nums[i])/2) return i;
        sumL += nums[i];
    }
    return -1;
};

๋‚˜์™€ ๋™์ผํ•˜๊ฒŒ reduce ๋ฉ”์„œ๋“œ๋กœ ์ดํ•ฉ์„ ๊ตฌํ•˜๊ณ ,
์™ผ์ชฝ ๋ˆ„์ ๊ฐ’์ด ์ดํ•ฉ์—์„œ ํ˜„์žฌ ์ธ๋ฑ์Šค๊ฐ’์„ ๋บ€ ๊ฒƒ์„ 2๋กœ ๋‚˜๋ˆˆ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋ฉด ๋ฐ˜ํ™˜ํ•˜๋Š” ์‹์ด๋‹ค.
2๋กœ ๋‚˜๋ˆˆ ๊ฒƒ์€ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ์˜ ๋ˆ„์ ๊ฐ’์ด ๋™์ผํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์„ ์ฐจ์šฉํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

 

 

์ถ”๊ฐ€๋กœ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ํ•˜๋‚˜ ๋” ์žˆ๋Š”๋ฐ, ์•„๋ž˜ ํŽ˜์ด์ง€์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

https://leetcode.com/problems/find-pivot-index/discuss/138027/javascript-solution%3A-beats-100.00-runtime-76ms

 

javascript solution: beats 100.00%, runtime 76ms - LeetCode Discuss

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

 

 


๐Ÿคธ‍โ™€๏ธ๐Ÿคธ‍โ™€๏ธ๐Ÿคธ‍โ™€๏ธ๐Ÿคธ‍โ™€๏ธ๐Ÿคธ‍โ™€๏ธ๐Ÿคธ‍โ™€๏ธ

๋ฐฉ๋ฌธํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๐Ÿ™Œ

ํฌ์ŠคํŒ…๋“ค์€ ๊ณต๋ถ€์ค‘์ธ ๋‚ด์šฉ์„ ๊ธ€๋กœ ์ž‘์„ฑํ•œ ๊ฒƒ์ด๋ผ ๋ถ€์กฑํ•œ ์ ์ด ๋งŽ์œผ๋‹ˆ ์ฐธ๊ณ  ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. 

๋ถ€์กฑํ•œ ๋ถ€๋ถ„์— ๋Œ€ํ•œ ์ฝ”๋ฉ˜ํŠธ๋Š” ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค.

์ข‹์€ ํ•˜๋ฃจ ๋˜์„ธ์š”, ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๐Ÿ™‚

728x90

'๐Ÿ““ Algorithm > LeetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[JavaScript] leetCode : Linked List Cycle II  (3) 2022.05.03
[JavaScript] leetCode : Valid Mountain Array  (3) 2022.05.02
[JavaScript] LeetCode : Add Binary  (0) 2022.04.22
[JavaScript] LeetCode : Plus One  (0) 2022.04.21
[JavaScript] LeetCode : Largest Number At Least Twice of Others  (1) 2022.04.20