[leetcode]_Best Time to Buy and Sell Stock I II
一个系列三道题,我都不会做,google之答案。过了两道,第三道看不懂,放置,稍后继续。
一、Best Time to Buy and Sell Stock I
题目:一个数组表示一支股票的价格变换。要求只买卖一次,获得最大收益。
思路:一开始我认为是寻找最大、最小值,但由于最大值不一定总是出现在最小值的后面,因此WA。
参考思路:DP。对第i个价格,减去前i-1个价格中的最小值(保证该收益是在第i个价格卖出的最大收益),其收益与之前获得的最大收益相比。
代码:
1 public int maxProfit(int[] prices) { 2 int len = prices.length ; 3 if(len < 2) return 0; 4 5 int min = prices[0] ; 6 int maxProfit = 0; 7 8 for(int i = 1 ; i < len ; i++){ 9 int temp = prices[i] - min; //当前值减去前i-1个值的最小值 10 if(maxProfit < temp) maxProfit = temp; //更新最大收益 11 if(prices[i] < min) min = prices[i]; //看是否需要更新前i个值的min值,用于下次循环 12 } 13 14 return maxProfit; 15 }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
二、Best Time to Buy and Sell Stock II
题目:在上一题的基础上,允许对一支股票任意次买卖,(同一时间可先卖出再马上买入),同样求最大收益。
思路:如果第i个价格大于第i-1个价格,则将此部分收益加入到最大收益中,因为可以在第i个价格处马上卖出再马上买入。
代码:
1 public int maxProfit(int[] prices) { 2 int profit = 0; 3 for(int i = 1 ; i < prices.length ; i++){ 4 if(prices[i] > prices[i - 1]) profit += prices[i] - prices[i - 1]; 5 } 6 return profit; 7 }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
三、待续
转载于:https://www.cnblogs.com/glamourousGirl/p/3753849.html
[leetcode]_Best Time to Buy and Sell Stock I II相关推荐
- leetcode: Best Time to Buy and Sell Stock 系列
leetcode: Best Time to Buy and Sell Stock 系列 一系列包括: - Best Time to Buy and Sell Stock Ⅰ - Best Time ...
- Leetcode Best Time to Buy and Sell Stock III
Leetcode Best Time to Buy and Sell Stock III,本算法的关键为找出其动态子结构.可以发现,序列中的最小值可以做为其的一个分割,令左边序列为left,右边的序列 ...
- Leetcode Best Time to Buy and Sell Stock
Leetcode Best Time to Buy and Sell Stock 相关代码,本题使用dp算法完成,本算应该算得上一个经典的dp算法题. #include <iostream> ...
- Best Time to Buy and Sell Stock I II III IV (第四周 动态规划)
Best Time to Buy and Sell Stock I II III IV (第四周 动态规划) Best Time to Buy and Sell Stock I Say you hav ...
- LeetCode Best Time to Buy and Sell Stock II
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题目: Say you have an array ...
- [LeetCode] 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 ...
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告
原文地址:http://liangjiabin.com/blog/2015/04/leetcode-best-time-to-buy-and-sell-stock.html Best Time to ...
- LeetCode Best Time to Buy and Sell Stock(dp)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LeetCode Best Time to Buy and Sell Stock with Cooldown(动态规划)
问题:给定一个整数数组,其中第i个元素表示第i天的股票价格.可以多次买卖股票,卖出股票后,无法在第二天买入股票. 思路:动态规划法,用dp(i,j)表示第i天持有或者不持有股票的最大利润.用dp(i, ...
最新文章
- Eclipse SVN插件检出Src下面的包变成了文件夹解决
- pandas删除某列有空值的行_Python-零基础学习Pandas知识点整理(2)
- .NET Core微服务开发选项
- Winform DataGridView数据绑定问题
- Nucleic Acids Research | NONCODE数据库V6版发布,涵盖全面的动植物长非编码RNA注释
- [源码]java.lang.reflect.Proxy
- 原价19万的美系插混,2年后落地只要11万~15万,微蓝6 PHEV为啥这么惨
- 特斯拉Model 3再次停工!这一次还是因为自动化机器人……
- 软件测试linux笔试题目,linux基础面试题
- SpringMVC之安全性(一)
- 电视android怎么连接手机助手下载,电视与手机怎么连接?最实用的三种小技巧!...
- 适合Java初学者练手的网站
- 产品读书《用户故事与敏捷方法》
- OPENSSL s_client 实例测试- SSL连接单向验证
- 微信个人好友增加到10000人啦!
- Cathy推荐Java面试题
- Uaexpert操作手册
- Ray在蚂蚁大规模生成落地中的优化与实践
- C语言程序中紧急情况,《C语言及程序设计》实践参考——紧急救援
- 苹果尼玛 你在逗我么
热门文章
- C++ 变量和常量
- opencv-api filter2D
- Pandas GroupBy对象 索引与迭代
- 一步步实现SDDC-分布式交换机入门
- 关于注册表、组策略、设置灰色联想
- Java基础学习总结(134)——JDK 11 是否值得更新的思考
- Spring Boot学习总结(12)——Spring Boot Admin 2.0应用监控示例
- Kubernetes学习总结(2)——Kubernetes设计架构
- java编写程序_对输入的年月日_给出该天是该年的第多少天?_编写Java程序。当以年月日的格式输入一个日期时,输出该年是否是闰年,该月有几天,该日是星期几...
- 一分钟看懂mysql_冬天发财树“最怕”它,遇上就烂根,一分钟看懂就能养好