1. 程式人生 > >410. Split Array Largest Sum 把數組劃分為m組,怎樣使最大和最小

410. Split Array Largest Sum 把數組劃分為m組,怎樣使最大和最小

sum lar mil 輸入 examples follow font AS height

[抄題]:

Given an array 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.

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 = 2

Output:
18

Explanation:
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.

[暴力解法]:

時間分析:

空間分析:

[優化後]:

時間分析:

空間分析:

[奇葩輸出條件]:

[奇葩corner case]:

[思維問題]:

不知道幹嘛用二分法:二分法可以通過mid的移動 找一個位置

[英文數據結構或算法,為什麽不用別的數據結構或算法]:

[一句話思路]:

想不到:最大的數肯定要分隔開,就看能往左劃幾個數和它一組

[7,2,5,10,8,105550]
3
返回105550

分組不超過m就往擴展 否則往右

[輸入量]:空: 正常情況:特大:特小:程序裏處理到的特殊情況:異常情況(不合法不合理的輸入):

[畫圖]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分鐘肉眼debug的結果]:

[總結]:

多試試幾個case 自然就明白了

[復雜度]:Time complexity: O() Space complexity: O()

[算法思想:遞歸/分治/貪心]:

[關鍵模板化代碼]:

[其他解法]:

[Follow Up]:

[LC給出的題目變變變]:

[代碼風格] :

[是否頭一次寫此類driver funcion的代碼] :

410. Split Array Largest Sum 把數組劃分為m組,怎樣使最大和最小