记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步
目录
-
-
- [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 的最少操作次数
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