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

相关推荐
feiduoge7 分钟前
教程 42 - 可写纹理
windows·游戏引擎·图形渲染
charlie11451419114 分钟前
深入解构:MSVC 调试机制与 Visual Studio 调试器原理
c++·ide·windows·学习·visual studio·调试·现代c++
武藤一雄40 分钟前
[奇淫巧技] WPF篇 (长期更新)
windows·microsoft·c#·.net·wpf
菜鸟233号1 小时前
力扣78 子集 java实现
java·数据结构·算法·leetcode
月明长歌2 小时前
【码道初阶】【Leetcode94&144&145】二叉树的前中后序遍历(非递归版):显式调用栈的优雅实现
java·数据结构·windows·算法·leetcode·二叉树
DanyHope2 小时前
《LeetCode 49. 字母异位词分组:哈希表 + 排序 全解析》
算法·leetcode·哈希算法·散列表
iAkuya2 小时前
(leetcode) 力扣100 15轮转数组(环状替代)
数据结构·算法·leetcode
李斯维2 小时前
安装 WSL 最好的方式
linux·windows
努力学算法的蒟蒻2 小时前
day38(12.19)——leetcode面试经典150
算法·leetcode·面试
ITHAOGE152 小时前
下载 | Win11 官方精简版,系统占用空间极少!(12月更新、Win 11 IoT物联网 LTSC版、适合老电脑安装使用)
windows·科技·物联网·microsoft·微软·电脑