LeetCode(2023-09-21)


2260. 必须拿起的最小连续卡牌数

2260. 必须拿起的最小连续卡牌数

class Solution:
    def minimumCardPickup(self, cards: List[int]) -> int:
        res = {}
        mark = {cards[k]: False for k in range(len(cards))}
        for i in range(len(cards)):
            if cards[i] in res:
                if res[cards[i]][1] - res[cards[i]][0] > i - res[cards[i]][2] or res[cards[i]][1] == res[cards[i]][0]:
                    res[cards[i]] = [res[cards[i]][2], i, i]
                mark[cards[i]] = True
                res[cards[i]][2] = i
            else:
                res[cards[i]] = [i, i, i]
        ans = float('inf')
        for k, v in res.items():
            if v[0] == v[1]:
                continue
            if mark[k] and ans > v[1] - v[0] + 1:
                ans = v[1] - v[0] + 1
        return ans if ans != float('inf') else -1

1893. 检查是否区域内所有整数都被覆盖

1893. 检查是否区域内所有整数都被覆盖

class Solution:
    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:
        ranges.sort(key=lambda x: x[0])
        father = [ranges[i][0] for i in range(len(ranges))]
        for i in range(1, len(ranges)):
            if ranges[i][0] < ranges[i - 1][1] and ranges[i][1] < ranges[i - 1][1]:
                ranges[i][1] = ranges[i - 1][1]
        for i in range(1, len(ranges)):
            if ranges[i][0] - ranges[i - 1][1] <= 1:
                father[i] = father[i - 1]
        left_father = None
        right_father = None
        for i in range(len(ranges)):
            if ranges[i][0] <= left <= ranges[i][1] and not left_father:
                left_father = father[i]
            if ranges[i][0] <= right <= ranges[i][1] and not right_father:
                right_father = father[i]
            if left_father and right_father:
                break
        if left_father is None and right_father is None:
            return False
        else:
            return left_father == right_father

2269. 找到一个数字的 K 美丽值

2269. 找到一个数字的 K 美丽值

class Solution:
    def divisorSubstrings(self, num: int, k: int) -> int:
        num = str(num)
        ans = 0
        for i in range(len(num) - k + 1):
            sub_num = int(num[i:i+k])
            if sub_num == 0:
                continue
            if int(num) % sub_num == 0:
                ans += 1
        return ans

1779. 找到最近的有相同 X 或 Y 坐标的点

1779. 找到最近的有相同 X 或 Y 坐标的点

class Solution:
    def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
        ans = 0
        distance = float('inf')
        for i in range(len(points)):
            if points[i][0] == x or points[i][1] == y:
                dis = abs(points[i][0] - x) + abs(points[i][1] - y)
                if dis < distance:
                    distance = dis
                    ans = i

        return ans if distance != float('inf') else -1

2233. K 次增加后的最大乘积

2233. K 次增加后的最大乘积

贪心

import heapq

class Solution:
    def maximumProduct(self, nums: List[int], k: int) -> int:
        heapq.heapify(nums)
        for i in range(k):
            heapq.heapreplace(nums, nums[0] + 1)
        ans = 1
        for i in range(len(nums)):
            ans *= nums[i]
            ans %= (10 ** 9 + 7)
        return ans

声明:Hello World|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - LeetCode(2023-09-21)


我的朋友,理论是灰色的,而生活之树是常青的!