Last active
February 8, 2020 17:46
-
-
Save jakab922/092450bb16fabe40eda9ae90d94e11cf to your computer and use it in GitHub Desktop.
Solution of https://leetcode.com/problems/split-array-largest-sum/ in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Solution { | |
fn rec_solve(nums: &Vec<i32>, start: usize, left: usize, cache: &mut Vec<Vec<i32>>) -> i32 { | |
if left == 1 { | |
cache[start][1] = nums.iter().skip(start).sum(); | |
return cache[start][1]; | |
} | |
let l = nums.len(); | |
let mut s = 0; | |
for last in start..(l - (left - 1)) { | |
s += nums[last]; | |
if cache[last + 1][left - 1] == i32::max_value() { | |
cache[last + 1][left - 1] = Solution::rec_solve(nums, last + 1, left - 1, cache); | |
} | |
cache[start][left] = i32::min(cache[start][left], i32::max(s, cache[last + 1][left - 1])); | |
} | |
cache[start][left] | |
} | |
pub fn split_array(nums: Vec<i32>, m: i32) -> i32 { | |
let mut cache = vec![vec![i32::max_value(); 51]; 1000]; | |
let um: usize = m as usize; | |
Solution::rec_solve(&nums, 0, um, &mut cache); | |
cache[0][um] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment