当前位置:   article > 正文

Leetcode 第70场双周赛Python题解_打折购买糖果的最小开销python

打折购买糖果的最小开销python

1 打折购买糖果的最小开销

排序,倒序每取两个就跳过一个

class Solution:
    def minimumCost(self, cost: List[int]) -> int:
        cost.sort()
        res = 0
        i = len(cost) - 1
        while i >= 0:
            res += cost[i]
            i -= 1

            if i >= 0:
                res += cost[i]
                i -= 1
            i -= 1
        return res
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2 统计隐藏数组数目

前缀和,找到最大最小值,并判断在low到up区间上,加上最大最小值是否在还符合在low到up区间

class Solution:
    def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:
        for i in range(1, len(differences)):
            differences[i] = differences[i] + differences[i - 1]

        max_num = -10000001
        min_num = 10000001
        for difference in differences:
            max_num = max(max_num, difference)
            min_num = min(min_num, difference)

        res = 0
        for i in range(lower, upper + 1):
            if i + min_num >= lower and i + max_num <= upper:
                res += 1
        return res
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3 价格范围内最高排名的K样物品

BFS自定义排序

class Solution:
    def highestRankedKItems(self, grid: List[List[int]], pricing: List[int], start: List[int], k: int) -> List[
        List[int]]:
        queue = [start]
        res = []
        m, n = len(grid), len(grid[0])
        used = set()
        used.add((start[0], start[1]))

        while queue:
            next = []
            cur = []
            for node in queue:
                row = node[0]
                col = node[1]
                for xi, yi in ((1, 0), (0, 1), (-1, 0), (0, -1)):
                    if 0 <= row + xi < m and 0 <= col + yi < n:
                        if grid[row + xi][col + yi] != 0 and (row + xi, col + yi) not in used:
                            used.add((row + xi, col + yi))
                            next.append([row + xi, col + yi])
                if pricing[0] <= grid[row][col] <= pricing[1]:
                    cur.append([grid[row][col], row, col])
            cur.sort(key=lambda x: (x[0], x[1], x[2]))
            while cur and k:
                node = cur.pop(0)
                res.append([node[1], node[2]])
                k -= 1
            if k == 0:
                return res
            queue = next

        return res
  • 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
  • 29
  • 30
  • 31
  • 32

4 分隔长廊的方案数

模拟,只需考虑"S"的位置,并计算一组的后一个S位置到后一组的前一个S位置的距离,距离结果相乘。

class Solution:
    def numberOfWays(self, corridor: str) -> int:
        seat_index = []
        for index in range(len(corridor)):
            if corridor[index] == 'S':
                seat_index.append(index)

        if len(seat_index) % 2 != 0 or len(seat_index) == 0:
            return 0
        res, MOD = 1, 10 ** 9 + 7
        for i in range(2, len(seat_index), 2):
            res *= (seat_index[i] - seat_index[i - 1])
            res %= MOD

        return res
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/121034
推荐阅读
相关标签
  

闽ICP备14008679号