【题目】

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

【题意】

给定一个数组prices, prices[i]表示第i天的股价。本题没有买卖次数的限制,问最大收益是多少

【思路】

贪心思想。假设股价一路上扬,则持币观望,直到股价升到最高点抛售获利最大。

【代码】

class Solution {
public:
int maxProfit(vector<int> &prices) {
int size=prices.size();
if(size<=1)return 0; int maxProfit=0;
int buyDate=0;
for(int i=1; i<size; i++){
if(prices[i-1]>prices[i]){
//股价開始下跌,在i-1天抛售
maxProfit+=prices[i-1]-prices[buyDate];
buyDate=i;
}
}
//【注意】别忘了最后一次买卖
maxProfit+=prices[size-1]-prices[buyDate];
return maxProfit;
}
};

LeetCode: Best Time to Buy and Sell Stock II [122]的更多相关文章

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

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

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

  4. [LeetCode] 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 (股票买卖时机问题2)

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

  6. [leetcode]Best Time to Buy and Sell Stock II @ Python

    原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意: Say you have an array ...

  7. [LeetCode] 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 ...

  8. LeetCode——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. ...

  9. [Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机

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

随机推荐

  1. node.js基础:数据存储

    无服务器的数据存储 内存存储 var http = require('http'); var count = 0; //服务器访问次数存储在内存中 http.createServer(function ...

  2. .NET Framework 4.0 以上版本 下载地址

    https://msdn.microsoft.com/zh-cn/library/5a4x27ek(v=vs.110).aspx

  3. HDU更多的学校比赛9场 HDU 4965Fast Matrix Calculation【矩阵运算+数学技巧】

    困难,.,真,,,不是太困难 的问题是,有一个矩阵运算优化 您有权发言权N*K矩阵A给K*N矩阵B(1<=N<=1000 && 1=<K<=6).他们拿起了第一 ...

  4. 文章13称号 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  5. 2.Cocos2dx 3.2重力系统Box2D

     1 加入Box2D相关的库 步骤1:右击项目所在的解决方式à加入->现有项目àE:\Installed\cocos2d-x-3.2\cocos2d-x-3.2\external\Box2D ...

  6. 猪和python(pig and python)

    Python 真是无处不在国内. pig 0.9后python作为嵌入式语音,采用Jython解释器使用python2.5特征,此接口是最上层org.apache.pig.scripting.Pig首 ...

  7. 第三篇——第二部分——第一文 SQL Server镜像简介

    原文:第三篇--第二部分--第一文 SQL Server镜像简介 原文出处:http://blog.csdn.net/dba_huangzj/article/details/26951563 镜像是什 ...

  8. nyoj 322 Sort 【树阵】

    这个问题实际上是在测试树的数组. 代码: #include <cstdio> #include <cstring> int c[1005]; int lowbit(int x) ...

  9. Mesos-error

    1,configure: error: cannot find libcurl 解决 yum install  curl-devel 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  10. 网络语音视频技术浅议 Visual Studio 2010(转)

    我们在开发实践中常常会涉及到网络语音视频技术.诸如即时通讯.视频会议.远程医疗.远程教育.网络监控等等,这些网络多媒体应用系统都离不开网络语音视频技术.本人才疏学浅,对于网络语音视频技术也仅仅是略知皮 ...