Best Time to Buy and Sell Stock

ㅋㅋ·2023년 2월 25일
0

알고리즘-leetcode

목록 보기
118/135

날짜별 가격이 적힌 정수형 벡터 prices를 받는다.

한 번 사서 한 번 팔 때, 가장 큰 이득을 구하는 문제

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        
        int lower{numeric_limits<int>::max()};
        int result{0};
        for (auto& price : prices)
        {
            result = std::max(result, price - lower);

            lower = std::min(lower, price);
        }

        return result;
    }
};

0개의 댓글