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 ...
随机推荐
- [CodeForces - 678F] Lena and Queries 线段树维护凸包
大致题意: 给出三种操作 1.往平面点集中添加一个点 2.删除第i次添加的点 3.给出一个q,询问平面点集中的q*x+y的最大值 首先对于每个询问,可将z=q*x+y转化为y=z-q*x,即过点(x, ...
- Python实现QQ自动点赞
用Python做一个QQ自动点赞神器,上代码: 1 def QQZan(qq): 2 browser = webdriver.Chrome() 3 browser.maximize_window() ...
- gzez某蒟蒻lyy的博客
在gz,想去sn幻想乡也行,现在高一并且是已经高二但仍然是机房最弱,没救了 愿诸位身体健康 水平不行,写出来的东西很sb,但还是会偶尔记录一下... 数学公式测试:$\binom n{n_1\cdot ...
- 【贪心】Codeforces Round #480 (Div. 2) C. Posterized
题意:让你对[0,255]这个序列任意划分成一些不重叠的子段,每个子段的大小不超过K.给你n个不超过255的数,让你将每个数替换成它所在子段的任意一个元素,使得最终这个n个数的序列的字典序最小. p[ ...
- 【状压dp】Petrozavodsk Winter Training Camp 2018 Day 1: Jagiellonian U Contest, Tuesday, January 30, 2018 Problem E. Guessing Game
题意:给你n个两两不同的零一串,Alice在其中选定一个,Bob去猜,每次询问某一位是0 or 1.问你最坏情况下最少要猜几次. f(22...2)表示当前状态的最小步数,2表示这位没确定,1表示确定 ...
- 【2017多校训练08 1002】【HDOJ 6134】Battlestation Operational
典型的数列反演题. 运用莫比乌斯反演的一个结论 $[n = 1] = \sum_{d | n} \mu(d)$,将表达式做如下转化: $$ ans = \sum_{i=1}^n \sum_{j=1}^ ...
- 读书笔记_Effective_C++_条款三十七:绝不重新定义继承而来的缺省参数值
先看下面的例子: enum MyColor { RED, GREEN, BLUE, }; class Shape { public: ; }; class Rectangle: public Shap ...
- jquery 图片预加载
/图片无序预加载 (function($){ function Preload(imgs,fns){ this.imgs=(typeof imgs==="string")?[img ...
- linux下面mmap和setsignal函数用法
#include <stdio.h> #include <stdlib.h> #include <sys/mman.h> #include <fcntl.h& ...
- 在Linux下使用sprintf代替atoi实现整型转化为char*
程序中需要用到将整型转化为char*类型,然后将两个char*类型的变量拼接.将整型转化为char*自然想到了itoa函数: 头文件:#include <stdio.h> char *it ...