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

解题:简单贪心,如果今天买,明天涨就不卖,如果明天跌就卖掉,如果后天涨明天就又买进来。

代码:

 class Solution {
public:
int maxProfit(vector<int> &prices) {
int answer = ;
if(prices.size() == )
return ;
for(int i = ;i < prices.size()-;i++)
{
if(prices[i+]>prices[i])
answer += prices[i+]-prices[i];
}
return answer;
}
};

我还问了这个问题:http://oj.leetcode.com/discuss/4082/why-do-i-have-to-add-if-prices-size-0-return-0

Java版本:

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

【leetcode刷题笔记】Best Time to Buy and Sell Stock II的更多相关文章

  1. 【LEETCODE】37、122题,Best Time to Buy and Sell Stock II

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  2. LeetCode(122) 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 ...

  3. LeetCode Array Easy 122. Best Time to Buy and Sell Stock II

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

  4. [LeetCode&Python] Problem 122. 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 ...

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

  7. 122. Best Time to Buy and Sell Stock II@python

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

  8. leetcode刷题笔记

    (1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...

  9. 【刷题-LeetCode】122 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 ...

  10. LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV

    Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...

随机推荐

  1. 程序员不修复BUG怎么办

    在测试过程中,难免遇到开发人员因为一些原因不想修改个别bug的情况.遇到这种问题时,该如何去推进开发修改bug呢? 一.现状分析 1.开发人员为啥不愿意修复BUG? (1)开发与测试对bug的定义理解 ...

  2. JQuery加载并解析XML

    转自http://blog.csdn.net/pan_junbiao/article/details/7441003,致谢! 1.简述 XML(eXtensible Markup Language)即 ...

  3. cdn日志统一下载程序

    最近实现了网宿cdn,阿里云cdn,腾讯cdn的日志统一格式下载程序,使用简单方便,有需要详见代码: https://github.com/mikeluwen/CdnLogDownload

  4. Qemu事件处理机制简介

    Qmeu 采用了基于事件驱动的架构,所有的事件都在一个事件循环(event loop)中被处理,系统中默认的事件循环是在main-loop.c 中的主循环(main loop).我们也可以使用 –ob ...

  5. centos root登录password 忘记解决的方法

    Centos系统 登陆root忘记password 解决方式: (1)开机启动系统,在进入linux系统之前按键Esc 进入例如以下界面:(须要注意:Centos是安装在虚拟机里面的话,须要将鼠标点进 ...

  6. cache和内存

    CPU与内存 北桥:主桥,主要用来处理高速信号,负责与处理器的联系:CPU通过FSB前端总线来访问内存控制器. 南桥:IO桥,负责IO总线之间的通信,比如PCI总线.SATA.USB等,可以连接光驱. ...

  7. 在spring mvc中利用ajax批量删除数据

    1.前台代码: $("#batchdelete").click(function(){ $.ajax({ type: "post", url: url, dat ...

  8. Swift迎来了1.0 GM 版(2014.09.09)

    2014年6月2日,swift开发团队将swift语言公之于众.而2014年9月9日迎来了swift的第二个里程碑,swift1.0版本号(GM),这意味着无论你的应用有一部分功能是用swift写的, ...

  9. mysql 5.7 迁移数据方案

    从一台服务器迁移至其他服务器,如何选择最短的停服时间方案 方案一.凌晨3点的全备份+停服后一天的大概一天的增备 1. 拷贝前一天的全备份至新的服务器 rsync -auzrP /Data/dbbak/ ...

  10. 细细品味大数据--初识hadoop

    初识hadoop 前言 之前在学校的时候一直就想学习大数据方面的技术,包括hadoop和机器学习啊什么的,但是归根结底就是因为自己太懒了,导致没有坚持多长时间,加上一直为offer做准备,所以当时重心 ...