Question

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

Solution

The key to this problem is to only consider local minimizer point and local maximizer point. And then add up sums. In this way, we avoid processing situation that transactions happen on one day.

 public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2)
return 0;
int result = 0, localMin = prices[0], localMax = prices[0], i = 0, length = prices.length;
while (i < length) {
// To find local minimizer point
while (i < length - 1 && prices[i + 1] < prices[i])
i++;
localMin = prices[i]; // To find local maximizer point
i++;
if (i == length) {
localMax = prices[length - 1];
} else {
while (i < length - 1 && prices[i + 1] > prices[i])
i++;
if (i < length - 1)
localMax = prices[i];
else if (i == length - 1)
localMax = prices[length - 1];
}
result += (localMax - localMin);
}
return result;
}
}

Best Time to Buy and Sell Stock II 解答的更多相关文章

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

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

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

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

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

  6. 31. 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 price ...

  7. LeetCode: Best Time to Buy and Sell Stock II 解题报告

    Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...

  8. Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)

    先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...

  9. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

随机推荐

  1. linux环境下java读取sh脚本并执行

    Process process;           String cmd = "/home/ty/t.sh";//这里必须要给文件赋权限 chmod u+x fileName; ...

  2. 一个sql很多个not like的简化语句

    如: select * from table where `zongbu` not like '%北京%' and `zongbu` not like '%上海%' and `zongbu` not ...

  3. Unity PlayerPrefs类进行扩展(整个对象进行保存)

    盘子脸在制作单机游戏的时候,先以为没有好多数据需要保存本地. 就没有使用json等格式自己进行保存. 使用PlayerPrefs类,但是后面字段越来越多的时候. PlayerPrefs保存就发现要手动 ...

  4. jQuery插件Jeditable的使用(Struts2处理)

        Jeditable - Edit In Place Plugin For jQuery,是一款JQuery就地编辑插件.也就是在页面直接点击需要编辑的内容,就会自动变成文本框进行编辑.它的官方 ...

  5. 迭代 Iterate

    迭代:指按照某种顺序逐个访问列表中的每一项.比如:for语句 逐个访问: lst = ['q', 'i', 'w', 's', 'i', 'r'] for i in lst: print (i), # ...

  6. 安卓查询当前所在地天气及查询地区(城市)代码cityCode localCode

    源码可获取用户当前位置的天气情况 本代码最有价值的部分在于关于城市码的获取,我用了两个小时才将全国主要城市的编码整理成HashMap,下载即可用! 试一试:点击下载. ---------------- ...

  7. HDU4907小技巧

    原题http://acm.hdu.edu.cn/showproblem.php?pid=4907 Task schedule Time Limit: 2000/1000 MS (Java/Others ...

  8. Linux系统守护进程详解ntsysv 可以关掉那些服务

    acpid, haldaemon, messagebus, klogd,network, syslogd  以上几个服务必须开启!其他的分析如下: 1.NetworkManager,NetworkMa ...

  9. 【Android】实现动态显示隐藏密码输入框的内容

    在设置输入密码框时,有些时候需要按钮控制输入的是“明文”或者“暗文”. 这里提供一种Android实现动态显示隐藏密码输入框的内容的方法: 主要是通过设置EditText的setTransformat ...

  10. iOS新的旅程之Swift语言的学习

    好久都没有来这个熟悉而又陌生的地方啦, 想想已经有两三个月了吧,不过我相信以后还是会经常来的啦,因为忙碌的学习已经过去啦,剩下的就是要好好的总结好好的复习了,好好的熟悉下我们之前学习的知识点,将他们有 ...