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. html 中添加背景音乐

    Embed (一).基本语法: embed src=url 说明:embed可以用来插入各种多媒体,格式可以是 Midi.Wav.AIFF.AU.MP3等等, Netscape及新版的IE 都支持.u ...

  2. jquery插件库

    jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多javascript高手加入其team. jQuery是继prototype之后又一个优秀的Javascrīpt框架.其经典 ...

  3. 18.Android之SharedPreferences数据存储学习

    SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来 ...

  4. BZOJ-2190 仪仗队 数论+欧拉函数(线性筛)

    今天zky学长讲数论,上午水,舒爽的不行..后来下午直接while(true){懵逼:}死循全程懵逼....(可怕)Thinking Bear. 2190: [SDOI2008]仪仗队 Time Li ...

  5. 【bzoj3246】 Ioi2013—Dreaming

    www.lydsy.com/JudgeOnline/problem.php?id=3246 (题目链接) 题意 给出一棵不完全的树,要求在树上连最少的边使得所有点联通,并且使得两点间最大距离最小. S ...

  6. 【poj3348】 Cows

    http://poj.org/problem?id=3348 (题目链接) 题意 给出平面上n个点,以这n个点中的一些围成的多边形面积 div 50的最大值. Solution 凸包求面积. 很好做, ...

  7. CruiseControl.NET开篇

    在这里说明一下,我终于踏上了CruiseControl.NET这条不归路了,为什么我会觉得是一条不归路,原因很简单,就是这东西在现在这个阶段已经很久没有在园子里有活跃度了,基本上到了已经可以到了让大家 ...

  8. Linux 内核通知链机制的原理及实现

    一.概念: 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在发生某个事件时通知其它的子 系统,Linux内核提供了通知链的机制.通 ...

  9. iOS 知识点梳理

    OC的理解与特性 OC作为一门面向对象的语言,自然具有面向对象的语言特性:封装.继承.多态.它既具有静态语言的特性(如C++),又有动态语言的效率(动态绑定.动态加载等).总体来讲,OC确实是一门不错 ...

  10. jQuery EasyUI API 中文文档

    http://www.cnblogs.com/Philoo/tag/jQuery/ 共2页: 1 2 下一页  jQuery EasyUI API 中文文档 - 树表格(TreeGrid) 风流涕淌 ...