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 的子数组

遍历数组尾numsi

从后往前遍历j j\~i

如果x为numsj子集 后续已经在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中每个数的所有因子个数 cntc

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

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. 鸡蛋掉落-两枚鸡蛋

动态规划

dpi表示i层需要的最少操作次数

选择k往下扔

如果没有碎那么答案在k+1,i i-k层建筑中 等同于dpi-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]

相关推荐
淡海水16 小时前
07-04-并发-ConcurrentBag-T-工作窃取WorkStealing算法
开发语言·算法·c#·bag·concurrent·workstealing
leobertlan16 小时前
好玩系列:训练一个神经网络模型指导小孩玩游戏2-大局观教练
算法
Tisfy17 小时前
LeetCode 3876.构造奇偶一致的数组 II:三种情况分类讨论(其实还是脑筋急转弯)
算法·leetcode·题解·脑筋急转弯
乐迪信息17 小时前
智慧港口船舶AI算法实现在线状态监测
大数据·人工智能·深度学习·算法·计算机视觉
木井巳19 小时前
【BFS/DFS 解决 FloodFill 算法】太平洋大西洋水流问题
java·算法·leetcode·深度优先·广度优先·宽度优先·推荐算法
心抵鹊19 小时前
归并排序之翻转对(hard)
数据结构·算法
白山编程大哥19 小时前
Java 集合算法:从排序、查找到底层原理的实战指南
java·python·算法
shehuiyuelaiyuehao20 小时前
算法34,位运算符操作,总结
算法
Navigator_Z20 小时前
LeetCode //C - 1224. Maximum Equal Frequency
c语言·算法·leetcode
Navigator_Z21 小时前
LeetCode //C++ - 1226. The Dining Philosophers
c语言·算法·leetcode