codekofi
← All problems

Problem 119

Medium

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 bestRun(const vector<int>& money, int lo, int hi) {
2
3 int skip = 0;
4 int take = 0;
5
6 for (int i = lo; i <= hi; i++) {
7
8 int next = max(take, skip + money[i]);
9
10 skip = take;
11 take = next;
12 }
13
14 return take;
15}
16
17int solution(const vector<int>& money) {
18
19 int n = money.size();
20
21 if (n == 0) {
22 return 0;
23 }
24
25 if (n == 1) {
26 return money[0];
27 }
28
29 int dropLast = bestRun(money, 0, n - 2);
30 int dropFirst = bestRun(money, 1, n - 1);
31
32 return max(dropLast, dropFirst);
33}

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.