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. [转]修改DLL

    部分内容来自:http://blog.csdn.net/csdncshuan/article/details/51477705 为没有源码的DLL文件添加强名称 如果项目中引用了其他没有源码的dll文 ...

  2. linux 下 rpc python 实例之使用XML-RPC进行远程文件共享

    这是个不错的练习,使用python开发P2P程序,或许通过这个我们可以自己搞出来一个P2P下载工具,类似于迅雷.XML-RPC是一个远程过程调用(remote procedure call,RPC)的 ...

  3. 每秒查询率QPS

    每秒查询率QPS是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准,在因特网上,作为域名系统服务器的机器的性能经常用每秒查询率来衡量. 原理:每天80%的访问集中在20%的时间里,这20%时 ...

  4. [UE4]为什么会有类型检查

    类型检查 定义:对象的行为是否符合类型的行为 作用:帮助开发者找出潜在的错误. 类型转换 隐式类型转换:整数可以和浮点数运算 显式类型转换/强制类型转换

  5. Delphi Webbrowser使用方法详解(二)

    delphi如何用webbrowser模拟登录网站? 我们就以如何登录博客园来做示例: 1.要登入一个网站,首先要获取网页的源代码,我们可以通过网页菜单--查看--查看源代码来获取. 2.我们找到登录 ...

  6. nginx经过多层代理后获取真实来源ip

    nginx取 $remote_addr 当做真实ip,而事实上,$http_X_Forwarded_For 才是用户真实ip,$remote_addr只是代理上一层的地址 解决方案: 在 http 模 ...

  7. 18.OGNL与ValueStack(VS)-值栈入门

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 下面我们建立struts2ognl项目来练习ognl的使用. 步骤一.搭建s ...

  8. 1.Spring AOP (上)

    转自:https://blog.csdn.net/yusheng8711/article/details/21622773 Spring有两大核心,IOC和AOP.IOC在java web项目中无时无 ...

  9. 安装新操作系统 Windows 路径设置 节省C盘空间

    1.QQ个人文件夹设置到D盘,D:\ProgramData\QQ 2.IE收藏夹设置到D盘,IE缓存设置到D盘 例如我想把收藏夹默认的保存路径改到D:\study下.关闭Internet Explor ...

  10. as2 fla 关于影片在时间轴的问题

    多帧上面放着没实例名而且里面内容一致的影片,主要一开始读取了,那么跳帧的时候.会自动获取当前帧上的相同内容的影片. 但如果内容不一致的影片,那么在跳帧后,不会获取当前的影片,旧的影片也无法获取.只在当 ...