-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathplanting_trees.py
More file actions
62 lines (46 loc) · 1.78 KB
/
planting_trees.py
File metadata and controls
62 lines (46 loc) · 1.78 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Planting Trees
Given an even number of trees along one side of a road, calculate the
minimum total distance to move them into valid positions on both sides
at even intervals.
Reference: https://en.wikipedia.org/wiki/Dynamic_programming
Complexity:
Time: O(n^2)
Space: O(n^2)
"""
from __future__ import annotations
from math import sqrt
def planting_trees(trees: list[int], length: int, width: int) -> float:
"""Compute the minimum distance to rearrange trees to valid positions.
Args:
trees: Sorted list of current tree positions along the road.
length: Length of the road.
width: Width of the road.
Returns:
Minimum total distance the trees must be moved.
Examples:
>>> planting_trees([0, 1, 10, 10], 10, 1)
2.414213562373095
"""
trees = [0] + trees
n_pairs = int(len(trees) / 2)
space_between_pairs = length / (n_pairs - 1)
target_locations = [location * space_between_pairs for location in range(n_pairs)]
cmatrix = [[0 for _ in range(n_pairs + 1)] for _ in range(n_pairs + 1)]
for r_i in range(1, n_pairs + 1):
cmatrix[r_i][0] = cmatrix[r_i - 1][0] + sqrt(
width + abs(trees[r_i] - target_locations[r_i - 1]) ** 2
)
for l_i in range(1, n_pairs + 1):
cmatrix[0][l_i] = cmatrix[0][l_i - 1] + abs(
trees[l_i] - target_locations[l_i - 1]
)
for r_i in range(1, n_pairs + 1):
for l_i in range(1, n_pairs + 1):
cmatrix[r_i][l_i] = min(
cmatrix[r_i - 1][l_i]
+ sqrt(width + (trees[l_i + r_i] - target_locations[r_i - 1]) ** 2),
cmatrix[r_i][l_i - 1]
+ abs(trees[l_i + r_i] - target_locations[l_i - 1]),
)
return cmatrix[n_pairs][n_pairs]