Java DP solution Passed but test case strange


  • 0

    Here is my simple DP solution. However, when n=20, k=4, both my answer and expected answer shows -1322257744. Anyone knows the reason behind?

    public class Solution {
        public int numWays(int n, int k) {
            int a0 = k;
            int a1 = (int)Math.pow((double)k,(double)2);
            int a2 = a1*k - a0;
            int ans = 0;
    
            //Initialization
            if (n==0) return 0;
            if (n==1) return a0;
            if (n==2) return a1;
            if (n==3) return a2;
            
            for(int i=4; i<=n; i++){
    
                ans = a2*k - a0*(k-1);
    
                a0 = a1;
                a1 = a2;
                a2 = ans;
            }
            
            return ans;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.