본문 바로가기
python-algorithm

leetcode 121. Best Time to Buy and Sell Stock

by 무적김두칠 2023. 2. 11.

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

 

Best Time to Buy and Sell Stock - LeetCode

Best Time to Buy and Sell Stock - You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        profit, min_price = 0, sys.maxsize
 
        for price in prices:
            min_price = min(min_price, price)
            profit = max(profit, price-min_price)
 
        return profit
 
cs
반응형

댓글