codekofi
← All problems

Problem 141

Hard

1 · Worked examples

0 / 3

Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 3 outputs correctly.

The accepted solution

1int solution(const vector<int>& nums) {
2
3 int n = nums.size();
4 vector<int> pad(n + 2, 1);
5
6 for (int i = 0; i < n; i++) {
7 pad[i + 1] = nums[i];
8 }
9
10 vector<vector<int>> best(n + 2, vector<int>(n + 2, 0));
11
12 for (int len = 1; len <= n; len++) {
13 for (int lo = 1; lo + len - 1 <= n; lo++) {
14
15 int hi = lo + len - 1;
16
17 for (int last = lo; last <= hi; last++) {
18
19 int burst = pad[lo - 1] * pad[last] * pad[hi + 1];
20 int rest = best[lo][last - 1] + best[last + 1][hi];
21
22 best[lo][hi] = max(best[lo][hi], burst + rest);
23 }
24 }
25 }
26
27 return best[1][n];
28}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.