We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
the problem is actually quite easy. the bigger problem is how to handle the time limit. that's where we needed the "cache". golang map is excellent in this
Golang :
varfiboVal=make(map[int64]int64)funcisFibo(nint64)string{// Write your code herevarisFiboVar=make(map[int64]bool)if_,isExist:=isFiboVar[0];!isExist{isFiboVar[0]=true}if_,isExist:=isFiboVar[1];!isExist{isFiboVar[1]=true}fori:=int64(2);;i++{val:=fibo(i)isFiboVar[val]=trueifval>n{break}}ifisFiboVar[n]{return"IsFibo"}return"IsNotFibo"}funcfibo(nint64)int64{if_,isExist:=fiboVal[n];isExist{returnfiboVal[n]}ifn==0{fiboVal[0]=0return0}ifn==1{fiboVal[1]=1return1}ifn>1{fiboVal[n]=fibo(n-1)+fibo(n-2)returnfiboVal[n]}return0}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Is Fibo
You are viewing a single comment's thread. Return to all comments →
the problem is actually quite easy. the bigger problem is how to handle the time limit. that's where we needed the "cache". golang map is excellent in this
Golang :