第一次AK,心情十分预约!
T1
class Solution:
def divideArray(self, nums: List[int]) -> bool:
dic={}
for i in range(len(nums)):
if nums[i] in dic:
dic[nums[i]]+=1
else:
dic[nums[i]]=1
for key in dic.keys():
if dic[key]%2!=0:
return False
return True
T2
class Solution:
def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
if pattern[0]==pattern[1]:
count=1
for t in text:
if t==pattern[0]:
count+=1
return count*(count-1)//2
else:
count1,count2=0,0
ans=0
for t in text:
if t==pattern[0]:
count1+=1
if t==pattern[1]:
count2+=1
ans+=count1
ans+=max(count1,count2)
return ans
T3
from heapq import *
class Solution:
def halveArray(self, nums: List[int]) -> int:
suma=sum(nums)
half=suma/2
nums.sort(reverse=True)
heap,lth=[],len(nums)
for i in range(lth):
heappush(heap,-nums[i])
cnt,total=0,0
while total
T4
class Solution:
def minimumWhiteTiles(self, floor: str, numCarpets: int, carpetLen: int) -> int:
#暴力
# def count(sta,res):
# ans=0
# for i in range(sta,sta+carpetLen):
# if res[i]=="1":
# ans+=1
# return ans
# lth=len(floor)
# res=[c for c in floor]
# while numCarpets>0:
# index=0
# maxnum=0
# for i in range(lth-carpetLen+1):
# tmp=count(i,res)
# if tmp>maxnum:
# maxnum=tmp
# index=i
# for i in range(index,index+carpetLen):
# if res[i]=="1":
# res[i]="0"
# numCarpets-=1
# ans=0
# for i in range(lth):
# if res[i]=="1":
# ans+=1
# return ans
n=len(floor)
tree=[0]*(n+1)
res=[c for c in floor]
res.insert(0,0)
if numCarpets*carpetLen>=n:
return 0
#树状数组优化
def lowbit(x):
return x&(-x)
def add(index,val):
while index<=n:
tree[index]+=val
index+=lowbit(index)
def query(index):
ans=0
while index:
ans+=tree[index]
index-=lowbit(index)
return ans
#建树
for i in range(1,n+1):
if res[i]=="1":
add(i,1)
while numCarpets>0:
maxnum,sta=0,0
for i in range(carpetLen,n+1):
if res[i]=="1":
t=query(i)-query(i-carpetLen)
if t>maxnum:
maxnum=t
sta=i
if sta!=0:
for i in range(sta-carpetLen+1,sta+1):
if res[i]=="1":
res[i]="0"
add(i,-1)
else:
return 0
numCarpets-=1
ans=0
for i in range(1,n+1):
if res[i]=="1":
ans+=1
return ans



