์•Œ๊ณ ๋ฆฌ์ฆ˜๐Ÿฅš/๋ฌธ์ œํ’€์ด (Python)

[LeetCode] 121. Best_Time_to_Buy_and_Sell_Stock (Easy) 2023/5/3

๐Ÿช„ํ•˜๋ฃจ๐Ÿช„ 2023. 5. 3. 09:21
728x90
๋ฌธ์ œ ์ œ๋ชฉ ์ •๋‹ต๋ฅ  ๋‚œ์ด๋„
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]

 

728x90