Best Time to Buy and Sell Stock

(onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/

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

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

注意: 限制条件: 先买后卖(不同天)。

思想: 买了后,1. 若以后价格不变,不买不卖。 1. 更价格低,重新买。2. 价格升高,假定抛售,更新一下利润值。

class Solution {
public:
int maxProfit(vector<int> &prices) {
int buy = 0x7fffffff, maxProfile = 0;
for(int i = 0; i < prices.size(); ++i) {
if(prices[i] == buy) continue;
if(prices[i] < buy) { buy = prices[i]; continue; }
else maxProfile = max(maxProfile, prices[i]-buy);
}
return maxProfile;
}
};

Best Time to Buy and Sell Stock II

(onlineJudge:https://oj.leetcode.com/problems/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 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).

思想:求出所有非递减序列两端绝对值之和。我贴在 leedcode 的代码和证明:

class Solution {
public:
int maxProfit(vector<int> &prices) {
int n = prices.size();
if(n == 0) return 0;
int start = 0, profile = 0;
for(int i = 1; i < n; ++i) {
if(prices[i] < prices[i-1]) {
profile += prices[i-1] - prices[start];
start = i;
}
}
profile += prices[n-1] - prices[start];
return profile;
}
};
/*********************** *provement ***************/
/*Explain the code I pasted above: From left to right I find out every subsequence that not exist decrease. such as: l ... k1 ...k2 ... h (l <=...<= k1 <= ... k2 <= ... <= h) In this sequence: ( k1-l ) + ( h-k2 ) = ( h-l ) - ( k2-k1 ) <= ( h-l ); So (h - l) will be the maximum profit in this days. Another case: L1 ...d1... H1 K2 ...k... K3 L2 ...d2... H2 (L1 <=... H1 > K2 >=...k >=... K3 > L2 <=... H2 ) K2 ... K3 is not exist increase sequence. then for any k in that position, ( k-d1 ) + ( d2-k ) <= ( K2-L1 ) + ( H2-K3 ) < ( H1-L1 ) + ( H2-L2 ) In my code, variant "start" is the start of every no decrease sequence.*/

A little Adjustment.

class Solution {
public:
int maxProfit(vector<int> &prices) {
int start = 0, profile = 0;
for(size_t i = start+1; i < prices.size(); ++i) {
if(prices[i] < prices[i-1]) {
profile += prices[i-1] - prices[start];
start = i;
}
else if(i+1 == prices.size())
profile += prices[i] - prices[start];
}
return profile;
}
};

Best Time to Buy and Sell Stock III

(onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/

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 at most two transactions.

Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

思想:动态规划。 记录下从各位置(含)开始之前的最大利润和此时开始到最后的最大利润。

class Solution {
public:
int maxProfit(vector<int> &prices) {
vector<int> preProfile(prices.size()+2, 0), postProfile(prices.size()+2, 0); int minPrice = 0x7fffffff;
for(size_t i = 1; i <= prices.size(); ++i) {
minPrice = min(minPrice, prices[i-1]);
preProfile[i] = max(prices[i-1] - minPrice, preProfile[i-1]);
} int maxPrice = 0;
for(int i = prices.size(); i >= 1; --i) {
maxPrice = max(maxPrice, prices[i-1]);
postProfile[i] = max(maxPrice - prices[i-1], postProfile[i+1]);
} int maxProfile = 0;
for(size_t i = 1; i <= prices.size(); ++i)
maxProfile = max(maxProfile, preProfile[i] + postProfile[i]);
return maxProfile;
}
};

27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III的更多相关文章

  1. [Swift]LeetCode122. 买卖股票的最佳时机 II | 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 ...

  2. [Leetcode 122]买股票II 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 ...

  3. [LeetCode 122] - 买入与卖出股票的最佳时机II(Best Time to Buy and Sell Stock II)

    问题 假设你有一个数组,其中的第i个元素表示一只股票在第i天的价格. 设计一个算法找出最大的利润值.你可以进行任意多次的交易(即多次的卖出并买入一份股票).你不能在同一时间进行多次交易(即你必须在再次 ...

  4. [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

    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] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

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

  6. [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三

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

  7. Best Time to Buy and Sell Stock with Cooldown

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

  8. LEETCODE —— 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 ...

  9. Best Time to Buy and Sell Stock系列

    I题 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...

随机推荐

  1. 1900. Brainwashing Device

    http://acm.timus.ru/problem.aspx?space=1&num=1900 题目大意: 有N个车站,相邻车站之间形成一个段,这样就有N-1个段,每个段最多可以放一个洗脑 ...

  2. HBase with MapReduce (Only Read)

    最近在学习HBase,在看到了如何使用Mapreduce来操作Hbase,下面将几种情况介绍一下,具体的都可以参照官网上的文档说明.官网文档连接:http://hbase.apache.org/boo ...

  3. juqey.html(),text(),val()

    .html()用为读取和修改元素的HTML标签 .text()用来读取或修改元素的纯文本内容 .val()用来读取或修改表单元素的value值. 这三个方法功能上的对比 .html(),.text() ...

  4. oracle 拼接一张表所有字段

    declare t_name varchar2(100) := upper('dba_tab_columns'); cursor c_col is select column_name from db ...

  5. adobe form

    Call Adobe Form through ABAP Program 2015-04-24      0个评论    来源:ChampaignWolf的专栏   收藏    我要投稿 Scenar ...

  6. 服务器自己用户名下编译gcc

    要点: 1.上传gcc 学习命令 scp 具体格式: scp local_file remote_username@remote_ip:remote_folder scp /home/linux/so ...

  7. WINDONWS7+VS2012+Cocos2d-x

    一:准备工作 准备下载文件 1.VS2012,到处都有咱就不发链接了. 2.Cocos2d-x的最新版本 http://www.cocos2d-x.org/projects/cocos2d-x/wik ...

  8. hadoop删除节点。

    hadoop节点摘除操作: 1.确定exclude文件的位置. <property> <name>dfs.hosts.exclude</name> <valu ...

  9. Mongodb在NUMA机器上的优化

    10gen在mongodb的部署指南上,提到了在NUMA机器上,mongodb可能会出现问题,参见:http://docs.mongodb.org/manual/administration/prod ...

  10. 网页闯关游戏(riddle webgame)--游戏玩法和整体介绍

    前言: 记得上大学那会, 有位传说中的大牛, 写了一个网页闯关类的游戏. 当时我们玩得不亦乐乎, 也是第一次接触到这种形式的游戏. 不过当时纯玩家心态, 并没有想过去创造一个. 最近想起这事, 突然想 ...