LEETCODE-DAY31


title: LEETCODE-DAY31
date: 2024-03-22 19:50:30
tags:

今日内容:贪心算法455.分发饼干、376. 摆动序列、53. 最大子序和

T1

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        count=0
        i=len(g)-1
        j=len(s)-1
        while j>=0:
            while i>=0:
                if s[j]>=g[i]:
                    count+=1
                    break
                i-=1
            j-=1
        return count

g =
[1,2]
s =
[1,2,3]
输出
3
预期结果
2

break后的i-=1没执行

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        count=0
        i=len(g)-1
        j=len(s)-1
        while j>=0:
            while i>=0:
                if s[j]>=g[i]:
                    count+=1
                    i-=1
                    break
                i-=1
            j-=1
        return count

AC


T2

class Solution:
    def wiggleMaxLength(self, nums: List[int]) -> int:
        if len(nums) <= 1:
            return len(nums)  # 如果数组长度为0或1,则返回数组长度
        preDiff,curDiff ,result  = 0,0,1  #题目里nums长度大于等于1,当长度为1时,其实到不了for循环里去,所以不用考虑nums长度
        for i in range(len(nums) - 1):
            curDiff = nums[i + 1] - nums[i]
            if curDiff * preDiff <= 0 and curDiff !=0:  #差值为0时,不算摆动
                result += 1
                preDiff = curDiff  #如果当前差值和上一个差值为一正一负时,才需要用当前差值替代上一个差值
        return result

T3

class Solution:
    def maxSubArray(self, nums):
        result = float('-inf')  # 初始化结果为负无穷大
        count = 0
        for i in range(len(nums)):
            count += nums[i]
            if count > result:  # 取区间累计的最大值(相当于不断确定最大子序终止位置)
                result = count
            if count <= 0:  # 相当于重置最大子序起始位置,因为遇到负数一定是拉低总和
                count = 0
        return result

相关推荐

  1. LEETCODE-DAY31

    2024-03-24 20:16:02       37 阅读
  2. LeetCode day30

    2024-03-24 20:16:02       71 阅读
  3. LEETCODE-DAY32

    2024-03-24 20:16:02       38 阅读
  4. LEETCODE-DAY35

    2024-03-24 20:16:02       42 阅读
  5. LEETCODE-DAY36

    2024-03-24 20:16:02       38 阅读
  6. LEETCODE-DAY37

    2024-03-24 20:16:02       34 阅读
  7. LEETCODE-DAY38

    2024-03-24 20:16:02       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-24 20:16:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-24 20:16:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-24 20:16:02       82 阅读
  4. Python语言-面向对象

    2024-03-24 20:16:02       91 阅读

热门阅读

  1. MONSD和SSD的区别

    2024-03-24 20:16:02       47 阅读
  2. 新概念英语1:Lesson11学习笔记

    2024-03-24 20:16:02       45 阅读
  3. ChatGPT:提升论文写作能力

    2024-03-24 20:16:02       38 阅读
  4. Rust 语言中 Vec 的元素的删除方法

    2024-03-24 20:16:02       38 阅读
  5. 【深度学习】NestedTensors

    2024-03-24 20:16:02       35 阅读
  6. ubuntu安装k8s

    2024-03-24 20:16:02       34 阅读
  7. 用 Delphi 做 FTP 服务器以及如何配置防火墙

    2024-03-24 20:16:02       41 阅读
  8. spring boot整合elasticsearch实现查询功能

    2024-03-24 20:16:02       41 阅读
  9. vim | vim的快捷命令行

    2024-03-24 20:16:02       38 阅读
  10. 阅读 MySQL知识1

    2024-03-24 20:16:02       37 阅读