# leetcode中的股票问题（1）

## 原题

Say you have an array for which the ith$i^{th}$ element is the price of a given stock on day i$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.

## 代码

public int maxProfit(int[] prices) {
//less then two days, no game
if(prices.length < 2)
{
return 0;
}

//tag to maintian the min price
int leastTag = prices[0];
//tag to maintian the max profit
int profit = 0;
for(int i = 1; i < prices.length; i++)
{
if( prices[i] - leastTag > profit)
{
profit = prices[i] - leastTag;
}
//else means price[i] < profit + leastTag
//but not necessarily price[i] < leastTag
else if(prices[i] < leastTag)
{
leastTag = prices[i];
}
}
return profit;
}

#### leetcode【121+122+123 best time to buy and sell stock】【python】

2016-09-12 21:04:35

#### 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

2015-04-06 11:10:34

#### Best Time to Buy and Sell Stock -- LeetCode

2014-04-09 05:33:32

#### [leetcode] 309. Best Time to Buy and Sell Stock with Cooldown 解题报告

2016-06-14 14:53:30

#### [LeetCode] Best Time to Buy and Sell Stock IV

2015-02-23 00:08:34

#### Leetcode_122_Best Time to Buy and Sell Stock II

2015-01-26 18:28:17

#### leetcode_c++：Best Time to Buy and Sell Stock II（122）

2016-06-10 23:19:48

#### 关于LeetCode中Best Time to Buy and Sell Stock一题的理解

2016-09-09 13:55:36

#### 【LeetCode】【Python题解】Best Time to Buy and Sell Stock II

2014-07-24 10:11:44

#### weekly contest 55 Best Time to Buy and Sell Stock with Transaction Fee

2017-10-23 14:21:20

## 不良信息举报

Best Time to Buy and Sell Stock