• + 0 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 :

    var fiboVal = make(map[int64]int64)
    
    func isFibo(n int64) string {
        // Write your code here
        var isFiboVar = make(map[int64]bool)
        
        if _, isExist := isFiboVar[0]; !isExist {
            isFiboVar[0] = true
        }
        
        if _, isExist := isFiboVar[1]; !isExist {
            isFiboVar[1] = true
        }
        
        for i := int64(2);;i++ {
            val := fibo(i)
            isFiboVar[val] = true
           
            if val > n {
                break
            }
        }
    
        if isFiboVar[n] {
            return "IsFibo"
        }
    
        return "IsNotFibo"
    }
    
    func fibo(n int64) int64 {
        if _, isExist := fiboVal[n]; isExist {
            return fiboVal[n]
        }
        
        if n == 0 {
            fiboVal[0] = 0
            return 0
        }
        
        if n == 1 {
            fiboVal[1] = 1
            return 1
        }
        
        if n > 1 {
            fiboVal[n] = fibo(n-1) + fibo(n-2)
            return fiboVal[n]
        }
        
        return 0
    }