2025. 分割数组的最多方案数

2025. 分割数组的最多方案数

给你一个下标从 0 开始且长度为 n 的整数数组 nums 。分割 数组 nums 的方案数定义为符合以下两个条件的 pivot 数目:

  • 1 <= pivot < n
  • nums[0] + nums[1] + … + nums[pivot - 1] == nums[pivot] + nums[pivot + 1] + … + nums[n -1]
    同时给你一个整数 k 。你可以将 nums 中 一个 元素变为 k 或 不改变 数组。

请你返回在 至多 改变一个元素的前提下,最多 有多少种方法 分割 nums 使得上述两个条件都满足。

示例 1:输入:nums = [2,-1,2], k = 3
输出:1
解释:一个最优的方案是将 nums[0] 改为 k 。数组变为 [3,-1,2] 。
有一种方法分割数组:
- pivot = 2 ,我们有分割 [3,-1 | 2]:3 + -1 == 2 。
示例 2:输入:nums = [0,0,0], k = 1
输出:2
解释:一个最优的方案是不改动数组。
有两种方法分割数组:
- pivot = 1 ,我们有分割 [0 | 0,0]:0 == 0 + 0 。
- pivot = 2 ,我们有分割 [0,0 | 0]: 0 + 0 == 0 。
示例 3:输入:nums = [22,4,-25,-20,-15,15,-16,7,19,-10,0,-13,-14], k = -33
输出:4
解释:一个最优的方案是将 nums[2] 改为 k 。数组变为 [22,4,-33,-20,-15,15,-16,7,19,-10,0,-13,-14] 。
有四种方法分割数组。

提示:

  • n == nums.length
  • 2 <= n <= 10510^5105
  • −105-10^5105 <= k, nums[i] <= 10510^5105

解题思路

对于 nums[0] + nums[1] + ... + nums[pivot - 1] == nums[pivot] + nums[pivot + 1] + ... + nums[n - 1]

我们把nums[0] + nums[1] + ... + nums[pivot - 1] 称为front,后半部分nums[pivot] + nums[pivot + 1] + ... + nums[n - 1]称为back,整个数组的总和为sum。

维护一个数组的前缀和pre

  1. 当不改变数组的时候,只有满足sum[i]*2=sum的前缀和,才能成为一种分隔方法
  2. 当改变数组的一个元素的时候,我们遍历所有的元素,尝试将其改变,维护两个map,分别代表当前元素前面出现过的前缀和以及后面的前缀和。可以发现当我们尝试改变元素时,是不会影响前面的前缀和,而是会导致后面的前缀和产生变化d(d为目标元素k与原来元素的差值)。因此对于前边满足条件的前缀和,我们的计算公式为sum[i]=sum+d-sum[i],而对于后面的前缀和计算公式为 sum[i]+d=sum+d-(sum[i]+d)

代码


class Solution {
public:int waysToPartition(vector<int> nums, int k) {int res(0), n(nums.size());vector<long long> pre(n + 1);map<long long, int> ml;map<long long,int> mr;for (int i = 0; i < n; ++i) {pre[i + 1] = pre[i] + nums[i];if(i+1<n)mr[pre[i+1]]++;}long long sum = pre[n];if (sum%2==0)res=mr[sum/2];for (int i = 1; i <= n; ++i) {long long d=k-nums[i-1],new_sum=sum+d;if (new_sum%2==0){res=max(res,ml[new_sum/2]+mr[new_sum/2-d]);}ml[pre[i]]++;mr[pre[i]]--;}return res;}
};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389788.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

您是六个主要数据角色中的哪一个

When you were growing up, did you ever play the name game? The modern data organization has something similar, and it’s called the “Bad Data Blame Game.” Unlike the name game, however, the Bad Data Blame Game is played when data downtime strikes and no…

命令查看linux主机配置

查看cpu&#xff1a; # 总核数 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 物理CPU个数 X 每颗物理CPU的核数 X 超线程数# 查看物理CPU个数 cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l# 查看每个物理CPU中core的个数(即核数) cat /proc/cpui…

C#中全局处理异常方式

using System; using System.Configuration; using System.Text; using System.Windows.Forms; using ZB.QueueSys.Common;namespace ZB.QueueSys {static class Program{/// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main(){Appli…

5911. 模拟行走机器人 II

5911. 模拟行走机器人 II 给你一个在 XY 平面上的 width x height 的网格图&#xff0c;左下角 的格子为 (0, 0) &#xff0c;右上角 的格子为 (width - 1, height - 1) 。网格图中相邻格子为四个基本方向之一&#xff08;“North”&#xff0c;“East”&#xff0c;“South”…

自定义按钮动态变化_新闻价值的变化定义

自定义按钮动态变化I read Bari Weiss’ resignation letter from the New York Times with some perplexity. In particular, I found her claim that she “was hired with the goal of bringing in voices that would not otherwise appear in your pages” a bit strange: …

Linux记录-TCP状态以及(TIME_WAIT/CLOSE_WAIT)分析(转载)

1.TCP握手定理 2.TCP状态 l CLOSED&#xff1a;初始状态&#xff0c;表示TCP连接是“关闭着的”或“未打开的”。 l LISTEN &#xff1a;表示服务器端的某个SOCKET处于监听状态&#xff0c;可以接受客户端的连接。 l SYN_RCVD &#xff1a;表示服务器接收到了来自客户端请求…

677. 键值映射

677. 键值映射 实现一个 MapSum 类&#xff0c;支持两个方法&#xff0c;insert 和 sum&#xff1a; MapSum() 初始化 MapSum 对象 void insert(String key, int val) 插入 key-val 键值对&#xff0c;字符串表示键 key &#xff0c;整数表示值 val 。如果键 key 已经存在&am…

算法 从 数中选出_算法可以选出胜出的nba幻想选秀吗

算法 从 数中选出Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without …

jQuery表单校验

小小Demo&#xff1a; <script>$(function () {//给username绑定失去焦点事件$("#username").blur(function () {//得到username文本框的值var nameValue $(this).val();//每次清除数据$("table font:first").remove();//校验username是否合法if (n…

5912. 每一个查询的最大美丽值

5912. 每一个查询的最大美丽值 给你一个二维整数数组 items &#xff0c;其中 items[i] [pricei, beautyi] 分别表示每一个物品的 价格 和 美丽值 。 同时给你一个下标从 0 开始的整数数组 queries 。对于每个查询 queries[j] &#xff0c;你想求出价格小于等于 queries[j] …

django-rest-framework第一次使用使用常见问题

2019独角兽企业重金招聘Python工程师标准>>> 记录在第一次使用django-rest-framework框架使用时遇到的问题&#xff0c;为了便于理解在这里创建了Person和Grade这两个model from django.db import models class Person(models.Model):SHIRT_SIZES ((S, Small),(M, …

插入脚注把脚注标注删掉_地狱司机不应该只是英国电影历史数据中的脚注,这说明了为什么...

插入脚注把脚注标注删掉Cowritten by Andie Yam由安迪(Andie Yam)撰写 Hell Drivers”, 1957地狱司机 》电影海报 Data visualization is a great way to celebrate our favorite pieces of art as well as reveal connections and ideas that were previously invisible. Mor…

vue之axios 登陆验证及数据获取

登陆验证&#xff0c;获取token methods:{callApi () {var vm thisvm.msg vm.result //验证地址vm.loginUrl http://xxx///查询地址vm.apiUrl http://yyy/vm.loginModel {username: 你的用户名,password: 你的密码,// grant_type: password,}//先获取 tokenaxios.post(v…

5926. 买票需要的时间

5926. 买票需要的时间 有 n 个人前来排队买票&#xff0c;其中第 0 人站在队伍 最前方 &#xff0c;第 (n - 1) 人站在队伍 最后方 。 给你一个下标从 0 开始的整数数组 tickets &#xff0c;数组长度为 n &#xff0c;其中第 i 人想要购买的票数为 tickets[i] 。 每个人买票…

贝叶斯统计 传统统计_统计贝叶斯如何补充常客

贝叶斯统计 传统统计For many years, academics have been using so-called frequentist statistics to evaluate whether experimental manipulations have significant effects.多年以来&#xff0c;学者们一直在使用所谓的常客统计学来评估实验操作是否具有significant效果。…

吴恩达机器学习+林轩田机器学习+高等数学和线性代数等视频领取

机器学习一直是一个热门的领域。这次小编应大家需求&#xff0c;整理了许多相关学习视频和书籍。本次分享包含&#xff1a;台湾大学林轩田老师的【机器学习基石】和【机器学习技法】视频教学、吴恩达老师的机器学习分享、徐小湛的高等数学和线性代数视频&#xff0c;还有相关机…

saltstack二

配置管理 haproxy的安装部署 haproxy各版本安装包下载路径https://www.haproxy.org/download/1.6/src/&#xff0c;跳转地址为http&#xff0c;改为https即可 创建相关目录 # 创建配置目录 [rootlinux-node1 ~]# mkdir /srv/salt/prod/pkg/ [rootlinux-node1 ~]# mkdir /srv/sa…

319. 灯泡开关

319. 灯泡开关 初始时有 n 个灯泡处于关闭状态。第一轮&#xff0c;你将会打开所有灯泡。接下来的第二轮&#xff0c;你将会每两个灯泡关闭一个。 第三轮&#xff0c;你每三个灯泡就切换一个灯泡的开关&#xff08;即&#xff0c;打开变关闭&#xff0c;关闭变打开&#xff0…

如何生成随机不重复的11位数字

要求 不重复随机11位数字不占存储我们都知道11位数字(random)对应有最大值max和最小值min99999999999和10000000000.很简单的从最小值开始按顺序分发到最大值&#xff0c;就满足了不重复&#xff0c;不占存储&#xff0c;11位数字的特性。那么接下来就要考虑如何生成随机数字这…

因为你的电脑安装了即点即用_即你所爱

因为你的电脑安装了即点即用Data visualization is a great way to celebrate our favorite pieces of art as well as reveal connections and ideas that were previously invisible. More importantly, it’s a fun way to connect things we love — visualizing data and …