iT邦幫忙

0

leetcode 365天 #Day118

  • 分享至 

  • xImage
  •  

繼續努力開寫!有時候一些題目寫不出來,不是自己沒學過該知識,而是思路整個是錯的。
這種狀況下更要好好檢討自己,避免下次又走錯路。
Yes


  1. Perform String Shifts (easy)

https://leetcode.com/problems/perform-string-shifts/
Submission:https://leetcode.com/problems/perform-string-shifts/submissions/858335703/

You are given a string s containing lowercase English letters, and a matrix shift, where shift[i] = [directioni, amounti]:

directioni can be 0 (for left shift) or 1 (for right shift).
amounti is the amount by which string s is to be shifted.
A left shift by 1 means remove the first character of s and append it to the end.
Similarly, a right shift by 1 means remove the last character of s and add it to the beginning.
Return the final string after all operations.

簡單來說,shift裡面會控制s字串裡的字元要左移還是右移,要回傳最終結果

class Solution:
    def stringShift(self, s: str, shift: List[List[int]]) -> str:
        move = 0
        for i in shift:
            if i[0] == 0:
                move -= i[1]
            else:
                move += i[1]
        move %= len(s)#別忘記他加減的數字可以很大
        return s[-move:] + s[:-move]
        

  1. Counting Elements (easy)

https://leetcode.com/problems/counting-elements/
Submission:https://leetcode.com/problems/counting-elements/submissions/858337334/

Given an integer array arr, count how many elements x there are, such that x + 1 is also in arr. If there are duplicates in arr, count them separately.
arr裡面有多個整數,檢查裡面有多少個arr[i] + 1的存在

from collections import Counter
class Solution:
    #Counter + loop結束
    def countElements(self, arr: List[int]) -> int:
        arrC = Counter(arr)
        ans = 0
        for key,val in arrC.items():
            if key+1 in arrC:
                ans += val
        return ans

  1. Delete and Earn (medium)

https://leetcode.com/problems/delete-and-earn/
Submission:https://leetcode.com/problems/delete-and-earn/submissions/858851180/

You are given an integer array nums. You want to maximize the number of points you get by performing the following operation any number of times:

Pick any nums[i] and delete it to earn nums[i] points. Afterwards, you must delete every element equal to nums[i] - 1 and every element equal to nums[i] + 1.
Return the maximum number of points you can earn by applying the above operation some number of times.

從nums裡面抓取其中一個值,獲得該值後刪除他並且刪去比他大1點以及少1點的「全部」的值,最後計算出總合為多少。
這題我卡很久,最後是稍微偷看提示,有人說跟某題很像。然後才靈光一閃想到可以怎麼做。但我覺得我該學習的不是這題要怎麼做,而是要分辨出這題應該要用什麼方法。

from collections import Counter
class Solution:
    #從nums裡面抓取其中一個值
    #獲得該值後刪除他並且刪去比他大1點以及少1點的「全部」的值

    #下次要重寫,這題是經過提醒才寫出來的
    #跟house robber一樣要用跳過的技巧
    def deleteAndEarn(self, nums: List[int]) -> int:
        nC = Counter(nums)
        numSet = sorted(set(nums))
        if len(numSet) == 1:
            return numSet[0]*nC[numSet[0]]
        dp = [0]*len(numSet)
        dp[0] = numSet[0]*nC[numSet[0]]
        if numSet[1] == numSet[0] + 1:
            dp[1] = max(numSet[1]*nC[numSet[1]],dp[0])
        else:
            dp[1] = dp[0] + numSet[1]*nC[numSet[1]]
        for i in range(2,len(numSet)):
            if numSet[i] == numSet[i-1] + 1:
                dp[i] = max(dp[i-1],dp[i-2] + numSet[i]*nC[numSet[i]])
            else:
                dp[i] = dp[i-1] + numSet[i]*nC[numSet[i]]
        return dp[-1]

  1. Maximum Sum Circular Subarray (medium)

https://leetcode.com/problems/maximum-sum-circular-subarray/
Submission:https://leetcode.com/problems/maximum-sum-circular-subarray/submissions/858312500/

Given a circular integer array nums of length n, return the maximum possible sum of a non-empty subarray of nums.

A circular array means the end of the array connects to the beginning of the array. Formally, the next element of nums[i] is nums[(i + 1) % n] and the previous element of nums[i] is nums[(i - 1 + n) % n].

A subarray may only include each element of the fixed buffer nums at most once. Formally, for a subarray nums[i], nums[i + 1], ..., nums[j], there does not exist i <= k1, k2 <= j with k1 % n == k2 % n.

這次又是死在環形題目,感覺太少思考這東西,直接被刷掉,算是之後必要重新思考的題目

from collections import deque
class Solution:
    #變成環形的

    #做兩次?
    #下次寫到要重寫
    def maxSubarraySumCircular(self, nums: List[int]) -> int:
        nL = len(nums)
        queue = deque([(0, -1)])  #(總和,索引值)
        ans = nums[0]
        total = 0
        for i in range(2*nL):
            total += nums[i % nL] #以nL為循環,所以 i % nL
            if i - queue[0][1] > nL:  #如果最後面的數字抓到前面的數字就要把前面的數字pop掉
                queue.popleft()
            ans = max(ans, total - queue[0][0])  #每次都去比較大小
            while queue and queue[-1][0] >= total:  
                #如果queue裡有東西
                #因為queue[i][0]存的都是prefix,那要比較大小的時候一定是扣掉小一點prefix,這樣反彈更大
                #所以大的可以pop掉
                queue.pop()
            queue.append((total, i))
        return ans
        

以上就是今天的練習


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

尚未有邦友留言

立即登入留言