码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode 每日一题 2023/10/23-2023/10/29


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


    目录

        • 10/23 2678. 老人的数目
        • 10/24 1155. 掷骰子等于目标和的方法数
        • 10/25 2698. 求一个整数的惩罚数
        • 10/26 2520. 统计能整除数字的位数
        • 10/27 1465. 切割后面积最大的蛋糕
        • 10/28 2558. 从数量最多的堆取走礼物
        • 10/29 274. H 指数


    10/23 2678. 老人的数目

    取第12-13位数值

    def countSeniors(details):
        """
        :type details: List[str]
        :rtype: int
        """
        ans = 0
        for d in details:
            age = int(d[11:13])
            if age>60:
                ans +=1
        return ans
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    10/24 1155. 掷骰子等于目标和的方法数

    递归 mem记录已有过的情况

    def numRollsToTarget(n, k, target):
        """
        :type n: int
        :type k: int
        :type target: int
        :rtype: int
        """
        if n>target or target>n*k:
            return 0
        MOD = 10**9+7
        mem = {(1,x):1 for x in range(1,k+1)}
        def check(n,target):
            if target<=0:
                return 0
            if (n,target) in mem:
                return mem[(n,target)]
            ans = 0
            if n==0:
                ans = target==0
            else:
                for i in range(1,k+1):
                    ans += check(n-1,target-i)
            mem[(n,target)] = ans
            return ans%MOD
        return check(n,target)
                
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    10/25 2698. 求一个整数的惩罚数

    遍历每个数 判断是否满足条件

    def punishmentNumber(n):
        """
        :type n: int
        :rtype: int
        """
        def check(s,i,x):
            m = len(s)
            if i>=m:
                return x==0
            y = 0
            for j in range(i,m):
                y = y*10+int(s[j])
                if y>x:
                    break
                if check(s,j+1,x-y):
                    return True
            return False
        ans = 0
        for i in range(1,n+1):
            if check(str(i*i),0,i):
                ans+=i*i
        return ans
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    10/26 2520. 统计能整除数字的位数

    按每一位数字判断

    def countDigits(num):
        """
        :type num: int
        :rtype: int
        """
        ans = 0
        n = num
        while n:
            v = n%10
            if v>0 and num%v==0:
                ans +=1
            n//=10
        return ans
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    10/27 1465. 切割后面积最大的蛋糕

    将切口位置从小到大排序 计算两个切口间的距离
    寻找最大距离 相乘

    def maxArea(h, w, horizontalCuts, verticalCuts):
        """
        :type h: int
        :type w: int
        :type horizontalCuts: List[int]
        :type verticalCuts: List[int]
        :rtype: int
        """
        horizontalCuts.sort()
        verticalCuts.sort()
        if len(horizontalCuts)>0:
            maxh = max(horizontalCuts[0],h-horizontalCuts[-1])
        else:
            maxh = h
        if len(verticalCuts)>0:
            maxv = max(verticalCuts[0],w-verticalCuts[-1])
        else:
            maxv = w
        for i in range(1,len(horizontalCuts)):
            maxh = max(maxh,horizontalCuts[i]-horizontalCuts[i-1])
        for i in range(1,len(verticalCuts)):
            maxv = max(maxv,verticalCuts[i]-verticalCuts[i-1])
        return (maxv*maxh)%(10**9+7)
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    10/28 2558. 从数量最多的堆取走礼物

    大顶堆 每次取最大的礼物 进行处理

    def pickGifts(gifts, k):
        """
        :type gifts: List[int]
        :type k: int
        :rtype: int
        """
        import heapq,math
        ans = sum(gifts)
        l = [-x for x in gifts]
        heapq.heapify(l)
        for i in range(k):
            print(l)
            v = -heapq.heappop(l)
            num = int(math.sqrt(v))
            ans -= (v-num)
            heapq.heappush(l, -num)
            
        return ans
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    10/29 274. H 指数

    将引用次数从小到大排序 从小开始判断是否满足

    def hIndex(citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        citations.sort()
        n = len(citations)
        for i in range(len(citations)):
            if citations[i]>=n-i:
                return n-i
        return 0
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

  • 相关阅读:
    一本通1061;求整数的和与均值
    【TVM源码学习笔记】3 模型编译
    竞赛选题 深度学习+opencv+python实现车道线检测 - 自动驾驶
    猿创征文|List 列表(创建、索引和切片、列表反转、添加删除修改查找元素)__全实例详解(四)
    智能化“竞赛下半场”,什么才是可量产的最佳实践?
    BFS之最短路径
    【bootstrap】bootstrap布局范例--20220917
    spark shuffle写操作——UnsafeShuffleWriter
    速盾:使用 CDN 可以隐藏 IP 吗?该怎样应对防御?
    Coumarin 343 X azide/carboxylic acid/NHS ester,香豆素343 X 叠氮化物/羧基羧酸/琥珀酰亚胺活化酯
  • 原文地址:https://blog.csdn.net/zkt286468541/article/details/134050595
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号