LeetCode 每日一题 2024/8/26-2024/9/1

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


目录

      • [8/26 690. 员工的重要性](#8/26 690. 员工的重要性)
      • [8/27 3134. 找出唯一性数组的中位数](#8/27 3134. 找出唯一性数组的中位数)
      • [8/28 3144. 分割字符频率相等的最少子字符串](#8/28 3144. 分割字符频率相等的最少子字符串)
      • [8/29 3142. 判断矩阵是否满足条件](#8/29 3142. 判断矩阵是否满足条件)
      • [8/30 3153. 所有数对中数位不同之和](#8/30 3153. 所有数对中数位不同之和)
      • 8/31
      • 9/1

8/26 690. 员工的重要性

BFS

python 复制代码
class Employee(object):
    def __init__(self, id, importance, subordinates):
        # It's the unique id of each node.
        # unique id of this employee
        self.id = id
        # the importance value of this employee
        self.importance = importance
        # the id of direct subordinates
        self.subordinates = subordinates
        
def getImportance(employees, id):
    """
    :type employees: Employee
    :type id: int
    :rtype: int
    """
    if len(employees)==0:
        return 0
    dic = {}
    for v in employees:
        dic[v.id] = v
    ret = 0
    l =[]
    l.append(id)
    while len(l)>0:
        n = l.pop(0)
        e = dic.get(n)
        ret += e.importance
        l.extend(e.subordinates)
    return ret

8/27 3134. 找出唯一性数组的中位数

共有m=1+2+...+n=n*(n+1)/2个费控连续子数组

中位数是第k=m//2个元素

对于一个子数组如果变长 它的不同元素个数不会变少

check(upper)检查小于upper数有多少个

python 复制代码
def medianOfUniquenessArray(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    import bisect
    from collections import defaultdict
    n=len(nums)
    k=(n*(n+1)//2+1)//2
    def check(upper):
        cnt = 0
        l = 0
        fre = defaultdict(int)
        for r,v in enumerate(nums):
            fre[v] +=1
            while len(fre)>upper:
                out = nums[l]
                fre[out]-=1
                if fre[out]==0:
                    del fre[out]
                l+=1
            cnt += r-l+1
            if cnt>=k:
                return True
        return False
    return bisect.bisect_left(range(len(set(nums))), True,1,key=check)

8/28 3144. 分割字符频率相等的最少子字符串

dp[i]记录以i结尾的前缀字符串最少平和字符串个数

对于每个i j从后往前遍历 m[x]存储字符x出现次数

对于子字符串s[j:i] 记录字符出现最大次数cnt

如果每个字符出现次数相同那么cnt*len(m)=字符串长度i-j+1

python 复制代码
def minimumSubstringsInPartition(s):
    """
    :type s: str
    :rtype: int
    """
    from collections import defaultdict
    n=len(s)
    dp = [float("inf")]*(n+1)
    dp[0]=0
    for i in range(1,n+1):
        m = defaultdict(int)
        cnt = 0
        for j in range(i,0,-1):
            m[s[j-1]]+=1
            cnt = max(cnt,m[s[j-1]])
            if cnt*len(m)==i-j+1 and dp[j-1]!=float("inf"):
                dp[i]=min(dp[i],dp[j-1]+1)
    return dp[n]

8/29 3142. 判断矩阵是否满足条件

判断第一行 相邻是否不同

判断每一列 是否相同

python 复制代码
def satisfiesConditions(grid):
    """
    :type grid: List[List[int]]
    :rtype: bool
    """
    n,m=len(grid),len(grid[0])
    for j in range(m):
        if j>0 and grid[0][j-1]==grid[0][j]:
            return False
        for i in range(1,n):
            if grid[i-1][j]!=grid[i][j]:
                return False
    return True

8/30 3153. 所有数对中数位不同之和

依次统计每一位上所有数值的个数

n=len(nums)

如果数值x出现m次 那么有n-m种情况会出现该位是不同的

累加最后因为重复计算除以二

python 复制代码
def sumDigitDifferences(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    n=len(nums)
    ans = 0
    while nums[0]>0:
        m = [0]*10
        for i in range(n):
            m[nums[i]%10]+=1
            nums[i]//=10
        for x in range(10):
            ans += (n-m[x])*m[x]
    return ans//2
            

8/31

python 复制代码

9/1

python 复制代码

相关推荐
天选之女wow12 分钟前
【代码随想录算法训练营——Day32】动态规划——509.斐波那契数、70.爬楼梯、746.使用最小花费爬楼梯
算法·leetcode·动态规划
java1234_小锋15 分钟前
TensorFlow2 Python深度学习 - TensorFlow2框架入门 - 神经网络基础原理
python·深度学习·tensorflow·tensorflow2
JJJJ_iii16 分钟前
【深度学习03】神经网络基本骨架、卷积、池化、非线性激活、线性层、搭建网络
网络·人工智能·pytorch·笔记·python·深度学习·神经网络
红衣小蛇妖19 分钟前
LeetCode-704-二分查找
java·算法·leetcode·职场和发展
rongqing201924 分钟前
问题记录:一个简单的字符串正则匹配算法引发的 CPU 告警
算法
JJJJ_iii25 分钟前
【深度学习05】PyTorch:完整的模型训练套路
人工智能·pytorch·python·深度学习
无限进步_41 分钟前
C语言字符串与内存操作函数完全指南
c语言·c++·算法
rengang661 小时前
07-逻辑回归:分析用于分类问题的逻辑回归模型及其数学原理
人工智能·算法·机器学习·分类·逻辑回归
Zzzzmo_1 小时前
【Java】杨辉三角、洗牌算法
java·数据结构·算法
闻缺陷则喜何志丹1 小时前
【C++贪心】P10537 [APIO2024] 九月|普及+
c++·算法·贪心·洛谷