LeetCode 每日一题 2024/10/7-2024/10/13

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


目录

      • [10/7 871. 最低加油次数](#10/7 871. 最低加油次数)
      • [10/8 1436. 旅行终点站](#10/8 1436. 旅行终点站)
      • [10/9 3171. 找到按位或最接近 K 的子数组](#10/9 3171. 找到按位或最接近 K 的子数组)
      • [10/10 3162. 优质数对的总数 I](#10/10 3162. 优质数对的总数 I)
      • [10/11 3164. 优质数对的总数 II](#10/11 3164. 优质数对的总数 II)
      • [10/12 3158. 求出出现两次数字的 XOR 值](#10/12 3158. 求出出现两次数字的 XOR 值)
      • [10/13 1884. 鸡蛋掉落-两枚鸡蛋](#10/13 1884. 鸡蛋掉落-两枚鸡蛋)

10/7 871. 最低加油次数

依次经过加油站 将能够加的油放入大顶堆中

如果无法到达加油站 从能够加的油中选出最多的加入

python 复制代码
def minRefuelStops(target, startFuel, stations):
    """
    :type target: int
    :type startFuel: int
    :type stations: List[List[int]]
    :rtype: int
    """
    import heapq
    fuel = startFuel
    pre = 0
    ans = 0
    stations.append([target,0])
    l = []
    for loc,f in stations:
        v = loc-pre
        fuel -= v
        while fuel<0 and l:
            tmp = -heapq.heappop(l)
            ans +=1
            fuel += tmp
        if fuel < 0:
            return -1
        heapq.heappush(l,-f)
        pre = loc
        
    return ans

10/8 1436. 旅行终点站

target存储所有出现的终点站

source存储所有出现的起点

从target中找到一个未出现在source中的点即为最终终点站

python 复制代码
def destCity(paths):
    """
    :type paths: List[List[str]]
    :rtype: str
    """
    target = set()
    source = set()
    for s,t in paths:
        source.add(s)
        target.add(t)
    
    for loc in target:
        if loc not in source:
            return loc

10/9 3171. 找到按位或最接近 K 的子数组

遍历数组尾nums[i]

从后往前遍历j [j~i]

如果x为nums[j]子集 后续已经在i=j时处理过不需要继续进行

python 复制代码
def minimumDifference(nums, k):
    """
    :type nums: List[int]
    :type k: int
    :rtype: int
    """
    ans=float("inf")
    for i,x in enumerate(nums):
        ans = min(ans,abs(x-k))
        j = i-1
        while j>=0 and nums[j]|x!=nums[j]:
            nums[j] |= x
            ans = min(ans,abs(nums[j]-k))
            j-=1
    return ans

10/10 3162. 优质数对的总数 I

遍历每一对数是否优质

python 复制代码
def numberOfPairs(nums1, nums2, k):
    """
    :type nums1: List[int]
    :type nums2: List[int]
    :type k: int
    :rtype: int
    """
    ans = 0
    for n1 in nums1:
        for n2 in nums2:
            if n1%(n2*k)==0:
                ans+=1
    return ans

10/11 3164. 优质数对的总数 II

nums1优质的必须能被k整除

除以k后 统计nums1中每个数的所有因子个数 cnt[c]

只要nums2中数值num的优质数对就是以num为因子统计到的个数cnt[num]

python 复制代码
def numberOfPairs(nums1, nums2, k):
    """
    :type nums1: List[int]
    :type nums2: List[int]
    :type k: int
    :rtype: int
    """
    import math
    cnt={}
    for num in nums1:
        if num%k>0:
            continue
        num = num//k
        for d in range(1,int(math.sqrt(num))+1):
            if num%d>0:
                continue
            cnt[d] = cnt.get(d,0)+1
            if d**2<num:
                cnt[num//d]=cnt.get(num//d,0)+1            
    ans = 0
    for num in nums2:
        ans += cnt.get(num,0)
    return ans

10/12 3158. 求出出现两次数字的 XOR 值

从头遍历 记录出现过的数字 如果出现第二次则将其异或

python 复制代码
def duplicateNumbersXOR(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    ans = 0
    s =set()
    for num in nums:
        if num in s:
            ans ^= num
        s.add(num)
    return ans

10/13 1884. 鸡蛋掉落-两枚鸡蛋

动态规划

dp[i]表示i层需要的最少操作次数

选择k往下扔

如果没有碎那么答案在[k+1,i] i-k层建筑中 等同于dp[i-k]

如果碎了答案在[1,k-1] 依次试需要k-1次

python 复制代码
def twoEggDrop(n):
    """
    :type n: int
    :rtype: int
    """
    dp=[0]+[float("inf")]*n
    for i in range(1,n+1):
        for k in range(1,i+1):
            dp[i] = min(dp[i],max(k-1,dp[i-k])+1)
    return dp[n]

相关推荐
草莓熊Lotso13 分钟前
【C++】--函数参数传递:传值与传引用的深度解析
c语言·开发语言·c++·其他·算法
不知名。。。。。。。。19 分钟前
算法 ----- 链式
算法
网易独家音乐人Mike Zhou21 分钟前
【Python】圆柱体内部3D点云仿真及ply文件生成,圆形3D点云检测及拟合算法
stm32·单片机·mcu·物联网·算法·点云·iot
scx201310041 小时前
20250822 组题总结
c++·算法
智驱力人工智能1 小时前
智慧工厂烟雾检测:全场景覆盖与精准防控
人工智能·算法·安全·智慧城市·烟雾检测·明火检测·安全生产
浩少7023 小时前
LeetCode-17day:贪心算法
算法·leetcode·贪心算法
weixin_516875654 小时前
力扣 30 天 JavaScript 挑战 第37天 第九题笔记 知识点: 剩余参数,拓展运算符
javascript·笔记·leetcode
mashanshui7 小时前
Https之(二)TLS的DH密钥协商算法
算法·https·tls·dh·ecdhe
wearegogog12310 小时前
MATLAB的脉搏信号分析预处理
算法·matlab
fs哆哆10 小时前
在VB.net中一维数组,与VBA有什么区别
java·开发语言·数据结构·算法·.net