iT邦幫忙

0

leetcode 365天 #Day122

  • 分享至 

  • xImage
  •  

不知道為啥0人觀看直播卻6x人點進來的一天
Yes


  1. Maximum Product Subarray (medium)

https://leetcode.com/problems/maximum-product-subarray/description/
Submission:https://leetcode.com/problems/maximum-product-subarray/submissions/860362726/
Given an integer array nums, find a
subarray
that has the largest product, and return the product.

The test cases are generated so that the answer will fit in a 32-bit integer.
算出最大乘積

class Solution:
    #找出最大乘積,麻煩的點在於有負號存在
    #有0時必定切割,乘積裡有0都會變成0
    #有負號時可能需要切割,因為兩個負號會變正
    #我是覺得全部乘起來即可,當有奇數個負號時再進行處理即可
    #1. 有0必處理
    #2. 有奇數個0必處理
    #Error 原本的做法有思考誤區,而且滿大的,只能刪除處理。
    #我少思考到下面所寫的東西,那再怎麼樣都不可能對


    #別人寫的超簡單寫法,思考一下為什麼算雙向的前綴積就可以了?
    #靠邀,想一下大概就通了
    #n為偶數
    #用上面的想法探討假設它有n+1個負數,因為要追求最大數字,所以一定是拿掉最左邊或最右邊的負數
    #0,我上面提到過就是切割的概念,也就是說可以等於為新的數列,那用一樣的做法持續做下去就可以了
    def maxProduct(self, A):
        B = A[::-1]
        for i in range(1, len(A)):
            A[i] = A[i] * (A[i - 1] or 1)#檢查上一個是否為0,若是的話保持自身
            B[i] = B[i] * (B[i - 1] or 1)
        return max(A + B)

  1. Maximum Length of Subarray With Positive Product (medium)

https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/description/
Submission:https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/submissions/860380318/

Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive.

A subarray of an array is a consecutive sequence of zero or more values taken out of that array.

Return the maximum length of a subarray with positive product.
計算若乘積為正時的最大長度,一開始因為跟上一題擺在一起,直接題目看錯。
跟在寫東西時spec看錯一樣只有死字可以寫==

class Solution:
    #152. Maximum Product Subarray 的延伸題
    #我想152做出來這題應該是秒解吧?並沒有==
    #以上為誤會
    #這題只需要最後的數字為正即可
    #所以要從頭到尾找長度
    #注意負數跟0

    #這邊用positive跟negative來記錄目前正數跟負數的長度
    #最好再重寫
    def getMaxLen(self, nums: List[int]) -> int:
        nL = len(nums)
        positive,negative = 0,0
        if nums[0] > 0:
            positive += 1
        elif nums[0] < 0:
            negative += 1
        ans = positive
        for i in range(1,nL):
            if nums[i] > 0:
                positive += 1
                if negative > 0:
                    negative += 1
            elif nums[i] < 0:
                tmp = positive
                if negative > 0:
                    positive = negative + 1
                else:
                    positive = 0
                negative = tmp+1
            else:
                positive,negative = 0,0
            ans = max(ans,positive)
        return ans


  1. Largest Local Values in a Matrix (easy)

https://leetcode.com/problems/largest-local-values-in-a-matrix/description/
Submission:https://leetcode.com/problems/largest-local-values-in-a-matrix/submissions/860385080/

You are given an n x n integer matrix grid.

Generate an integer matrix maxLocal of size (n - 2) x (n - 2) such that:

maxLocal[i][j] is equal to the largest value of the 3 x 3 matrix in grid centered around row i + 1 and column j + 1.
In other words, we want to find the largest value in every contiguous 3 x 3 matrix in grid.

Return the generated matrix.
升成一個答案,為(n-2) x (n-2)矩陣
其中每格大小為原grid[i-1][j-1]~grid[i+1][j+1]的最大值

class Solution:
    #ans要回傳一個(n-2) x (n-2)矩陣
    #其中每格大小為原grid[i-1][j-1]~grid[i+1][j+1]的最大值
    def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
        n = len(grid)-2
        ans = [[0 for i in range(n)] for j in range(n)]
        for i in range(n):
            for j in range(n):
                maxNum = 0
                for k in range(i,i+3):
                    for l in range(j,j+3):
                        maxNum = max(grid[k][l],maxNum)
                ans[i][j] = maxNum
        return ans

  1. Minimum Hours of Training to Win a Competition (easy)

https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition/description/
Submission:https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition/submissions/860390415/

You are entering a competition, and are given two positive integers initialEnergy and initialExperience denoting your initial energy and initial experience respectively.

You are also given two 0-indexed integer arrays energy and experience, both of length n.

You will face n opponents in order. The energy and experience of the ith opponent is denoted by energy[i] and experience[i] respectively. When you face an opponent, you need to have both strictly greater experience and energy to defeat them and move to the next opponent if available.

Defeating the ith opponent increases your experience by experience[i], but decreases your energy by energy[i].

Before starting the competition, you can train for some number of hours. After each hour of training, you can either choose to increase your initial experience by one, or increase your initial energy by one.

Return the minimum number of training hours required to defeat all n opponents.
算是模擬題吧,計算要花多少時間訓練才可以打到最後

class Solution:
    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:
        #重點在於更多能量與經驗,也就是說不能夠剛好達標
        neededEnergy = sum(energy) - initialEnergy + 1
        neededEnergy = max(0,neededEnergy)
        neededEx = 0
        for i in experience:
            if initialExperience > i:
                initialExperience += i
            else:
                neededEx = neededEx + i - initialExperience + 1
                initialExperience += i - initialExperience + 1 + i
        return neededEnergy + neededEx

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

尚未有邦友留言

立即登入留言