标签:规划 int use range min mic sel turn etc
方法:动态规划
class Solution: def minDistance(self, houses: List[int], k: int) -> int: houses.sort() n = len(houses) cost = [[0]*n for _ in range(n)] for i in range(n): for j in range(i+1, n): mid = houses[(i+j)//2] for l in range(i, j+1): cost[i][j] += abs(mid - houses[l]) dp = [[float(‘inf‘)]*k for _ in range(n)] for i in range(n): for l in range(k): if l == 0: dp[i][l] = cost[0][i] elif l >= i: dp[i][l] = 0 else: dp[i][l] = min(dp[j][l-1] + cost[j+1][i] for j in range(i)) return dp[-1][-1
标签:规划 int use range min mic sel turn etc
原文地址:https://www.cnblogs.com/oldby/p/13154281.html