動態規劃——Split Array Largest Sum

來源:互聯網
上載者:User

標籤:長度   最大值   疊加   sub   spl   ons   nts   縮小   for迴圈   

題意大概就是,給定一個包含非負整數的序列nums以及一個整數m,要求把序列nums分成m份,並且要讓這m個子序列各自的和的最大值最小(minimize the largest sum among these m subarrays)。Note:
If n is the length of array, assume the following constraints are satisfied:
1 ≤ n ≤ 1000
1 ≤ m ≤ min(50, n)Examples:
Input:
nums = [7,2,5,10,8]
m = 2Output:
18Explanation:
There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8],
where the largest sum among the two subarrays is only 18.狀態:dp[j][i]就是簡單的縮小題目的規模,把j長的序列分成i個連續的子序列,子序列和的最大值中的最小值。
狀態轉移方程:每次我們只關注整個序列中最後一個元素加入時對dp值的影響,由於是要分成連續的序列,所以最後一個元素只能與它前面的若干元素組成子序列,需要一個for來枚舉包含最後一個元素的子序列的情況,例如我現在要求dp[j][i],在放入最後一個元素nums[j]時,設klen為第i個連續子序列的長度,這個子序列的和為
dp[nums.size][1]-dp[nums.size-klen][1],而前nums-klen個元素組成的i-1個連續子序列和的最大值的最小值為dp[nums.size][i-1]已經在前面的計算過程中完成了計算,易知dp[j][i] = min(max(dp[nums.size][1]-dp[nums.size-klen][1],dp[nums.size][i-1])),這個題可以很明顯的看出動態規劃的最優子結構不過為了方便起見,先將所有的dp[j][1]計算出來,計算很簡單也好理解,就是for迴圈疊加,不過如果單純用int數組的話可能越界,比如:[1,2147483647],會在dp數組中出現-2147483648這樣的元素。由於輸入樣本都是整數,可以使用double數組,返回的結果用int強制轉換即可 
 1     public int splitArray(int[] nums,int m) { 2         int nlen = nums.length; 3         int[]num = new int[nlen+1]; 4         double[][]dp = new double[nlen+1][m+1]; 5         double temp = 0; 6         num[0] = nums.length; 7         for(int i = 1;i<=nlen;i++) 8             num[i] = nums[i-1]; 9         for(int i = 0;i<=nlen;i++)10             dp[i][0] = 0;11         for(int i = 0;i<=m;i++)12             dp[0][i] = 0;13         for(int i = 1;i<=m;i++) {14             for(int j = i;j<=nlen;j++) {15                 if(i==1)dp[j][i] = dp[j-1][i]+num[j];16                 else {17                     dp[j][i] = dp[nlen][1];18                     for(int k = i-1;k<j;k++) {19                         temp = dp[k][i-1]>(dp[j][1]-dp[k][1])?dp[k][i-1]:(dp[j][1]-dp[k][1]);20                         dp[j][i] = dp[j][i]<temp?dp[j][i]:temp;21                     }22                 }23             }24         }25         26         for(int i = 1;i<=m;i++) {27             for(int j = 1;j<=nlen;j++) 28                 System.out.print(dp[j][i]+" ");29             System.out.println();30         }31         32         return (int) dp[nlen][m];33     }

 

動態規劃——Split Array Largest Sum

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.