{"id":8451,"date":"2021-05-29T15:33:31","date_gmt":"2021-05-29T22:33:31","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8451"},"modified":"2021-05-29T15:36:53","modified_gmt":"2021-05-29T22:36:53","slug":"leetcode-1857-largest-color-value-in-a-directed-graph","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1857-largest-color-value-in-a-directed-graph\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1857. Largest Color Value in a Directed Graph"},"content":{"rendered":"\n<p>There is a&nbsp;<strong>directed graph<\/strong>&nbsp;of&nbsp;<code>n<\/code>&nbsp;colored nodes and&nbsp;<code>m<\/code>&nbsp;edges. The nodes are numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>.<\/p>\n\n\n\n<p>You are given a string&nbsp;<code>colors<\/code>&nbsp;where&nbsp;<code>colors[i]<\/code>&nbsp;is a lowercase English letter representing the&nbsp;<strong>color<\/strong>&nbsp;of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;node in this graph (<strong>0-indexed<\/strong>). You are also given a 2D array&nbsp;<code>edges<\/code>&nbsp;where&nbsp;<code>edges[j] = [a<sub>j<\/sub>, b<sub>j<\/sub>]<\/code>&nbsp;indicates that there is a&nbsp;<strong>directed edge<\/strong>&nbsp;from node&nbsp;<code>a<sub>j<\/sub><\/code>&nbsp;to node&nbsp;<code>b<sub>j<\/sub><\/code>.<\/p>\n\n\n\n<p>A valid&nbsp;<strong>path<\/strong>&nbsp;in the graph is a sequence of nodes&nbsp;<code>x<sub>1<\/sub>&nbsp;-&gt; x<sub>2<\/sub>&nbsp;-&gt; x<sub>3<\/sub>&nbsp;-&gt; ... -&gt; x<sub>k<\/sub><\/code>&nbsp;such that there is a directed edge from&nbsp;<code>x<sub>i<\/sub><\/code>&nbsp;to&nbsp;<code>x<sub>i+1<\/sub><\/code>&nbsp;for every&nbsp;<code>1 &lt;= i &lt; k<\/code>. The&nbsp;<strong>color value<\/strong>&nbsp;of the path is the number of nodes that are colored the&nbsp;<strong>most frequently<\/strong>&nbsp;occurring color along that path.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>largest color value<\/strong>&nbsp;of any valid path in the given graph, or&nbsp;<\/em><code>-1<\/code><em>&nbsp;if the graph contains a cycle<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/04\/21\/leet1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> colors = \"abaca\", edges = [[0,1],[0,2],[2,3],[3,4]]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>\"a\" (red in the above image)<\/code>.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/04\/21\/leet2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> colors = \"a\", edges = [[0,0]]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> There is a cycle from 0 to 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == colors.length<\/code><\/li><li><code>m == edges.length<\/code><\/li><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= m &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>colors<\/code>&nbsp;consists of lowercase English letters.<\/li><li><code>0 &lt;= a<sub>j<\/sub>, b<sub>j<\/sub>&nbsp;&lt; n<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Topological Sorting<\/strong><\/h2>\n\n\n\n<p>freq[n][c] := max freq of color <code>c<\/code> after visiting node <code>n<\/code>.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n*26)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">python<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def largestPathValue(self, colors: str, edges: List[List[int]]) -> int:\n    INF = 1e9\n    n = len(colors)\n    g = [[] for _ in range(n)]\n    for u, v in edges:\n      g[u].append(v)    \n    visited = [0] * n\n    freq = [[0] * 26 for _ in range(n)]\n    def dfs(u: int) -> int:\n      idx = ord(colors[u]) - ord('a')\n      if not visited[u]:\n        visited[u] = 1 # visiting\n        for v in g[u]:\n          if (dfs(v) == INF):\n            return INF\n          for c in range(26):\n            freq[u][c] = max(freq[u][c], freq[v][c])\n        freq[u][idx] += 1\n        visited[u] = 2 # done\n      return freq[u][idx] if visited[u] == 2 else INF\n    ans = 0\n    for u in range(n):\n      ans = max(ans, dfs(u))\n      if ans == INF: break\n    return -1 if ans == INF else ans\n\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is a&nbsp;directed graph&nbsp;of&nbsp;n&nbsp;colored nodes and&nbsp;m&nbsp;edges. The nodes are numbered from&nbsp;0&nbsp;to&nbsp;n &#8211; 1. You are given a string&nbsp;colors&nbsp;where&nbsp;colors[i]&nbsp;is a lowercase English letter representing the&nbsp;color&nbsp;of the&nbsp;ith&nbsp;node&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[77,217,137],"class_list":["post-8451","post","type-post","status-publish","format-standard","hentry","category-graph","tag-graph","tag-hard","tag-topological-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8451"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8451\/revisions"}],"predecessor-version":[{"id":8455,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8451\/revisions\/8455"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}