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. javascript对象讲解

    js的数据类型 基本数据类型:string   undefined   null   boolean    number 引用数据类型:object 二者的区别: 基本数据类型就是简单的赋值,引用数据 ...

  2. [Java基础] Java float保留两位小数或多位小数

    方法1:用Math.round计算,这里返回的数字格式的. float price=89.89; int itemNum=3; float totalPrice=price*itemNum; floa ...

  3. [UE4]OnComponentBeginOverlap.AddDynamic 的编译错误

    以 Character 类为例,假设有 PacManCharacter 派生自 Character类首先在 PacManCharacter.h 头文件中添加碰撞函数的声明: OnCollision 为 ...

  4. 【Unix网络编程】chapter1简介

    1.1 概述 1.2一个简单的时间获取客户程序 网际套接字地址结构中IP地址和端口号这两个成员必须使用特定的格式,为此我们调用库函数htons("主机到网络端整数")去转换二进制端 ...

  5. OpenGL chapter5 基础纹理

    Chapter5 基础纹理 Contents: ==================================================== | 任务 | 使用的函数 ========== ...

  6. 代码生成器 CodeSmith 的使用(四)

    在上一篇的版本中,我们生成了数据库中的字段,使生成的属性更加简洁,可读性也提高了很多,但都是钍对一个数据库的单个表,如果要将数据库中的所有 的表都生成相应的类,表中的字段也都生成属性,运行一次就可以将 ...

  7. opencv3.1+contrib的配置大总结(配置了两天,遇到问题无数)

    开门见山的说:别用opencv3.0,这个版本添加扩展库不怎么好,能不能成功我不敢说,我是试了无数次都不行!!! 我的配置:W7+64位+opencv3.1+Cmake3.7.2 下载 下载什么的大家 ...

  8. svn完整搭建

    安装软件 # yum install httpd mod_dav_svn subversion mod_ssl 查看是否安装成功   #svn --version 如果出现版本号如 则说明svn安装成 ...

  9. 谈谈 Python 程序的运行原理

    因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,谈谈 Python 程序的运行原理 这篇文章准确说是『Python 源码剖析』的 ...

  10. linux c++下载http文件并显示进度<转>

    #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <net ...