iT邦幫忙

0

leetcode 365天 #Day121

  • 分享至 

  • xImage
  •  

在做題目的過程中,真的看到很多沒有見識過的題目,也引發了很多不同的想法。
我平常的思考方式太過直接,要多多思考
Yes


  1. Best Time to Buy and Sell Stock with Cooldown (medium)

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/
Submission:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/submissions/859890321/

You are given an array prices where prices[i] is the price of a given stock on the ith day.

Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:

After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

這題加了一個賣出冷卻時間。
感覺好像沒有變化多少,但個人認為變化超級大,瞬間變成一個我幾乎看不懂的題目。
後來大概才思考明白,這題要當成三個狀態來變換以及看待。

class Solution:
    #這題要重做
    #買賣股票
    #買完股票後必須賣掉才可以再買
    #賣完股票後必須冷卻至少一天
    #buy, sell, cooldown
    #必定會是這三種動作
    #也就是說在這一格時可能會有以下狀態
    #買股票
    #持有股票
    #賣股票
    #賣完後的冷卻
    #空手
    #------
    #購買後需要扣錢 no 空手->持有 hold 
    #賣完後冷卻必空手 cooldown -> no #會造成cooldown狀態是因為hold的股票在這時候賣掉
    def maxProfit(self, prices: List[int]) -> int:
        pL = len(prices)
        if pL < 2:
            return 0
        #這三個變數是在記錄,目前在這一格時所做的事情造成的最大利益
        no = 0
        hold = -8000 #一開始一定不會持有
        cooldown = -8000 #一開始不會在冷卻中
        for i in prices:
            hold = max(hold,no - i)#目前持有的狀態,跟購買股票後的狀態
            #在第一天時會優先持有一張,後續進行比較,若下一張更便宜就會持有下一張
            no = max(no,cooldown)#進入no狀態時的所持有最大的錢,因為錢是從cooldown來的
            #所以到下次迴圈時,可以直接從no扣錢變成hold狀態
            cooldown = hold + i #假設賣掉後的狀態,這邊儲存在這格賣掉後的錢
            print(hold,no,cooldown)
        return max(no,cooldown)#一定是在賣掉的狀態,或者是賣後在冷卻的狀態下會有比較多錢

  1. Best Time to Buy and Sell Stock with Transaction Fee (medium)

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/
Submission:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/submissions/859896621/

You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee.

Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

每次賣股票都會被課稅,重點有兩個,降低被課稅的次數,當有下一個高點時,如何買回來然後不被課稅

class Solution:
    #這題未來再練習一次
    #每筆交易都會被課fee的稅收
    #交易次數也要壓低
    #所以應該在購買後,把這個fee加到下次購買時的成本裡
    def maxProfit(self, prices: List[int], fee: int) -> int:
        left,right = 0,1
        ans = 0
        minPrices = prices[0]
        for i in prices:
            if i < minPrices:#如果後面的東西便宜,當然換後面東西的價格
                minPrices = i
            elif i > minPrices + fee:#賣掉時不會變成負的
                ans += i - minPrices - fee#賺到的錢
                minPrices = i - fee#如果後面有更大的數字,我不希望重複計算fee
                #所以先暫時把商品價格變成少掉fee的狀態
                #這樣後面就算是價格繼續往上遞增,也不會重複地被扣fee
                #那如果後面有比被扣掉fee的商品還要更低的價格
                #那當然是選擇那更低的
        return ans


  1. Array Transformation (easy)

Given an initial array arr, every day you produce a new array using the array of the previous day.

On the i-th day, you do the following operations on the array of day i-1 to produce the array of day i:

If an element is smaller than both its left neighbor and its right neighbor, then this element is incremented.
If an element is bigger than both its left neighbor and its right neighbor, then this element is decremented.
The first and last elements never change.
After some days, the array does not change. Return that final array.

這題就單純比大小而已,如果四周的數字比自己大,自己加一,反之自己減一。
重點是紀錄完狀態才能去做加減法

class Solution:
    #比大小,比旁邊都小自己+1,比旁邊都大自己-1
    #重點是,是全部比較完後才改變狀態
    def transformArray(self, arr: List[int]) -> List[int]:
        situation = [0]*len(arr)
        while 1:
            change = 1
            for i in range(1,len(arr)-1):
                if arr[i-1]>arr[i] and arr[i+1]>arr[i]:
                    situation[i] = 1
                    change = 0
                elif arr[i-1]<arr[i] and arr[i+1]<arr[i]:
                    situation[i] = -1
                    change = 0
            if change:
                break
            for i in range(1,len(arr)-1):
                arr[i] += situation[i]
                situation[i] = 0
        return arr
                

  1. Check if an Array Is Consecutive (easy)

Given an integer array nums, return true if nums is consecutive, otherwise return false.

An array is consecutive if it contains every number in the range [x, x + n - 1] (inclusive), where x is the minimum number in the array and n is the length of the array.

class Solution:
    #找出array的範圍是否在 x~x+n-1之間
    #x必為array裡最小數
    #n為array的長度
    def isConsecutive(self, nums: List[int]) -> bool:
        x,nL = min(nums),len(nums)
        return sorted(nums) == list(range(x,x+nL))

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言