0
点赞
收藏
分享

微信扫一扫

python 2024-9


第一课

问题

a, b 求最大值?分类讨论

if a > b:
        print("最大值 = ", a)
    else:
        print("最大值 = ", b)

a, b, c 求最大值?

条件语句 if ... elif ... else

列表最大值?与参照物循环比较

a = [1.7, 1.65, 1.8, 1.55, 1.6] # 身高列表
    mx = 0 # 初始化最大值
    for x in a:
        if x > mx:
            mx = x
    print("最高身高为", mx)

循环语句 for x in a
内置函数 max

Python 实例教学_01_基础语法

第二课

Python 1-03 条件语句Python 1-05 控制流练习

2591. 将钱分给最多的儿童

class Solution:
    def distMoney(self, money: int, children: int) -> int:
        money -= children  # 每人至少 1 美元
        if money < 0: return -1
        res = min(money // 7, children)  # 初步分配,让尽量多的人分到 8 美元
        money -= res * 7
        children -= res
        # children == 0 and money:必须找一个前面分了 8 美元的人,分配完剩余的钱
        # children == 1 and money == 3:不能有人恰好分到 4 美元
        if children == 0 and money or \ # \ 续行符,money 非零为真,0 为假
           children == 1 and money == 3:
	           res -= 1
        return res

class Solution:
    def distMoney(self, money: int, children: int) -> int:
        money -= children  # 每人至少 1 美元
        if money < 0: return -1
        d = money // 7 # // 整除,/ 除法
        rem = money % 7 # % 取余
        # 分类讨论 每人可分得 8 元,剩余的加在其中某人身上。
        if d > children: return children - 1
        if d == children:
            return children - 1 if rem > 0 else children
        if d == children - 1 and rem == 3: return d - 1
        return d

第三课

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

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

第四课

Python 1-04 循环语句

412. Fizz Buzz

class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        res = [''] * 列表包含 n # n 个 空串
        # res = []
        for i in range(1, n + 1):
            if i % 15 == 0: x = "FizzBuzz" # 先处理 15 的倍数
            elif i % 3 == 0: x = "Fizz"
            elif i % 5 == 0: x = "Buzz"
            else: x = str(i) # 转换成字符串
            res[i - 1] = x
            # res.append(x)
        return res

        # return ['Fizz'[i%3*4:]+"Buzz"[i%5*4:] or str(i) for i in range(1, n+1)]

第五课

747. 至少是其他数字两倍的最大数

知识点: 条件表达式

class Solution:
    def dominantIndex(self, nums: List[int]) -> int:
        # 方法一:最大值 >= 2 倍次大值
        first = second = id = 0
        for i, n in enumerate(nums):
            if n > first:
                first, second = n, first
                id = i
            elif n > second:
                second = n
        
        return id if first >= 2 * second else -1
        # 方法二:求最大值
        ans = max_ = -1        
        for i, x in enumerate(nums):  
            if x >= max_ * 2: ans = i # 当前的 max_ 对当前的 x 是满足条件的,先保存下来,但不一定是最终值。
            elif x > max_ // 2: ans = -1 # 说明 max_ 不符合条件,先记 ans = -1           
            max_ = max(max_, x) 
        return ans
        # 方法三:最大值函数
        max_, ans = max(nums), -1
        for i, x in enumerate(nums):
            if max_ == x: ans = i
            elif x > max_ // 2: return -1
        return ans
        # 方法四:离线排序
        q = sorted(range(len(nums)), key=lambda i:nums[i])
        return -1 if nums[q[-2]]*2 > nums[q[-1]] else q[-1]

第六课

1518. 换酒问题

知识点: while, /, %

class Solution:
    def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
        res = rem = numBottles # 全部喝完,rem 为空瓶数
        while rem >= numExchange:
            numBottles, rem = divmod(rem, numExchange) # 可换酒 numBottles 瓶,剩余 rem 个空瓶。
            res += numBottles # 全部喝完
            rem += numBottles # + 空瓶           
        return res

第七课

2239. 找到最接近 0 的数字

class Solution:
    def findClosestNumber(self, nums: List[int]) -> int:
        ans = inf
        for x in nums:
            if abs(x) == abs(ans): ans = max(x, ans)
            elif abs(x) < abs(ans):  ans = x                
        return ans

3289. 数字小镇中的捣蛋鬼

标记法

class Solution:
    def getSneakyNumbers(self, nums: List[int]) -> List[int]:
        res = [0, 0]
        n = len(nums)
        i = 0
        for x in nums:
            x %= n
            if nums[x] >= n:
                res[i] = x
                i += 1
                if i == 2: break
            nums[x] += n
        return res

2432… 处理用时最长的那个任务的员工

class Solution:
    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:
        ans = pre = most = 0
        for i, t in logs: 
            dif = t - pre
            pre = t
            if most < dif or most == dif and ans > i:
                ans = i
                most = dif

        return ans


举报

相关推荐

0 条评论