# Solution For Minimum Score Of A Path Between Two Cities

The Minimum Score of a Path Between Two Cities problem on LeetCode is a dynamic programming problem where we have to find the minimum score of a path between two cities in a graph. The cities are represented by the nodes of the graph and the cost of traveling between the cities is represented by the edges of the graph. The score of a path is the maximum cost of the edges on the path.

Algorithm:

1) Start by creating a 2D array D where D[i][j] represents the minimum score of the path between city i and city j. Initialize all elements with Infinity.

2) Fill in the diagonal elements of the array with 0 as the minimum score between a city and itself is 0.

3) For each edge in the graph, we update the minimum score of the path between the two cities connected by the edge. If the new score is less than the current score, we update the D array with the new score.

4) For each city i, we iterate through all the other cities j and k. We update the D[i][j] with the minimum of D[i][j] and max(D[i][k], D[k][j]) where k represents an intermediary city on the path between i and j. This step is known as Floyd-Warshall’s Algorithm.

5) We return the value of D[start][end] where start and end are the source and destination cities respectively.

Code:

Here is the Python code for the above algorithm:

“`

def minimumScore(numCities, cities, start, end):

D = [[float(‘inf’)] * numCities for _ in range(numCities)]

```
for i in range(numCities):
D[i][i] = 0
for u, v, w in cities:
D[u][v] = min(D[u][v], w)
D[v][u] = min(D[v][u], w)
for k in range(numCities):
for i in range(numCities):
for j in range(numCities):
D[i][j] = min(D[i][j], max(D[i][k], D[k][j]))
return D[start][end] if D[start][end] != float('inf') else -1
```

“`

The function takes four arguments – the number of cities, the list of cities with their connecting edges and costs, the source city and the destination city. It returns the minimum score of the path between the source and destination cities.

Time Complexity:

The time complexity of the above algorithm is O(n^3) where n is the number of cities. This is because we have three nested loops of size n in the Floyd-Warshall’s algorithm. Therefore, the algorithm may not be suitable for large input sizes.

## Step by Step Implementation For Minimum Score Of A Path Between Two Cities

class Solution { public int minScore(int[][] grid) { int rows = grid.length; int cols = grid[0].length; int[][] dp = new int[rows][cols]; // initialize the first row and column of the dp array dp[0][0] = grid[0][0]; for (int i = 1; i < rows; i++) { dp[i][0] = dp[i-1][0] + grid[i][0]; } for (int j = 1; j < cols; j++) { dp[0][j] = dp[0][j-1] + grid[0][j]; } // fill in the rest of the dp array for (int i = 1; i < rows; i++) { for (int j = 1; j < cols; j++) { dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + grid[i][j]; } } return dp[rows-1][cols-1]; } }

We can use a graph to model the problem, with each node representing a city and each edge representing the distance between two cities. Then, we can use a breadth-first search algorithm to find the shortest path between the two cities. The Python code for this solution is as follows: def minScore(graph, src, dst): # create a queue to store the nodes to visit queue = [] # create a set to store the visited nodes visited = set() # add the source node to the queue queue.append(src) # mark the source node as visited visited.add(src) # keep track of the minimum score min_score = float('inf') # while there are nodes in the queue while queue: # get the next node from the queue node = queue.pop(0) # if this node is the destination node if node == dst: # return the minimum score return min_score # for each neighbor of the node for neighbor in graph[node]: # if the neighbor has not been visited if neighbor not in visited: # add the neighbor to the queue queue.append(neighbor) # mark the neighbor as visited visited.add(neighbor) # update the minimum score min_score = min(min_score, graph[node][neighbor]) # if we reach here, then there is no path between the two cities return -1

There are two cities, A and B. You are given a 2D array with the distances between each city. Find the minimum score of a path between A and B, where the score of a path is the sum of the distances of each step. var minimumScoreOfAPathBetweenTwoCities = function(distances) { // your code here };

int minScore(int n, int[][] edges, int source, int target) { // Create a map of all the nodes and their corresponding edges Map> map = new HashMap<>(); for (int[] edge : edges) { if (!map.containsKey(edge[0])) { map.put(edge[0], new ArrayList<>()); } if (!map.containsKey(edge[1])) { map.put(edge[1], new ArrayList<>()); } map.get(edge[0]).add(new int[] {edge[1], edge[2]}); map.get(edge[1]).add(new int[] {edge[0], edge[2]}); } // Perform Dijkstra's algorithm to find the shortest path from source to target PriorityQueue pq = new PriorityQueue<>((a, b) -> (a[1] - b[1])); pq.add(new int[] {source, 0}); int res = 0; while (!pq.isEmpty()) { int[] curr = pq.poll(); int node = curr[0]; int cost = curr[1]; // If we have reached the target, return the cost if (node == target) { return cost; } // Otherwise, continue searching if (map.containsKey(node)) { for (int[] next : map.get(node)) { int nextNode = next[0]; int nextCost = next[1]; pq.add(new int[] {nextNode, cost + nextCost}); } } } return res; }

using System; public class Solution { //This is a C# solution for the leetcode problem minimum-score-of-a-path-between-two-cities //Output should only consist of exact code with comments and nothing else public int MinScore(int[][] grid) { //We first check if the grid is valid if (grid == null || grid.Length == 0) { return -1; } //We then get the number of rows and columns in the grid int rows = grid.Length; int cols = grid[0].Length; //We create a 2D array to store the minimum score of each cell int[][] dp = new int[rows][]; for (int i = 0; i < rows; i++) { dp[i] = new int[cols]; } //We fill in the first row and column of the dp array dp[0][0] = grid[0][0]; for (int i = 1; i < rows; i++) { dp[i][0] = dp[i - 1][0] + grid[i][0]; } for (int j = 1; j < cols; j++) { dp[0][j] = dp[0][j - 1] + grid[0][j]; } //We then fill in the rest of the dp array for (int i = 1; i < rows; i++) { for (int j = 1; j < cols; j++) { dp[i][j] = Math.Min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; } } //We return the bottom right element of the dp array as this represents the minimum score required to reach the end return dp[rows - 1][cols - 1]; } }