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

 
Example

Given an example [2,1,2,0,1], return 2

解法一:

 class Solution {
public:
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
int maxProfit(vector<int> &prices) {
int total = ;
if (!prices.empty()) {
for (int i = ; i < prices.size() - ; i++) {
if (prices[i + ] > prices[i]) {
total += (prices[i + ] - prices[i]);
}
}
}
return total;
}
};

greedy的思想,只要是有收益就加上。

参考@NineChapter 的代码

解法二:

 public class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
for (int i = 0; i < prices.length - 1; i++) {
int diff = prices[i + 1] - prices[i];
if (diff > 0) {
profit += diff;
}
}
return profit;
}
}

和解法一思路一样,看起来更舒服一些的代码风格。

参考@NineChapter 的代码

150. Best Time to Buy and Sell Stock II【medium】的更多相关文章

  1. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  2. Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

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

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

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

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

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

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

随机推荐

  1. 【项目 部署】部署项目web context root,项目跟路径跟项目实际名称不符

    项目如下: 但是部署到 tomcat下后,tomcat中项目根目录变成: 并且项目启动起来之后, 解决方法: 发现此处 不能更改,然后 在workspace下找到本项目,定位到.setting文件夹下 ...

  2. C语言void关键字的深刻含义

    C语言void关键字的深刻含义 .概述 本文将对void关键字的深刻含义进行解说,并详述void及void指针类型的使用方法与技巧. .void的含义 void的字面意思是“无类型”,void *则为 ...

  3. http://www.cnblogs.com/zhengyun_ustc/p/55solution2.html

    http://www.cnblogs.com/zhengyun_ustc/p/55solution2.html http://wenku.baidu.com/link?url=P756ZrmasJTK ...

  4. 【Python】解决Django Admin管理界面样式表(CSS Style)丢失问题

    配置Django Admin,关于如何启用请参考Django官方文档<Activate the admin site>.但是我在配置过程中登录http://example.com/admi ...

  5. Zend Guard Run-time support missing 问题的解决

    Zend Guard是目前市面上最成熟的PHP源码加密产品了. 刚好需要对自己的产品进行加密,折腾了一晚上,终于搞定,将碰到的问题及解决方法记录下来,方便日后需要,也可以帮助其他人. 我使用的是Wam ...

  6. [ES6] 09. Destructuring Assignment -- 2

    Read More: http://es6.ruanyifeng.com/#docs/destructuring Array “模式匹配”,只要等号两边的模式相同,左边的变量就会被赋予对应的值: Ex ...

  7. 云计算之路-试用Azure:搭建自己的内网DNS服务器

    之前我们写过一篇博文谈到Azure内置的内网DNS服务器不能跨Cloud Service,而我们的虚拟机部署场景恰恰需要跨多个Cloud Service,所以目前只能选择用Azure虚拟机搭建自己的内 ...

  8. 30个iPhone健康应用帮助你保持身体健康

    来源:GBin1.com 技 术进步的最大缺陷是,现在大部分人花费大量时间在他们的电脑前和移动设备上.他们没有任何时间锻炼和顾及他们的健康.这些科技产品让我们变得慵 懒,甚至 让我们愿意花费闲暇的时间 ...

  9. java中的Iterator接口

    Iterator接口 Iterator接口也是Java集合框架的成员,但它与Collection系列.Map系列的集合不一样:Collection系列集合.Map系列集合主要用于盛装其他对象,而Ite ...

  10. webpack安装以及一些配置

    在用webpack之前... 或说没有实现组件化之前的web1.0时代! 最终迈向web2..0之后的时代! ===============华丽的分割线================== 安装步骤有 ...