代码随想录算法训练营第四十八天|● 322. 零钱兑换 ● 279.完全平方数

322. 零钱兑换

int coinChange(int* coins, int coinsSize, int amount){
    int dp[amount + 1];
    for (int i = 0; i <= amount; i++)
    {
        dp[i] = INT_MAX;
    }
    dp[0] = 0;
    int i, j;
    for (i = 0; i < coinsSize; i++)
    {
        for (j = coins[i]; j <= amount; j++)
        {
            if (dp[j - coins[i]] != INT_MAX)
            {
                dp[j] = fmin(dp[j - coins[i]] + 1, dp[j]);
            }
        }
    }
    if (dp[amount] == INT_MAX) return -1;
    return dp[amount];
}

279. 完全平方数

int numSquares(int n){
    int dp[n + 1];
    for (int i=0; i<=n; i++)
    {
        dp[i] = INT_MAX;
    }
    dp[0] = 0;
    int i, j;
    for (int i = 1; i * i <= n; i++)
    {
        for (int j = i * i; j <= n; j++)
        {
            dp[j] = fmin(dp[j], dp[j - i*i] + 1);
        }
    }
    if (dp[n] == INT_MAX) return -1;
    return dp[n];
}

你可能感兴趣的:(代码随想录算法,1024程序员节,算法,数据结构,c语言,leetcode)