题目:

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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: ...

  5. 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 ...

  6. [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 ...

  7. 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 ...

  8. 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- ...

  9. 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 ...

随机推荐

  1. BZOJ2152 [国家集训队] 聪聪可可 [点分治]

    题目传送门 聪聪可可 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 5237  Solved: 2750[Submit][Status][Discuss ...

  2. CSS3组件化之单线箭头

    <div class="parent-box"> <div class="top-arrow"></div> <div ...

  3. 打开tcp_tw_recycle引起的一次投诉分析

    背景: 我们有个基于oauth2.0协议给第三方授权以及信息的业务,年前对接入层.业务层做了次迁移.业务架构简单介绍下: lvs接入---> nginx ---> tomcat   问题: ...

  4. KVM libvirt的CPU热添加

    一. NUMA1. NUMA 介绍    早期的时候,每台服务器都是单CPU,随着技术的发展,出现了多CPU共同工作的需求.    NUMA(Non-Uniform Memory Access,非一致 ...

  5. 深入理解ajax系列第九篇

    前面的话 jQuery提供了一些日常开发中需要的快捷操作,例如load.ajax.get和post等,使用jQuery开发ajax将变得极其简单.这样开发人员就可以将程序开发集中在业务和用户体验上,而 ...

  6. quote函数什么意思,怎么用

    转自: https://blog.csdn.net/qiqiyingse/article/details/70046543 quote函数 属于urllib库里面的一个函数 屏蔽特殊的字符.比如如果u ...

  7. 机器学习之路: python 朴素贝叶斯分类器 MultinomialNB 预测新闻类别

    使用python3 学习朴素贝叶斯分类api 设计到字符串提取特征向量 欢迎来到我的git下载源代码: https://github.com/linyi0604/MachineLearning fro ...

  8. Qt中文本编辑器实现语法高亮功能(Qscitinlla)

    Scintilla是一个免费.跨平台.支持语法高亮的编辑控件.它完整支持源代码的编辑和调试,包括语法高亮.错误指示.代码完成(code completion)和调用提示(call tips).能包含标 ...

  9. 02-c#基础之01-基础语法(一)

    1.注释符 1)注销 2) 解释 2.C#中的3种注释符 1)单行注释// 2)多行注释/*要注释的内容*/ 3)文档注释///多用来解释类或者方法 2.VS中的快捷键

  10. android viewHolder static 静态

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 不是静态内部类 会 持有 外部类的 引用.  就像经常自定义的 适配器 类 作为内部类 ...