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 price = prices[];
int profit=;
for(auto it = prices.begin();it!=prices.end();++it)
{
if(price<*it)
profit += *it - price;
price = *it;
}
return profit;
}
};

和最大子序列思路差不多。这里是前一天和后一天比较,如果前一天价格小于后一天就先买后卖,否则这一天不交易。

Best Time to Buy and Sell Stock II的更多相关文章

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

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

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

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

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

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

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

  9. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

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

随机推荐

  1. sencha panel的头header上添加刷新按钮

    var plet3=Ext.create('portaltest3.view.Portlet',                   { title: '提醒',                   ...

  2. 【jQuery基础学习】06 jQuery表单验证插件-Validation

    jQuery的基础部分前面都讲完了,那么就看插件了. 关于jQuery表单验证插件-Validation validation特点: 内置验证规则:拥有必填.数字.E-Mail.URL和信用卡号码等1 ...

  3. cURL和HTTPie

    http://lingxiankong.github.io/blog/2014/08/19/curl-httpie/ 前两天在网上看到一个号称比cURL更牛逼的命令行工具HTTPie,提供命令行交互方 ...

  4. [moka同学笔记]yii2.0导航栏

    导航栏 <?php use yii\helpers\Url; /** * $navbar说明 * label:显示的标签 * url:跳转地址 * action:判断激活的操作 * class: ...

  5. 如何实现两个Activity 之间如何通讯

    <转> 今天主要学习了Activity 组件,在这里作一下总结 1,学习如何创建Activity 创建 Activity 要点: (1) 一个Activity就是一个类,并且这个类要继承A ...

  6. (七)play之yabe项目【CRUD】

    (七)play之yabe项目[CRUD] 博客分类: 框架@play framework    增加CRUD功能 使用CRUD能干嘛?----> 在页面对模型进行增删改查操作,这样有什么实际意义 ...

  7. css实现垂直居中的方法

    1,设置其line-height值,使之与其高度相同 2,设置table结构,用vertical-align:middle; 3,应用定位,父级别:position:relative:子级:posit ...

  8. ASP.NET获取请求的url信息汇总

    ASP.NET获取请求的url信息汇总 最近做项目需要处理一个用代码获取当前网站的域名或ip信息的问题,于是尝试了ASP.NET中各种获取url信息的方法,在此总结一下: 在Global.asax文件 ...

  9. JavaScript this特性,静态方法 和实例方法,prototype

    <script type="text/javascript"> function logs(str) { document.write(str + "< ...

  10. SAP技术相关Tcode

    ABAP的常用tcode 开发----------------------------------------------- SE51  屏幕制作 SE91  MESSAGE OBJECT SE80  ...