Problem with "submission" process?


  • 0
    J

    I'm getting flagged for wrong answers when I hit the submission button that appear to be correct when I use the run cod button. (C++.)

    Examples: I hit submit and get exactly the following (spaced as showen):

    Input:
       [186
        419
        83
        408]
        6249
    Output:
        18
    Expected:
        20
    

    When I run this input using Run Code, it tells me 20 is correct. (I'm not working this one out by hand to find out which is correct, but I'm fairly confident in my code.)


  • 0
    J

    I should add: I am not using global variables, or anything unusual. Just the single coinChange method.


  • 0

    Could you please test your code again? I've fixed the formatting in the test case.


  • 0
    J

    Still having problems. When I submitted it told me I had a runtime error on:

    [1,2]
    3

    but the code worked when I ran specifically on that input.

    Posting my code below. I really don't think this is a mistake on my part, but I could be missing something.

    int coinChange(vector<int>& coins, int amount) {
        vector<unsigned> D(amount+1, INT_MAX);
        D[0] = 0;
        sort(coins.begin(), coins.end());
        for (int a=coins[0]; a <= amount; a++)
            for (int c=0; c <= coins.size() && coins[c] <= a; c++)
                D[a] = min(D[a], 1 + D[a - coins[c]]);
        return D[amount] < INT_MAX ? D[amount] : -1;
    }

  • 0

    This line causes set fault when c == coins.size()

    c <= coins.size() && coins[c] <= a 

  • 0
    J

    Darn it -- looked at that line 10 times and missed that every time. Thanks.

    Now working.


Log in to reply
 

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