[Python] LeetCode 121 Best Time to Buy and Sell Stock (DP)

์„ ์ฃผยท2021๋…„ 12์›” 24์ผ
0

Python PS

๋ชฉ๋ก ๋ณด๊ธฐ
6/65
post-thumbnail

๐Ÿ“Œ ๋ฌธ์ œ

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.

์˜ˆ์ œ 1

Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

์˜ˆ์ œ 2

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.

์กฐ๊ฑด

  • 1 <= prices.length <= 105
  • 0 <= prices[i] <= 104

๐Ÿ“Œ ํ’€์ด

๐Ÿ‘† ์ฒซ ๋ฒˆ์งธ ์‹œ๋„ (์‹œ๊ฐ„์ดˆ๊ณผ)

  • profit : i๋ฒˆ์งธ ๋‚ ์— ํŒ”์•˜์„ ๋•Œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์ˆ˜์ต
  • (i๋ฒˆ์งธ ๋‚ ์˜ ๊ฐ€๊ฒฉ) - (0~i-1๋ฒˆ์งธ ๋‚  ์ค‘ ๊ฐ€๊ฒฉ์ด ๊ฐ€์žฅ ๋‚ฎ์•˜๋˜ ๋‚ ์˜ ๊ฐ€๊ฒฉ)์„ profit์— ์ €์žฅํ•˜๋˜ ํ•ด๋‹น ๊ฐ’์ด ์Œ์ˆ˜์ด๋ฉด 0์„ ์ €์žฅํ•œ๋‹ค.
  • price[0:i]๋กœ ์Šฌ๋ผ์ด์‹ฑํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค.
class Solution(object):
    def maxProfit(self, price):
        profit = [0] * len(price)
        
        for i in range(1, len(price)):
            profit[i] = max(0, price[i] - min(price[0:i]))
        
        return max(profit)

โœŒ ๋‘ ๋ฒˆ์งธ ์‹œ๋„ (์„ฑ๊ณต)

  • profit : i๋ฒˆ์งธ ๋‚ ์— ํŒ”์•˜์„ ๋•Œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์ˆ˜์ต
  • minprice[i] : 0~i-1๋ฒˆ์งธ ๋‚  ์ค‘ ๊ฐ€๊ฒฉ์ด ๊ฐ€์žฅ ๋‚ฎ์•˜๋˜ ๋‚ ์˜ ๊ฐ€๊ฒฉ
  • ์œ„์˜ ๋ฐฉ๋ฒ•๊ณผ ์•„์ด๋””์–ด๋Š” ๋™์ผํ•˜๋‚˜ minprice๋ฅผ ์Šฌ๋ผ์ด์‹ฑ์ด ์•„๋‹Œ ์›์†Œ๋น„๊ต๋กœ ๋ณ€๊ฒฝํ–ˆ๋‹ค.
class Solution(object):
    def maxProfit(self, price):
        
        if len(price) == 1:
            return 0
        elif len(price) == 2:
            return max(0, price[1] - price[0])
        else:
            minprice = [0] * len(price)
            profit = [0] * len(price)
            
            minprice[0] = 0
            profit[0] = 0
            
            minprice[1] = price[0]
            profit[1] = max(0, price[1] - minprice[1])

            for i in range(2, len(price)):
                minprice[i] = min(minprice[i-1], price[i-1])
                profit[i] = max(0, price[i] - minprice[i])

            return max(profit)

๊ทธ์น˜๋งŒ ํšจ์œจ์ ์ด์ง€ ์•Š์•„ ๋‹ค์‹œ ํ’€๊ณ  ์‹ถ์—ˆ๋‹ค -.- ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ์ฝ์–ด๋ณด๋‹ค๊ฐ€ ์ด๊ฑด ๊ตณ์ด DP๋กœ ํ’€ ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ๊ฑธ ์•Œ์•˜๋‹ค.

โœŒ ์„ธ ๋ฒˆ์งธ ์‹œ๋„ (์„ฑ๊ณต)

  • minprice : 0~i๋ฒˆ์งธ ๋‚  ์ค‘ ๊ฐ€๊ฒฉ์ด ๊ฐ€์žฅ ๋‚ฎ์€ ๋‚ ์˜ ๊ฐ€๊ฒฉ
  • 0 <= prices[i] <= 10^4๋ผ๋Š” ์กฐ๊ฑด์ด ์ฃผ์–ด์กŒ์œผ๋ฏ€๋กœ minprice๋ฅผ price์˜ ์ตœ๋Œ“๊ฐ’์ธ 10^4๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  for๋ฌธ์„ ๋Œ๋ฉด์„œ ์—…๋ฐ์ดํŠธํ•ด์ค€๋‹ค.
class Solution(object):
    def maxProfit(self, price):
        
        minprice = 10000
        profit = 0
        
        for i in price:
            profit = max(profit, i - minprice)
            if i < minprice:
                minprice = i
            
        return profit

ํŽธ-์•ˆ

profile
๊ธฐ๋กํ•˜๋Š” ๊ฐœ๋ฐœ์ž ๐Ÿ‘€

0๊ฐœ์˜ ๋Œ“๊ธ€