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
相关推荐
见合八方3 分钟前
【滤波器】热调谐FP滤波器
人工智能·算法
古城小栈4 分钟前
cargo-pprof:Rust性能调优
人工智能·算法·rust
x_xbx14 分钟前
LeetCode:543. 二叉树的直径
算法·leetcode·职场和发展
QiLinkOS15 分钟前
QiLink 技术委员会选举实施细则
c语言·数据结构·c++·单片机·嵌入式硬件·算法·开源
我材不敲代码19 分钟前
Python基础: 函数超全详解:定义、参数、返回值、作用域与递归
开发语言·python·算法
罗超驿25 分钟前
11.LeetCode 1004. 最大连续1的个数 III | 滑动窗口解法详解(Java)
java·算法·leetcode
QiLinkOS29 分钟前
发明人与专利价值共生逻辑
c语言·数据结构·c++·人工智能·单片机·嵌入式硬件·算法
计算机安禾40 分钟前
【算法分析与设计】第21篇:回溯法的状态空间树与剪枝函数设计
大数据·人工智能·算法·机器学习·数据挖掘·剪枝
磊 子42 分钟前
STL之set以及set和map区别
开发语言·c++·算法
Promise微笑1 小时前
算法突围:“双核四驱”理论下的“官网”AI引用概率提升指南
人工智能·算法·chatgpt