码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Python 1-06 练习


    2591. 将钱分给最多的儿童

    class Solution:
        def distMoney(self, money: int, children: int) -> int:
            if money < children: return -1
            money -= children # 每人至少 1 美元
            res = min(money // 7, children) # 初步分配,让尽量多的人分到 8 美元
            money -= res * 7
            children -= res
            if (children == 0 and money > 0) or (children == 1 and money == 3):
                res -= 1
            return res
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2706. 购买两块巧克力

    算法:最小值与次最小值

    class Solution:
        def buyChoco(self, prices: List[int], money: int) -> int:
            a = b = inf
            for x in prices:
                if x < a:
                    b, a = a, x
                elif x < b:
                    b = x
            return money - a - b if a + b <= money else money 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    374. 猜数字大小

    算法:二分查找

    # The guess API is already defined for you.
    # @param num, your guess
    # @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
    # def guess(num: int) -> int:
    
    class Solution:
        def guessNumber(self, n: int) -> int:
            l, r = 1, n
            while l <= r:
                mid = l + r >> 1
                if guess(mid) == 1: l = mid + 1
                elif guess(mid) == -1: r = mid - 1
                else: return mid
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    1732. 找到最高海拔

    算法:差分逆运算前缀和

    class Solution:
        def largestAltitude(self, gain: List[int]) -> int:
            res = acc = 0
            for x in gain:
                acc += x
                if acc > res: res = acc
            return res
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    1413. 逐步求和得到正数的最小值

    算法:前缀和

    class Solution:
        def minStartValue(self, nums: List[int]) -> int:
            ans, acc = 1, 0
            for x in nums:
                acc += x
                if acc < 0:
                    ans = max(ans, 1 - acc)
            return ans
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2057. 值相等的最小索引

    class Solution:
        def smallestEqual(self, nums: List[int]) -> int:
            for i, x in enumerate(nums):
                if i % 10 == x: return i
            return -1
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1217. 玩筹码

    第 i 个芯片的 位置 是 position[i],注意不是筹码数。
    知识点: for, %, min(), +=

    class Solution:
        def minCostToMoveChips(self, position: List[int]) -> int:        
            # odd = even = 0
            # for k in position:
            #     if k % 2: odd += 1
            #     else: even += 1
            # return min(even, odd)
            
            # d = [0, 0]
            # for i in position: d[i%2] += 1
            # return min(d)
    
            return min(x := sum(i & 1 for i in position), len(position) - x) 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    1184. 公交站间的距离

    class Solution:
        def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:       
            if destination < start: # 交换出发点和目的地距离相等
                start, destination = destination, start            
            d = sum(distance[start : destination]) # 出发点到目的地距离
            return min(d, sum(distance) - d)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2057. 值相等的最小索引

    class Solution:
        def smallestEqual(self, nums: List[int]) -> int:
            for i, x in enumerate(nums):
                if i % 10 == x: return i
            return -1 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2455. 可被三整除的偶数的平均值

    class Solution:
        def averageValue(self, nums: List[int]) -> int:
            sum = count = 0
            for x in nums:
                if x % 6 == 0:
                    sum += x
                    count += 1
            return 0 if count == 0 else sum // count
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2078. 两栋颜色不同且距离最远的房子

    class Solution:
        def maxDistance(self, colors: List[int]) -> int:
            n = len(colors)
            res = 0   # 两栋颜色不同房子的最远距离
            # 遍历两栋房子下标并维护最远距离
            for i in range(n):
                for j in range(i + 1, n):
                    if colors[i] != colors[j]:
                        res = max(res, j - i)
            return res
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    优化

    class Solution:
        def maxDistance(self, colors: List[int]) -> int:
            n = len(colors)
            c = colors[0]
            if c != colors[-1]: return n - 1
            i, j = 1, n - 2
            while colors[i] == c: i += 1
            while colors[j] == c: j -= 1
            return max(j, n - i - 1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2073. 买票需要的时间

    class Solution:
        def timeRequiredToBuy(self, tickets: List[int], k: int) -> int:
            n = len(tickets)
            res = 0
            for i in range(n):
                # 遍历计算每个人所需时间
                if i <= k:
                    res += min(tickets[i], tickets[k])
                else:
                    res += min(tickets[i], tickets[k] - 1)
            return res
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    1848. 到目标元素的最小距离

    class Solution:
        def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
            res = inf
            for i, x in enumerate(nums):
                if x == target:
                    res = min(res, abs(i - start))
            return res
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    聚观早报|华为Mate 60 Pro支持面容支付;特斯拉重回底特律车展
    基于Next.js、Prisma、Postgres和Fastfy构建全栈APP
    android UI到系统揭秘
    神经网络最重要的是什么,什么神经网络最好用
    JAVA毕业论文答辩管理系统计算机毕业设计Mybatis+系统+数据库+调试部署
    Java——逻辑控制1
    OCP Java17 SE Developers 复习题06
    部署LVS-NAT群集实验
    网页设计期末课程大作业:基于HTML+CSS+JavaScript+Bootstrap制作响应式网站信息技术交流博客(7页)
    【英语:基础进阶_核心词汇扩充】E4.常见词根拓词
  • 原文地址:https://blog.csdn.net/weixin_43955170/article/details/133201260
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号