Posts

Showing posts with the label Dijkstra

Minimum Cost to Convert String I - Dijkstra's | Floyd-Warshall [JS]

Description   Solution 1: Dijkstra's Algorithm Since each  original[i]  and  changed[i]  will be a lowercase letter, there are only 26 characters. Find the minimum cost between each pair of characters ( 26 * 26 ). For each character  i , use Dijkstra's algorithm to find the minimum cost from  i  to each other character. Go through each  source[i]  and check if it's possible to turn  source[i]  into  target[i]  and use the precomputed minimum cost. n = length of source ,  m = length of original ,  k = number of characters Time Complexity:  O(m + n + k * (k + m log(m))) Space Complexity:  O(m + k^2) var minimumCost = function ( source, target, original, changed, cost ) { let graph = Array ( 26 ).fill( 0 ).map( () => []); for ( let i = 0 ; i < original.length; i++) { graph[original[i].charCodeAt() - 97 ].push([changed[i].charCodeAt() - 97 , cost[i]]); } let minCost = Array ( 26...