From 12a0147e0a93cccad3316d5abae34e5b20fa9cda Mon Sep 17 00:00:00 2001 From: gyeo-ri Date: Sat, 4 Apr 2026 21:57:44 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20if/else=20=EB=AC=B8=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- best-time-to-buy-and-sell-stock/gyeo-ri.py | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/gyeo-ri.py diff --git a/best-time-to-buy-and-sell-stock/gyeo-ri.py b/best-time-to-buy-and-sell-stock/gyeo-ri.py new file mode 100644 index 0000000000..cbb852925d --- /dev/null +++ b/best-time-to-buy-and-sell-stock/gyeo-ri.py @@ -0,0 +1,30 @@ +class Solution: + def maxProfit(self, prices: list[int]) -> int: + max_profit = 0 + buy_at = None + + for p in prices: + if buy_at is None: + buy_at = p + elif buy_at > p: + buy_at = p + elif buy_at < p: + max_profit = max(max_profit, p - buy_at) + + return max_profit + + +if __name__ == "__main__": + test_cases = [ + ([7, 1, 5, 3, 6, 4], 5), + ([7, 6, 4, 3, 1], 0), + ([2, 4, 1], 2), + ] + + solution = Solution() + for idx, case_ in enumerate(test_cases): + prices, answer = case_ + result = solution.maxProfit(prices) + assert ( + answer == result + ), f"Test Case {idx} Failed: Expected {answer}, Got {result}" From ecc9423ba106064e22a4b06f8473c2c139fb4252 Mon Sep 17 00:00:00 2001 From: gyeo-ri Date: Sat, 4 Apr 2026 22:19:27 +0900 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=EC=84=B1=EB=8A=A5=EC=9D=80=20?= =?UTF-8?q?=EB=B9=84=EC=8A=B7=ED=95=98=EB=82=98=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=EA=B0=84=EC=86=8C=ED=99=94=ED=95=9C=20=EB=B2=84?= =?UTF-8?q?=EC=A0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- best-time-to-buy-and-sell-stock/gyeo-ri.py | 25 +++++++++++++++------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/best-time-to-buy-and-sell-stock/gyeo-ri.py b/best-time-to-buy-and-sell-stock/gyeo-ri.py index cbb852925d..8834e7dbb1 100644 --- a/best-time-to-buy-and-sell-stock/gyeo-ri.py +++ b/best-time-to-buy-and-sell-stock/gyeo-ri.py @@ -1,17 +1,25 @@ +""" +[결과 요약] +# 시도한 로직 수: 4 + 1. if/else 문으로 분기 처리(시간: O(n) / 공간: O(1)) + - + 2. if문 대신 min / max를 계속 계산하는 로식 + - None 대신 float(inf)를 사용하여 None 검증 분기 제거 + - inf 사용을 위해서 마지막 return에 int() 타입 변환 필요 + - 성능은 1과 큰 차이 없으며 코드가 간소화 +""" + + class Solution: def maxProfit(self, prices: list[int]) -> int: max_profit = 0 - buy_at = None + min_price = float("inf") for p in prices: - if buy_at is None: - buy_at = p - elif buy_at > p: - buy_at = p - elif buy_at < p: - max_profit = max(max_profit, p - buy_at) + min_price = min(min_price, p) + max_profit = max(max_profit, p - min_price) - return max_profit + return int(max_profit) if __name__ == "__main__": @@ -19,6 +27,7 @@ def maxProfit(self, prices: list[int]) -> int: ([7, 1, 5, 3, 6, 4], 5), ([7, 6, 4, 3, 1], 0), ([2, 4, 1], 2), + ([1, 2, 4, 10000, 2, 1, 3], 9999), ] solution = Solution() From 5cf7923d52ec72f9ec27771d8fd5af7164c4a9d0 Mon Sep 17 00:00:00 2001 From: gyeo-ri Date: Sat, 4 Apr 2026 22:20:19 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=EC=8B=9C=EB=8F=84=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=88=98=20=EC=98=A4=ED=83=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- best-time-to-buy-and-sell-stock/gyeo-ri.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/best-time-to-buy-and-sell-stock/gyeo-ri.py b/best-time-to-buy-and-sell-stock/gyeo-ri.py index 8834e7dbb1..776d59e4fc 100644 --- a/best-time-to-buy-and-sell-stock/gyeo-ri.py +++ b/best-time-to-buy-and-sell-stock/gyeo-ri.py @@ -1,6 +1,6 @@ """ [결과 요약] -# 시도한 로직 수: 4 +# 시도한 로직 수: 2 1. if/else 문으로 분기 처리(시간: O(n) / 공간: O(1)) - 2. if문 대신 min / max를 계속 계산하는 로식