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
相关推荐
地平线开发者10 小时前
SparseDrive 模型导出与性能优化实战
算法·自动驾驶
董董灿是个攻城狮10 小时前
大模型连载2:初步认识 tokenizer 的过程
算法
地平线开发者11 小时前
地平线 VP 接口工程实践(一):hbVPRoiResize 接口功能、使用约束与典型问题总结
算法·自动驾驶
罗西的思考11 小时前
AI Agent框架探秘:拆解 OpenHands(10)--- Runtime
人工智能·算法·机器学习
HXhlx14 小时前
CART决策树基本原理
算法·机器学习
Wect15 小时前
LeetCode 210. 课程表 II 题解:Kahn算法+DFS 双解法精讲
前端·算法·typescript
颜酱15 小时前
单调队列:滑动窗口极值问题的最优解(通用模板版)
javascript·后端·算法
Gorway1 天前
解析残差网络 (ResNet)
算法
拖拉斯旋风1 天前
LeetCode 经典算法题解析:优先队列与广度优先搜索的巧妙应用
算法
Wect1 天前
LeetCode 207. 课程表:两种解法(BFS+DFS)详细解析
前端·算法·typescript