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. c# list exists(contains) delegate 委托判断 元素是否在LIST中存在

    static void Main(string[] args)        {            List<GoodsInfo> list = new List<GoodsIn ...

  2. 《Java程序员面试笔试宝典》之为什么Java中有些接口没有任何方法

    由于Java不支持多重继承,即一个类只能有一个父类,为了克服单继承的缺点,Java语言引入了接口这一概念.接口是抽象方法定义的集合(接口中也可以定义一些常量值),是一种特殊的抽象类.接口中只包含方法的 ...

  3. (转)25个增强iOS应用程序性能的提示和技巧--中级篇

    在性能优化时,当你碰到一些复杂的问题,应该注意和使用如下技巧: 9.重用和延迟加载View10.缓存.缓存.缓存11.考虑绘制12.处理内存警告13.重用花销很大的对象14.使用Sprite Shee ...

  4. Myeclipse安装破解

  5. POJ 1469 ZOJ1140 二分匹配裸题

    很裸,左点阵n,右点阵m 问最大匹配是否为n #include <cstdio> #include <cstring> #include <vector> usin ...

  6. linux用户创建删除以及文件权限查看修改

    一. 1.查看用户 命令如下:whoami 2.创建用户 创建用户命令:sudo adduser hello 超级用户是 root 删除用户名命令:sudo deluser hello --remov ...

  7. Selenium WebDriver 学习笔记

    1. 打开VS2012 2. 新建工程(单元测试工程或控制台程序都可以, 看需求) 3. 工具->NuGet程序包管理器->程序包管理器控制台 4. 输入"Install-Pac ...

  8. [转]CSS vertical-align属性详解 作者:黄映焜

      CSS vertical-align属性详解 posted @ 2014-08-26 17:44 黄映焜   前言:关于vertical-align属性. 实践出真知. 垂直居中. 第二种用法. ...

  9. JS正则表达式收集篇

    1.验证只可输入整数或小数点后两位的数字:/^([1-9]{1}|[1-9]{1}[0-9])+(.[1-9]{1,2})?$/ 2.验证Email: /^([a-zA-Z0-9]+[_|\_|\.] ...

  10. ios开发 block语句块

    ios开发 block语句块 1.block 理解为匿名函数 2.block变量的定义 //定义block变量,^表示定义block //技巧:函数名左右加括号,在函数名前面在加^ void (^bl ...