1. 题目要求

Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee representing a transaction fee.

You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. You may not buy more than 1 share of a stock at a time (ie. you must sell the stock share before you buy again.)

Return the maximum profit you can make.

2. Code

DP解法:

  pay用于计算买入价格

  sell用于计算当前利润

  DP的算法是,假定我们当前买入第一天的股票,开销是 【第一天价格】+手续费。利润0。接下来是类似滑窗的设计,数组中,只有i-1和i中的数值是有效的,其中i-1记录着之前的开销,i位置则是当前i-1处的利润-i处的价格+手续费,如果赚到的钱在买了新股,并交了手续费后赚到了钱,就买入。i位记录新的数值,否则就不买,i位记录之前的开销。这样保证买在低点。(开销为负)

  接下来的利润为开销+当前卖出价,i-1初始为0,i处则是i-1买入价格+i卖出价格,即当前的balance,这样计算可以保证卖在高点。

public int maxProfit(int[] prices, int fee) {
int res = 0;
int size = prices.length;
int[] pay = new int[size];
int[] sell = new int[size]; pay[0] = 0 - prices[0] - fee; for(int i = 0 ; i < prices.length ; i ++) { pay[i] = Math.max(pay[i - 1], sell[i - 1] - prices[i] - fee);
sell[i] = Math.max(sell[i - 1], pay[i - 1] + prices[i]); } return sell[size - 1];
}

Leetcode 714 - Node的更多相关文章

  1. [LeetCode] Delete Node in a BST 删除二叉搜索树中的节点

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  2. [LeetCode] Delete Node in a Linked List 删除链表的节点

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  3. Leetcode Delete Node in a Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  4. Leetcode: Delete Node in a BST

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  5. LeetCode Delete Node in a Linked List (删除链表中的元素)

    题意:给一个将要删除的位置的指针,要删除掉该元素.被删元素不会是链尾(不可能删得掉). 思路:将要找到前面的指针是不可能了,但是可以将后面的元素往前移1位,再删除最后一个元素. /** * Defin ...

  6. LeetCode——Delete Node in a Linked List

    Description: Write a function to delete a node (except the tail) in a singly linked list, given only ...

  7. [LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee 买卖股票的最佳时间有交易费

    Your are given an array of integers prices, for which the i-th element is the price of a given stock ...

  8. leetcode 714. 买卖股票的最佳时机含手续费

    继承leetcode123以及leetcode309的思路,,但应该也可以写成leetcode 152. 乘积最大子序列的形式 class Solution { public: int maxProf ...

  9. LeetCode——714. 买卖股票的最佳时机含手续费.

    给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 :非负整数 fee 代表了交易股票的手续费用. 你可以无限次地完成交易,但是你每次交易都需要付手续费.如果你已经购买了一个 ...

随机推荐

  1. 【做题】CF119D. String Transformation——KMP

    题意:有两个字符串\(a,b\),下标从\(0\)开始.求数对\((i,j)\)满足\(a[i+1:j] + r(a[j:n]) + r(a[0:i+1]) = b\),其中\(r(s)\)表示字符串 ...

  2. golang解析json配置文件

    安装 go get github.com/akkuman/parseConfig 使用说明 环境假设 . ├── config.go ├── config.json config.json内容 { & ...

  3. 【Runtime Error】打开Matlib7.0运行程序报错的解决办法

    1.在C盘建立一个文件夹temp,存放临时文件: 2.右键我的电脑-属性-高级系统设置-环境变量-系统变量,将TEMP.TMP的值改成C:\temp: 3.还是在第2步那里,新建变量,变量名称为BLA ...

  4. C++中的string常用函数用法

    标准c++中string类函数介绍   注意不是CString 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而 ...

  5. [thymeleaf] - 1.Thymeleaf是什么

    Thymeleaf是⾯向Web和独⽴环境的现代服务器端Java模板引擎,能够处 理HTML,XML,JavaScript,CSS甚⾄纯⽂本. Thymeleaf旨在提供⼀个优雅的.⾼度可维护的创建模板 ...

  6. LightOJ 1258 Making Huge Palindromes(KMP)

    题意 给定一个字符串 \(S\) ,一次操作可以在这个字符串的右边增加任意一个字符.求操作之后的最短字符串,满足操作结束后的字符串是回文. \(1 \leq |S| \leq 10^6\) 思路 \( ...

  7. Dispose in c#

    在标准的Dispose模式中,真正的IDisposable接口的Dispose方法并没有做实际的清理工作,它其实是调用了下面的这个带bool参数且受保护的的虚方法: protected virtual ...

  8. ifconfig 输出里没有IP地址

    转载: http://blog.csdn.net/johnstrive/article/details/5625121 inet addr:....Bcast:.....Mask:255.255.25 ...

  9. js中获取当前浏览器类型

    本文为博主原创,转载请注明出处: 在应用POI进行导出时,先应用POI进行数据封装,将数据封装到Excel中,然后在进行download下载操作,从而完成 POI导出操作.由于在download操作时 ...

  10. 无法启动此程序,因为计算机中丢失api-ms-win-crt-runtime-|1-1-0.dll

    今天想把自己电脑上的python2换成python3时,安装完python3后,命令行启动时需要出现了上述错误,在网上查了资料后应该是库文件遭到了破坏,于是我下了一个东西安装后就解决了,如果出现了此问 ...