蓝桥杯 第三十二天 瞎练练

目录

1.55. 跳跃游戏 - 力扣(LeetCode) (leetcode-cn.com)

(1)DP

(2)贪心

2.写文章-CSDN博客

3.918. 环形子数组的最大和 - 力扣(LeetCode) (leetcode-cn.com)

4.152. 乘积最大子数组 - 力扣(LeetCode) (leetcode-cn.com)

5.1567. 乘积为正数的最长子数组长度 - 力扣(LeetCode) (leetcode-cn.com)

6.55. 跳跃游戏 - 力扣(LeetCode) (leetcode-cn.com)


1.55. 跳跃游戏 - 力扣(LeetCode) (leetcode-cn.com)

动归的话python超时了,要用贪心

(1)DP

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        n=len(nums)
        dp=[False for i in range(n)]
        dp[0]=True
        for i in range(n):
            if dp[i]:
                for j in range(1,nums[i]+1):
                    if i+j

(2)贪心

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        n=len(nums)
        if n==1:
            return True
        cur=0
        ne=0
        while nums[cur]!=0:
            maxlength=-1
            for i in range(1,nums[cur]+1):
                # print(cur, cur + i + nums[cur + i], maxlength, ne)
                if cur+imaxlength:
                    maxlength=cur+i+nums[i+cur]
                    ne=cur+i

            cur=ne
            if cur+nums[cur]>=n-1:
                return True
        return False

2.写文章-CSDN博客

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        n=len(nums)
        dp=[0 for i in range(n)]
        dp[0]=nums[0]
        for i in range(1,n):
            if dp[i-1]<0:
                dp[i]=nums[i]
            else:
                dp[i]=dp[i-1]+nums[i]
        return max(dp)

3.918. 环形子数组的最大和 - 力扣(LeetCode) (leetcode-cn.com)

class Solution:
    def maxSubarraySumCircular(self, nums: List[int]) -> int:
        n=len(nums)
        dpmax=[0 for i in range(n)]
        dpmax[0]=nums[0]
        for i in range(1,n):
            if dpmax[i-1]<0:
                dpmax[i]=nums[i]
            else:
                dpmax[i]=dpmax[i-1]+nums[i]
        dpmin=[0 for i in range(n)]
        dpmin[0]=nums[0]
        for i in range(1,n):
            if dpmin[i-1]>0:
                dpmin[i]=nums[i]
            else:
                dpmin[i]=dpmin[i-1]+nums[i]
        if sum(nums)-min(dpmin)==0:
            return max(dpmax)
        return max(max(dpmax),sum(nums)-min(dpmin))

4.152. 乘积最大子数组 - 力扣(LeetCode) (leetcode-cn.com)

class Solution:
    def maxProduct(self, nums: List[int]) -> int:
        n=len(nums)
        dpmax=[-1<<31 for i in range(n)]
        dpmin=[1<<31 for i in range(n)]
        dpmax[0]=nums[0]
        dpmin[0]=nums[0]
        for i in range(1,n):
            dpmax[i]=max(dpmax[i-1]*nums[i],nums[i],dpmin[i-1]*nums[i])
            dpmin[i]=min(dpmin[i-1]*nums[i],nums[i],dpmax[i-1]*nums[i])
        return max(dpmax)

5.1567. 乘积为正数的最长子数组长度 - 力扣(LeetCode) (leetcode-cn.com)

class Solution:
    def getMaxLen(self, nums: List[int]) -> int:
        n=len(nums)
        dpplus=[0 for i in range(n)]
        dpminus=[0 for i in range(n)]
        if nums[0]>0:
            dpplus[0]+=1
        elif nums[0]<0:
            dpminus[0]+=1
        for i in range(1,n):
            if nums[i]>0:
                dpplus[i]=dpplus[i-1]+1
                if dpminus[i-1]!=0:
                    dpminus[i]=dpminus[i-1]+1
            elif nums[i]<0:
                if dpminus[i-1]!=0:
                    dpplus[i]=dpminus[i-1]+1
                dpminus[i]=dpplus[i-1]+1
        return max(dpplus)

6.55. 跳跃游戏 - 力扣(LeetCode) (leetcode-cn.com)

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        n=len(nums)
        if n==1:
            return True
        cover=1+nums[0]
        i=1
        while i=n:
                return True
            i+=1
        return False  

你可能感兴趣的:(动态规划,leetcode,算法)