코테준비 - Best Time to Buy and Sell Stock

정상화·2023년 2월 26일

LeetCode

목록 보기
118/222

Best Time to Buy and Sell Stock

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int minVal = 20000;

        int maxProfit = -20000;
        for (int i = 0; i < prices.size(); i++) {
            minVal = min(minVal, prices[i]);
            maxProfit = max(maxProfit, prices[i] - minVal);
        }

        return maxProfit <= 0 ? 0 : maxProfit;
    }
};
profile
백엔드 희망

0개의 댓글