codekofi
← All problems

Problem 26

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 solution(const vector<char>& tasks, int gap) {
2
3 if (tasks.empty()) {
4 return 0;
5 }
6
7 vector<int> count(26, 0);
8
9 for (char c : tasks) {
10 count[c - 'A']++;
11 }
12
13 int most = 0;
14
15 for (int c : count) {
16 most = max(most, c);
17 }
18
19 int tied = 0;
20
21 for (int c : count) {
22 if (c == most) {
23 tied++;
24 }
25 }
26
27 int frame = (most - 1) * (gap + 1) + tied;
28 int total = tasks.size();
29
30 return max(total, frame);
31}

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.