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. 关于DELL服务器如果采购散件,进行服务器升级的相关说明

    拿DELL服务器内存来说明这个情况,其他硬盘等等是 一样的: 1.DELL服务器的内存散件,从购买之日起,质保期是一年: 2.但是如果你把内存插到能兼容这个内存的服务器上去使用,请注意我的字眼,是能兼 ...

  2. FDD vs TDD

    双工方式 FDD vs TDD  频分双工(FDD) 收发信各占用一个频率. 优点是收.发信号同时进行,时延小,技术成熟,缺点是设备成本高.  时分双工(TDD) 收发信使用同一个频率,但使用不同 ...

  3. RAC 11.2的新特性

    网格即插即用(GPnP) 网格即插即用帮助管理员来维护集群,以前增加或删除节点需要的一些手动操作的步骤现在可以由GPnP来自动实现. GPnP不是一个单独的概念,它依赖于以下特性:在一个XML配置文件 ...

  4. Spark运行模式概述

    Spark编程模型的回顾 spark编程模型几大要素 RDD的五大特征 Application program的组成 运行流程概述 具体流程(以standalone模式为例) 任务调度 DAGSche ...

  5. javascript 常用获取页面宽高信息 API

    在页面的构建中 常常会需要获取页面的一些宽高信息,例如实现 惰性加载图片 需要获取页面的可见区域高度 和 已滚动区域的高度,以判断图片所在位置是否可见来决定加载图片的时间, 花点时间整理了一下,获取页 ...

  6. JavaScript中判断函数、变量是否存在

    转载:http://www.jb51.net/article/67551.htm 一.是否存在指定函数 function isExitsFunction(funcName) { try { if (t ...

  7. 温故而知新-MySQL高级编程

    1 load data infile语句 MySQL下的命令  登录mysql命令行模式 load data infile "/var/www/1.txt" into table ...

  8. rhel7配置yum的方法

    参考网站:https://www.cnblogs.com/haoyufeng/p/4393005.html rhel的版本号:::::::::: [root@LinuxS04 yum.repos.d] ...

  9. vmware桥接模式无法上网

    环境:本机win10系统,ip地址固定,(估计存在vlan网络), 状况:vmware的nat模式可以上网,桥接模式不能上网, 解决办法:找网管把本机设置成dhcp模式,才行了

  10. 1.HTML编码解码URL替换--代码整理

    public class HtmlCode { public static String encode(String str){ String s = ""; if (str.le ...