LeetCode 每日一题 2025/11/3-2025/11/9

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


目录

      • [11/3 1578. 使绳子变成彩色的最短时间](#11/3 1578. 使绳子变成彩色的最短时间)
      • [11/4 3318. 计算子数组的 x-sum I](#11/4 3318. 计算子数组的 x-sum I)
      • [11/5 3321. 计算子数组的 x-sum II](#11/5 3321. 计算子数组的 x-sum II)
      • [11/6 3607. 电网维护](#11/6 3607. 电网维护)
      • [11/7 2528. 最大化城市的最小电量](#11/7 2528. 最大化城市的最小电量)
      • [11/8 1611. 使整数变为 0 的最少操作次数](#11/8 1611. 使整数变为 0 的最少操作次数)
      • 11/9

11/3 1578. 使绳子变成彩色的最短时间

连续的颜色 保留时间最长的那个

python 复制代码
def minCost(colors, neededTime):
    """
    :type colors: str
    :type neededTime: List[int]
    :rtype: int
    """
    ind,ans=0,0
    n=len(colors)
    while ind<n:
        c = colors[ind]
        maxv=0
        total=0
        
        while ind<n and colors[ind]==c:
            maxv=max(maxv,neededTime[ind])
            total+=neededTime[ind]
            ind+=1
        ans+=total-maxv
    return ans

11/4 3318. 计算子数组的 x-sum I

cnt[v]记录v的出现次数

L,R维护(cnt[v],v)

L记录当前子数组中次数多的前x个元素

R保留剩余元素状态

维护长度为k的滑动窗口

有元素新增时移除先前状态,cnt[v]+1,将新状态添加

python 复制代码
def findXSum(nums, k, x):
    """
    :type nums: List[int]
    :type k: int
    :type x: int
    :rtype: List[int]
    """
    from collections import defaultdict
    from sortedcontainers import SortedList
    cnt=defaultdict(int)
    L=SortedList()
    R=SortedList()
    global suml
    suml=0
    
    def add(v):
        global suml
        if cnt[v]==0:
            return
        p=(cnt[v],v)
        if L and p>L[0]:
            suml += p[0]*p[1]
            L.add(p)
        else:
            R.add(p)
    def remove(v):
        global suml
        if cnt[v]==0:
            return 
        p=(cnt[v],v)
        if p in L:
            suml-=p[0]*p[1]
            L.remove(p)
        else:
            R.remove(p)
    def l2r():
        global suml
        p=L[0]
        suml-=p[0]*p[1]
        L.remove(p)
        R.add(p)
    def r2l():
        global suml
        p=R[-1]
        suml+=p[0]*p[1]
        R.remove(p)
        L.add(p)
        
    ans=[0]*(len(nums)-k+1)
    for r,num in enumerate(nums):
        remove(num)
        cnt[num]+=1
        add(num)
        
        l=r-k+1
        if l<0:
            continue
        
        while R and len(L)<x:
            r2l()
        while len(L)>x:
            l2r()
        ans[l]=suml
        
        out=nums[l]
        remove(out)
        cnt[out]-=1
        add(out)
    return ans

11/5 3321. 计算子数组的 x-sum II

cnt[v]记录v的出现次数

L,R维护(cnt[v],v)

L记录当前子数组中次数多的前x个元素

R保留剩余元素状态

维护长度为k的滑动窗口

有元素新增时移除先前状态,cnt[v]+1,将新状态添加

python 复制代码
def findXSum(nums, k, x):
    """
    :type nums: List[int]
    :type k: int
    :type x: int
    :rtype: List[int]
    """
    from collections import defaultdict
    from sortedcontainers import SortedList
    cnt=defaultdict(int)
    L=SortedList()
    R=SortedList()
    global suml
    suml=0
    
    def add(v):
        global suml
        if cnt[v]==0:
            return
        p=(cnt[v],v)
        if L and p>L[0]:
            suml += p[0]*p[1]
            L.add(p)
        else:
            R.add(p)
    def remove(v):
        global suml
        if cnt[v]==0:
            return 
        p=(cnt[v],v)
        if p in L:
            suml-=p[0]*p[1]
            L.remove(p)
        else:
            R.remove(p)
    def l2r():
        global suml
        p=L[0]
        suml-=p[0]*p[1]
        L.remove(p)
        R.add(p)
    def r2l():
        global suml
        p=R[-1]
        suml+=p[0]*p[1]
        R.remove(p)
        L.add(p)
        
    ans=[0]*(len(nums)-k+1)
    for r,num in enumerate(nums):
        remove(num)
        cnt[num]+=1
        add(num)
        
        l=r-k+1
        if l<0:
            continue
        
        while R and len(L)<x:
            r2l()
        while len(L)>x:
            l2r()
        ans[l]=suml
        
        out=nums[l]
        remove(out)
        cnt[out]-=1
        add(out)
    return ans

11/6 3607. 电网维护

将同一个电网的放入一个堆中

记录在离线的电站

python 复制代码
def processQueries(c, connections, queries):
    """
    :type c: int
    :type connections: List[List[int]]
    :type queries: List[List[int]]
    :rtype: List[int]
    """
    import heapq
    
    g=[[] for _ in range(c+1)]
    for x,y in connections:
        g[x].append(y)
        g[y].append(x)
    belong=[-1]*(c+1)
    heap=[]
    def dfs(x):
        belong[x]=len(heap)
        h.append(x)
        for y in g[x]:
            if belong[y]<0:
                dfs(y)
                
    for i in range(1,c+1):
        if belong[i]>=0:
            continue
        h=[]
        dfs(i)
        heapq.heapify(h)
        heap.append(h)
        
    ans=[]
    off=[False]*(c+1)
    for op,x in queries:
        if op==2:
            off[x]=True
            continue
        if not off[x]:
            ans.append(x)
            continue
        h=heap[belong[x]]
        while h and off[h[0]]:
            heapq.heappop(h)
        ans.append(h[0] if h else -1)
    return ans
        

11/7 2528. 最大化城市的最小电量

二分确定最大值

check(x)判定电量x是否能够达到

python 复制代码
def maxPower(stations, r, k):
    """
    :type stations: List[int]
    :type r: int
    :type k: int
    :rtype: int
    """
    n=len(stations)
    cnt=[0]*(n+1)
    
    for i in range(n):
        le=max(0,i-r)
        ri=min(n,i+r+1)
        cnt[le]+=stations[i]
        cnt[ri]-=stations[i]

    def check(x):
        diff = cnt[:]
        total=0
        cur=k
        
        for i in range(n):
            total += diff[i]
            if total<x:
                add = x-total
                if cur<add:
                    return False
                cur-=add
                end = min(n,i+2*r+1)
                diff[end]-=add
                total+=add
        return True
    le,ri=min(stations),sum(stations)+k
    ans=0
    while le<=ri:
        mid=(le+ri)//2
        if check(mid):
            ans=mid
            le=mid+1
        else:
            ri=mid-1
    return ans

11/8 1611. 使整数变为 0 的最少操作次数

题解:https://leetcode.cn/problems/minimum-one-bit-operations-to-make-integers-zero/solutions/2477522/shi-zheng-shu-bian-wei-0-de-zui-shao-cao-gxmc/?envType=daily-question\&envId=2025-11-08

python 复制代码
def minimumOneBitOperations(n):
    """
    :type n: int
    :rtype: int
    """
    def check(n):
        if n==0:
            return 0
        x=len(bin(n))-3
        return (1<<(x+1))-1-check(n-(1<<x))
    return check(n)

11/9

python 复制代码

相关推荐
习惯就好zz8 小时前
WSL2 安装Ubuntu卡在安装进度0%无响应问题解决
linux·windows·ubuntu·wsl·wsl2
仰望—星空12 小时前
MiniEngine学习笔记 : CommandListManager
c++·windows·笔记·学习·cg·direct3d
ue星空14 小时前
Windows内核函数使用
windows
业余幻想家16 小时前
Windows10/Windows11家庭版系统关闭自动更新
运维·windows
阿猿收手吧!17 小时前
windows本机vscode通过ssh免密登录远程linux服务器 && git push/pull 免密
服务器·windows·vscode
zxm851317 小时前
如何在Windows系统中加入程序自启动
windows
小安同学iter17 小时前
SQL50+Hot100系列(11.7)
java·算法·leetcode·hot100·sql50
~~李木子~~17 小时前
Windows软件自动扫描与分类工具 - 技术文档
windows·分类·数据挖掘
-指短琴长-19 小时前
Qt的下载和安装【Windows】
开发语言·windows·qt