当前位置: 首页 > article >正文

LeetCode 每日一题 2024/9/16-2024/9/22

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


目录

      • 9/16 1184. 公交站间的距离
      • 9/17 815. 公交路线
      • 9/18 2332. 坐上公交的最晚时间
      • 9/19 2414. 最长的字母序连续子字符串的长度
      • 9/20 2376. 统计特殊整数
      • 9/21 2374. 边积分最高的节点
      • 9/22


9/16 1184. 公交站间的距离

先求总和total
从start向右到dest距离可以遍历得到x 从start向左到dest可以total-x

def distanceBetweenBusStops(distance, start, destination):
    """
    :type distance: List[int]
    :type start: int
    :type destination: int
    :rtype: int
    """
    n = len(distance)
    total = sum(distance)
    x = 0
    while start!=destination:
        x += distance[start]
        start = (start+1)%n
    return min(x,total-x)



9/17 815. 公交路线

m记录每个站拥有的公交线路
双向BFS 考虑站点可以坐的公交线路
mem记录已坐过的线路 乘坐同一条线路无意义
从起点、终点分别获取可以乘坐的公交线路 l1,l2
每次选取较少的情况考虑 len(l1)>len(l2)
如果坐到了同一条公交线必定可以到达公交线路所在站点 返回答案
统计线路中所有站点下一次能够乘坐的未使用公交线路 m[station]-mem

def numBusesToDestination(routes, source, target):
    """
    :type routes: List[List[int]]
    :type source: int
    :type target: int
    :rtype: int
    """
    if source == target:
        return 0
    from collections import defaultdict
    m = defaultdict(set)
    for i,stations in enumerate(routes):
        for station in stations:
            m[station].add(i)
            
    routes = [set(x) for x in routes]
            
    l1 = m[source]
    l2 = m[target]
    step = 0
    mem = set()
    while l1 and l2:
        if len(l1)>len(l2):
            l1,l2 = l2,l1
        step +=1
        tmp = set()
        for route in l1:
            if route in l2:
                return step
            if route in mem:
                continue
            mem.add(route)
            for station in routes[route]:
                tmp.update(m[station]-mem)
        l1 = tmp
            
    return -1
    



9/18 2332. 坐上公交的最晚时间

将公交、乘客到达时间排序
模拟乘客上车 num为当前车辆空位
如果最后一辆车num大于0 说明最后一班车有空位 从最后一般发车时刻往前找一个没有乘客的时刻
如果num为0 表示最后一班公交车无空位 要在最后一个上车乘客到达前到达

def latestTimeCatchTheBus(buses, passengers, capacity):
    """
    :type buses: List[int]
    :type passengers: List[int]
    :type capacity: int
    :rtype: int
    """
    buses.sort()
    passengers.sort()
    pos = 0
    
    for b in buses:
        num = capacity
        while num>0 and pos<len(passengers) and passengers[pos]<=b:
            num-=1
            pos+=1
    pos-=1
    ans = passengers[pos]
    if num>0:
        ans = buses[-1]
    while pos>=0 and passengers[pos]==ans:
        pos-=1
        ans-=1
    return ans



9/19 2414. 最长的字母序连续子字符串的长度

依次遍历 cur记录当前满足条件的字符串长度
如果当前字符s[i]比前一个字符字母序大1则满足条件 cur+1

def longestContinuousSubstring(s):
    """
    :type s: str
    :rtype: int
    """
    ans = 1
    cur = 1
    for i in range(1,len(s)):
        if ord(s[i])-ord(s[i-1])==1:
            cur+=1
        else:
            cur=1
        ans=max(ans,cur)
    return ans



9/20 2376. 统计特殊整数

dfs(i,mask,limit,num)
判断第i位及其之后数位的合法方案
使用10位二进制mask记录之前选过的数字集合
limit表示当前位数值选择是否受到限制
num表示i前是否已经填写数字

def countSpecialNumbers(n):
    """
    :type n: int
    :rtype: int
    """
    s=str(n)
    mem={}
    def dfs(i,mask,limit,num):
        if (i,mask,limit,num) in mem:
            return mem[(i,mask,limit,num)]
        if i==len(s):
            return 1 if num else 0
        ans = 0
        if not num:
            ans = dfs(i+1,mask,False,False)
        low = 0 if num else 1
        up = int(s[i]) if limit else 9
        for v in range(low,up+1):
            if mask>>v &1==0:
                ans += dfs(i+1,mask|(1<<v),limit and v==up, True)
        mem[(i,mask,limit,num)]=ans
        return ans
    return dfs(0,0,True,False)



9/21 2374. 边积分最高的节点

依次统计涉及到的节点分数
记录最高分数及其对应的节点

def edgeScore(edges):
    """
    :type edges: List[int]
    :rtype: int
    """
    ans = 0
    curv = 0
    m = {}
    for i,v in enumerate(edges):
        m[v] = m.get(v,0)+i
        if m[v]>curv or (m[v]==curv and v<ans):
            ans = v
            curv=m[v]
    return ans




9/22






http://www.kler.cn/news/314953.html

相关文章:

  • 自然语言处理_tf-idf
  • Java 入门指南:Java 8 新特性 —— Stream 流
  • JavaScript 插入元素到数组三个方法代码示例
  • celery 结合 rabbitmq 使用时,celery 消费者执行时间太久发送 ack 消息失败
  • 【研发日记】嵌入式处理器技能解锁(六)——ARM的Cortex-M4内核
  • 【js常用函数封装】获取年龄,根据身份证获取年龄,性别,生日
  • 算法笔试-编程练习-好题-07
  • 《MmAP : Multi-Modal Alignment Prompt for Cross-Domain Multi-Task Learning》中文校对版
  • Homebrew安装与切换下载源
  • 单例模式(饿汉式-懒汉式)
  • leetcode:3232. 判断是否可以赢得数字游戏(python3解法)
  • FastDFS架构和原理
  • RabbitMQ:交换机详解(Fanout交换机、Direct交换机、Topic交换机)
  • Linux实用命令 df和du命令
  • 数据结构之‘栈’
  • 面向对象程序设计
  • VisionPro - 基础 - 模板匹配技术-Search/PMAlign/PatMax(6)-纹理屏蔽和重叠匹配
  • Redis面试真题总结(四)
  • 多模态交互才是人机交互的未来
  • MoFA: 迈向AIOS
  • 【QGIS入门实战精品教程】6.1:QGIS根据属性条件查询数据(SQL表达式)
  • 如何在GitHub上Clone项目:一步步指南
  • 暑假考研集训营游记
  • 李宏毅机器学习2023-HW13-Network Compression
  • 4.《DevOps》系列K8S部署CICD流水线之Helm部署Harbor私人镜像仓库
  • 机器学习中求解模型参数的方法
  • LabVIEW编程能力如何能突飞猛进
  • 【数据优化】基于GEE填补遥感缺失数据
  • 测试面试题:接口自动化测试流程?
  • Vue3入门 - ElementPlus中左侧菜单和Tabs菜单组合联动效果