题意:

  股票买卖第2题。给出每天的股票价格,每次最多买一股,可以多次操作,但是每次在买之前必须保证身上无股票。问最大的利润?

思路:

  每天的股票价格可以看成是一条曲线,能卖掉就卖掉,那么肯定是在上升的时候就可以卖掉,但是在不卖的时候要保证自己身上的那只股票的价格是最低价买进的。

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

AC代码

LeetCode Best Time to Buy and Sell Stock II (简单题)的更多相关文章

  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 [122]

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

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

  10. [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. .net core webapi +ddd(领域驱动)+nlog配置+swagger配置 学习笔记(2)

    DDD领域驱动模型设计 什么是DDD 软件开发不是一蹴而就的事情,我们不可能在不了解产品(或行业领域)的前提下进行软件开发,在开发前,通常需要进行大量的业务知识梳理,而后到达软件设计的层面,最后才是开 ...

  2. sqlachemy知识点

    mysql语句 1.GROUP BY基本语法格式: GROUP BY关键字可以将查询结果按照某个字段或多个字段进行分组.字段中值相等的为一组.基本的语法格式如下: GROUP BY 属性名 [HAVI ...

  3. MATLAB求解非线性方程组

    matlab中有专门的solve函数来解决方程组的(a-x)^2+(b-y)^2=e^2(C-x)^2+(D-y)^2=v^2已知a,b,c,d,e,v 值求解 X,Y 请问用 matlab 如何写, ...

  4. 20个问题(状压dp)

    20个问题(状压dp) 有n(<=128)个物体,m(<=11)个特征.每个物体用一个m位01串表示,表示每个特征是具备还是不具备.我在心里想一个物体,由你来猜.你每次可以询问一个特征,然 ...

  5. jenkins 12

    在“参数化构建过程”中选择“List Subversion tags(and more)”,填写相应业务svn的url:这里在对应项目的svn(如下面的svn://192.168.9.120/fang ...

  6. 扩展JMeter - 创建自定义函数 - String Joiner (翻译)

    JMeter是测试自动化社区中最好的开源工具之一.它提供了所有可能的扩展,可以快速提供我们的测试脚本.为了让我们的生活更轻松,它还让我们通过实现几个接口来提出我们自己的插件. 在本文中,让我们看看如何 ...

  7. js怎样实现继承

    js实现继承的5种方式 js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承有以下通用的几种方式1.使用对 ...

  8. CSS(一)清除浮动

    问题1:关于清除浮动 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  9. CompareToBuilder构建Comparator

    import org.apache.commons.lang.builder.CompareToBuilder; Collections.sort(outboundNotices, new Compa ...

  10. DateAdapterForDay

    public class DateAdapterForDay extends XmlAdapter<String, Date> { private SimpleDateFormat dat ...