class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
int max=amount+1;
vector<int> dp(amount+1,max);
dp[0]=0;
for(int i=1;i<=amount;i++)
{
for(int j=0;j<coins.size();j++)
{
if(coins[j]<=i)
{
dp[i]=min(dp[i],dp[i-coins[j]]+1);
}
}
}
return dp[amount]>amount?-1:dp[amount];
}
};
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
vector<int>dp (target+1);
dp[0]=1;
for(int i=1;i<=target;i++)
{
for (int& num : nums)
{
if (num <= i && dp[i - num] < INT_MAX - dp[i]) {
dp[i] += dp[i - num];
}
}
}
return dp[target];
}
};
class Solution {
int dfs(vector<vector<int>>& grid, int cur_i, int cur_j) {
//岛屿可以是靠近边界的
//下面的条件是控制 i,j不出界
if (cur_i < 0 || cur_j < 0 || cur_i == grid.size() || cur_j == grid[0].size() || grid[cur_i][cur_j] != 1) {
return 0;
}
//标记 应经统计过一次 下次直接略过
grid[cur_i][cur_j] = 0;
//四个方向
int di[4] = {0, 0, 1, -1};
int dj[4] = {1, -1, 0, 0};
int ans = 1;
for (int index = 0; index != 4; ++index) {
int next_i = cur_i + di[index], next_j = cur_j + dj[index];
ans += dfs(grid, next_i, next_j);
}
return ans;
}
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int ans = 0;
for (int i = 0; i != grid.size(); ++i) {
for (int j = 0; j != grid[0].size(); ++j) {
ans = max(ans, dfs(grid, i, j));
}
}
return ans;
}
};