LeetCode 每日一题 2025/12/15-2025/12/21

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


目录

      • [12/15 2110. 股票平滑下跌阶段的数目](#12/15 2110. 股票平滑下跌阶段的数目)
      • [12/16 3562. 折扣价交易股票的最大利润](#12/16 3562. 折扣价交易股票的最大利润)
      • [12/17 3573. 买卖股票的最佳时机 V](#12/17 3573. 买卖股票的最佳时机 V)
      • [12/18 3652. 按策略买卖股票的最佳时机](#12/18 3652. 按策略买卖股票的最佳时机)
      • [12/19 2092. 找出知晓秘密的所有专家](#12/19 2092. 找出知晓秘密的所有专家)
      • [12/20 944. 删列造序](#12/20 944. 删列造序)
      • 12/21

12/15 2110. 股票平滑下跌阶段的数目

dp[i]代表以i为最后一天的平滑下降阶段个数

dp[i]最小为1

python 复制代码
def getDescentPeriods(prices):
    """
    :type prices: List[int]
    :rtype: int
    """
    n = len(prices)
    dp = [1]*n
    ans = dp[0]
    for i in range(1,n):
        if prices[i]==prices[i-1]-1:
            dp[i]+=dp[i-1]
        ans +=dp[i]
    return ans

12/16 3562. 折扣价交易股票的最大利润

dp 当前节点是否有折扣 以及预算剩余

dp0表示不够买父节点 dp1表示购买父节点

subpro0表示不可以用优惠 subpro1表示可以用优惠

python 复制代码
def maxProfit(n, present, future, hierarchy, budget):
    """
    :type n: int
    :type present: List[int]
    :type future: List[int]
    :type hierarchy: List[List[int]]
    :type budget: int
    :rtype: int
    """
    g=[[]for _ in range(n)]
    for e in hierarchy:
        g[e[0]-1].append(e[1]-1)
    
    def dfs(u):
        cost = present[u]
        dcost = cost//2
        
        dp0=[0]*(budget+1)
        dp1=[0]*(budget+1)
        
        subpro0=[0]*(budget+1)
        subpro1=[0]*(budget+1)
        
        usize=cost
        
        for v in g[u]:
            cdp0,cdp1,vsize=dfs(v)
            usize+=vsize
            for i in range(budget,-1,-1):
                for sub in range(min(vsize,i)+1):
                    if i-sub>=0:
                        subpro0[i]=max(subpro0[i],subpro0[i-sub]+cdp0[sub])
                        subpro1[i]=max(subpro1[i],subpro1[i-sub]+cdp1[sub])
        for i in range(budget+1):
            dp0[i]=subpro0[i]
            dp1[i]=subpro0[i]
            if i>=dcost:
                dp1[i]=max(subpro0[i],subpro1[i-dcost]+future[u]-dcost)
            if i>=cost:
                dp0[i]=max(subpro0[i],subpro1[i-cost]+future[u]-cost)
        return dp0,dp1,usize
    return dfs(0)[0][budget]
    

12/17 3573. 买卖股票的最佳时机 V

dp[i,j,s]

第i天 第j比交易

s=0,1,2 表示不持有股票 持有股票 做空中 三种状态

python 复制代码
def maximumProfit(prices, k):
    """
    :type prices: List[int]
    :type k: int
    :rtype: int
    """
    n=len(prices)
    dp=[[0]*3 for _ in range(k+1)]
    
    for j in range(1,k+1):
        dp[j][1]=-prices[0]
        dp[j][2]=prices[0]

    for i in range(1,n):
        for j in range(k,0,-1):
            dp[j][0] = max(dp[j][0],max(dp[j][1]+prices[i],dp[j][2]-prices[i]))
            dp[j][1] = max(dp[j][1],dp[j-1][0]-prices[i])
            dp[j][2] = max(dp[j][2],dp[j-1][0]+prices[i])
    return dp[k][0]

12/18 3652. 按策略买卖股票的最佳时机

如果最佳区间为[i-k+1,i]

利润为j=[0,i-k]中s[j]*p[j]

j=[i-k/2+1,i]中p[j]

j=[i+1,n-1]中s[j]*p[j]

presum记录s[j]*p[j]的前缀和

psum记录p[j]的前缀和

python 复制代码
def maxProfit(prices, strategy, k):
    """
    :type prices: List[int]
    :type strategy: List[int]
    :type k: int
    :rtype: int
    """
    n=len(prices)
    presum=[0]*(n+1)
    psum=[0]*(n+1)
    for i in range(n):
        presum[i+1]=presum[i]+prices[i]*strategy[i]
        psum[i+1]=psum[i]+prices[i]
    ans=presum[n]
    for i in range(k-1,n):
        left = presum[i-k+1]
        right= presum[n]-presum[i+1]
        mid=psum[i+1]-psum[i-k//2+1]
        ans=max(ans,left+mid+right)
    return ans

12/19 2092. 找出知晓秘密的所有专家

done记录知晓秘密的专家情况 True为已知

按时间顺序从小到大排序 依次处理

每次处理相同时间点的会议

connect[x]记录这个时间点与专家x开过会的所有专家

选出当前时间点开了会并且知晓秘密的专家

广搜

遍历该知晓秘密专家x开过会的所有专家y

如果y不知晓秘密 则经过改时间点后 y知晓了秘密

并且加入下一轮考虑

python 复制代码
def findAllPeople(n, meetings, firstPerson):
    """
    :type n: int
    :type meetings: List[List[int]]
    :type firstPerson: int
    :rtype: List[int]
    """
    from collections import defaultdict
    m=len(meetings)
    meetings.sort(key=lambda x:x[2])
    done = [False]*n
    done[0]=True
    done[firstPerson]=True
    
    i=0    
    while i<m:
        j = i
        while j+1<m and meetings[j+1][2]==meetings[i][2]:
            j+=1
        
        connect = defaultdict(list)
        point = set()
        for k in range(i,j+1):
            x,y = meetings[k][0],meetings[k][1]
            point.update([x,y])
            connect[x].append(y)
            connect[y].append(x)
            
        l = [u for u in point if done[u]]
        while l:
            tmp = set()
            for p in l:
                for point in connect[p]:
                    if not done[point]:
                        done[point]=True
                        tmp.add(point)
            l = list(tmp)
        i=j+1
           
    return [i for i in range(n) if done[i]]
                

12/20 944. 删列造序

1:常规方法 一列列寻找是否符合

2:使用zip(*A)将A解压

python 复制代码
def minDeletionSize1(A):
    """
    :type A: List[str]
    :rtype: int
    """
    ret = 0
    if not A:
        return ret
    slen = len(A[0])
    for i in range(slen):
        pre = 'a'
        for j in range(len(A)):
            if A[j][i]<pre:
                ret+=1
                break
            else:
                pre= A[j][i]
    return ret

def minDeletionSize2(A):
    """
    :type A: List[str]
    :rtype: int
    """
    ret = 0
    for i in zip(*A):
        if list(i)!=sorted(i):
            ret+=1
    return ret

12/21

python 复制代码

相关推荐
写写闲篇儿4 小时前
下一个更大元素(一)
数据结构·算法
MobotStone6 小时前
从金鱼记忆到过目不忘:Transformer 如何让AI真正理解一句话?
算法
炽烈小老头7 小时前
【每天学习一点算法 2025/12/19】二叉树的层序遍历
数据结构·学习·算法
Xの哲學7 小时前
Linux grep命令:文本搜索的艺术与科学
linux·服务器·算法·架构·边缘计算
soft20015257 小时前
MySQL Buffer Pool深度解析:LRU算法的完美与缺陷
数据库·mysql·算法
WBluuue8 小时前
AtCoder Beginner Contest 436(ABCDEF)
c++·算法
fie88898 小时前
广义 S 变换(GST)地震信号时频谱
算法
json{shen:"jing"}9 小时前
1-C语言的数据类型
c语言·c++·算法
im_AMBER9 小时前
数据结构 13 图 | 哈希表 | 树
数据结构·笔记·学习·算法·散列表