<p>Given an integer array <code>nums</code> and an integer <code>k</code>, split <code>nums</code> into <code>k</code> non-empty subarrays such that the largest sum of any subarray is <strong>minimized</strong>.</p>

<p>Return <em>the minimized largest sum of the split</em>.</p>

<p>A <strong>subarray</strong> is a contiguous part of the array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [7,2,5,10,8], k = 2
<strong>Output:</strong> 18
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 2
<strong>Output:</strong> 9
<strong>Explanation:</strong> There are four ways to split nums into two subarrays.
The best way is to split it into [1,2,3] and [4,5], where the largest sum among the two subarrays is only 9.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
	<li><code>1 &lt;= k &lt;= min(50, nums.length)</code></li>
</ul>

class Solution:
    def splitArray(self, nums: List[int], k: int) -> int:
        #length into k groups,
        #at index i you can include up to the index len(nums)-remaining_groups_passed_in
        #[1,3,4,5,6,7,8,9,2,3,4] k= 6 i =1 1,1 -> 1,6 remaining = 5, 11-6 = 5
        #preprocess prefix sums:
        #[7, 9, 14, 24, 32]
        pref = []
        sm = 0
        for num in nums:
            sm+=num
            pref.append(sm)


        memo = [[float('inf') for j in range(len(nums)+1)] for i in range(k+1)]
        def backtrack(remaining, i):
            if memo[remaining][i] != float('inf'):
                return memo[remaining][i]
            if remaining == 0:
                if i == len(nums):
                    return 0
                return float('inf')
            mn_remaining = float('inf')
            for j in range(i, len(nums)-remaining+1):
                #calculate window sm from prefix
                if nums[j] == 0 and j != len(nums)-remaining:
                    continue
                sub = 0 if i-1 < 0 else pref[i-1]
                window_sm = pref[j] - sub
            
                if remaining >= 1:
                    mn_remaining = min(mn_remaining, max(window_sm, backtrack(remaining-1, j+1)))
            memo[remaining][i] = mn_remaining
            return mn_remaining
        return backtrack(k, 0)
19500