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. ASP.NET Web Pages:Chart 帮助器

    ylbtech-.Net-ASP.NET Web Pages:Chart 帮助器 1.返回顶部 1. ASP.NET Web Pages - Chart 帮助器 Chart 帮助器 - 众多有用的 A ...

  2. 转载-MyBatis学习总结

    MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合 孤傲苍狼 2015-02-07 00:09 阅读:89825 评论:54     MyBatis学习总结(七)——Myba ...

  3. javascript cookie操作.

    给cookie追加name: document.cookie="name="+escape("我叫钢蛋");//这里可以不写secape,这样写会更加的安全., ...

  4. 第2课 C 到 C++ 的升级

    1.  C与C++的关系 (1)C++继承了所有的C特性,并在C的基础上提供了更多的语法和特性. (2)C++的设计目标是运行效率与开发效率的统一,它更强调的是语言的实用性. 2. C到C++ 的升级 ...

  5. windows下面安装easy_install和pip教程

    方便安装whl:安装完成后,可以使用pip install   xxx.whl 安装一个python轮子 python扩展库的路径:Python\Python36\Lib\site-packages\ ...

  6. BPM编程模型(场景)

    一直开发基于操作的业务系统,主要就是通过界面,用户提交一些数据完成任务,大多数涉及多人协作的,基本都是浏览,少数可能对其进行审批,这里的审批不是电子政务那样的多人审批任务,仅仅是对数据的一个操作而已, ...

  7. Missing write access to /usr/local/lib/node_modules/webpack/node_modules/assert

    1. 加上sudo指令 sudo npm install ... 2. 可能是网络原因, 改用cnpm cnpm install ...

  8. NHibernate 学习笔记(一)

    NHibernate 的简介: NHibernate是一个面向.NET环境的对象/关系数据库映射工具.对象/关系数据库映射(object/relational mapping (ORM))这个术语表示 ...

  9. diffutils's diff

    比较文件的差异 diff,用来查看两个文件的差异之处,或者两个目录之中的对应文件.倘若比较的不是文本文件,而是二进制文件,只会报告两者不同.输出文本文件的异同时,可以按照多个格式输出,根据使用的选项决 ...

  10. git常用命令,冲突

    使用多个仓库git push cangkuming fenzhiming删除远程仓库 git push 远程仓库名 :删除目标分支 # 需要先删除本地目标分支 git pull <远程主机名&g ...