<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)410:分割數(shù)組的最大值

          共 1137字,需瀏覽 3分鐘

           ·

          2021-10-17 08:45

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?分割數(shù)組的最大值,我們先來看題面:
          https://leetcode-cn.com/problems/split-array-largest-sum/

          Given an array nums which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays.


          Write an algorithm to minimize the largest sum among these m subarrays.



          給定一個非負整數(shù)數(shù)組 nums 和一個整數(shù) m ,你需要將這個數(shù)組分成 m 個非空的連續(xù)子數(shù)組。

          設(shè)計一個算法使得這 m 個子數(shù)組各自和的最大值最小。

          示例


          示例 1:

          輸入:nums = [7,2,5,10,8], m = 2
          輸出:18
          解釋:
          一共有四種方法將 nums 分割為 2 個子數(shù)組。其中最好的方式是將其分為 [7,2,5] 和 [10,8] 。
          因為此時這兩個子數(shù)組各自的和的最大值為18,在所有情況中最小。

          示例 2:

          輸入:nums = [1,2,3,4,5], m = 2
          輸出:9

          示例 3:

          輸入:nums = [1,4,4], m = 3
          輸出:4


          解題


          利用動態(tài)規(guī)劃:
          狀態(tài)表示 dp[i][j] 表示nums[0..i]劃分成j段時的最大值
          狀態(tài)轉(zhuǎn)移:dp[i][j] 為0~i的前k個位置被分成了j-1段,然后最后一個部分的值是sub[i]-sub[j]
          其中sub[i] 是前i個nums元素的值
          初始條件:dp[0][0]=0

          class?Solution?{
          ????public?int?splitArray(int[] nums, int?m) {
          ??if?(nums == null?|| nums.length < m) return?-1;
          ??int?n = nums.length;
          ??// dp[i][j] 表示nums[0..i]劃分成j段時的最小情況
          ??int[][] dp = new?int[n + 1][m + 1];
          ??for?(int?i = 0; i <= n; i++) {
          ????Arrays.fill(dp[i], Integer.MAX_VALUE);
          ??}

          ??// sub[i]表示num[0..i]的和
          ??int[] sub = new?int[n + 1];
          ??for?(int?i = 0; i < n; i++) {
          ????sub[i + 1] = sub[i] + nums[i];
          ??}
          ??// 初始條件
          ??dp[0][0] = 0;
          ??for?(int?i = 1; i <= n; i++) {
          ????// 由于我們不能分出空的子數(shù)組,所以必須有i>=j
          ????for?(int?j = 1; j <= Math.min(i, m); j++) {
          ??????// nums的前k-1個數(shù)被分為j-1段,然后nums(k~i)為第j段
          ??????// k為啥可以從0開始還沒理解 2020/07/25
          ??????for?(int?k = 0; k < i; k++) {
          ????????// 狀態(tài)轉(zhuǎn)移方程
          ????????dp[i][j] = Math.min(dp[i][j], Math.max(dp[k][j - 1], sub[i] - sub[k]));
          ??????}
          ????}
          ??}
          ??return?dp[n][m];
          ????}
          }



          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-400題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)401:二進制手表

          LeetCode刷題實戰(zhàn)402:移掉 K 位數(shù)字

          LeetCode刷題實戰(zhàn)403:青蛙過河

          LeetCode刷題實戰(zhàn)404:左葉子之和

          LeetCode刷題實戰(zhàn)405:數(shù)字轉(zhuǎn)換為十六進制數(shù)

          LeetCode刷題實戰(zhàn)406:根據(jù)身高重建隊列

          LeetCode刷題實戰(zhàn)407:接雨水 II

          LeetCode刷題實戰(zhàn)408:有效單詞縮寫

          LeetCode刷題實戰(zhàn)409:最長回文串


          瀏覽 23
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  伊人中文在线 | 自拍偷拍第1页 | 韩国三级在线视频网址 | 日韩一级片免费观看 | 青青青青青青草草草草草草草视频 |