Posts

Showing posts from August, 2024

Modify Graph Edge Weights

Image
  You are given an   undirected weighted   connected   graph containing   n   nodes labeled from   0   to   n - 1 , and an integer array   edges   where   edges[i] = [a i , b i , w i ]   indicates that there is an edge between nodes   a i   and   b i   with weight   w i . Some edges have a weight of  -1  ( w i = -1 ), while others have a  positive  weight ( w i > 0 ). Your task is to modify  all edges  with a weight of  -1  by assigning them  positive integer values  in the range  [1, 2 * 10 9 ]  so that the  shortest distance  between the nodes  source  and  destination  becomes equal to an integer  target . If there are  multiple   modifications  that make the shortest distance between  source  and  destination  equal to  target , any of them will be consider...

Find edges in shortest path(leetcode hard)

Image
  You are given an undirected weighted graph of   n   nodes numbered from 0 to   n - 1 . The graph consists of   m   edges represented by a 2D array   edges , where   edges[i] = [a i , b i , w i ]   indicates that there is an edge between nodes   a i   and   b i   with weight   w i . Consider all the shortest paths from node 0 to node  n - 1  in the graph. You need to find a  boolean  array  answer  where  answer[i]  is  true  if the edge  edges[i]  is part of  at least  one shortest path. Otherwise,  answer[i]  is  false . Return the array  answer . Note  that the graph may not be connected.   Example 1: Input:   n = 6, edges = [[0,1,4],[0,2,1],[1,3,2],[1,4,3],[1,5,1],[2,3,1],[3,5,3],[4,5,2]] Output:   [true,true,true,false,true,true,true,false] My Solution: Store a map for every node containing all the path ...

Edit Distance (convert two one string to another in minimum opearation)

  Intuition : Here we have to find the minimum edit distance problem between two strings word1 and word2. The minimum edit distance is defined as the minimum number of operations required to transform one string into another. Approach : The approach here that I am using is dynamic programming. The idea is to build a 2D matrix dp where dp[i][j] represents the minimum number of operations required to transform the substring word1[0...i-1] into the substring word2[0...j-1]. How is Matrix built : The matrix is built iteratively using the following recurrence relation: If word1[i-1] == word2[j-1] , then dp[i][j] = dp[i-1][j-1] . That is, no operation is required because the characters at positions i-1 and j-1 are already the same. Otherwise, dp[i][j] is the minimum of the following three values: dp[i-1][j-1] + 1 : replace the character at position i-1 in word1 with the character at position j-1 in word2 . dp[i-1][j] + 1 : delete the character at position i-1 in word1. d...