Similar Problems

Similar Problems not available

Minimum Total Distance Traveled - Leetcode Solution

Companies:

LeetCode:  Minimum Total Distance Traveled Leetcode Solution

Difficulty: Hard

Topics: dynamic-programming sorting array  

Problem Statement:

Given an array of n integer coordinates where A[i] represents the coordinate of the ith point. You are allowed to move any number of times, but only in a straight line. What is the minimum total distance you have to travel to reach all points?

Example: Input: [[1,1],[3,4],[-1,0]] Output: 7

Explanation: One optimal solution: 1 -> 3 -> -1 -> 1: distance = 2 + 5 + 4 = 11

Solution:

The given problem can be solved using the greedy algorithm. We need to find the next point that is closest to the current point and go to that point. Repeat this process till we cover all points.

Let the current position be represented by the variable curr and let the total distance traveled so far be represented by the variable dist. We can keep track of the current position and the total distance using these variables.

For each point in the given array, we need to find the Euclidean distance between the current point and all other remaining points. We can use the Euclidean distance formula to calculate the distance between two points, which is sqrt(pow(x2-x1, 2) + pow(y2-y1, 2)).

We can store the distances in a priority queue in ascending order. We can then select the point with the smallest distance from the priority queue, travel to that point, and update the current position and total distance. We then repeat this process till all points are visited.

Algorithm:

  1. Initialize dist and curr variables as 0 and the first point in the array.
  2. Create an empty priority queue and insert all the points of the array, except the first one.
  3. While the priority queue is not empty, do the following: a. Remove the point with the smallest distance from the priority queue. b. Calculate the Euclidean distance between the current point curr and the removed point. c. Add the distance to dist. d. Update curr to the removed point. e. Remove the removed point from the priority queue.
  4. Return the total distance dist.

Time Complexity:

The time complexity of this algorithm is O(n^2 log n), where n is the number of points in the array. The reason is that, for each point, we are computing the Euclidean distance to all other remaining points, which is an O(n) operation. We are then adding and removing elements from the priority queue, which takes O(log n) time. Therefore, the overall time complexity is O(n^2 log n).

Code:

Here is the complete solution in Python:

import math import queue

def minTotalDistance(points: List[List[int]]) -> int: n = len(points) dist = 0 curr = points[0] pq = queue.PriorityQueue()

# insert all points except the first one
for i in range(1, n):
    pq.put((math.dist(curr, points[i]), points[i]))

# remove points from the priority queue and calculate the total distance
while not pq.empty():
    p = pq.get()[1]
    dist += math.dist(curr, p)
    curr = p

return int(dist)

Note: Since the result is a floating-point number, we convert it to an integer before returning. The math.dist function is used to calculate the Euclidean distance between two points.

Minimum Total Distance Traveled Solution Code

1