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) {
if(prices.empty())
{
return ;
}
int maxprofit = ;
int last = prices[];
vector<int>::iterator it;
for(it = prices.begin() + ; it < prices.end(); it++)
{
if(*it > last)
{
maxprofit += (*it - last);
}
last = *it;
}
return maxprofit;
}
};

【leetcode】Best Time to Buy and Sell 2(too easy)的更多相关文章

  1. 【leetcode】Best Time to Buy and Sell 3 (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. 【LeetCode】Best Time to Buy and Sell Stock IV

    Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...

  3. 【leetcode】Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

  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. 【leetcode】121-Best Time to Buy and Sell Stock

    problem 121. Best Time to Buy and Sell Stock code class Solution { public: int maxProfit(vector<i ...

  6. 【leetcode】Best Time to Buy and Sell (easy)

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

  7. 【LeetCode】Best Time to Buy and Sell Stock

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

  8. 【数组】Best Time to Buy and Sell Stock I/II

    Best Time to Buy and Sell Stock I 题目: Say you have an array for which the ith element is the price o ...

  9. 【Leetcode】【Medium】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 ...

随机推荐

  1. UnicodeDecodeError: 'utf8' codec can't decode

    数据库存了些中文字符, 比如'处理脚本'这样的汉字, 结果导致一个python程序报错. 下面记录处理过程和相关结论. ===========================dal.py 程序片段,p ...

  2. 设置二级域名共享一级域名Cookie和删除共享Cookie

     设置共享Cookie: 二级域名要想共享一级域名的cookie,只需要设置cookie.Domain = ".一级域名.com";   删除共享Cookie:  HttpCook ...

  3. 第二章平稳时间序列模型——ACF和PACF和样本ACF/PACF

    自相关函数/自相关曲线ACF   AR(1)模型的ACF: 模型为: 当其满足平稳的必要条件|a1|<1时(所以说,自相关系数是在平稳条件下求得的):          y(t)和y(t-s)的 ...

  4. firefox浏览器不能使用window.close的解决方案

    javascript中window.close()函数用来关闭窗体,而且IE.google.firefox浏览均支持,但由于firefox浏览器dom.allow_scripts_to_close_w ...

  5. 第12天 android studio

    1. http://jingyan.baidu.com/article/215817f7888dc21eda14230d.html Gradle DSL method not found:‘andro ...

  6. C#网络爬虫 WebUtility使用 转义字符 urlCode

    背景: 在C#写网络爬虫时候,有时候需要将html中的转义字符进行处理,还有网址中的中文处理 一.html转义字符处理 1.ASP.NET中的html解析 HttpUtility.HtmlDecode ...

  7. 安装JBPM6运行环境(JBPM6学习之二)

    安装Eclipse插件成功后,需要配置JBPM6的运行环境: 1. 第一步先将下载的jbpm6目录中的“jbpm-6.0.1.Final-bin.zip”找到,并解压缩到D盘根目录备用. 2. 第二步 ...

  8. 关于ubuntukylin安装后界面中英文混杂的问题

    起因 一直使用的是ubuntu原版的系统,ubuntukylin出来后也没用使用过.一次去其论坛逛了一圈之后决定使用一下. 安装后的截面和ubuntu原版的差不多,还是挺漂亮的. 但是有一个问题是,安 ...

  9. ja

    import java.util.*; class animal{     void cry(){            }     void get_animal_name(){           ...

  10. Python自动化之线程进阶篇(二)

    queue队列 class queue.Queue(maxsize=0) #先入先出 class queue.LifoQueue(maxsize=0) #后入先出 class queue.Priori ...