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

news/2024/7/24 11:46:31 标签: leetcode, 算法

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


目录

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


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

如果4个人的钱小于运行的钱 则必定亏本
依次遍历每个时间点的游客 wait记录当前等待游客数量
ans记录最大利润时的经营时间 cur记录当前利润 maxv记录最大利润
当没有后续游客时 继续考虑等待的游客 每次上4人使得利润最大化

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 寻找到能够开始循环的地方

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. 从链表中移除节点

递归 对右侧进行操作

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都被覆盖了

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. 队列中可以看到的人数

从最右侧开始考虑
对于右侧不高于自己的人 左侧的人必定看不到
维护一个单调栈 递减
忽略不高于自己的人

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. 在链表中插入最大公约数

遍历每一个节点 在节点后插入公约数
下一步跳过最新插入的节点

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检查是否满足

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





http://www.niftyadmin.cn/n/5313262.html

相关文章

ip协议历史

今天的互联网&#xff0c;是万维网&#xff08;WWW&#xff09;一家独大。而在上世纪七八十年代&#xff0c;人们刚开始尝试网络连接时&#xff0c;那时出现了计算机科学研究网络、ALOHA 网、因时网、阿帕网等不同类型的网络&#xff0c;这些网络之间互相通信是个难题。 于是&…

基于SpringBoot摄影跟拍预定管理系统(系统+数据库+文档)

&#x1f345;点赞收藏关注 → 私信领取本源代码、数据库&#x1f345; 本人在Java毕业设计领域有多年的经验&#xff0c;陆续会更新更多优质的Java实战项目 希望你能有所收获&#xff0c;少走一些弯路。&#x1f345;关注我不迷路&#x1f345;摘 要 首先,一开始便是清楚的论…

RocketMQ5-03RocketMQ-Dashboard和Java客户端访问示例

接上篇02快速部署RocketMQ5.x(手动和容器部署) 已经完成 RocketMQ5.0 环境的部署&#xff0c;就需要对这个环境进行测试&#xff0c;查看集群、写入消息、读取消息等 本篇教你如何使用和查看部署的服务&#xff1a; Docker部署 Dashboard 获取镜像并下载部署服务 客户端连接 …

【互联网安全架构】渗透测试及常见的Web攻击手段

1.网络安全和渗透测试 &#xff08;1&#xff09;什么是网络安全 网络防御主要是针对各类网络攻击提供网络安全防御方案&#xff0c;为了应对攻击技术的不断革新&#xff0c;防御技术已经逐步从被动防御的历史阶段转变为主动防御阶段。目前常见的网络防御技术大体类型有加密技…

物理机搭建hive

一、修改Hadoop配置 修改core-site.xml 配置yarn-site.xml 分发文件&#xff0c;然后重启集群 二、 Hive解压安装 上传文件 添加hive环境便量&#xff0c;source生效 启动高可用集群&#xff0c;启动hive 三、配置mysql元数据库 检查当前系统是否安装过Mysql&#xf…

10款以上开源工具,用于大型语言模型应用开发

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

【PostgreSQL】在DBeaver中实现序列、函数、视图、触发器设计

【PostgreSQL】在DBeaver中实现序列、函数、触发器、视图设计 基本配置一、序列1.1、序列使用1.1.1、设置字段为主键&#xff0c;数据类型默认整型1.1.2、自定义序列&#xff0c;数据类型自定义 1.2、序列延申1.2.1、理论1.2.2、测试1.2.3、小结 二、函数2.1、SQL直接创建2.1.1…

【Python】TensorFlow的详细介绍

一、简介 TensorFlow是一个由Google开发的开源机器学习框架&#xff0c;用于构建和训练深度学习模型。TensorFlow最初由Google Brain团队开发&#xff0c;并于2015年在GitHub上发布。它是一个用于构建和训练机器学习模型的强大框架&#xff0c;被广泛用于深度学习和其他机器学…