๋ฌธ์ ์ ๋ชฉ | ์ ๋ต๋ฅ | ๋์ด๋ |
121. Best-Time-to-Buy-and-Sell-Stock | 54.3% | Easy |
Best Time to Buy and Sell Stock - LeetCode
Can you solve this real interview question? 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 choosin
leetcode.com
๋ฌธ์ ์์ฝ
์ฃผ์ ๊ฑฐ๋๋ฅผ ํตํด ์ด์ต์ ์ต๋๊ฐ์ ์ฐ์ถํด๋ผ
์กฐ๊ฑด 1) ์ฃผ์์ ๋งค์ํ ๋ ๊ณผ ๋งค๋งคํ ๋ ์ ์๋ก ๋ฌ๋ผ์ผ ํ๋ค
์กฐ๊ฑด 2) 0 ์ด์์ ๊ฐ์ ๊ฐ์ง๋ ๊ฐ๊ฒฉ์ ๋ฐฐ์ด์ด ์ฃผ์ด์ง๋ค
์กฐ๊ฑด 3) ๋น์ฐํ ๋ง์ด์ง๋ง ๋งค๋งค์ผ์๋ ๋งค์์ผ ์ดํ์ด๋ค
์๊ฐํ๋ ํ์ด 1 (์๊ฐ์ด๊ณผ) : ํ์ฌ ์ธ๋ฑ์ค๋ฅผ ๋งค์(buy)ํ ๋ ๋ก ์๊ฐ
์ต๋ ์ด์ต ๋ฐฐ์ด์ ์ฐ์ถํ์ฌ ํด๋น ๋ฐฐ์ด์ ์ต๋๊ฐ์ ๋ฐํ
์ต๋ ์ด์ต ๋ฐฐ์ด : ํ์ฌ ์ธ๋ฑ์ค๋ฅผ ๊ธฐ์ค์ผ๋ก, ์ค๋ฅธ์ชฝ ๋ถ๋ถ์์ ์ต๋ ์ด์ต์ ์ฐ์ถํ๋ ๊ฐ์์ ํ์ฌ ๊ฐ์ ๋บ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅ. ํ์ฌ ์์ ์ ์ต๋ ์ด์ต์ ์ ์ฅํ๋ ๋ฐฐ์ด
์๊ฐํ๋ ํ์ด 2 : ํ์ฌ ์ธ๋ฑ์ค๋ฅผ ๋งค๋งค(sell)ํ ๋ ๋ก ์๊ฐ
ํ์ฌ ์์ ์์ ์ต์ ๋งค์๋ ์ ๊ตฌํด ์ต๋ ์ด์ต์ ์ ๋ฐ์ดํธ
ํ์ด 1 (์๊ฐ์ด๊ณผ)
ํ์ฌ ์ธ๋ฑ์ค๋ฅผ ๋งค์(buy)ํ ๋ ๋ก ์๊ฐ
Step1. ๋ฐฐ์ด์ด ์ฃผ์ด์ง๋ฉด ๋งค๋๊ฐ ์ต๋๋ ์ ๊ตฌํด ๋บ์ ์ ํ ๋ค profit ๋ฐฐ์ด์ ์ถ๊ฐ -> max(), ์ฌ๋ผ์ด์ฑ ์ด์ฉ
Step2. profit ๋ฐฐ์ด์ ์ต๋๊ฐ ๋ฐํ -> max() ํจ์ ์ด์ฉ
๊ตฌํ
class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit=[]
# Step 1
for buy_day in range(0, len(prices)-1):
profit.append(max(prices[buy_day+1:])-prices[buy_day])
profit.append(0)
# Step 2
return max(profit)
ํ์ด 1์์ ์๊ฐ ์ด๊ณผ๊ฐ ๋ ์ด์ ๋ max ํจ์๊ฐ ๋ฐฐ์ด์ ๊ณ์ ํ์ํ๋ฉด์ ์๊ฐ๋ณต์ก๋๊ฐ $O(n^{2})$์ด ๋์๊ธฐ ๋๋ฌธ์ด๋ผ๊ณ ํ๋จํ๋ค. ๋ฐ๋ผ์ max ํจ์์ ์๊ฐ ๋ณต์ก๋๋ฅผ ์ค์ด๋ ค๋ ๋ค๋ฅธ ์๊ฐ์ ํด ๋ณด์๋ค.
ํ์ด 2 (1070ms)
ํ์ฌ ์ธ๋ฑ์ค๋ฅผ ๋งค๋งค(sell)ํ ๋ ๋ก ์๊ฐ
Step1. ํ์ฌ ์ธ๋ฑ์ค๋ฅผ ๊ธฐ์ค์ผ๋ก ๋งค์ ์ต์๊ฐ๊ณผ ์ด์ต์ ์ต๋๊ฐ์ ๊ณ์ ์ ๋ฐ์ดํธ
๊ตฌํ (1057ms)
class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_price=prices[0]
max_profit=0
# Step 1
for buy_day in range(1, len(prices)):
min_price=min(min_price, prices[buy_day])
max_profit=max(max_profit, prices[buy_day]-min_price)
return max_profit
+์์ธ์ฒ๋ฆฌ 1(909ms)
์ต๋๊ฐ์ด ์ต์๊ฐ ์ค๋ฅธ์ชฝ์ ์์ ๊ฒฝ์ฐ ํด๋น ๊ฐ์ ๋ฐ๋ก ๋ฐํํ๋ค. -> max(), min(), list.index(value) ํจ์ ์ด์ฉ
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# ์์ธ์ฒ๋ฆฌ ์ฝ๋ ์ถ๊ฐ
max_val=max(prices)
min_val=min(prices)
if (len(prices)-1-list(reversed(prices)).index(max_val))>prices.index(min_val):
return max_val-min_val
min_price=prices[0]
max_profit=0
for buy_day in range(1, len(prices)):
min_price=min(min_price, prices[buy_day])
max_profit=max(max_profit, prices[buy_day]-min_price)
return max_profit
๋ฆฌ์คํธ์ ํด๋น ๊ฐ์ด ์๋์ง ํ์ธํ๋ ํจ์ | list.index(value) |
์ค๋ณต๋๋ ์์๋ค์ ์ธ๋ฑ์ค ๋ชจ๋ ์ฐพ๋ ํจ์ | numpy_array=numpy.array(list) numpy.where(numpy_array==๊ฐ)[0] |
'์๊ณ ๋ฆฌ์ฆ๐ฅ > ๋ฌธ์ ํ์ด (Python)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 21. Merge_Two_Sorted_Lists (Easy) 2023/5/5 (0) | 2023.05.05 |
---|---|
[LeetCode] 234. Palindrome_Linked_list (Easy) 2023/5/4 (0) | 2023.05.04 |
[LeetCode] 238. product_of_array_except_self (Medium) 2023/5/2 (0) | 2023.05.02 |
[LeetCode] 561. array_partition_i (Easy) 2023/5/2 (0) | 2023.05.02 |
[LeetCode] 15. 3sum (Medium) 2023/5/1 (0) | 2023.05.01 |