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


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


    目录

        • 10/9 2578. 最小和分割
        • 10/10 2731. 移动机器人
        • 10/11 2512. 奖励最顶尖的 K 名学生
        • 10/12 2562. 找出数组的串联值
        • 10/13 1488. 避免洪水泛滥
        • 10/14 136. 只出现一次的数字
        • 10/15 137. 只出现一次的数字 II


    10/9 2578. 最小和分割

    统计各个数字出现次数 从小到大排序 依次给两个数

    def splitNum(num):
        """
        :type num: int
        :rtype: int
        """
        s = sorted(str(num))
        ans = int(''.join(s[::2]))+int(''.join(s[1::2]))
        return ans
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    10/10 2731. 移动机器人

    根据题意碰撞可以忽略 两机器人可以视作互相穿透
    所以每个机器人可以得到最后位置为nums[i]+/-d
    排序后计算两两距离

    def sumDistance(nums, s, d):
        """
        :type nums: List[int]
        :type s: str
        :type d: int
        :rtype: int
        """
        mod=10**9+7
        n=len(nums)
        l = [nums[i]-d if s[i]=='L' else nums[i]+d for i in range(n)]
        l.sort()
        ans = 0
        for i in range(1,n):
            ans =(ans + (l[i]-l[i-1])*i*(n-i))%mod
        return ans
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    10/11 2512. 奖励最顶尖的 K 名学生

    一个分值ma存放词汇分值
    算出每个学生分数后排序

    def topStudents(positive_feedback, negative_feedback, report, student_id, k):
        """
        :type positive_feedback: List[str]
        :type negative_feedback: List[str]
        :type report: List[str]
        :type student_id: List[int]
        :type k: int
        :rtype: List[int]
        """
        m = {}
        for w in positive_feedback:
            m[w]=3
        for w in negative_feedback:
            m[w]=-1
        ans=[]
        for st,re in zip(student_id,report):
            v = 0
            for w in re.split():
                v+=m.get(w,0)
            ans.append((v,st))
        ans.sort(key=lambda x :(-x[0],x[1]))
        return [x[1] for x in ans[:k]]
    
    
    
    • 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/12 2562. 找出数组的串联值

    依次累加

    def findTheArrayConcVal(nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n = len(nums)
        ans = 0
        for i in range(n//2):
            ans += int(str(nums[i])+str(nums[n-1-i]))
        if n%2==1:
            ans += nums[n//2]
        return ans
            
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    10/13 1488. 避免洪水泛滥

    sun记录晴天 如果遇到洪水 找晚于第一次并且早于洪水的晴天 抽干

    
    def avoidFlood(rains):
        """
        :type rains: List[int]
        :rtype: List[int]
        """
        from sortedcontainers import SortedList
        ans = [1]*len(rains)
        sun = SortedList()
        m = {}
        for i,r in enumerate(rains):
            if r==0:
                sun.add(i)
            else:
                ans[i]=-1
                if r in m:
                    d = sun.bisect(m[r])
                    if len(sun)==d:
                        return []
                    ans[sun[d]]=r
                    sun.discard(sun[d])
                m[r]=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
    • 25

    10/14 136. 只出现一次的数字

    两两异或 相同的数字会抵消
    最后剩下来的就是只出现过一次的

    def singleNumber(nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        ans = 0
        for num in nums:
            ans ^=num
        return ans
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    10/15 137. 只出现一次的数字 II

    1.one,two,three分别代表出现了一次 两次 三次
    位运算使得一个数在出现了三次后 被重置
    2.通用方法 set去重 每个数乘3求和 减去原有和 剩下的为出现一次的数值的两倍

    def singleNumber(nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        one,two,three=0,0,0
        for num in nums:
            two = two | (one & num)
            one = one ^ num
            three = (one & two)
            two = two & ~three
            one = one & ~three
        return one
    
    def singleNumber2(nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return int((sum(set(nums))*3-sum(nums))/2)
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

  • 相关阅读:
    面试中的自我激励:如何展示你的内在驱动力
    关系型数据库之MySQL8——由内而外的深化全面学习
    前端代码规范
    Golang-channel合集——源码阅读、工作流程、实现原理、已关闭channel收发操作、优雅的关闭等面试常见问题。
    传递泛型给JSX元素
    golang 工厂模式
    Linux命令详解-find命令(一)
    迅为IMX8M开发板设备树下的platform驱动实验程序编写
    论文笔记:DETR: End-to-End Object Detection with Transformers (from 李沐老师and朱老师)
    使用Locust进行性能测试
  • 原文地址:https://blog.csdn.net/zkt286468541/article/details/133811215
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号