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^5?105 <= 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.pswp.cn/news/389788.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389788.shtml
英文地址,請注明出處:http://en.pswp.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 …