9.14--贪心算法列题

455.分发饼干

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        res = 0
        g.sort()
        s.sort()

        g_l=len(g)
        s_l=len(s)

        i=0
        j=0
        while i

122.​​​​​​买卖股票的最佳时机 II

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        res=0
        for i in range(1,len(prices)):
            if prices[i]>prices[i-1]:
                res+=(prices[i]-prices[i-1])
            else:
                continue
        return res

55.跳跃游戏 

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        if nums==None:
            return False

        n_l=len(nums)
        end_num=n_l-1


        for i in range(n_l-1,-1,-1):
            if (i+nums[i])>=end_num:
                end_num=i

                print(end_num)
            
        if end_num==0:

            return True
        return False

 

你可能感兴趣的:(贪心算法,算法,python)