• leetcodedp714


    /**
    <p>给定一个整数数组&nbsp;<code>prices</code>,其中 <code>prices[i]</code>表示第&nbsp;<code>i</code>&nbsp;天的股票价格 ;整数&nbsp;<code>fee</code> 代表了交易股票的手续费用。</p>
    
    <p>你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。</p>
    
    <p>返回获得利润的最大值。</p>
    
    <p><strong>注意:</strong>这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。</p>
    
    <p>&nbsp;</p>
    
    <p><strong>示例 1:</strong></p>
    
    <pre>
    <strong>输入:</strong>prices = [1, 3, 2, 8, 4, 9], fee = 2
    <strong>输出:</strong>8
    <strong>解释:</strong>能够达到的最大利润:  
    在此处买入&nbsp;prices[0] = 1
    在此处卖出 prices[3] = 8
    在此处买入 prices[4] = 4
    在此处卖出 prices[5] = 9
    总利润:&nbsp;((8 - 1) - 2) + ((9 - 4) - 2) = 8</pre>
    
    <p><strong>示例 2:</strong></p>
    
    <pre>
    <strong>输入:</strong>prices = [1,3,7,5,10,3], fee = 3
    <strong>输出:</strong>6
    </pre>
    
    <p>&nbsp;</p>
    
    <p><strong>提示:</strong></p>
    
    <ul>
    	<li><code>1 &lt;= prices.length &lt;= 5 * 10<sup>4</sup></code></li>
    	<li><code>1 &lt;= prices[i] &lt; 5 * 10<sup>4</sup></code></li>
    	<li><code>0 &lt;= fee &lt; 5 * 10<sup>4</sup></code></li>
    </ul>
    <div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>动态规划</li></div></div><br><div><li> 754</li><li> 0</li></div>
    */
    
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int maxProfit(int[] prices, int fee) {
    		int n = prices.length;
    
    		int[][] dp = new int[n+1][2];
    		for (int i = 0; i < n; i++) {
    
    			if(i-1==-1){
    				dp[i][0]=0;
    				dp[i][1]=-prices[i]-fee;
    				continue;
    			}
    			// 每次交易只需要付一次手续费
    			dp[i][0]=Math.max(dp[i-1][0],dp[i-1][1]+prices[i]);
    			dp[i][1]=Math.max(dp[i-1][1],dp[i-1][0]-prices[i]-fee);
    
    		}
    		return dp[n-1][0];
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)
    
    
    
  • 相关阅读:
    6个实例详解如何把if-else代码重构成高质量代码
    Fiddler抓包工具总结
    thinkphp5 图片上传七牛云
    mysql事件(定时任务)处理超时失效订单
    修改PhpStorm创建Php类文件时头部作者
    sed理论讲解、实战
    sed实战、find实战、grep实战
    通配符、正则表达式、python去重
    python3:requests模块-写了一点
    《流畅的Python》一副扑克牌中的难点
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/16489281.html
Copyright © 2020-2023  润新知