LeetCode 每日一题 2025/11/3-2025/11/9
记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步
目录
- 11/3 1578. 使绳子变成彩色的最短时间
- 11/4 3318. 计算子数组的 x-sum I
- 11/5 3321. 计算子数组的 x-sum II
- 11/6 3607. 电网维护
- 11/7 2528. 最大化城市的最小电量
- 11/8
- 11/9
11/3 1578. 使绳子变成彩色的最短时间
连续的颜色 保留时间最长的那个
def minCost(colors, neededTime):""":type colors: str:type neededTime: List[int]:rtype: int"""ind,ans=0,0n=len(colors)while ind<n:c = colors[ind]maxv=0total=0while ind<n and colors[ind]==c:maxv=max(maxv,neededTime[ind])total+=neededTime[ind]ind+=1ans+=total-maxvreturn ans
11/4 3318. 计算子数组的 x-sum I
cnt[v]记录v的出现次数
L,R维护(cnt[v],v)
L记录当前子数组中次数多的前x个元素
R保留剩余元素状态
维护长度为k的滑动窗口
有元素新增时移除先前状态,cnt[v]+1,将新状态添加
def findXSum(nums, k, x):""":type nums: List[int]:type k: int:type x: int:rtype: List[int]"""from collections import defaultdictfrom sortedcontainers import SortedListcnt=defaultdict(int)L=SortedList()R=SortedList()global sumlsuml=0def add(v):global sumlif cnt[v]==0:returnp=(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 sumlif 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 sumlp=L[0]suml-=p[0]*p[1]L.remove(p)R.add(p)def r2l():global sumlp=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]+=1add(num)l=r-k+1if l<0:continuewhile R and len(L)<x:r2l()while len(L)>x:l2r()ans[l]=sumlout=nums[l]remove(out)cnt[out]-=1add(out)return ans
11/5 3321. 计算子数组的 x-sum II
cnt[v]记录v的出现次数
L,R维护(cnt[v],v)
L记录当前子数组中次数多的前x个元素
R保留剩余元素状态
维护长度为k的滑动窗口
有元素新增时移除先前状态,cnt[v]+1,将新状态添加
def findXSum(nums, k, x):""":type nums: List[int]:type k: int:type x: int:rtype: List[int]"""from collections import defaultdictfrom sortedcontainers import SortedListcnt=defaultdict(int)L=SortedList()R=SortedList()global sumlsuml=0def add(v):global sumlif cnt[v]==0:returnp=(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 sumlif 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 sumlp=L[0]suml-=p[0]*p[1]L.remove(p)R.add(p)def r2l():global sumlp=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]+=1add(num)l=r-k+1if l<0:continuewhile R and len(L)<x:r2l()while len(L)>x:l2r()ans[l]=sumlout=nums[l]remove(out)cnt[out]-=1add(out)return ans
11/6 3607. 电网维护
将同一个电网的放入一个堆中
记录在离线的电站
def processQueries(c, connections, queries):""":type c: int:type connections: List[List[int]]:type queries: List[List[int]]:rtype: List[int]"""import heapqg=[[] 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:continueh=[]dfs(i)heapq.heapify(h)heap.append(h)ans=[]off=[False]*(c+1)for op,x in queries:if op==2:off[x]=Truecontinueif not off[x]:ans.append(x)continueh=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是否能够达到
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=0cur=kfor i in range(n):total += diff[i]if total<x:add = x-totalif cur<add:return Falsecur-=addend = min(n,i+2*r+1)diff[end]-=addtotal+=addreturn Truele,ri=min(stations),sum(stations)+kans=0while le<=ri:mid=(le+ri)//2if check(mid):ans=midle=mid+1else:ri=mid-1return ans
11/8
11/9
