round1/day21&22/动规-股票问题

1. 关于动规-股票状态

分成两种状态:dp[0]持有,dp[1]不持有
没懂。。。
还在用贪心

2. 例题

lc买卖股票的最佳时机 I

思路

还是贪心的思路,待解决动规
dp[i] 在第i天卖出时的最大利润
在不在这天卖?
- 不卖:还是i-1时存储的最大利润dp[i-1]
- 卖:nums[i]-nums[buyIn]

待解决

动规的思路?

代码实现

/***/
class Solution {
    public int maxProfit(int[] prices) {
        int amount=0;
        if(prices.length==1 || prices.length==0){
            return 0;
        }

        for(int i=1;i<prices.length;i++){
            if(prices[i]>prices[i-1]){
                amount+=prices[i]-prices[i-1];
            }
        }
        return amount;
    }
}

**

lc买卖股票的最佳时机 II

思路

还是贪心的思路,待解决动规
每天两种状态:操作卖出,或不操作,然后累加amount
这样不用考虑最高点-最低点在哪里,也不用分情况成最后一个、第一个、中间段

待解决

动规的思路?
如果限制交易的次数?

代码实现

/***/

class Solution {
    public int maxProfit(int[] prices) {
        int amount=0;
        if(prices.length==1 || prices.length==0){
            return 0;
        }

        for(int i=1;i<prices.length;i++){
            if(prices[i]>prices[i-1]){
                amount+=prices[i]-prices[i-1];
            }
        }
        return amount;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值