Best Time to Buy and Sell Stock II leetcode java
题目:
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).
题解:
简单的方法就是一旦第二天的价格比前一天的高,就在前一天买入第二天卖出。代码如下:
- 1 public int maxProfit(int[] prices) {
- 2 int total = 0;
- 3 for (int i=1; i< prices.length; i++) {
- 4 if (prices[i]>prices[i-1]){
- 5 int profit = prices[i]-prices[i-1];
- 6 total += profit;
- 7 }
- 8 }
- 9 return total;
- }
但是这个会违反“不能同一天买卖的规则”,例如3天的价格分别为1,2,3,那么按上述算法就会在2那天同一天买卖了。。。
正确的做法是: 第1天买第3天卖。
虽然两种方法数字结果是对的,但是逻辑不一样。。
不过虽然这么说,这道题的本事仍然是让你找最大利润,并没有让你明确哪天买哪天卖。
所以对面试官你仍然可以说,这种方法只是帮助我找到最大利润,我并没有说是要在同一天买卖,只是计算:所有第二天比前一天大的差值的合,我是为了找最大利润而已(画个趋势图讲解一下就行了。。)。
不过如果不是那样略有点投机取巧的话,干嘛非要每一次有一点点增长就加总和,带来不必要的解释麻烦?
何不先找到递减的局部最低点,再找到递增的局部最高点,算一次加和,然后再这么找? 这样就能保证买卖不会在同一天了。。
代码如下:
- 1 public static int maxProfit(int[] prices) {
- 2 int len = prices.length;
- 3 if(len <= 1)
- 4 return 0;
- 5
- 6 int i = 0;
- 7 int total = 0;
- 8 while(i < len - 1){
- 9 int buy,sell;
- //寻找递减区间的最后一个值(局部最小点)
- while(i+1 < len && prices[i+1] < prices[i])
- i++;
- //局部最小点作为买入点
- buy = i;
- //找下一个点(卖出点至少为下一个点)
- i++;
- //不满足。。继续往下找递增区间的最后一个值(局部最高点)
- while(i<len && prices[i] >= prices[i-1])
- i++;
- //设置卖出点
- sell = i-1;
- //计算总和
- total += prices[sell] - prices[buy];
- }
- return total;
- }
感谢JustdoIT(http://www.cnblogs.com/TenosDoIt/p/3436457.html)的方法,要不然网上都是弥漫着第一个不太好说明白的,但是看起来简洁的方法。。这个方法更令人信服。
Best Time to Buy and Sell Stock II leetcode java的更多相关文章
- leetcode:122. Best Time to Buy and Sell Stock II(java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...
- Best Time to Buy and Sell Stock II ——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 122. Best Time to Buy and Sell Stock II ——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Best Time to Buy and Sell Stock II [LeetCode]
Problem Description: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ Basic idea: ...
- Best Time to Buy and Sell Stock III leetcode java
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- [LintCode] 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 al ...
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- Leetcode-122 Best Time to Buy and Sell Stock II
#122 Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the pric ...
随机推荐
- Spring单例 和 Scope注解
关键字 @Scope @Qualifier Singleton 单例 Spring是单例模式.结合Springboot的例子. Controller @Autowired private Tes ...
- 移动端h5下ul实现横向滚动css代码
html代码: <ul id="category"> <li>品牌团</li> <li>美体个护</li> <li ...
- python实现括号匹配
1.用一个栈[python中可以用List]就可以解决,时间和空间复杂度都是O(n) # -*- coding: utf8 -*- # 符号表 SYMBOLS = {'}': '{', ']': '[ ...
- 机器学习之路:python k近邻回归 预测波士顿房价
python3 学习机器学习api 使用两种k近邻回归模型 分别是 平均k近邻回归 和 距离加权k近邻回归 进行预测 git: https://github.com/linyi0604/Machine ...
- 数据储存为base64编码,如何实现模糊搜索
假设字段title存储的是经过base64编码后的字符串,$key是存搜索关键字的变量 则普通的查询方法, select * from 表名 where title like '$key'; 无法正确 ...
- java知识点总结
一.java 1.容器 1)List Java中ArrayList和LinkedList区别 2)Set 理解HashSet及使用 HashMap和HashSet的区别 3Map HashMap的容量 ...
- [UVa10296]Jogging Trails
题目大意: 中国邮递员问题. 给你一个无向带权连通图,求经过所有边并返回起点的最短路径. 思路: Edmonds-Johnson算法. 显然,当原图为欧拉图时,答案即为其欧拉回路的长度. 考虑原图不存 ...
- 【HDU】2866:Special Prime【数论】
Special Prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- 【洛谷】2324:[SCOI2005]骑士精神【IDA*】
P2324 [SCOI2005]骑士精神 题目描述 输入输出格式 输入格式: 第一行有一个正整数T(T<=10),表示一共有N组数据.接下来有T个5×5的矩阵,0表示白色骑士,1表示黑色骑士,* ...
- python开发_tkinter_自己做的猜数字小程序
读到这篇文章[python 3.3下结合tkinter做的猜数字程序]的时候,就复制了代码,在自己机器上面跑了一下 源程序存在一个缺陷: 即当用户答对了以后,用户再点击'猜'按钮,最上面的提示标签还会 ...