Clumsy Factorial LeetCode solution in C++

1
class Solution {
2
public:
3
int calc(int num) {
4
int a = num, b = num - 1, c = num - 2;
5
return a * b / c;
6
}
7
int clumsy(int n) {
8
if (n == 1) return 1;
9
if (n == 2) return 2;
10
if (n == 3) return 6;
11
int res = calc(n), i;
12
res += n - 3;
13
for (i = n - 4; i >= 4; i -= 4) {
14
int val = calc(i);
15
res -= val;
16
res += i - 3;
17
}
18
if (i == 1)
19
return res - 1;
20
else if (i == 2)
21
return res - 2;
22
else if (i == 3)
23
return res - 6;
24
return res;
25
}
26
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0