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
相关推荐
aaaameliaaa6 小时前
字符函数和字符串函数
c语言·笔记·算法
城管不管7 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
月疯7 小时前
二分法算法(水平等分图形面积)
算法
豆瓣鸡7 小时前
算法日记 - Day3
java·开发语言·算法
白白白小纯8 小时前
算法篇—反转链表
c语言·数据结构·算法·leetcode
Achou.Wang8 小时前
深入理解go语言-第5章 并发编程——Go的灵魂
大数据·算法·golang
The Chosen One9858 小时前
高进度算法模板速记(待完善)
java·前端·算法
圣保罗的大教堂10 小时前
leetcode 3517. 最小回文排列 I 中等
leetcode
土豆.exe11 小时前
Fastjson2 2.0.53 哈希碰撞 RCE:从原理到三种打法
算法·哈希算法
黄河123长江11 小时前
有限Abel群的结构()
算法