codekofi
← All problems

Problem 121

Easy

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>& cost) {
2
3 int n = cost.size();
4
5 if (n == 0) {
6 return 0;
7 }
8
9 int back2 = 0;
10 int back1 = 0;
11
12 for (int i = 2; i <= n; i++) {
13
14 int oneStep = back1 + cost[i - 1];
15 int twoStep = back2 + cost[i - 2];
16
17 int here = min(oneStep, twoStep);
18
19 back2 = back1;
20 back1 = here;
21 }
22
23 return back1;
24}

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.