From 817a5fe6bd8ba469e907adbb115c3a587a0e8768 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Fri, 22 Aug 2025 18:29:59 +0900 Subject: [PATCH] =?UTF-8?q?best-time-to-buy-and-sell-stock=20=EB=B3=B5?= =?UTF-8?q?=EC=8A=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- best-time-to-buy-and-sell-stock/hi-rachel.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) 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 풀이