[LeetCode] 3432. Count Partitions with Even Sum Difference

Chobby·2026년 1월 12일

LeetCode

목록 보기
909/962

😎풀이

  1. 좌측 파티션에 미리 모든 배열의 합 계산
  2. 한 요소 씩 오른쪽 파티션으로 옮기며, 두 파티션의 차가 짝수인 경우 탐색
  3. 파티션 분할로 확인할 수 있는 차가 짝수인 경우의 수 반환
function countPartitions(nums: number[]): number {
    const n = nums.length
    let evenPartitions = 0
    let left = nums.reduce((acc, cur) => acc + cur, 0)
    let right = 0
    for(let i = 0; i < n - 1; i++) {
        const curVal = nums[n - i - 1]
        left -= curVal
        right += curVal
        const gap = Math.abs(left - right)
        if((gap & 1) === 1) continue
        evenPartitions++
    }
    return evenPartitions
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글