题目1: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.

分析:

题意给我们一个数组prices[], 用来表示股票每一天的价格,问我们假设“仅仅多进行一次交易”(即买进之后再卖出), 应该在哪天买进,哪天卖出所获得的利润能达到最大值。

如:prices={1, 3, 4, 10, 1};
那么最大的利润是第一天买进(价格为1),然后第四天卖出(价格为10), 利润最大为9

明确了题意之后,我们来看看能如何解决这道题目。

因为是仅仅能交易一次,并且必须先有“买进”,才干有“卖出”
因此我们仅仅须要数组的最后一位往前扫描。依次得到哪一天的prices是最大的(设为maxPrices),然后在算出和这一天前面的某一天prices[i]的差值 (maxPrices - prices[i]),假设大于最大的利润值,则更新最大利润值maxMoney;

AC代码: 
public class Solution {

    public int maxProfit(int[] prices) {
int size = prices.length;
if (size == 0){
return 0;
}
int maxPrice = prices[size-1];//初始化最大price
int maxMoney = 0;//初始化利润值
for (int i=size-1; i>=0; --i){
maxPrice = maxPrice > prices[i] ? maxPrice : prices[i];//假设第i天的值大于最大price,则更新最大price的值
maxMoney = maxMoney > (maxPrice - prices[i]) ? maxMoney : (maxPrice - prices[i]);//更新最大利润值
}
return maxMoney;
}
}

题目2: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).


分析:

这道题目。关键是要理解题意,题意中和题目I是有点像,唯一不同的是这道题目能够进行多次交易,可是要注意的是,每次最多仅仅能持有一支股票在手上,也就是说你要再买入的时候。必须先把手头上的这股票先卖掉!

举个样例:如Prices[] = {1,3,4,10,2};
这样子的话,你能够採取的方式有
1、第1天买入(price==1)。第2天卖出(price==3), 第3天买入(price==4),第4天卖出(price==10)  :   8
2、第1天买入(price==1)。第3天卖出(price==4)       : 3
3、第2天买入(price==3),第3天卖出(price==4)       : 1
4、第3天买入(price==4),第4天卖出(price==10)     : 6
5、第1天买入(price==1),第4天卖出(price==10)     : 9
但事实上假设细致观察easy发现 : 
3 - 1 = 2
4 - 3 = 1
10 - 4 = 6

然后result = 2 + 1 + 6 = 9
因此事实上我们仅仅是要找增长的序列对。并求出他们的差值的和
index :  1 ~~ prices.size()-1
通过这样分析的话,我们非常easy知道事实上仅仅要从头到尾遍历,假设 prices[index] > prices[index-1] 

AC代码:

public class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
int size = prices.length;
if (size < 2){
return profit;
}
for (int index=1; index<size; ++index){
int value = prices[index] - prices[index-1];
if (value > 0){
profit += value;
}
}
return profit;
}
}

题目3:

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).

分析:
因为最多仅仅能够进行两次交易,并且两次交易之间必须没有交集。
这题用DP来做.
那我们非常easy想到。将数组划分成两块,左边一块 | 右边一块,我们仅仅须要求出左边的最大利润,再求出右边的最大利润。然后相加起来就得到了利润的最大值
我们用
用left[i] 来表示[0,...,i]中的最大利润

用right[i]来表示[i,...,n-1]上的最大利润

AC代码:
public class Solution {
public int maxProfit(int[] prices) {
int size = prices.length;
if (size < 2)
return 0;
int[] left = new int[size];
int[] right = new int[size];
int minValue = prices[0];
int maxValue = prices[size-1];
for (int i=1; i<size; ++i){
left[i] = left[i-1] > (prices[i] - minValue) ? left[i-1] : (prices[i] - minValue);
minValue = minValue < prices[i] ? minValue : prices[i];
}
for (int i=size-2; i>=0; --i){
right[i] = right[i+1] > (maxValue - prices[i]) ? right[i+1] : (maxValue - prices[i]);
maxValue = maxValue > prices[i] ? maxValue : prices[i];
}
int profit=0;
for (int i=0; i<size; ++i){
profit = profit > (left[i] + right[i]) ? profit : (left[i] + right[i]);
}
return profit;
}
}


Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [LeetCode] 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 ...

  5. [LeetCode] 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 ...

  6. [LintCode] 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 ...

  7. [LintCode] 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 ...

  8. LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)

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

  9. 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记

    123. Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the pric ...

随机推荐

  1. JTAG接线描述

    http://www.dzsc.com/data/html/2008-12-23/75397.html JTAG测试信号由下面5个信号组成. TRST:测试复位输入信号,测试接口初始化 TCK:测试时 ...

  2. make mrproper and make clean

    make mrproper命令会删除所有的编译生成文件.内核配置文件(.config文件)和各种备份文件,所以几乎只在第一次执行内核编译前才用这条命令. make clean命令则是用于删除大多数的编 ...

  3. 服务器返回的“未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0提供程序””错误解决

    未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0提供程序”

  4. 结合MapReduce和数据集Combining datasets with MapReduce

    While in the SQL-world is very easy combining two or more datasets - we just need to use the JOIN ke ...

  5. 高效的数据压缩编码方式 Protobuf

    一. protocol buffers 是什么? Protocol buffers 是一种语言中立,平台无关,可扩展的序列化数据的格式,可用于通信协议,数据存储等. Protocol buffers ...

  6. cout的输出格式初探3

    #include <iostream> #include <iomanip> using namespace std; int main() { double f=2.0/3. ...

  7. AndEngine中文文档下载地址

    AndEngine doc  downloadhere 下载地址:http://pan.baidu.com/s/1bnjcL0V 文档是由github仓库AndEngine的代码生成. 本doc中包括 ...

  8. OTL使用指南

    1 OTL简介 OTL 是 Oracle, Odbcand DB2-CLI Template Library 的缩写,是一个C++编译中操控关系数据库的模板库,它目前几乎支持当前所有的各种主流数据库, ...

  9. go语言基础之二维数组

    1.二维数组 示例: package main //必须有个main包 import "fmt" func main() { //有多少个[]就是多少维 //有多少个[]就用多少个 ...

  10. linux系统下调度数据库类型资源库中的kettle job

    已经存在kettle的一个资源库enfo,在目录/works/wxj下面有一个job (testmailsuccess.kjb)如何实现手工在kettle外部执行此job和让系统每天定时的调用此job ...