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. 实现自动解析properties文件并装配到Bean

    主要实现了,配置的属性就装配, 没有配置的属性不装配 思路: 1 . 通过反射获取类内部所有方法名称 2 . 获取perperties 的key集合 3 .  处理字符串,比较两个匹配,如果匹配成功就 ...

  2. RapidXml使用方法

    一.写xml 文件 [cpp] view plaincopy #include <iostream> #include "rapidxml/rapidxml.hpp" ...

  3. cocos2d-x Cygwin编译 recipe for target `obj/local/armeabi/libcocos2d.so' fail 解决办法

    在编译cocos2d-x的helloworld 或者 tests的时候. 官网上使用ndk4.ndk5,这里是使用 ndkr7b.ndkr8或ndkr8b .操作会简单很多,但是出了些小问题也是很坑人 ...

  4. 一日一点RakNet(3)--介绍

    介绍 安装 请参考Compiler Setup页,有什么问题在这块有解答.如果有额外的问题参考FAQ,http://www.jenkinssoftware.com,或者邮件联系我们.高级用户可以直接跳 ...

  5. Netty游戏服务器之五Unity3d登陆消息

    今天我们来讲客户端Unity和服务器收发消息的具体过程. 首先,我们要在unity上搭建登陆界面的UI,这里呢,我用的是NGUI插件. 相信做过unity3d前端的都对这个非常的熟悉,最近官方的UGU ...

  6. 配置Yum源repo文件及搭建本地Yum服务器

    分享一篇配置Yum源repo文件及搭建本地Yum服务器的方法,希望对大家有用. Yum源的话有三大类: Base Extra Epel Base:就是你下载的光盘镜像里面的DVD1Extra:就是你下 ...

  7. python 如何调用子文件下的模块

    在python开发中,经常会出现调用子文件夹下的py模块 如上图,如果在test.py文件中,要调用meeting文件夹下面的huodongshu.py 模块, 直接在test.py 中 import ...

  8. sparkStreaming读取kafka的两种方式

    概述 Spark Streaming 支持多种实时输入源数据的读取,其中包括Kafka.flume.socket流等等.除了Kafka以外的实时输入源,由于我们的业务场景没有涉及,在此将不会讨论.本篇 ...

  9. LINUX 下编译不通过解答

    在linux下编译android源码或者webkit等程序源码时,不论在源码下加什么错误,编译器都默认正确,检索不到错误,此时,可能是之前编译的生成文件默认编译器不再检索编译新修改过的文件,只是发现修 ...

  10. scala之Actors

    这多半是因为actor是共享线程,所以阻塞线程会导致其他线程获取不到线程.