(Python, Java) 리트코드 - best time to buy and sell stock

[문제 링크]

Python 풀이

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        min_price = int(1e9)
        profit = 0

        for price in prices:
            min_price = min(min_price, price)
            profit = max(profit, price - min_price)

        return profit

Java 풀이

class Solution {
    public int maxProfit(int[] prices) {
        int profit = 0;
        int minPrice = Integer.MAX_VALUE;

        for (int price : prices) {
            minPrice = Math.min(price, minPrice);
            profit = Math.max(price - minPrice, profit);
        }
        return profit;
    }
}

© 2021. By Backtony