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

其核心就是找到所有的单调递增区间,然后卖出去
 
最简单的,每当有涨价,就卖出去,累加所有收益(Accept了,不过似乎 buy one and sell one share of the stock multiple times了)
 

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

 

 
下面代码找到了递增的区间,然后在最后一天卖出
 class Solution {
public:
int maxProfit(vector<int> &prices) { int profit=;
int n=prices.size(); if(n==)
{
return ;
}
int buyPrice=prices[]; for(int i=;i<n;i++)
{
if(prices[i]<prices[i-])
{
profit+=prices[i-]-buyPrice;
buyPrice=prices[i];
}
} profit+=prices[n-]-buyPrice; return profit; }
};
 
 

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

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

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

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

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

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

  6. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

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

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

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

随机推荐

  1. sphinx在c#.net平台下使用(一)

    Sphinx是由俄罗斯人Andrew Aksyonoff开发的一个可以结合MySQL,PostgreSQL全文检索引擎.意图为其他应用提供高速.低空间占用.高结果 相关度的全文搜索功能.是做站内全文搜 ...

  2. SQLServer复制表

    把a的表结构复制到a1表,1=2不复制数据,如果要复制数据,就不要whereselect * into a1 from a where 1=2注意:这种方式不能复制主键.索引等信息如果要全部复制,只能 ...

  3. 轻量级应用开发之(10) UINavigationController导航控制器

    一 多控制器 1)一个iOS的app很少只由一个控制器组成,除非这个app极其简单2)当app中有多个控制器的时候,我们就需要对这些控制器进行管理3)有多个view时,可以用一个大的view去管理1个 ...

  4. PHP中应用GD2函数在图像上添加文字

    <?php header("Content-type:text/html;charset=utf-8"); header("Content-type:image/g ...

  5. Python socket编程之三:模拟数据库循环发布数据

    1. f1.py # -*- coding: utf-8 -*- import socket import struct import sqlalchemy import pandas ####### ...

  6. varnish4.0简介

    Varnish 4.0 简介 Varnish 是一款开源的HTTP加速器和反向代理服务器,它的主要特点有: (1)是基于内存缓存,重启后数据将消失.(2)利用虚拟内存方式,io性能好.(3)支持设置0 ...

  7. WPF 动态布局Grid

    //开启线程加载 Action a = () => { ; ; var path = "../../face_img/"; var files = Directory.Get ...

  8. 如何在eclipse中使用XYLayout布局?在此介绍如何把XYLayout导入到eclipse .

    XYLayout布局是jbuilder中自带的布局,它存在于jbcl.jar包中 ,而jbcl.jar包在JBuilder安装目录的lib目录下.它是每个控件按(x,y)坐标安排位置的布局.属于Bor ...

  9. Gradle用户指南(2)-基本命令

    1.定义任务 使用task 关键字来定义gradle 任务 例:创建一个 build.gradle 命名的文件: build.gradle task hello { doLast { println ...

  10. 【转】不得不看的两次从C++回归C的高手评论C++

    不得不看的两次从C++回归C的高手评论C++ Linux之父炮轰C++:糟糕程序员的垃圾语言 Linux之父话糙理不糙 不得不看的两次从C++回归C的高手评论C++ C语言是否该扔进垃圾桶 为什么每个 ...