LeetCode 每日一题 2024/1/1-2024/1/7

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


目录

      • [1/1 1599. 经营摩天轮的最大利润](#1/1 1599. 经营摩天轮的最大利润)
      • [1/2 466. 统计重复个数](#1/2 466. 统计重复个数)
      • [1/3 2487. 从链表中移除节点](#1/3 2487. 从链表中移除节点)
      • [1/4 2397. 被列覆盖的最多行数](#1/4 2397. 被列覆盖的最多行数)
      • [1/5 1944. 队列中可以看到的人数](#1/5 1944. 队列中可以看到的人数)
      • [1/6 2807. 在链表中插入最大公约数](#1/6 2807. 在链表中插入最大公约数)
      • [1/7 383. 赎金信](#1/7 383. 赎金信)

1/1 1599. 经营摩天轮的最大利润

如果4个人的钱小于运行的钱 则必定亏本

依次遍历每个时间点的游客 wait记录当前等待游客数量

ans记录最大利润时的经营时间 cur记录当前利润 maxv记录最大利润

当没有后续游客时 继续考虑等待的游客 每次上4人使得利润最大化

python 复制代码
def minOperationsMaxProfit(customers, boardingCost, runningCost):
    """
    :type customers: List[int]
    :type boardingCost: int
    :type runningCost: int
    :rtype: int
    """
    if 4*boardingCost<runningCost:
        return -1
    wait = 0
    ans = -1
    cur = -999
    maxv = -999
    num = 0
    for cus in customers:
        wait += cus
        tmp = min(4,wait)
        wait -=tmp
        cur += tmp*boardingCost-runningCost
        num +=1
        if cur>maxv:
            maxv = cur
            ans = num
    while wait>0:
        tmp = min(4,wait)
        wait -=tmp
        cur += tmp*boardingCost-runningCost
        num +=1
        if cur>maxv:
            maxv = cur
            ans = num
    return ans

1/2 466. 统计重复个数

从1个s1开始寻找s2 不停的添加s1 寻找到能够开始循环的地方

python 复制代码
def getMaxRepetitions(s1, n1, s2, n2):
    """
    :type s1: str
    :type n1: int
    :type s2: str
    :type n2: int
    :rtype: int
    """
    if n1==0:
        return 0
    s1cnt,ind,s2cnt = 0,0,0
    m = {}
    while True:
        s1cnt +=1
        for c in s1:
            if c==s2[ind]:
                ind+=1
                if ind==len(s2):
                    s2cnt+=1
                    ind=0
        if s1cnt==n1:
            return s2cnt//n2
        if ind in m:
            s1p,s2p = m[ind]
            pre = (s1p,s2p)
            nxt =(s1cnt-s1p,s2cnt-s2p)
            break
        else:
            m[ind] = (s1cnt,s2cnt)
    ans = pre[1]+(n1-pre[0])//(nxt[0])*nxt[1]
    l = (n1-pre[0])%nxt[0]
    for i in range(l):
        for c in s1:
            if c==s2[ind]:
                ind+=1
                if ind==len(s2):
                    ans+=1
                    ind=0
    return ans//n2
            

1/3 2487. 从链表中移除节点

递归 对右侧进行操作

python 复制代码
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
def removeNodes(head):
    """
    :type head: Optional[ListNode]
    :rtype: Optional[ListNode]
    """
    def do(node):
        if node is None:
            return None
        node.next = do(node.next)
        if node.next and node.val<node.next.val:
            return node.next
        else:
            return node
    return do(head)
    

1/4 2397. 被列覆盖的最多行数

枚举每一种情况

cur为选取的状态 1为选择 0位不选

每一行使用二进制表示 mask[i]

与cur相与如果数值不变说明所有1都被覆盖了

python 复制代码
def maximumRows(matrix, numSelect):
    """
    :type matrix: List[List[int]]
    :type numSelect: int
    :rtype: int
    """
    m,n=len(matrix),len(matrix[0])
    mask = [sum(v<<j for j,v in enumerate(row)) for i,row in enumerate(matrix)]
    ans,limit = 0,1<<n
    for cur in range(1,limit):
        if cur.bit_count()!=numSelect:
            continue
        t = sum((mask[j]&cur)==mask[j] for j in range(m))
        ans = max(ans,t)
    return ans

1/5 1944. 队列中可以看到的人数

从最右侧开始考虑

对于右侧不高于自己的人 左侧的人必定看不到

维护一个单调栈 递减

忽略不高于自己的人

python 复制代码
def canSeePersonsCount(heights):
    """
    :type heights: List[int]
    :rtype: List[int]
    """
    st=[]
    n = len(heights)
    ans = [0]*n
    for i in range(n-1,-1,-1):
        cur = heights[i]
        while st and cur>st[-1]:
            ans[i]+=1
            st.pop()
        if st:
            ans[i]+=1
        st.append(cur)
    return ans

1/6 2807. 在链表中插入最大公约数

遍历每一个节点 在节点后插入公约数

下一步跳过最新插入的节点

python 复制代码
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
def insertGreatestCommonDivisors(head):
    """
    :type head: Optional[ListNode]
    :rtype: Optional[ListNode]
    """
    import math
    node = head
    while node.next is not None:
        node.next = ListNode(math.gcd(node.val, node.next.val), node.next)
        node = node.next.next
    return head

1/7 383. 赎金信

记录magazine中的字符个数

遍历ransom检查是否满足

python 复制代码
def canConstruct(ransomNote, magazine):
    """
    :type ransomNote: str
    :type magazine: str
    :rtype: bool
    """
    if len(magazine)<len(ransomNote):
        return False
    from collections import defaultdict
    m = defaultdict(int)
    for c in magazine:
        m[c]+=1
        
    for c in ransomNote:
        m[c]-=1
        if m[c]<0:
            return False
    return True

相关推荐
颜酱5 小时前
图的数据结构:从「多叉树」到存储与遍历
javascript·后端·算法
zone773910 小时前
006:RAG 入门-面试官问你,RAG 为什么要切块?
后端·算法·面试
CoovallyAIHub13 小时前
OpenClaw 近 2000 个 Skills,为什么没有一个好用的视觉检测工具?
深度学习·算法·计算机视觉
CoovallyAIHub13 小时前
CVPR 2026 | 用一句话告诉 AI 分割什么——MedCLIPSeg 让医学图像分割不再需要海量标注
深度学习·算法·计算机视觉
CoovallyAIHub13 小时前
Claude Code 突然变成了 66 个专家?这个 5.8k Star 的开源项目,让我重新理解了什么叫"会用 AI"
深度学习·算法·计算机视觉
兆子龙13 小时前
前端哨兵模式(Sentinel Pattern):优雅实现无限滚动加载
前端·javascript·算法
xlp666hub16 小时前
Leetcode第五题:用C++解决盛最多水的容器问题
linux·c++·leetcode
CoovallyAIHub17 小时前
9个视觉语言模型工厂实测:Qwen 87.9%碾压全场,你的显卡能跑哪个?
算法
SparkX开源AI知识库17 小时前
手摸手带你安装OpenClaw并对接飞书
算法·架构
一语071617 小时前
3分钟搞懂深度学习AI:实操篇:卷积层
人工智能·算法