class Solution {
public:
int maxProduct(int length) {
// write your solution here
vector<int> memo;
// memo[i] is the max for length = i
memo.push_back(0); // placeholder for position 0
memo.push_back(1); // result for position 1
memo.push_back(1); // result for position 2
for (int i = 3; i <= length; i++){
int curr_max = 0;
for (int j = 1; j <= i/2; j++){
int left_max = std::max(j, memo[j]);
int right_max = std::max(i-j, memo[i-j]);
int candidate = left_max * right_max;
curr_max = curr_max > candidate? curr_max : candidate;
}
memo.push_back(curr_max);
}
return memo[length];
}
};
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.