Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions best-time-to-buy-and-sell-stock/hi-rachel.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
# 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:
max_profit = 0
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 풀이
Expand Down