-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathallocate-mailboxes.py
More file actions
26 lines (26 loc) · 804 Bytes
/
allocate-mailboxes.py
File metadata and controls
26 lines (26 loc) · 804 Bytes
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
class Solution:
def minDistance(self, houses: List[int], k: int) -> int:
n = len(houses)
houses.sort()
@lru_cache(maxsize = None)
def dp(i, rem):
if rem < 0:
return float('inf')
if i >= n:
return 0
res = float('inf')
median = i
sv = 2 * houses[i]
lv = 2
s = 0
for j in range(i, n):
lv -= 1
sv -= houses[j]
while median < (i + j) // 2:
median += 1
sv += 2 * houses[median]
lv += 2
cost = houses[median] * lv - sv
res = min(res, cost + dp(j + 1, rem - 1))
return res
return dp(0, k)