# Middle-题目23：121. 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.

public class Solution {
public int maxProfit(int[] prices) {
if(prices.length == 0)
return 0;
int min = prices[0];
int profit = 0;
for(int price:prices) {
if(price < min)
min = price;
if(price - min > profit)
profit = price - min;
}
return profit;
}
}

2ms,beats 60.20%,众数3ms,44.72%
cmershen的碎碎念：

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

2016-09-12 21:04:35

#### LeetCode(121)Best Time to Buy and Sell Stock

2014-01-21 02:39:12

#### 121. Best Time to Buy and Sell Stock [easy] (Python)

2016-05-28 01:02:17

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

2016-06-10 23:19:48

#### 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock（最佳买卖股票的时间）】

2015-08-14 06:16:32

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

2016-09-09 13:55:36

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

2015-04-06 11:10:34

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

2014-07-24 10:11:44

#### 动态规划-121. Best Time to Buy and Sell Stock

2017-10-08 09:57:08

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

2016-06-14 14:53:30