aboutsummaryrefslogtreecommitdiff
path: root/works/solutions/leetcode/cpp/121.cpp
blob: 8561fa31ea13fa4256589d030654e56a5ef63ebc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <vector>

using std::vector;

class Solution
{
public:
    int maxProfit(vector<int> &prices)
    {
        if (prices.size() <= 1) return 0;

        int result = 0;
        int min = prices.front();
        for (int i = 1; i < prices.size(); i++)
        {
            if (prices[i] - min > result)
                result = prices[i] - min;
            if (prices[i] < min)
                min = prices[i];
        }

        return result;
    }
};