Leetcode 3432. Count Partitions with Even Sum Difference

Problem

You are given an integer array nums of length n.

A partition is defined as an index i where 0 <= i < n - 1, splitting the array into two non-empty subarrays such that:

  • Left subarray contains indices [0, i].
  • Right subarray contains indices [i + 1, n - 1].

Return the number of partitions where the difference between the sum of the left and right subarrays is even.

Algorithm

Translate into English: Given an array, compute all possible partitioning methods such that the difference between the sum of the left subarray and the sum of the right subarray is even. A partitioning method refers to dividing the array into non-empty left and right subarrays.

Code

python3 复制代码
class Solution:
    def countPartitions(self, nums: List[int]) -> int:
        sum_n, len_n = sum(nums), len(nums)
        l_v, r_v, cnts = 0, sum_n, 0
        for i in range(len_n-1):
            l_v += nums[i]
            r_v -=nums[i]
            if (l_v - r_v) % 2 == 0:
                cnts += 1
        return cnts
相关推荐
wuhen_n3 小时前
LeetCode -- 15. 三数之和(中等)
前端·javascript·算法·leetcode
sin_hielo3 小时前
leetcode 2483
数据结构·算法·leetcode
Xの哲學4 小时前
Linux多级时间轮:高精度定时器的艺术与科学
linux·服务器·网络·算法·边缘计算
大头流矢4 小时前
归并排序与计数排序详解
数据结构·算法·排序算法
油泼辣子多加4 小时前
【信创】算法开发适配
人工智能·深度学习·算法·机器学习
Aaron15885 小时前
AD9084和Versal RF系列具体应用案例对比分析
嵌入式硬件·算法·fpga开发·硬件架构·硬件工程·信号处理·基带工程
laocooon5238578865 小时前
插入法排序 python
开发语言·python·算法
wuhen_n5 小时前
LeetCode -- 1:两数之和(简单)
javascript·算法·leetcode·职场和发展
林shir7 小时前
Java基础1.7-数组
java·算法