【题目】

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. Android lint具 常见问题检查

    1. Correctness 1) DuplicatedIds Layout于id应该唯一 2) NewApi 代码中使用的某些API高于Manifest中的Min SDK 3) Inconsiste ...

  2. [渣译文] SignalR 2.0 系列: 支持的平台

    原文:[渣译文] SignalR 2.0 系列: 支持的平台 英文渣水平,大伙凑合着看吧,并不是逐字翻译的…… 这是微软官方SignalR 2.0教程Getting Started with ASP. ...

  3. c++日历改进版

    #include<iostream> # include<fstream> #include<time.h> #include<string> #inc ...

  4. 怎样配置git ssh连接,怎样在GitHub上加入协作开发人员,怎样配置gitignore和怎样在GitHub上删除资源库.

    **********1.在运行git push origin master指令时报例如以下错误: iluckysi@ILUCKYSI-PC /d/ilucky/message/code (master ...

  5. String.format()【演示具体的例子来说明】

    String.format()[演示样例具体解释] 整理者:Vashon 前言: String.format 作为文本处理工具.为我们提供强大而丰富的字符串格式化功能,为了不止步于简单调用 Strin ...

  6. 的无线通信网络的学习LTE的关键技术HARQ(20141217)

    今天,我们就来一起看一下LTE申请的关键技术HARQ(自己主动混合重传技术) 因为在信道传输过程中的信息,它会产生信息丢失,因此,为了维持的信息的完整性.总是有重发信息,完成所有的迄今收到的资料. 首 ...

  7. Java中导入、导出Excel

    原文:Java中导入.导出Excel 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已 ...

  8. 怎样用C#代码管理SharePoint解决方式

    怎样用C#代码管理SharePoint解决方式         本文我们将了解怎样用代码管理SharePoint解决方式.我们使用server端对象模型抽取解决方式.         SharePoi ...

  9. linux学习(一个) 在unbuntu通过添加新的用户

    最近安装了双系统,开始折腾unbuntu该.Linux系统是一个多用户操作系统,非常多的人才完整的操作需要管理员权限,完全管理员权限是非常重要的.人谁是刚开始学习,般用户的权限即可了,相对于刚開始学习 ...

  10. linux并发连接数查看

    1.查看Webserver(Nginx Apache)的并发请求数及其TCP连接状态: netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) pri ...