diff --git a/best-time-to-buy-and-sell-stock/hi-rachel.py b/best-time-to-buy-and-sell-stock/hi-rachel.py index e58cb6791..ff5a05bb7 100644 --- a/best-time-to-buy-and-sell-stock/hi-rachel.py +++ b/best-time-to-buy-and-sell-stock/hi-rachel.py @@ -1,4 +1,16 @@ -# TC: O(N), SC: O(1) +""" +https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ + +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 stock. + +Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. + +TC: O(N), SC: O(1) +""" + +from typing import List class Solution: def maxProfit(self, prices: List[int]) -> int: @@ -6,8 +18,9 @@ def maxProfit(self, prices: List[int]) -> int: min_price = prices[0] for price in prices: - max_profit = max(price - min_price, max_profit) min_price = min(price, min_price) + max_profit = max(price - min_price, max_profit) + return max_profit # TS 풀이