[InterviewBit] Sum Of Fibonacci Numbers

Sum Of Fibonacci Numbers

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int Solution::fibsum(int A) {
vector<int> fibo{1,1};
while(fibo.back() < A) fibo.push_back(fibo[fibo.size() - 1] + fibo[fibo.size() - 2]);
int res = 0;
while(A and fibo.size()) {
if(fibo.back() <= A) {
A -= fibo.back();
res += 1;
}
fibo.pop_back();
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/08/PS/interviewbit/sum-of-fibonacci-numbers/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.