面试题 17.16. 按摩师
面试题 17.16. 按摩师 - 力扣(LeetCode)
class Solution {
public:int massage(vector<int>& nums) {int n = nums.size();//特殊情况:空数组if(n==0)return 0;vector<int> f(n);vector<int> g(n);//处理越界f[0] = nums[0];for(int i = 1;i<n;i++){f[i] = g[i-1]+nums[i];g[i] = max(f[i-1],g[i-1]);}return max(g[n-1],f[n-1]);}
};
LCR 090. 打家劫舍 II
LCR 090. 打家劫舍 II - 力扣(LeetCode)
//打家劫舍II
class Solution {
public:int rob(vector<int>& nums) {int n = nums.size();return max(nums[0]+rob1(nums,2,n-2),rob1(nums,1,n-1));}int rob1(vector<int>& nums,int left,int right){int n = nums.size();//特殊情况:空数组if(left>right)return 0;vector<int> f(n);vector<int> g(n);//处理越界f[left] = nums[left];for(int i = left+1;i<=right;i++){f[i] = g[i-1]+nums[i];g[i] = max(f[i-1],g[i-1]);}return max(g[right],f[right]);}
};
740. 删除并获得点数
740. 删除并获得点数 - 力扣(LeetCode)
//删除并获得点数
class Solution {
public:int deleteAndEarn(vector<int>& nums) {int n = 10001;int arr[10001] = {0};for(auto x:nums){arr[x] +=x;}vector<int> f(n);vector<int> g(n);f[0] = arr[0];for(int i = 1;i<n;i++){f[i] = g[i-1]+arr[i];g[i] = max(f[i-1],g[i-1]);}return max(f[n-1],g[n-1]);}
};
LCR 091. 粉刷房子
LCR 091. 粉刷房子 - 力扣(LeetCode)
class Solution {
public:int minCost(vector<vector<int>>& costs) {int m = costs.size();vector<vector<int>> dp(m+1,vector<int>(3));for(int i = 1;i<=m;i++){dp[i][0] = min(dp[i-1][1],dp[i-1][2])+costs[i-1][0];dp[i][1] = min(dp[i-1][0],dp[i-1][2])+costs[i-1][1];dp[i][2] = min(dp[i-1][0],dp[i-1][1])+costs[i-1][2];}return min(dp[m][0],min(dp[m][1],dp[m][2]));}
};
309. 买卖股票的最佳时机含冷冻期
309. 买卖股票的最佳时机含冷冻期 - 力扣(LeetCode)
class Solution {
public:int maxProfit(vector<int>& prices) {int n = prices.size();vector<vector<int>> dp(n,vector<int>(3));dp[0][0] = -prices[0];for(int i = 1;i<n;i++){dp[i][0] = max(dp[i-1][1]-prices[i],dp[i-1][0]);dp[i][1] = max(dp[i-1][2],dp[i-1][1]);dp[i][2] = dp[i-1][0] + prices[i];}return max(dp[n-1][0],max(dp[n-1][1],dp[n-1][2]));}
};
714. 买卖股票的最佳时机含手续费
714. 买卖股票的最佳时机含手续费 - 力扣(LeetCode)
class Solution {
public:int maxProfit(vector<int>& prices, int fee) {int n = prices.size();vector<int> f(n);vector<int> g(n);f[0] = -prices[0];for(int i = 1;i<n;i++){f[i] = max(f[i-1],g[i-1]-prices[i]);g[i] = max(g[i-1],f[i-1]+prices[i]-fee);}return max(f[n-1],g[n-1]);}
};
123. 买卖股票的最佳时机 III
123. 买卖股票的最佳时机 III - 力扣(LeetCode)
class Solution {
public:const int INF = 0x3f3f3f3f;int maxProfit(vector<int>& prices) {int n = prices.size();vector<vector<int>> f(n,vector<int>(3,-INF));vector<vector<int>> g(n,vector<int>(3,-INF));f[0][0] = -prices[0];g[0][0] = 0;int ret = 0;for(int i = 1;i<n;i++){for(int j = 0;j<3;j++){f[i][j] = max(f[i-1][j],g[i-1][j]-prices[i]);g[i][j] = g[i-1][j];if(j>=1){g[i][j] = max(g[i-1][j],f[i-1][j-1]+prices[i]);}}}for(int j = 0;j<3;j++){ret = max(ret,g[n-1][j]);}return ret;}
};
188. 买卖股票的最佳时机 IV
188. 买卖股票的最佳时机 IV - 力扣(LeetCode)
class Solution {
public:const int MIN = 0x3f3f3f3f;int maxProfit(int k, vector<int>& prices) {int n = prices.size();k = min(k,n/2);//最多完成k次交易,确保达到kvector<vector<int>> f(n,vector<int>(k+1,-MIN));vector<vector<int>> g(n,vector<int>(k+1,-MIN));f[0][0] = -prices[0];g[0][0] = 0;for(int i = 1;i<n;i++){for(int j = 0;j<k+1;j++){f[i][j] = max(f[i-1][j],g[i-1][j]-prices[i]);g[i][j] = g[i-1][j];if(j>=1){g[i][j] = max(g[i-1][j],f[i-1][j-1]+prices[i]);}}}int ret = 0;for(int j = 0;j<k+1;j++){ret = max(ret,g[n-1][j]);}return ret;}
};