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 ...
随机推荐
- Java拾遗补缺
JDK9的lib目录下已经不再包含dt.jar和tool.jar.
- JSP九大内置对象简介
1.out对象,是JspWriter类的实例,是向客户端输出内容常用的对象 方法: void println() 向客户端打印字符串 void clear() 清除缓冲区的内容,如果在flush之后调 ...
- Linux-CentOs7-svn安装及钩子配置
做个svn的教程 首先进入test目录下,新建一个svn目录,准备做svn测试cd /testmkdir svncd svn然后使用yum安装svn,这里就不使用编译安装了,这玩意只要能用就行,版本无 ...
- 求N!末尾所得数字0的个数
题目:给定一个整数N ,那么N 的阶乘N !末尾有多少个0呢? 例如:N = 10,N! = 3628800,所以N!末尾就有2个零. 分析:如果直接先算出N!阶乘,很容易导致内存溢出.显然,直接算出 ...
- UVALive 5058 Counting BST 数学
B - Counting BST Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit S ...
- LR监控Apache资源
前提本文使用的是lampp环境下自带的Apache服务 步骤1.修改Apache中Httpd.conf文件,添加如下代码:文件位置为:/opt/lampp/etc/httpd.conf,如下图: &l ...
- select 语句的执行顺序
select 语句的执行顺序 借用ItZik Ben-Gan.Lubor Kollar.Dejan Sarka所著的<Sql Server 2005 技术内幕:T-SQL查询>的一段话足以 ...
- 移动平台的WebApp之Meta标签
对于桌面平台web布局中大家对meta标签再熟悉不过了,它永远位于 head 元素内部,对做SEO的朋友一定对meta有种特殊的感情吧,今天我们就来说说移动平台的meta标签,在移动平台meta标签究 ...
- 从零开始搭建linux下laravel 5.5所需环境(二)
我们已经装好了nginx,现在我们开始装mysql和php 我们同样使用yum来安装. 先安装MySQL软件(客户端.服务器端.依赖库) yum install -y mysql mysql-serv ...
- POJ 3580 SuperMemo (splay tree)
SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6841 Accepted: 2268 Case Ti ...