Day70力扣打卡

打卡记录

Day70力扣打卡_第1张图片


收集足够苹果的最小花园周长(找规律 + 二分)

链接

class Solution:
    def minimumPerimeter(self, neededApples: int) -> int:
        l, r = 1, 10 ** 5
        while l < r:
            mid = (l + r) >> 1
            if 2 * (2 * (mid ** 3) + 3 * (mid ** 2) + mid) >= neededApples:
                r = mid
            else:
                l = mid + 1
        return 8 * l

转换字符串的最小成本 I(矩阵间的Floyd)

链接

class Solution:
    def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int:
        dis = [[inf] * 26 for _ in range(26)]
        for i in range(26):
            dis[i][i] = 0

        for x, y, c in zip(original, changed, cost):
            x = ord(x) - ord('a')
            y = ord(y) - ord('a')
            dis[x][y] = min(dis[x][y], c)

        for k in range(26):
            for i in range(26):
                for j in range(26):
                    dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j])

        ans = sum(dis[ord(x) - ord('a')][ord(y) - ord('a')] for x, y in zip(source, target))
        return ans if ans < inf else -1

你可能感兴趣的:(从零开始的算法打灰,leetcode,算法,职场和发展,python)