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
相关推荐
F_D_Z13 小时前
扩散模型快速采样:从渐进蒸馏到并行推理
人工智能·算法·加速采样
睡一觉就好了。13 小时前
哈希表(一)
算法·散列表
輕華13 小时前
Word2Vec与CBOW算法实战:从词向量到上下文感知
人工智能·算法·word2vec
Matlab程序猿小助手13 小时前
【MATLAB源码-第315期】基于matlab的䲟鱼优化算法(ROA)无人机三维路径规划,输出做短路径图和适应度曲线.
开发语言·算法·matlab
圣保罗的大教堂13 小时前
leetcode 874. 模拟行走机器人 中等
leetcode
luoganttcc14 小时前
CUDA grid/block 到矩阵映射示例(矩阵加法)
人工智能·算法·机器学习
YuanDaima204814 小时前
Python 数据结构与语法速查笔记
开发语言·数据结构·人工智能·python·算法
XY_墨莲伊14 小时前
【编译原理】实验一:基于正则文法的词法分析器设计与实现
开发语言·数据结构·算法
剑挑星河月14 小时前
394.字符串解码
数据结构·算法·leetcode
算法鑫探14 小时前
C语言实现 简易计算器教程
c语言·数据结构·算法·新人首发