Posis

[LeetCode][JavaScript] 561. Array Partition I 본문

알고리즘/leetcode

[LeetCode][JavaScript] 561. Array Partition I

CooNiHong 2021. 7. 29. 15:10

[LeetCode][JavaScript] 561. Array Partition I

문제 출처: https://leetcode.com/problems/array-partition-i/

 

Array Partition I - LeetCode

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

[직접 푼 코드]

var arrayPairSum = function(nums) {
    nums.sort((a,b) => a-b)
    let result = 0
    
    for(let i=0; i<nums.length; i+=2) {
        result += nums[i]
    }
    
    return result
};

 

[문제 풀이]

1. 배열을 정렬합니다. (sort 메서드)

2. 반복문을 활용하여 2칸씩 올라가 더해줍니다. [1, 2, 2, 5, 6, 6] = 9


알고리즘 스터디: https://github.com/ROUTINE-STUDY/Algorithm

 

GitHub - ROUTINE-STUDY/Algorithm: 초보 알고리즘 스터디 / 누구나 참여 가능

초보 알고리즘 스터디 / 누구나 참여 가능 :runner:. Contribute to ROUTINE-STUDY/Algorithm development by creating an account on GitHub.

github.com

 

누구나 참여 가능한 알고리즘 스터디입니다.
알고리즘뿐만 아니라 개발을 하면서 겪은 이슈, 이론을 정리하는 Routine-Study를 운영 중입니다.
728x90