최대 1 분 소요

[leetcode] Best Time to Buy and Sell Stock

문제 링크

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

        int ans = 0;
        int minimum = prices[0];
        
        for(int i=1; i<n; i++) {
            if(minimum < prices[i]) {
                ans = max(ans, prices[i] - minimum);
            }
            
            minimum = min(minimum, prices[i]);
        }
        
        return ans;
    }
};

카테고리:

업데이트: