Here is my code, and it passes for the 'Run Code' as the result are expected. But when I submit the code, it reported 'double free or corruption'. Anyone has ideas why ?
It's for the 'coin change' :
class Solution {
public:
int coinChange(vector& coins, int amount) {
static vector ls_minNumChange(amount, 0); // Initialize as 0;
if ( ls_minNumChange[amount] != 0) // If this is calculated before, just return;
return ls_minNumChange[amount];
vector<int> l_coinChange(coins.size(), amount + 1 );
int i = 0;
for ( auto coin : coins)
{
if ( coin == amount )
{
l_coinChange[i] = 1;
break;
}
else if ( amount > coin )
{
l_coinChange[i] = coinChange(coins, amount-coin) + 1;
}
i++;
}
ls_minNumChange[amount] = *min_element (l_coinChange.begin(),l_coinChange.end());
return ls_minNumChange[amount];
} };