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 ...
随机推荐
- Date日期
当我们只需要一个日期时,或从系统取得,或从数据库查询,都可以放入一个Date对象. 当我们需要对Date进行详细分析,获取其中的年月日分秒各个部分的信息,用Calendar类. 当我们需要对一个字符串 ...
- ZJOI2018 day2游记
省选讲课:还不错吧 ZJOI RP++ Day2: 题出的好!覆盖知识点广,题目又着切合实际的背景,解法比较自然. 给出题人点赞 ! 意识模糊地点开了题(考前不熬夜似乎还是很困qaq) T1:前一个小 ...
- 用C读取系统明文(附源码)
从一好朋友那得到一个好东西 可以读取系统明文 请用vc++ 6.0编译 #include <windows.h> #include <stdio.h> // // Vsbat[ ...
- java的多线程之四(线程的操作)
本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17560467,转载请注明. 线程中断 线程中断涉及到三个方法,如下 ...
- Kibana安装及使用说明
Kibana安装及使用说明 Kibana是一个针对Elasticsearch的开源分析及可视化平台,用来搜索.查看交互存储在Elasticsearch索引中的数据. 官方地址:https://www. ...
- SpringBoot静态资源目录
在web开发中,静态资源的访问是必不可少的,如:图片.js.css 等资源的访问. SpringBoot对静态资源访问提供了很好的支持,基本使用默认配置就能满足开发需求. 在传统的web项目中静态资源 ...
- zoj 2587 判断最小割的唯一性
算法: 先求出残量网络,计算出从src能够到的点集A,再求出能够到dst的点集B,如果所有点都被访问到了,那么割就是唯一的,即(A,B),否则(A,V-A)和(V-B,B)都是最小割. (注意因为割的 ...
- A server is already running. Check /home/peter/stock/tmp/pids/server.pid. Exiting【Xshell 运行rails s 报错】
- jmeter3.3—插件管理器的安装
一.介绍JMeter Plugins 一直以来, JMeter Plugins 为我们提供了很多高价值的JMeter插件,比如: 用于服务器性能监视的 PerfMon Metrics Collecto ...
- 使用Layui和Vue实现分页
原理就是利用Layui的分页组件和Vue组件的模板渲染功能. 我下面直接贴代码,比较直观. index.html <!DOCTYPE html> <html> <head ...