My solution outputs the correct answer for the same test case that fails during judging!


  • 0
    L

    I am having some issue with the judge. For the problem "Coin Change 2", the judge reports that my solution outputs the wrong answer from the test case "3 [2]" - the correct answer should be 0 and the judge states that my program outputs 3. However, when I use this as a sample test case, my program actually outputs the correct answer!

    I thought that my program has some undefined behaviour. But after checking through my code, I can't seem to understand the cause of the difference. Could anyone please explain the cause of this difference?

    My code:

    class Solution {
    public:
        int change(int amount, vector<int>& coins) {
            int dp[2][5001];
            int n = coins.size();
            for(int i = 0; i <= 1; i++) dp[i][0] = 1;
            for(int i = 1; i <= n; i++) {
                for(int j = 1; j <= amount; j++) {
                    if(j >= coins[i-1]) {
                        dp[i%2][j] = dp[i%2][j-coins[i-1]]+dp[(i+2-1)%2][j];
                    } else {
                        dp[i%2][j] = dp[(i+2-1)%2][j];
                    }
                }
            }
            return dp[n%2][amount];
        }
    };
    

Log in to reply
 

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