题目描述:
解法一:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n=prices.size();
if(n<2) return 0;
int minp=prices[0];
int res=0;
for(int i=1;i<n;i++){
if(prices[i]>minp){
res=max(res,prices[i]-minp);
}
else
minp=prices[i];
}
return res;
}
};
解法二(传统动态规划):
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n=prices.size();
if(n<2) return 0;
int dp[n][2];
for(int i=0;i<n;i++){
if(i==0){
dp[i][0]=0;
dp[i][1]=-prices[i];
}
else{
dp[i][0]=max(dp[i-1][0],dp[i-1][1]+prices[i]);
dp[i][1]=max(dp[i-1][1],-prices[i]);
}
}
return dp[n-1][0];
}
};
解法三(改进动态规划):
class Solution {
public:
int maxProfit(vector<int>& prices) {
int dp_i0=0,dp_i1=INT_MIN;
for(int i=0;i<prices.size();i++){
dp_i0=max(dp_i0,dp_i1+prices[i]);
dp_i1=max(dp_i1,-prices[i]);
}
return dp_i0;
}
};
题目描述:
解法一:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n=prices.size();
int i=0,j;
int res=0;
while(i<n-1){
while(i<n-1&&prices[i]>=prices[i+1]) i++; //谷
j=i;
while(j<n-1&&prices[j]<=prices[j+1]) j++; //峰
res+=prices[j]-prices[i];
i=j;
}
return res;
}
};
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n=prices.size();
if(n<2) return 0;
int maxval=0;
int valley=prices[0],peak=prices[0];
int i=0;
while(i<n-1){
while(i<n-1&&prices[i]>=prices[i+1])
i++;
valley=prices[i]; //谷
while(i<n-1&&prices[i]<=prices[i+1])
i++;
peak=prices[i]; //峰
maxval+=peak-valley;
}
return maxval;
}
};
解法二:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n=prices.size();
int res=0;
for(int i=0;i<n-1;i++){
if(prices[i]<prices[i+1]){
res+=prices[i+1]-prices[i]; //分段加
}
}
return res;
}
};
解法三(传统动态规划):
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n=prices.size();
if(n<2) return 0;
int dp[n][2];
for(int i=0;i<n;i++){
if(i==0){
dp[i][0]=0;
dp[i][1]=-prices[i];
}
else{
dp[i][0]=max(dp[i-1][0],dp[i-1][1]+prices[i]);
dp[i][1]=max(dp[i-1][1],dp[i-1][0]-prices[i]);
}
}
return dp[n-1][0];
}
};
解法四(改进动态规划):
class Solution {
public:
int maxProfit(vector<int>& prices) {
int dp_i0=0,dp_i1=INT_MIN;
for(int i=0;i<prices.size();i++){
int temp=dp_i0;
dp_i0=max(dp_i0,dp_i1+prices[i]);
dp_i1=max(dp_i1,temp-prices[i]);
}
return dp_i0;
}
};
题目描述:
解法一(最初的动态规划):
int maxProfit(vector<int>& prices) {
int n = prices.size();
vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(3, vector<int>(2, 0)));
dp[0][0][1] = dp[0][1][1] = dp[0][2][1] = INT_MIN;
for (int i = 1; i<n + 1; i++) {
for (int k = 1; k<3; k++) {
dp[i][k][0] = max(dp[i - 1][k][0], dp[i - 1][k][1] + prices[i-1]);
dp[i][k][1] = max(dp[i - 1][k][1], dp[i - 1][k-1][0] - prices[i-1]);
}
}
return dp[n][2][0];
}
解法二(进一步的动态规划):
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if(n<2) return 0;
vector<vector<vector<int>>> dp(n, vector<vector<int>>(3, vector<int>(2, 0)));
for (int i = 0; i<n; i++) {
for (int k = 1; k<3; k++) {
if (i == 0) {
dp[i][k][0] = 0;
dp[i][k][1] = -prices[i];
}
else {
dp[i][k][0] = max(dp[i - 1][k][0], dp[i - 1][k][1] + prices[i]);
dp[i][k][1] = max(dp[i - 1][k][1], dp[i - 1][k - 1][0] - prices[i]);
}
}
}
return dp[n - 1][2][0];
}
};
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n=prices.size();
if(n<2) return 0;
int dp[n][3][2];
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++){
for(int k=1;k<3;k++){
if(i==0){
dp[i][k][0]=0;
dp[i][k][1]=-prices[i];
}
else{
dp[i][k][0]=max(dp[i-1][k][0],dp[i-1][k][1]+prices[i]);
dp[i][k][1]=max(dp[i-1][k][1],dp[i-1][k-1][0]-prices[i]);
}
}
}
return dp[n-1][2][0];
}
};
解法三(较优的动态规划):
class Solution {
public:
int maxProfit(vector<int>& prices) {
int dp_i10=0,dp_i20=0,dp_i11=INT_MIN,dp_i21=INT_MIN;
for(int i=0;i<prices.size();i++){
dp_i20=max(dp_i20,dp_i21+prices[i]);
dp_i21=max(dp_i21,dp_i10-prices[i]);
dp_i10=max(dp_i10,dp_i11+prices[i]);
dp_i11=max(dp_i11,-prices[i]);
}
return dp_i20;
}
};
解法一:
class Solution {
public:
int helper(vector<int>& prices){
int res=0;
for(int i=1;i<prices.size();i++){
if(prices[i]>prices[i-1])
res+=prices[i]-prices[i-1];
}
return res;
}
int maxProfit(int k, vector<int>& prices) {
int n=prices.size();
if(n<2) return 0;
if(k>n/2) return helper(prices);
int dp[n][k+1][2];
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++){
for(int f=1;f<=k;f++){
if(i==0){
dp[i][f][0]=0;
dp[i][f][1]=-prices[i];
}
else{
dp[i][f][0]=max(dp[i-1][f][0],dp[i-1][f][1]+prices[i]);
dp[i][f][1]=max(dp[i-1][f][1],dp[i-1][f-1][0]-prices[i]);
}
}
}
return dp[n-1][k][0];
}
};
解法二:
class Solution {
public:
int helper(vector<int>& prices){
int dp_i0=0,dp_i1=INT_MIN;
for(int i=0;i<prices.size();i++){
int temp=dp_i0;
dp_i0=max(dp_i0,dp_i1+prices[i]);
dp_i1=max(dp_i1,temp-prices[i]);
}
return dp_i0;
}
int maxProfit(int k, vector<int>& prices) {
int n=prices.size();
if(n<2) return 0;
if(k>n/2) return helper(prices);
int dp[n][k+1][2];
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++){
for(int m=1;m<=k;m++){
if(i==0){
dp[i][m][0]=0;
dp[i][m][1]=-prices[i];
}
else{
dp[i][m][0]=max(dp[i-1][m][0],dp[i-1][m][1]+prices[i]);
dp[i][m][1]=max(dp[i-1][m][1],dp[i-1][m-1][0]-prices[i]);
}
}
}
return dp[n-1][k][0];
}
};
题目描述:
解法一:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n=prices.size();
if(n<2) return 0;
int dp_i0=0,dp_i1=INT_MIN;
int dp[n];
for(int i=0;i<n;i++){
if(i<2){
dp_i0=max(dp_i0,dp_i1+prices[i]);
dp_i1=max(dp_i1,-prices[i]);
dp[i]=dp_i0;
}
else{
dp_i0=max(dp_i0,dp_i1+prices[i]);
dp_i1=max(dp_i1,dp[i-2]-prices[i]);
dp[i]=dp_i0;;
}
}
return dp_i0;
}
};
解法二:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int dp_i0=0,dp_i1=INT_MIN;
for(int i=0;i<prices.size();i++){
int temp=dp_i0;
dp_i0=max(dp_i0,dp_i1+prices[i]);
dp_i1=max(dp_i1,temp-prices[i]);
}
return dp_i0;
}
题目描述:
解法:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int dp_i0=0,dp_i1=INT_MIN;
for(int i=0;i<prices.size();i++){
int temp=dp_i0;
dp_i0=max(dp_i0,dp_i1+prices[i]);
dp_i1=max(dp_i1,temp-prices[i]);
}
return dp_i0;
}
};
讲的很好,感谢!