Why my code RE?


  • 0
    Q
    #include <iostream>
    #include <vector>
    using namespace std;
    class Solution {
    public:
      int coinChange(vector<int> &coins, int amount) {
        int n = coins.size();
        if (n == 0)
          return -1;
        if (amount == 0)
          return 0;
        int c[amount + 1];
        for (int i = 0; i < amount + 1; i++) {
          c[i] = 99999;
        }
        for (auto i : coins) {
          c[i] = 1;
        }
        c[0] = 0;
        for (int i = 0; i <= amount; i++) {
          for (auto j : coins) {
            if (i + j <= amount) {
              c[i + j] = c[i + j] > c[i] + 1 ? c[i] + 1 : c[i + j];
            }
          }
        }
        return c[amount] == 99999 ? -1 : c[amount];
      }
    };
    

    if I use global array c instead of new a int c[amount+1],it accepts.But why RE when I int c[amount+1]?


Log in to reply
 

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