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
相关推荐
囊中之锥.6 小时前
机器学习算法详解:DBSCAN 聚类原理、实现流程与优缺点分析
算法·机器学习·聚类
AlenTech6 小时前
152. 乘积最大子数组 - 力扣(LeetCode)
算法·leetcode·职场和发展
Piar1231sdafa6 小时前
基于yolo13-C3k2-RVB的洗手步骤识别与检测系统实现_1
人工智能·算法·目标跟踪
做科研的周师兄6 小时前
【MATLAB 实战】|多波段栅格数据提取部分波段均值——批量处理(NoData 修正 + 地理信息保真)_后附完整代码
前端·算法·机器学习·matlab·均值算法·分类·数据挖掘
天赐学c语言7 小时前
1.18 - 滑动窗口最大值 && 子类的指针转换为父类的指针,指针的值是否会改变
数据结构·c++·算法·leecode
甄心爱学习7 小时前
KMP算法(小白理解)
开发语言·python·算法
wen__xvn7 小时前
牛客周赛 Round 127
算法
大锦终8 小时前
dfs解决FloodFill 算法
c++·算法·深度优先
橘颂TA8 小时前
【剑斩OFFER】算法的暴力美学——LeetCode 200 题:岛屿数量
算法·leetcode·职场和发展
苦藤新鸡8 小时前
14.合并区间(1,3)(2,5)=(1,5)
c++·算法·leetcode·动态规划