[Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III
Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
买卖股票,只能买卖一次。那么只需要简单遍历一遍,记录利润值和买入值,每次遇到更大的利润值就更新,遇到更小的买入值就更新。这样在每个day i处计算出的利润值为在第i天卖出所能得到的最大利润。不断更新这个利润,最后得到的即为最大利润值。
public int maxProfit(int[] prices) {
if(prices.length<=0)
return 0;
int buy = prices[0];
int benifit = 0;
for(int i=0;i<prices.length;i++) {
benifit = Math.max(benifit, prices[i]-buy);
buy = Math.min(buy, prices[i]);
}
return benifit;
}
Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
无限次买卖股票,看似更难,实际更简单了。只需要得到所有攀升段的总值,即为总最大利润。那么只要第二天值比第一天更贵,则把它们的差值加到总利润。
public int maxProfit(int[] prices) {
int re = 0;
for(int i=1;i<prices.length;i++) {
if(prices[i]>prices[i-1])
re += prices[i]-prices[i-1];
}
return re;
}
Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
discussion里有人提出了一个dp方法适用于k次买卖的情况,很好理解。这里就直接照搬他的思路了:
// f[k, ii] 表示直到 prices[ii] 的最大利润 在最多k次交易的情况下.
// 转移函数:f[k, ii] = max(f[k, ii-1], prices[ii] - prices[jj] + f[k-1, jj]) { jj in range of [0, ii-1] } = max(f[k, ii-1], prices[ii] + max(f[k-1, jj] - prices[jj]))
// 基本情况:f[0, ii] = 0; 0次交易将无利润
// 基本情况:f[k, 0] = 0; 如果只有一天也将无利润
public int maxProfit(int[] prices) {
if(prices.length<=1)
return 0;
int k=2;
int[][] dp = new int[k+1][prices.length];
int re = 0;
for(int i=1;i<=k;i++) {
int temp = dp[i-1][0]-prices[0];
for(int j=1;j<prices.length;j++) {
temp = Math.max(temp, dp[i-1][j]-prices[j]);
dp[i][j] = Math.max(dp[i][j-1], prices[j]+temp);
}
}
return dp[k][prices.length-1];
}
[Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III的更多相关文章
- [LeetCode][Java] Best Time to Buy and Sell Stock IV
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- [LeetCOde][Java] Best Time to Buy and Sell Stock III
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...
- [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法
题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...
- Best Time to Buy and Sell Stock I,II,III [leetcode]
Best Time to Buy and Sell Stock I 你只能一个操作:维修preMin拍摄前最少发生值 代码例如以下: int maxProfit(vector<int> & ...
- Best Time to Buy and Sell Stock I II III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- 解题思路:best time to buy and sell stock i && ii && iii
这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富? best time to buy and sell stock i: 最多允许买卖一次 b ...
随机推荐
- java 获取classpath下文件多种方式
java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...
- Python之路【第十六篇】:Django【基础篇】
Python之路[第十六篇]:Django[基础篇] Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了O ...
- Myeclipse解决dubbo标签不识别问题
Myeclipse解决dubbo标签不识别问题,引入dubbo.xsd文件,即可:
- CentOS6开启FTP及telnet服务教程
先来开通CentOS6的FTP服务吧.telnet服务也一并学习学习吧.在安装好CentOS以后,需要设置Ftp和Telnet服务文件,才能启动Ftp和Telnet服务,可以通过远程控制进行开启. 开 ...
- Bootstrap修改input file默认样式
html部分 <div class="form-group"> <label class="col-sm-3 control-label"&g ...
- Android 中的 Service 全面总结(转载)
转载地址:http://www.cnblogs.com/newcj/archive/2011/05/30/2061370.html 感谢作者 Android 中的 Service 全面总结 1.Ser ...
- Python 2.7_First_try_爬取阳光电影网_20161206
之前看过用Scrapy 框架建立项目爬取 网页解析时候用的Xpath进行解析的网页元素 这次尝试用select方法匹配元素 1.入口爬取页面 http://www.ygdy8.com/index.ht ...
- MySQL_关于用嵌套表计算的可以不用 20161205
计算求和类的指标,其实用不到嵌套表,比如计算各城市产品分类的订单额. 如果要计算不重复的指标 比如一个用户一天下了多个订单 用这样的表计算一天有多少用户下单 这个用户肯定是去重的 下多个订单也应该视为 ...
- NOI 2001 食物链(eat)
1074 食物链 2001年NOI全国竞赛 时间限制: 3 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description ...
- LINUX内核分析期末总结
韩玉琪 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.课程总结 1 ...