My O(N) solution is giving time limit exceeded


#1

public int fibsum(int A) {
ArrayList fibNums = new ArrayList();
fibNums.add(1);
fibNums.add(1);
int sum =2;
int currentIndex =0;
HashSet mySet = new HashSet();
mySet.add(1);
while(sum<=A){
sum = fibNums.get(currentIndex)+fibNums.get(currentIndex+1);
if(sum<=A){
fibNums.add(sum);
mySet.add(sum);
}
currentIndex++;
}
int count=0;
int i=fibNums.size()-1;
while(i>=0 && A!=0){
if(A>=fibNums.get(i)){
A-=fibNums.get(i);
count++;
if(mySet.contains(A)){
while(A==fibNums.get(i))
i–;
}else
i–;
}
}
return count;
}