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 复制代码

相关推荐
?!714几秒前
算法打卡第18天
c++·算法
springfe010113 分钟前
构建大顶堆
前端·算法
凌辰揽月1 小时前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
lifallen1 小时前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法
jingfeng5141 小时前
数据结构排序
数据结构·算法·排序算法
能工智人小辰1 小时前
Codeforces Round 509 (Div. 2) C. Coffee Break
c语言·c++·算法
kingmax542120081 小时前
CCF GESP202503 Grade4-B4263 [GESP202503 四级] 荒地开垦
数据结构·算法
岁忧1 小时前
LeetCode 高频 SQL 50 题(基础版)之 【高级字符串函数 / 正则表达式 / 子句】· 上
sql·算法·leetcode
eachin_z2 小时前
力扣刷题(第四十九天)
算法·leetcode·职场和发展
闻缺陷则喜何志丹2 小时前
【强连通分量 缩点 拓扑排序】P3387 【模板】缩点|普及+
c++·算法·拓扑排序·洛谷·强连通分量·缩点