LeetCode 每日一题 2025/12/8-2025/12/14

LeetCode 每日一题 2025/1/1-2025/1/7
记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步


目录

      • [12/8 1925. 统计平方和三元组的数目](#12/8 1925. 统计平方和三元组的数目)
      • [12/9 3583. 统计特殊三元组](#12/9 3583. 统计特殊三元组)
      • [12/10 3577. 统计计算机解锁顺序排列数](#12/10 3577. 统计计算机解锁顺序排列数)
      • [12/11 3531. 统计被覆盖的建筑](#12/11 3531. 统计被覆盖的建筑)
      • [12/12 3433. 统计用户被提及情况](#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

python 复制代码
def countTriples(n):
    """
    :type n: int
    :rtype: int
    """
    import math
    ans =0
    for a in range(1,n):
        for b in range(a+1,n):
            c=int(math.sqrt(a**2+b**2))
            if c<=n and c**2==a**2+b**2:
                ans+=2
    return ans

12/9 3583. 统计特殊三元组

从右到左枚举j

左侧每个数出现的次数放入left 右侧每个数出现次数放入right

当前nums[j] 两边nums[j]*2 的个数相乘

python 复制代码
def specialTriplets(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    from collections import defaultdict
    MOD=10**9+7
    left=defaultdict(int)
    right=defaultdict(int)
    n=len(nums)
    for i in range(n-1):
        left[nums[i]]+=1
    right[nums[-1]]+=1
    
    ans=0
    for num in nums[-2:0:-1]:
        left[num]-=1
        ans = (ans+left[num*2]*right[num*2])%MOD
        right[num]+=1
        
    return ans

12/10 3577. 统计计算机解锁顺序排列数

如果能够全部解锁那么complexity[0]必定最小

即如果存在complexity[i]<=complexity[0] 这个i无法解锁 答案为0

否则答案为(n-1)!

python 复制代码
def countPermutations(complexity):
    """
    :type complexity: List[int]
    :rtype: int
    """
    MOD=10**9+7
    n=len(complexity)
    for i in range(1,n):
        if complexity[i]<=complexity[0]:
            return 0
        
    ans=1
    for i in range(2,n):
        ans = (ans*i)%MOD
    return ans

12/11 3531. 统计被覆盖的建筑

记录每一行每一列出现建筑的最小位置和最大位置

遍历半段当前x,y是否都在最小位置和最大位置之间

python 复制代码
def countCoveredBuildings(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)
    
    for x,y in buildings:
        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=0
    for x,y in buildings:
        if rowmin[y]<x<rowmax[y] and colmin[x]<y<colmax[x]:
            ans+=1
    return ans

12/12 3433. 统计用户被提及情况

按时间顺序先排序

遍历每一个event

nxtonline[i]记录用户i下一次上线的时间

分情况处理

python 复制代码
def countMentions(numberOfUsers, events):
    """
    :type numberOfUsers: int
    :type events: List[List[str]]
    :rtype: List[int]
    """
    events.sort(key=lambda x:(int(x[1]),x[0]=="MESSAGE"))
    cnt=[0]*numberOfUsers
    nxtonline=[0]*numberOfUsers
    for evt in events:
        cur = int(evt[1])
        if evt[0]=="MESSAGE":
            if evt[2]=="ALL":
                for i in range(numberOfUsers):
                    cnt[i]+=1
            elif evt[2]=="HERE":
                for i,t in enumerate(nxtonline):
                    if t<=cur:
                        cnt[i]+=1
            else:
                for i in evt[2].split():
                    cnt[int(i[2:])]+=1
        else:
            nxtonline[int(evt[2])]=cur+60
    return cnt

12/13

python 复制代码

12/14

python 复制代码

相关推荐
橘颂TA13 小时前
【剑斩OFFER】算法的暴力美学——重排链表
算法·结构与算法
zl_vslam13 小时前
SLAM中的非线性优-3D图优化之相对位姿Between Factor位姿图优化(十三)
人工智能·算法·计算机视觉·3d
Timmylyx051813 小时前
CF 新年赛 Goodbye 2025 题解
算法·codeforces·比赛日记
闻缺陷则喜何志丹13 小时前
【二分查找】P10091 [ROIR 2022 Day 2] 分数排序|普及+
c++·算法·二分查找
only-qi14 小时前
leetcode2. 两数相加
算法·leetcode
鲨莎分不晴14 小时前
拯救暗淡图像:深度解析直方图均衡化(原理、公式与计算)
人工智能·算法·机器学习
DuHz14 小时前
242-267 GHz双基地超外差雷达系统:面向精密太赫兹传感与成像的65nm CMOS实现——论文阅读
论文阅读·物联网·算法·信息与通信·毫米波雷达
AI科技星15 小时前
时空的固有脉动:波动方程 ∇²L = (1/c²) ∂²L/∂t² 的第一性原理推导、诠释与验证
数据结构·人工智能·算法·机器学习·重构
2401_8414956415 小时前
【LeetCode刷题】寻找重复数
数据结构·python·算法·leetcode·链表·数组·重复数
罗技12315 小时前
Easysearch 集群监控实战(下):线程池、索引、查询、段合并性能指标详解
前端·javascript·算法