Home Leetcode 121 Best Time to Buy and Sell Stock
Post
Cancel

Leetcode 121 Best Time to Buy and Sell Stock

Best Time to Buy and Sell Stock problem results

Best Time to Buy and Sell Stock

Result

First Blood

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
    class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int lessest = INT_MAX;
        int max = 0;
        int curr = 0;
        
        for(int i = 0; i < prices.size(); i++)
        {
            if(prices[i] < lessest)
            {
                lessest = prices[i];
            }
            
            curr = prices[i] - lessest;
            
            if(curr > max)
            {
                max = curr;
            }
        }
        
        return max;
    }
};
}

Double Kill

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int lessest = INT_MAX;
        int max_profit = 0;
        
        for(int i = 0; i < prices.size(); i++)
        {
            lessest = min(lessest,prices[i]);
            max_profit = max(max_profit,prices[i] - lessest);
        }
        
        return max_profit;
    }
};
}
This post is licensed under CC BY 4.0 by the author.
Trending Tags