LeetCode 每日一题 2022/1/24-2022/1/30

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




1/24 2045. 到达目的地的第二短时间

matrix[i]代表i能够到达的点
每段距离耗时相等 需要得到两点间走了多少步
dis[i][0]代表从1到i的最短距离 dis[i][1]代表从1到i的次短距离
因为广搜 步数只增不减 所以更新状态时步数必定非减
只需要考虑是否是第一次更新<float(‘inf’)即可
对于一段时间t 如果mod=t%(2change)>change 说明t经历了一次变灯
需要等待 2
change-mod

def secondMinimum(n, edges, time, change):
    """
    :type n: int
    :type edges: List[List[int]]
    :type time: int
    :type change: int
    :rtype: int
    """
    import collections
    matrix= [[] for _ in range(n+1)]
    for x,y in edges:
        matrix[x].append(y)
        matrix[y].append(x)
    dis = [[float('inf')]*2 for _ in range(n+1)]
    dis[1][0]=0
    l = collections.deque([(1,0)])
    while dis[n][1]==float('inf'):
        loc,step = l.popleft()
        step +=1
        for nxt in matrix[loc]:
            if step<dis[nxt][0]:
                dis[nxt][0]=step
                l.append((nxt,step))
            elif dis[nxt][0]<step<dis[nxt][1]:
                dis[nxt][1] = step
                l.append((nxt,step))
    ans = 0
    for _ in range(dis[n][1]):
        if ans%(2*change)>=change:
            ans +=2*change-ans%(2*change)
        ans+=time
    return ans
                

1/25 1688. 比赛中的配对次数

1.递归
2.比赛一次淘汰一个人 需要淘汰n-1个人 比赛n-1次

def numberOfMatches(n):
    """
    :type n: int
    :rtype: int
    """
    def recu(n):
        if n==1:
            return 0
        if n%2==0:
            return n//2
        else:
            return n//2+recu(n//2+1)
    return recu(n)

def numberOfMatches2(n):
    """
    :type n: int
    :rtype: int
    """
    return n-1

1/26 2013. 检测正方形

哈希表统计坐标
对于x,y 如果y固定了 找另一个点的tmpx,y
边长为diff=tmpx-x
寻找(x,y) (tmpx,y),(x,y+diff),(tmpx,y+diff)

from collections import defaultdict,Counter
class DetectSquares(object):

    def __init__(self):
        self.m = defaultdict(Counter)


    def add(self, point):
        """
        :type point: List[int]
        :rtype: None
        """
        x,y = point
        self.m[x][y]+=1


    def count(self, point):
        """
        :type point: List[int]
        :rtype: int
        """
        ans = 0
        x,y = point
        if x not in self.m:
            return 0
        othery = self.m[x]
        
        for tmpx,cnt in self.m.items():
            if tmpx!=x:
                diff = tmpx-x
                ans += cnt[y]*othery[y+diff]*cnt[y+diff]
                ans += cnt[y]*othery[y-diff]*cnt[y-diff]
        return ans

1/27 2047. 句子中的有效单词数

按空格分割成若干小段s
对每个s中的字符依次处理
分为数字;-;标点;英文字符

def countValidWords(sentence):
    """
    :type sentence: str
    :rtype: int
    """
    biao = ["!",".",","]
    def check(s):
        if len(s)==0:
            return False
        word = 0
        line = 0
        has = False
        for i,c in enumerate(s):
            if "0"<=c<="9":
                return False
            elif c=="-":
                if line>0 or word==0:
                    return False
                line+=1
                has = True
            elif c in biao:
                if i<len(s)-1:
                    return False
            else:
                word+=1
                has = False
        if has:
            return False
        return True                
    l = sentence.split(" ")
    ans = 0
    for s in l:
        if check(s):
            print(s)
            ans+=1
    return ans

1/28 1996. 游戏中弱角色的数量

将攻击力a从大到小排序 同时攻击力相同时防御力b从小到大排序
记录最大防御力maxb 可以保证如果当前防御力b小于最大防御力maxb
则攻击力a必定也小于最大攻击力maxa

def numberOfWeakCharacters(properties):
    """
    :type properties: List[List[int]]
    :rtype: int
    """
    properties.sort(key = lambda x:(-x[0],x[1]))
    ans = 0
    maxb = 0
    for a,b in properties:
        if maxb==0:
            maxb = b
        else:
            if maxb>b:
                ans+=1
            else:
                maxb = b
    return ans

1/29 1765. 地图中的最高点

BFS
初始化水域高度为0 陆地高度为无穷
从水域开始向外扩展 依次增加高度 陆地高度取较小值

def highestPeak(isWater):
    """
    :type isWater: List[List[int]]
    :rtype: List[List[int]]
    """
    n,m = len(isWater),len(isWater[0])
    matrix = [[float('inf')]*m for _ in range(n)]
    l = []
    for i in range(n):
        for j in range(m):
            if isWater[i][j]==1:
                l.append((i,j))
                matrix[i][j]=0
    steps = [(0,-1),(0,1),(1,0),(-1,0)]
    while l:
        tmp = set()
        for i,j in l:
            for x,y in steps:
                newi,newj = i+x,j+y
                if 0<=newi<n and 0<=newj<m:
                    if matrix[i][j]+1<matrix[newi][newj]:
                        matrix[newi][newj] = matrix[i][j]+1
                        tmp.add((newi,newj))
        l = list(tmp)
    return matrix

1/30 884. 两句话中的不常见单词

题意可知
将所有单词统计到一起 出现一次的就是不常见单词

def uncommonFromSentences(s1, s2):
    """
    :type s1: str
    :type s2: str
    :rtype: List[str]
    """
    ans = []
    m = {}
    for word in s1.split(" "):
        m[word] = m.get(word,0)+1
    for word in s2.split(" "):
        m[word] = m.get(word,0)+1
    for word in m:
        if m[word]==1:
            ans.append(word)
    return ans
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值