Leetcode 714 - Node
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的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- LeetCode Delete Node in a Linked List (删除链表中的元素)
题意:给一个将要删除的位置的指针,要删除掉该元素.被删元素不会是链尾(不可能删得掉). 思路:将要找到前面的指针是不可能了,但是可以将后面的元素往前移1位,再删除最后一个元素. /** * Defin ...
- 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 ...
- [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 ...
- leetcode 714. 买卖股票的最佳时机含手续费
继承leetcode123以及leetcode309的思路,,但应该也可以写成leetcode 152. 乘积最大子序列的形式 class Solution { public: int maxProf ...
- LeetCode——714. 买卖股票的最佳时机含手续费.
给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 :非负整数 fee 代表了交易股票的手续费用. 你可以无限次地完成交易,但是你每次交易都需要付手续费.如果你已经购买了一个 ...
随机推荐
- loj#2305. 「NOI2017」游戏 2-sat
链接 https://loj.ac/problem/2305 https://www.luogu.org/problemnew/show/P3825 思路 3-sat神马的就不要想了,NP问题 除去x ...
- 【教程】Git在Eclipse中的安装和基本使用
一.安装 点击 Help->Install New Software->add 安装地址为:http://download.eclipse.org/egit/updates/ 选择插件 ...
- IDEA新建一个Project和Module的问题
- MD5+salt 工具类
import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.securit ...
- dfs序七个经典问题
update-2018.07.23: 原文问题五思路描述有误,已更正. 参考自:<数据结构漫谈>-许昊然 dfs序是树在dfs先序遍历时的序列,将树形结构转化成序列问题处理. dfs有一个 ...
- (转载)http和socket之长连接和短连接区别
TCP/IPTCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层.在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议.在传输层中有TCP协议与UDP协议.在应用层有: ...
- jsp的九大内置对象及作用
内置对象名 类型 request HttpServletRequest ...
- HDU 5119 Happy Matt Friends(递推)
http://acm.hdu.edu.cn/showproblem.php?pid=5119 题意:给出n个数和一个上限m,求从这n个数里取任意个数做异或运算,最后的结果不小于m有多少种取法. 思路: ...
- 2nd,Python基础2——02
1 列表.元组操作 列表可以对数据实现最方便的存储.修改等操作 names = ['Jack', 'Leon','Eric'] 通过下表访问列表中的元素,下标从0开始计数 names = ['Jack ...
- JS中的document.title可以获取当前网页的标题
<!DOCTYPE html> <html> <head> <title>jb51.net</title> </head> &l ...