LeetCode 每日一题 2025/1/1-2025/1/7
记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步
目录
- 12/8 1925. 统计平方和三元组的数目
- 12/9 3583. 统计特殊三元组
- 12/10 3577. 统计计算机解锁顺序排列数
- 12/11 3531. 统计被覆盖的建筑
- 12/12 3433. 统计用户被提及情况
- 12/13
- 12/14
12/8 1925. 统计平方和三元组的数目
遍历
如果a2+b2=c^2
a一定不等于b
假设a<b<c (a,b,c) (b,a,c)都满足
所以找到一组ans+2
defcountTriples(n):""" :type n: int :rtype: int """importmath ans=0forainrange(1,n):forbinrange(a+1,n):c=int(math.sqrt(a**2+b**2))ifc<=nandc**2==a**2+b**2:ans+=2returnans12/9 3583. 统计特殊三元组
从右到左枚举j
左侧每个数出现的次数放入left 右侧每个数出现次数放入right
当前nums[j] 两边nums[j]*2 的个数相乘
defspecialTriplets(nums):""" :type nums: List[int] :rtype: int """fromcollectionsimportdefaultdict MOD=10**9+7left=defaultdict(int)right=defaultdict(int)n=len(nums)foriinrange(n-1):left[nums[i]]+=1right[nums[-1]]+=1ans=0fornuminnums[-2:0:-1]:left[num]-=1ans=(ans+left[num*2]*right[num*2])%MOD right[num]+=1returnans12/10 3577. 统计计算机解锁顺序排列数
如果能够全部解锁那么complexity[0]必定最小
即如果存在complexity[i]<=complexity[0] 这个i无法解锁 答案为0
否则答案为(n-1)!
defcountPermutations(complexity):""" :type complexity: List[int] :rtype: int """MOD=10**9+7n=len(complexity)foriinrange(1,n):ifcomplexity[i]<=complexity[0]:return0ans=1foriinrange(2,n):ans=(ans*i)%MODreturnans12/11 3531. 统计被覆盖的建筑
记录每一行每一列出现建筑的最小位置和最大位置
遍历半段当前x,y是否都在最小位置和最大位置之间
defcountCoveredBuildings(n,buildings):""" :type n: int :type buildings: List[List[int]] :rtype: int """rowmin=[n+1]*(n+1)colmin=[n+1]*(n+1)rowmax=[0]*(n+1)colmax=[0]*(n+1)forx,yinbuildings:rowmin[y]=min(rowmin[y],x)rowmax[y]=max(rowmax[y],x)colmin[x]=min(colmin[x],y)colmax[x]=max(colmax[x],y)ans=0forx,yinbuildings:ifrowmin[y]<x<rowmax[y]andcolmin[x]<y<colmax[x]:ans+=1returnans12/12 3433. 统计用户被提及情况
按时间顺序先排序
遍历每一个event
nxtonline[i]记录用户i下一次上线的时间
分情况处理
defcountMentions(numberOfUsers,events):""" :type numberOfUsers: int :type events: List[List[str]] :rtype: List[int] """events.sort(key=lambdax:(int(x[1]),x[0]=="MESSAGE"))cnt=[0]*numberOfUsers nxtonline=[0]*numberOfUsersforevtinevents:cur=int(evt[1])ifevt[0]=="MESSAGE":ifevt[2]=="ALL":foriinrange(numberOfUsers):cnt[i]+=1elifevt[2]=="HERE":fori,tinenumerate(nxtonline):ift<=cur:cnt[i]+=1else:foriinevt[2].split():cnt[int(i[2:])]+=1else:nxtonline[int(evt[2])]=cur+60returncnt