leetcode121

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

class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.empty())
return 0;
int temp = 0, max = 0, min = *prices.begin();
for (vector<int>::iterator it = prices.begin(); it != prices.end()-1; ++it)
{
if (*it > *(it+1))
{
if (temp > max)
max = temp;
temp =0;
if (*(it+1) < min)
min = *(it+1);
}
else
temp = *(it+1) - min;
}
if (temp > max)
max = temp;

return max;
}
};

#### leetcode刷题，总结，记录，备忘 201

2016-04-24 23:45:59

#### leetcode刷题，总结，记录，备忘300

2015-11-08 20:04:14

#### leetcode刷题，总结，记录 ，备忘 172

2015-12-14 20:24:34

#### leetcode刷题总结 1-100

2018-03-08 14:42:57

#### leetcode-刷题经验

2016-08-06 08:16:56

#### 算法刷题LeetCode

2018年01月08日 837KB 下载

#### Leetcode按Tag刷题

2016-05-27 14:31:34

#### 使用python刷Leetcode算法题（第一周）

2017-12-23 15:23:14

#### leetcode刷题， 总结，记录，备忘145

2015-07-09 15:27:07

#### LeetCode刷题总结和技巧归纳

2016-12-29 19:13:04

## 不良信息举报

leetcode刷题， 总结 ，记录 ，备忘 121