[Codewars] John and Ann sign up for Codewars

John and Ann sign up for Codewars

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
std::vector<long long> j{0}, a{1}, jpre{0}, apre{1};
class Johnann
{
static void run(long long x) {
while(j.size() <= x) {
long long n = j.size();
j.push_back(n - a[j.back()]);
a.push_back(n - j[a.back()]);
jpre.push_back(jpre.back() + j.back());
apre.push_back(apre.back() + a.back());
}
}
public:
static std::vector<long long> john(long long n) {
run(n);
std::vector<long long> res;
for(long long i = 0; i < n; i++) res.push_back(j[i]);
return res;
}
static std::vector<long long> ann(long long n) {
run(n);
std::vector<long long> res;
for(long long i = 0; i < n; i++) res.push_back(a[i]);
return res;
}
static long long sumJohn(long long n) {
run(n);
return jpre[n-1];
}
static long long sumAnn(long long n) {
run(n);
return apre[n-1];
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/05/23/PS/Codewars/john-and-ann-sign-up-for-codewars/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.