LeetCode 每日一题 2025/3/31-2025/4/6

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


目录

      • [3/31 2278. 字母在字符串中的百分比](#3/31 2278. 字母在字符串中的百分比)
      • [4/1 2140. 解决智力问题](#4/1 2140. 解决智力问题)
      • [4/2 2873. 有序三元组中的最大值 I](#4/2 2873. 有序三元组中的最大值 I)
      • [4/3 2874. 有序三元组中的最大值 II](#4/3 2874. 有序三元组中的最大值 II)
      • [4/4 1123. 最深叶节点的最近公共祖先](#4/4 1123. 最深叶节点的最近公共祖先)
      • [4/5 1863. 找出所有子集的异或总和再求和](#4/5 1863. 找出所有子集的异或总和再求和)
      • [4/6 368. 最大整除子集](#4/6 368. 最大整除子集)

3/31 2278. 字母在字符串中的百分比

遍历 求出字母个数

python 复制代码
def percentageLetter(s, letter):
    """
    :type s: str
    :type letter: str
    :rtype: int
    """
    num = 0
    n=len(s)
    for c in s:
        if c==letter:
            num+=1
    return num*100//n

4/1 2140. 解决智力问题

一眼动归

因为对于某个位置i是否可以解决取决于他前面的问题 所以从后往前遍历更加方便

定义dp[i]为解决i~n之间的问题可以得到的最高分数

dp[i]=points[i]+dp[i+brainpower[i]+1]

python 复制代码
def mostPoints(questions):
    """
    :type questions: List[List[int]]
    :rtype: int
    """
    n=len(questions)
    dp=[0]*(n+1)
    for i in range(n-1,-1,-1):
        dp[i]=max(dp[i+1],questions[i][0]+dp[min(n,i+questions[i][1]+1)])
    return dp[0]

4/2 2873. 有序三元组中的最大值 I

1.遍历 三重循环

2.为了使值最大 对于每个j 要找到它左边的最大值nums[i] 和右边的最大值nums[k]

lmax[j],rmax[j]分别记录j左右的最大值

python 复制代码
def maximumTripletValue(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    cur = 0
    n=len(nums)
    for i in range(n-2):
        for j in range(i+1,n-1):
            for k in range(j+1,n):
                cur=max(cur,(nums[i]-nums[j])*nums[k])
    return cur

def maximumTripletValue2(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    n=len(nums)
    lmax=[0]*n
    rmax=[0]*n
    for i in range(1,n):
        lmax[i]=max(lmax[i-1],nums[i-1])
        rmax[n-1-i]=max(rmax[n-i],nums[n-i])
    cur=0
    for j in range(1,n-1):
        cur=max(cur,(lmax[j]-nums[j])*rmax[j])
    return cur

4/3 2874. 有序三元组中的最大值 II

数组长度很大不能循环遍历

为了使值最大 对于每个j 要找到它左边的最大值nums[i] 和右边的最大值nums[k]

lmax[j],rmax[j]分别记录j左右的最大值

python 复制代码
def maximumTripletValue(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    n=len(nums)
    lmax=[0]*n
    rmax=[0]*n
    for i in range(1,n):
        lmax[i]=max(lmax[i-1],nums[i-1])
        rmax[n-1-i]=max(rmax[n-i],nums[n-i])
    cur=0
    for j in range(1,n-1):
        cur=max(cur,(lmax[j]-nums[j])*rmax[j])
    return cur

4/4 1123. 最深叶节点的最近公共祖先

dfs

python 复制代码
class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def lcaDeepestLeaves(root):
    """
    :type root: Optional[TreeNode]
    :rtype: Optional[TreeNode]
    """
    global ans,maxd
    ans=None
    maxd = -1
    def dfs(node,dep):
        global ans,maxd
        if not node:
            maxd = max(maxd,dep)
            return dep
        left = dfs(node.left,dep+1)
        right = dfs(node.right,dep+1)
        if left==right==maxd:
            ans = node
        return max(left,right)
    dfs(root,0)
    return ans

4/5 1863. 找出所有子集的异或总和再求和

遍历

python 复制代码
def subsetXORSum(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    ans=0
    n=len(nums)
    for i in range(1<<n):
        tmp=0
        for j in range(n):
            if i&(1<<j):
                tmp^=nums[j]
        ans+=tmp
    return ans

4/6 368. 最大整除子集

  1. 遍历
    ans内每个num结尾都一个备选答案
    每一个数 遍历当前备选答案 保留长度最大的一个为当前Num的备选答案
    2.dp
    dp[i] nums[i]最大子集个数
python 复制代码
def largestDivisibleSubset(nums):
    """
    :type nums: List[int]
    :rtype: List[int]
    """
    n = len(nums)
    nums.sort()
    ans = [[nums[0]]]
    for i in range(1,n):
        c = nums[i]
        ret = []
        for j in range(len(ans)-1,-1,-1):
            tmp = ans[j][:]
            if c%tmp[-1]==0:
                tmp.append(c)
                if len(tmp)>len(ret):
                    ret = tmp[:]
        if not ret:
            ret = [c]
        ans.append(ret)
    
    ans.sort(key=lambda x: len(x))
    return ans[-1]

def largestDivisibleSubset2(nums):
    """
    :type nums: List[int]
    :rtype: List[int]
    """
    n=len(nums)
    nums.sort()
    dp = [1]*n
    msize,mval=1,1
    for i in range(n):
        for j in range(i):
            if nums[i]%nums[j]==0 and dp[j]+1>dp[i]:
                dp[i]=dp[j]+1
        if dp[i]>msize:
            msize=dp[i]
            mval = nums[i]
    
    if msize==1:
        return nums[:1]
    ret = []
    for i in range(n-1,-1,-1):
        if msize==0:
            return ret
        if dp[i]==msize and mval%nums[i]==0:
            ret.append(nums[i])
            mval = nums[i]
            msize-=1
    return ret

相关推荐
apcipot_rain15 分钟前
密码学——序列密码 序列线性复杂度 B-M算法 例题演示
算法·密码学
迪小莫学AI35 分钟前
LeetCode 1863. 找出所有子集的异或总和再求和
算法·leetcode·深度优先
技术小白Byteman1 小时前
蓝桥刷题note13(排序)
开发语言·数据结构·c++·学习·算法·visualstudio
芜湖xin2 小时前
【题解-Acwing】798. 差分矩阵
算法·差分
_星辰大海乀2 小时前
二叉树相关练习--2
java·开发语言·数据结构·算法·链表·idea
一只拉古2 小时前
掌握扫描线(sweep line)算法:从LeetCode到现实应用
算法·leetcode·面试
OneQ6662 小时前
C++自学笔记——动态创建对象
c++·笔记·算法
梭七y2 小时前
【力扣hot100题】(064)在排序数组中查找元素的第一个和最后一个位置
数据结构·算法·leetcode
Dream it possible!2 小时前
LeetCode 热题 100_完全平方数(84_279_中等_C++)(动态规划(完全背包))
c++·leetcode·动态规划·完全背包
Fantasydg2 小时前
DAY 39 leetcode 18--哈希表.四数之和
算法·leetcode·散列表