Thanksgiving Sale: Use Coupon Code THANKS25 to Get Extra 25% Off.
B where B[i] is the sum of the first i houses' locations.cal(i, j) that calculates the minimum distance for houses from index i to j with one mailbox placed at the median location.dp with high initial values, except dp[0] which should be 0.k from 1 to K.k, iterate over the end house index j from k-1 to n-1 (where n is the number of houses).j, iterate over the start house index i from k-2 to j-1.dp[j] with the minimum of its current value and dp[i] + cal(i+1, j).dp[n-1] will contain the minimum total distance for n houses with K mailboxes.