LeetCode 每日一题 2023/10/30-2023/11/5

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




10/30 275. H 指数 II

二分查找

def hIndex(citations):
    """
    :type citations: List[int]
    :rtype: int
    """
    n = len(citations)
    l,r = 0,n-1
    while l<=r:
        mid = (l+r)>>1
        if citations[mid]==n-mid:
            return n-mid
        elif citations[mid]<n-mid:
            l = mid+1
        else:
            r = mid-1
    return n-l




10/31 2003. 每棵子树内缺失的最小基因值

dfs 如果子树基因没有1 则答案全为1
从基因为1的子节点往上判断

def smallestMissingValueSubtree(parents, nums):
    """
    :type parents: List[int]
    :type nums: List[int]
    :rtype: List[int]
    """
    n = len(parents)
    ans = [1]*n
    if 1 not in nums:
        return ans
    
    g = [[] for _ in range(n)]
    for i in range(1,n):
        g[parents[i]].append(i)
    
    mem = set()
    def dfs(x):
        mem.add(nums[x])
        for nxt in g[x]:
            if nums[nxt] not in mem:
                dfs(nxt)
    miss = 2
    node = nums.index(1)
    while node>=0:
        dfs(node)
        while miss in mem:
            miss +=1
        ans[node] = miss
        node = parents[node]
    return ans
            



11/1 2127. 参加会议的最多员工数

拓扑排序
先剪掉图中树枝
在遍历环上的点
参考https://leetcode.cn/problems/maximum-employees-to-be-invited-to-a-meeting/solutions/1187830/nei-xiang-ji-huan-shu-tuo-bu-pai-xu-fen-c1i1b/?envType=daily-question&envId=2023-11-01

def maximumInvitations(favorite):
    """
    :type favorite: List[int]
    :rtype: int
    """
    from collections import deque
    n = len(favorite)
    deg = [0]*n
    for f in favorite:
        deg[f]+=1
    
    maxdep = [1]*n
    q = deque(i for i,d in enumerate(deg) if d==0)
    while q:
        x = q.popleft()
        y = favorite[x]
        maxdep[y] = maxdep[x]+1
        deg[y]-=1
        if deg[y]==0:
            q.append(y)
    
    maxring = sumchain = 0
    for i,d in enumerate(deg):
        if d==0:
            continue
        deg[i] = 0
        ringsize = 1
        x = favorite[i]
        while x!=i:
            deg[x] = 0
            ringsize+=1
            x = favorite[x]
        if ringsize ==2:
            sumchain +=maxdep[i]+maxdep[favorite[i]]
        else:
            maxring = max(maxring,ringsize)
    return max(maxring,sumchain)




11/2 2103. 环和杆

依序判断 l[i]记录位置i的各个颜色状态

def countPoints(rings):
    """
    :type rings: str
    :rtype: int
    """
    l = [0]*10
    n = len(rings)
    m = {'R':1,'G':2,'B':4}
    for i in range(0,n,2):
        c,loc = rings[i],int(rings[i+1])
        l[loc] |=m[c]
    
    ans = 0
    for i in range(10):
        if l[i]==7:
            ans +=1
    return ans



11/3 117. 填充每个节点的下一个右侧节点指针 II

BFS 广搜遍历每一层

class Node(object):
    def __init__(self, val=0, left=None, right=None, next=None):
        self.val = val
        self.left = left
        self.right = right
        self.next = next

def connect(root):
    """
    :type root: Node
    :rtype: Node
    """
    if not root:
        return root
    l = [root]
    while l:
        tmp = []
        for node in l:
            if node.left:
                tmp.append(node.left)
            if node.right:
                tmp.append(node.right)
        l.append(None)
        for i in range(len(l)-1):
            l[i].next = l[i+1]
        l = tmp
    return root



11/4 421. 数组中两个数的最大异或值

从最高位开始查看 是否能够取到1
如果可以则将该位置i设定为1
否则为0 判断下一位

def findMaximumXOR(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    ans = 0
    mask = 0
    maxbit = max(nums).bit_length()-1
    print(maxbit)
    for i in range(maxbit,-1,-1):
        mask |= 1<<i
        cur = ans |(1<<i)
        s = set()
        for x in nums:
            x &=mask
            if cur^x in s:
                ans = cur
                break
            s.add(x)
    return ans



11/5 187. 重复的DNA序列

set中存放出现过的序列

def findRepeatedDnaSequences(s):
    """
    :type s: str
    :rtype: List[str]
    """
    one,ret = set(),set()
    for i in range(len(s)-9):
        subs = s[i:i+10]
        if subs in one:
            ret.add(subs)
        else:
            one.add(subs)
    return list(ret)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值