LeetCode 每日一题 2024/5/27-2024/6/2

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步


目录

      • [5/27 2028. 找出缺失的观测数据](#5/27 2028. 找出缺失的观测数据)
      • [5/28 2951. 找出峰值](#5/28 2951. 找出峰值)
      • [5/29 2981. 找出出现至少三次的最长特殊子字符串 I](#5/29 2981. 找出出现至少三次的最长特殊子字符串 I)
      • [5/30 2982. 找出出现至少三次的最长特殊子字符串 II](#5/30 2982. 找出出现至少三次的最长特殊子字符串 II)
      • [5/31 2965. 找出缺失和重复的数字](#5/31 2965. 找出缺失和重复的数字)
      • [6/1 2928. 给小朋友们分糖果 I](#6/1 2928. 给小朋友们分糖果 I)
      • 6/2

5/27 2028. 找出缺失的观测数据

算出当前的总和sum(rolls)

算出m+n次的总和mean*(m+n)

后者减去前者就是n次总和s

如果s<n 或者s>6*n 那么不可能存在

先假定n次都是1

再从头开始将1变成6 如果剩余值不能变六就变成剩余值 结束操作

python 复制代码
def missingRolls(rolls, mean, n):
    """
    :type rolls: List[int]
    :type mean: int
    :type n: int
    :rtype: List[int]
    """
    s = mean*(n+len(rolls))-sum(rolls)
    if s<n or s>6*n:
        return []
    ans =[1]*n
    s-=n
    loc = 1
    while s:
        if s>=5:
            ans[loc]+=5
            loc+=1
            s-=5
        else:
            ans[loc]+=s
            break
    return ans

5/28 2951. 找出峰值

遍历数组

判断当前位置i的值是否大于左右相邻的值

python 复制代码
def findPeaks(mountain):
    """
    :type mountain: List[int]
    :rtype: List[int]
    """
    ans = []
    for i in range(1,len(mountain)-1):
        if mountain[i]>mountain[i-1] and mountain[i]>mountain[i+1]:
            ans.append(i)
    return ans

5/29 2981. 找出出现至少三次的最长特殊子字符串 I

记录每个字符最长的三个长度a>=b>=c

要满足三次出现可以有三种情况a-2,min(a-1,b),c

python 复制代码
def maximumLength(s):
    """
    :type s: str
    :rtype: int
    """
    from collections import defaultdict
    l = defaultdict(list)
    num = 0
    for i,c in enumerate(s):
        num+=1
        if i+1==len(s) or c!=s[i+1]:
            l[c].append(num)
            num=0
    ans = 0
    for a in l.values():
        a.sort(reverse=True)
        a.extend([0,0])
        ans = max(ans,a[0]-2,min(a[0]-1,a[1]),a[2])
    return ans if ans else -1

5/30 2982. 找出出现至少三次的最长特殊子字符串 II

与2981相同

记录每个字符最长的三个长度a>=b>=c

要满足三次出现可以有三种情况a-2,min(a-1,b),c

python 复制代码
def maximumLength(s):
    """
    :type s: str
    :rtype: int
    """
    from collections import defaultdict
    l = defaultdict(list)
    num = 0
    for i,c in enumerate(s):
        num+=1
        if i+1==len(s) or c!=s[i+1]:
            l[c].append(num)
            num=0
    ans = 0
    for a in l.values():
        a.sort(reverse=True)
        a.extend([0,0])
        ans = max(ans,a[0]-2,min(a[0]-1,a[1]),a[2])
    return ans if ans else -1

5/31 2965. 找出缺失和重复的数字

遍历统计每个数出现次数

python 复制代码
def findMissingAndRepeatedValues(grid):
    """
    :type grid: List[List[int]]
    :rtype: List[int]
    """
    n = len(grid)
    m = [0]*(n*n+1)
    for row in grid:
        for v in row:
            m[v]+=1
    
    ans = [0,0]
    for i in range(1,n*n+1):
        if m[i]==2:
            ans[0]=i
        elif m[i]==0:
            ans[1]=i
    return ans

6/1 2928. 给小朋友们分糖果 I

只有三个小朋友 枚举第一个小朋友i个 第二个小朋友j个 第三个小朋友n-i-j个

python 复制代码
def distributeCandies(n, limit):
    """
    :type n: int
    :type limit: int
    :rtype: int
    """
    ans = 0
    for i in range(limit+1):
        for j in range(min(n-i,limit)+1):
            if n-i-j<=limit:
                ans+=1
    return ans

6/2

python 复制代码

相关推荐
琳达爱分享9 分钟前
从单机令牌桶到集群限流:一次配额超发的复盘
后端·算法
range13 分钟前
带TTL的LRU缓存的实现-字节手撕-26/10/19
算法
糖果豆豆14 分钟前
n log n 到底从哪来?排序下界、2-SAT 与 3-SAT、停机问题,卡的是同一件事
算法·架构
地平线开发者16 分钟前
【玩转 征程6 算法工具链:PTQ 工具篇】hb_verifier
算法·自动驾驶
171320330计算机毕设编程17 分钟前
计算机毕业设计 基于springboot+vue的在线观鸟视频播放网站 LW PPT
java·大数据·vue.js·spring boot·算法·课程设计
himobrinehacken1 小时前
位运算揭秘:左右移与符号位陷阱
c语言·c++·算法·安全
INGNIGHT1 小时前
815 · 课程表IV(dfs)
算法·深度优先
文丑颜不良啊2 小时前
LeetCode1~50题目解析
leetcode
Pniubi2 小时前
力扣146.LRU缓存机制
算法·leetcode·缓存
云上工程笔记2 小时前
2026年7B到67B模型训练与推理GPU配置评测:LoRA、量化与全量微调对比
人工智能·算法·机器学习