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

法I:从左向右扫描,找到每个递增序列的第一个元素和最后一个元素

class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty()) return ;
if(prices.size() == ) return ;
int maxProfit = ;
int buyPrice = ;
int prePrice;
bool asc = false; //if in ascending sequence vector<int>::iterator it= prices.begin();
prePrice = *it;
it++;
for(; it < prices.end(); it++)
{
if ((*it)>prePrice)
{
if(!asc) //find the first element in the ascending sequence
{
buyPrice = prePrice;
asc = true;
}
}
else if ((*it)< prePrice)
{
if(asc) //find the last element in the ascending sequence
{
maxProfit = prePrice - buyPrice + maxProfit;
asc = false;
}
}
prePrice = *it;
}
if(asc)
{
maxProfit = prePrice - buyPrice + maxProfit;
}
return maxProfit;
}
};

法II:递增就就算profit

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

122. Best Time to Buy and Sell Stock II (Array;Greedy)的更多相关文章

  1. 122. Best Time to Buy and Sell Stock II (Array)

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

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

  3. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

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

  5. leetcode:122. Best Time to Buy and Sell Stock II(java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...

  6. 【刷题-LeetCode】122 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 ...

  7. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

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

  9. !!!!!122. 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. 十二、jdk工具之jcmd介绍(堆转储、堆分析、获取系统信息、查看堆外内存)

    目录 一.jdk工具之jps(JVM Process Status Tools)命令使用 二.jdk命令之javah命令(C Header and Stub File Generator) 三.jdk ...

  2. onload属性使用方法

    onload事件属性是页面的图片文字等全部加载完毕后执行的事件 window.onload=fun1;function fun1(){    document.getElementsByTagName ...

  3. linux下给php安装curl、gd(ubuntu)

    安装方法很简单,只需要一条命令. # sudo apt-get install curl libcurl3 libcurl3-dev php5-curl 恭喜,PHP5 cURL安装完毕.记得重启Ap ...

  4. mysql存储过程的参数名不要跟字段名一样 (血淋淋的代价)

    如题,将会导致的结果就是参数的值将不会是你传入的值,而是变成每条记录的那个字段的值. 这样的后果,是灰常严重的.比如执行删除操作,它能把整个表的记录全删了. 这个是我的血淋淋的代价啊. 死坑如下,勿跳 ...

  5. 自己写的一个jQuery分页插件

    ;(function($){ $.fn.extend({ pageList: function (json) { function PageList() { this.initHtml = " ...

  6. Windows Event 事件

    事件对象就像一个开关:它只有两种状态(开和关). 开状态:我们称其为“有信号” 关状态:我们称其为“无信号” 可以在一个线程的执行函数中创建一个事件对象,然后观察它的状态,如果是“无信号”就让该线程睡 ...

  7. 学习MongoDB 二:MongoDB添加、删除、修改

    一.简介 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSQL数据库产品中最热门的一种.数据被分组存储在数据集中,被称为一个集合(Collenction)和对于存储在MongoDB ...

  8. 查看linux服务器CPU数量

    首先,要区分两个概念:物理CPU和逻辑CPU. 物理CPU就是服务器上实际安装的CPU.但是一个物理CPU可以有多个核.例如,一个 i5 760 是双核,而一个 i5 2250 是四核.如果开启了In ...

  9. mysql 更新(九) pymysql模块的使用

    16-pymysql模块的使用   本节重点: pymysql的下载和使用 execute()之sql注入 增.删.改:conn.commit() 查:fetchone.fetchmany.fetch ...

  10. virtualenv之python虚拟环境

    virtualEnv可以方便的解决不同项目中,对类库的依赖问题.首先将常用的类库安装在系统环境中,然后为每个项目安装独立的类库环境.这样子可以保证每个项目都运行在独立的类库环境中. virtualen ...