Day70力扣打卡

2023-12-25 06:17:03

打卡记录

在这里插入图片描述


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

链接

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

文章来源:https://blog.csdn.net/qq947467490/article/details/135189177
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。