id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
1
{ "code": "class Solution {\r\npublic:\r\n int topoSort(vector<vector<int>>& graph, string colors){\r\n int count = 0;\r\n queue<int> data;\r\n \r\n int n = colors.size();\r\n vector<vector<int>> dp(n, vector<int>(26,0));\r\n \r\n vector<int> indegree(n,0);\r\n for(int i=0;i<n;i++){\r\n for(int j=0;j<graph[i].size();j++){\r\n indegree[graph[i][j]]++;\r\n }\r\n }\r\n\r\n for(int i=0;i<n;i++){\r\n if(indegree[i] == 0){\r\n data.push(i);\r\n dp[i][colors[i]-'a'] = 1;\r\n }\r\n }\r\n int ans = 0;\r\n\r\n while(!data.empty()){\r\n int node = data.front();\r\n data.pop();\r\n count++;\r\n ans = max(ans, dp[node][colors[node]-'a']);\r\n\r\n for(auto nei: graph[node]){\r\n indegree[nei]--;\r\n \r\n for(char ch='a';ch<='z';ch++){\r\n dp[nei][ch-'a'] = max(dp[nei][ch-'a'], dp[node][ch-'a'] + \r\n (colors[nei] == ch));\r\n }\r\n\r\n if(indegree[nei] == 0){\r\n data.push(nei);\r\n }\r\n }\r\n }\r\n \r\n if(count < n) return -1;\r\n\r\n return ans;\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n int e = edges.size();\r\n\r\n vector<vector<int>> graph(n);\r\n for(int i=0;i<e;i++){\r\n int n1 = edges[i][0];\r\n int n2 = edges[i][1];\r\n graph[n1].push_back(n2);\r\n }\r\n\r\n return topoSort(graph,colors);\r\n }\r\n};", "memory": "174908" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
1
{ "code": "typedef vector<vector<int>> Graph;\r\n\r\nclass Solution {\r\npublic:\r\n void addDirectedEdge(Graph& g, int u, int v){\r\n g[u].push_back(v);\r\n }\r\n int largestPathValue(string colors,vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n Graph g(n);\r\n vector<int> inDegree(n, 0);\r\n for(int i = 0; i < edges.size(); i++){\r\n if(edges[i][0] == edges[i][1]) return -1;\r\n addDirectedEdge(g, edges[i][0] , edges[i][1]);\r\n inDegree[edges[i][1]]++;\r\n }\r\n queue<int> q;\r\n int theColor = 0;\r\n vector<vector<int>> dp(n,vector<int>(26, 0));\r\n vector<pair<int,int>> pathNum (n,{-1,-1});\r\n for(int i = 0 ; i < n ; i++){\r\n if(inDegree[i] == 0) q.push(i);\r\n }\r\n int maxPath = -1;\r\n int numV = n;\r\n while(!q.empty()){\r\n int cur = q.front();\r\n q.pop();\r\n dp[cur][colors[cur] - 'a'] ++;\r\n numV--;\r\n for(int v : g[cur]){\r\n inDegree[v]--;\r\n if(inDegree[v] == 0)\r\n q.push(v);\r\n for(int i = 0 ; i < 26 ; i++)\r\n dp[v][i] = max(dp[v][i], dp[cur][i]);\r\n }\r\n }\r\n if(numV != 0) return -1;\r\n for(int i = 0 ; i < n ; i++){\r\n for(int j = 0 ; j < 26 ; j++){\r\n maxPath = max(maxPath, dp[i][j]);\r\n }\r\n }\r\n return maxPath;\r\n }\r\n};", "memory": "176786" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
1
{ "code": "class Solution {\r\npublic:\r\n vector<vector<int>>dp;\r\n int f(int node,int col,vector<int>adj[],string &colors)\r\n {\r\n if(dp[node][col]!=-1)\r\n return dp[node][col];\r\n int ans=0;\r\n for(auto it:adj[node])\r\n {\r\n ans=max(ans,f(it,col,adj,colors));\r\n }\r\n if((colors[node]-'a')==col)\r\n ans+=1;\r\n return dp[node][col]=ans;\r\n\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.size();\r\n vector<int>adj[n];\r\n vector<int>ind(n,0);\r\n for(auto it:edges)\r\n {\r\n adj[it[0]].push_back(it[1]);\r\n ind[it[1]]++;\r\n }\r\n queue<int>q;\r\n vector<int>ans;\r\n vector<int>starts;\r\n for(int i=0;i<n;i++)\r\n {\r\n if(ind[i]==0)\r\n {\r\n q.push(i);\r\n starts.push_back(i);\r\n }\r\n }\r\n while(!q.empty())\r\n {\r\n int node=q.front();\r\n q.pop();\r\n ans.push_back(node);\r\n for(auto it:adj[node])\r\n {\r\n ind[it]--;\r\n if(ind[it]==0)\r\n {\r\n q.push(it);\r\n }\r\n }\r\n }\r\n if(ans.size()!=n)\r\n return -1;\r\n dp.resize(n,vector<int>(26,-1));\r\n int res=INT_MIN;\r\n for(auto start:starts)\r\n {\r\n for(int i=0;i<26;i++)\r\n {\r\n res=max(res,f(start,i,adj,colors));\r\n }\r\n }\r\n return res;\r\n }\r\n};", "memory": "176786" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
1
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string cs, vector<vector<int>>& edges) {\r\n vector<vector<int>> al(cs.size()), cnt(cs.size(), vector<int>(26));\r\n vector<int> indegrees(cs.size());\r\n for (auto &e: edges) {\r\n al[e[0]].push_back(e[1]);\r\n ++indegrees[e[1]];\r\n }\r\n vector<int> q;\r\n for (int i = 0; i < cs.size(); ++i)\r\n if (indegrees[i] == 0)\r\n q.push_back(i);\r\n int res = 0, processed = 0;\r\n while (!q.empty()) {\r\n vector<int> q1;\r\n for (auto i : q) {\r\n ++processed;\r\n res = max(res, ++cnt[i][cs[i] - 'a']);\r\n for (auto j : al[i]) {\r\n for (auto k = 0; k < 26; ++k)\r\n cnt[j][k] = max(cnt[j][k], cnt[i][k]);\r\n if (--indegrees[j] == 0)\r\n q1.push_back(j);\r\n }\r\n }\r\n swap(q, q1);\r\n }\r\n return processed != cs.size() ? -1 : res;\r\n}\r\n};", "memory": "178665" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int largestPathValue(string cs, vector<vector<int>>& edges) {\n vector<vector<int>> al(cs.size()), cnt(cs.size(), vector<int>(26));\n vector<int> indegrees(cs.size());\n for (auto &e: edges) {\n al[e[0]].push_back(e[1]);\n ++indegrees[e[1]];\n }\n vector<int> q;\n for (int i = 0; i < cs.size(); ++i)\n if (indegrees[i] == 0)\n q.push_back(i);\n int res = 0, processed = 0;\n while (!q.empty()) {\n vector<int> q1;\n for (auto i : q) {\n ++processed;\n res = max(res, ++cnt[i][cs[i] - 'a']);\n for (auto j : al[i]) {\n for (auto k = 0; k < 26; ++k)\n cnt[j][k] = max(cnt[j][k], cnt[i][k]);\n if (--indegrees[j] == 0)\n q1.push_back(j);\n }\n }\n swap(q, q1);\n }\n return processed != cs.size() ? -1 : res;\n}\n};", "memory": "178665" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
1
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n // build graph do topological sort\r\n int N = colors.size();\r\n vector<list<int>> graph(N);\r\n vector<int> indegree(N, 0);\r\n\r\n for (const auto& e : edges) {\r\n graph[e[0]].push_back(e[1]);\r\n ++indegree[e[1]];\r\n }\r\n\r\n queue<int> q;\r\n for (int i = 0; i < N; ++i)\r\n if (indegree[i] == 0) q.push(i);\r\n\r\n // vector<int> topo;\r\n // while(!q.empty()) {\r\n // int cur = q.front(); q.pop();\r\n // topo.push_back(cur);\r\n // for (int& nxt : graph[cur]) {\r\n // --indegree[nxt];\r\n // if (indegree[nxt] == 0) q.push(nxt);\r\n // }\r\n // }\r\n \r\n int ct = 0;\r\n int res = 0;\r\n vector<vector<int>> dp(N, vector<int>(26, 0));\r\n for (int i = 0; i < N; ++i) {\r\n ++dp[i][colors[i] - 'a'];\r\n }\r\n\r\n while(!q.empty()) {\r\n int cur = q.front(); q.pop();\r\n ++ct;\r\n for (int& nxt : graph[cur]) {\r\n for (int i = 0; i<26; ++i) {\r\n dp[nxt][i] = max(dp[nxt][i], dp[cur][i] + ((colors[nxt] - 'a')==i));\r\n }\r\n --indegree[nxt];\r\n if (indegree[nxt] == 0) q.push(nxt);\r\n }\r\n res = max(res, *max_element(dp[cur].begin(), dp[cur].end()));\r\n }\r\n return ct == N ? res : -1;\r\n }\r\n};", "memory": "180544" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
1
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string color, vector<vector<int>>& edges) {\r\n int n = color.size();\r\n int cyc[n];\r\n int visited[n];\r\n vector<int>adj[n];\r\n int dp[n][26];\r\n memset(dp,0,sizeof(dp));\r\n for(auto e: edges) adj[e[0]].push_back(e[1]);\r\n memset(cyc, 0, sizeof(cyc));\r\n memset(visited, 0, sizeof(visited));\r\n bool cycle = false;\r\n function<void(int)> dfs = [&](int i) -> void {\r\n visited[i] = true;\r\n cyc[i]=true;\r\n for(auto x: adj[i]) {\r\n if(cyc[x]) {\r\n cycle = true;\r\n return;\r\n }\r\n if(!visited[x]) {\r\n dfs(x);\r\n }\r\n for(int k = 0; k < 26; k++) dp[i][k] = max(dp[i][k], ((color[i]-'a') == k) + dp[x][k]);\r\n }\r\n for(int k = 0; k < 26; k++) dp[i][k] =max((int)(color[i]-'a'==k),dp[i][k]);\r\n cyc[i]=false;\r\n };\r\n\r\n for(int i = 0; i < n; i++){\r\n if(!visited[i]) dfs(i);\r\n }\r\n if(cycle)return -1;\r\n int sol = 0;\r\n for(int i = 0; i < n; i++){\r\n for(int j =0; j < 26; j++) sol = max(sol,dp[i][j]);\r\n }\r\n return sol;\r\n }\r\n};", "memory": "180544" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
1
{ "code": "class Solution {\r\nprivate:\r\n int clr;\r\n string color;\r\n \r\n unordered_map<char,int> cl;\r\n vector<bool> vis;\r\n vector<int> pathvis;\r\n // vector<vector<int>> graph;\r\n // vector<vector<int>> dp(n,vector<int>(clr,0));\r\n\r\n bool dfs(int parent,vector<vector<int>>& graph,vector<vector<int>>& dp){\r\n vis[parent]=1;\r\n pathvis[parent]=1;\r\n for(auto it: graph[parent]){\r\n if(!vis[it]){\r\n if(dfs(it,graph,dp)==false) return false;\r\n }\r\n else if(pathvis[it]) return false;\r\n for(int i=0;i<clr;i++){\r\n dp[parent][i]=max(dp[parent][i],dp[it][i]);\r\n }\r\n }\r\n dp[parent][cl[color[parent]]]+=1;\r\n pathvis[parent]=0;\r\n return true;\r\n }\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.size();\r\n vector<vector<int>> graph(n);\r\n // graph(n);\r\n for(int i=0;i<edges.size();i++){\r\n graph[edges[i][0]].push_back(edges[i][1]);\r\n }\r\n for(int i=0;i<n;i++){\r\n cl[colors[i]]=1;\r\n }\r\n for(int i=0;i<n;i++){\r\n vis.push_back(0);\r\n pathvis.push_back(0);\r\n }\r\n clr=cl.size();\r\n int k=0;\r\n for(auto& it:cl){\r\n it.second=k;\r\n k++;\r\n }\r\n // vector<bool> vis(n,0);\r\n // vector<int> pathvis(n,0);\r\n // vis(n,0);\r\n color=colors;\r\n // pathvis(n,0);\r\n vector<vector<int>> col(n,vector<int>(clr,0));\r\n // col(n,vector<int>(clr,0));\r\n for(int i=0;i<n;i++){\r\n if(vis[i]==0){\r\n if(dfs(i,graph,col)==false) return -1;\r\n }\r\n }\r\n int maxi=0;\r\n for(int i=0;i<n;i++){\r\n for(int j=0;j<clr;j++)\r\n maxi=max(maxi,col[i][j]);\r\n }\r\n return maxi; \r\n }\r\n};", "memory": "182423" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
1
{ "code": "class Solution {\r\nprivate:\r\n int clr;\r\n string color;\r\n \r\n unordered_map<char,int> cl;\r\n vector<bool> vis;\r\n vector<int> pathvis;\r\n // vector<vector<int>> graph;\r\n // vector<vector<int>> dp(n,vector<int>(clr,0));\r\n\r\n bool dfs(int parent,vector<vector<int>>& graph,vector<vector<int>>& dp){\r\n vis[parent]=1;\r\n pathvis[parent]=1;\r\n for(auto it: graph[parent]){\r\n if(!vis[it]){\r\n if(dfs(it,graph,dp)==false) return false;\r\n }\r\n else if(pathvis[it]) return false;\r\n for(int i=0;i<clr;i++){\r\n dp[parent][i]=max(dp[parent][i],dp[it][i]);\r\n }\r\n }\r\n dp[parent][cl[color[parent]]]+=1;\r\n pathvis[parent]=0;\r\n return true;\r\n }\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.size();\r\n vector<vector<int>> graph(n);\r\n // graph(n);\r\n for(int i=0;i<edges.size();i++){\r\n graph[edges[i][0]].push_back(edges[i][1]);\r\n }\r\n for(int i=0;i<n;i++){\r\n cl[colors[i]]=1;\r\n }\r\n for(int i=0;i<n;i++){\r\n vis.push_back(0);\r\n pathvis.push_back(0);\r\n }\r\n clr=cl.size();\r\n int k=0;\r\n for(auto& it:cl){\r\n it.second=k;\r\n k++;\r\n }\r\n // vector<bool> vis(n,0);\r\n // vector<int> pathvis(n,0);\r\n // vis(n,0);\r\n color=colors;\r\n // pathvis(n,0);\r\n vector<vector<int>> col(n,vector<int>(clr,0));\r\n // col(n,vector<int>(clr,0));\r\n for(int i=0;i<n;i++){\r\n if(vis[i]==0){\r\n if(dfs(i,graph,col)==false) return -1;\r\n }\r\n }\r\n int maxi=0;\r\n for(int i=0;i<n;i++){\r\n for(int j=0;j<clr;j++)\r\n maxi=max(maxi,col[i][j]);\r\n }\r\n return maxi; \r\n }\r\n};", "memory": "182423" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
1
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.size();\r\n vector<int>adj[n];\r\n int ans=0;\r\n vector<int>indegree(n,0);\r\n for(auto it:edges){\r\n adj[it[0]].push_back(it[1]);\r\n indegree[it[1]]++;\r\n }\r\n \r\n queue<int>q;\r\n for(int i=0;i<n;i++){\r\n if(indegree[i]==0){\r\n q.push(i);\r\n }\r\n }\r\n vector<vector<int>>maxChar(n,vector<int>(26,0));\r\n int cnt=0;\r\n while(!q.empty()){\r\n int node=q.front();\r\n q.pop();\r\n cnt++;\r\n maxChar[node][colors[node]-'a']++;\r\n ans=max(ans,maxChar[node][colors[node]-'a']);\r\n for(auto it:adj[node]){\r\n for(int i=0;i<26;i++){\r\n maxChar[it][i]=max(maxChar[it][i],maxChar[node][i]);\r\n }\r\n if(--indegree[it]==0){\r\n q.push(it);\r\n }\r\n }\r\n }\r\n\r\n if(cnt<n)return -1;\r\n\r\n \r\n return ans;\r\n }\r\n};", "memory": "184301" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
1
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n vector<int> adj[n];\r\n vector<int> indegree(n,0);\r\n for(auto it: edges){\r\n int u = it[0];\r\n int v = it[1];\r\n adj[u].push_back(v);\r\n indegree[v]++; \r\n }\r\n\r\n queue<int> q;\r\n vector<vector<int>> t(n,vector<int>(26,0));\r\n for(int i = 0; i<n; i++){\r\n if(indegree[i] == 0){\r\n q.push(i);\r\n t[i][colors[i] - 'a'] = 1;\r\n }\r\n }\r\n\r\n int cnt = 0;\r\n int answer = 0;\r\n\r\n while(!q.empty()){\r\n int node = q.front();\r\n q.pop();\r\n\r\n cnt++;\r\n answer = max(answer,t[node][colors[node] - 'a']);\r\n\r\n for(auto adjnode: adj[node]){\r\n for(int i = 0; i<26; i++){\r\n t[adjnode][i] = max(t[adjnode][i] , t[node][i] + (colors[adjnode] - 'a' == i));\r\n }\r\n indegree[adjnode]--;\r\n if(indegree[adjnode] == 0){\r\n q.push(adjnode);\r\n }\r\n }\r\n\r\n }\r\n\r\n if(cnt < n){\r\n return -1;\r\n }\r\n return answer;\r\n }\r\n};", "memory": "184301" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
1
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string s, vector<vector<int>>& e) {\r\n int n=s.size();\r\n vector<int>ad[n];\r\n vector<int>ind(n,0);\r\n for(auto i:e){\r\n ad[i[0]].push_back(i[1]);\r\n ind[i[1]]++;\r\n }\r\n vector<int>col(n);\r\n for(int i=0;i<s.size();i++)col[i]=s[i]-'a';\r\n queue<int>q;\r\n int ans=0;\r\n int mx=1;\r\n vector<vector<int>>dp(n,vector<int>(26,0));\r\n vector<int>vis(n,0);\r\n for(int i=0;i<n;i++){\r\n if(ind[i]==0){\r\n vis[i]=1;\r\n q.push(i);\r\n dp[i][col[i]]++;\r\n }\r\n \r\n }\r\n while(q.size()){\r\n int x=q.front();\r\n q.pop();\r\n ans++;\r\n for(auto i:ad[x]){\r\n if(!vis[i]){\r\n for(int j=0;j<26;j++){\r\n dp[i][j]=max(dp[i][j],dp[x][j] + (j==col[i]));\r\n mx=max(mx,dp[i][j]);\r\n }\r\n }\r\n ind[i]--;\r\n if(ind[i]==0)q.push(i);\r\n }\r\n }\r\n if(ans!=n)return -1;\r\n else return mx;\r\n }\r\n};", "memory": "186180" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
1
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string s, vector<vector<int>>& e) {\r\n int n=s.size();\r\n vector<int>ad[n];\r\n vector<int>ind(n,0);\r\n for(auto i:e){\r\n ad[i[0]].push_back(i[1]);\r\n ind[i[1]]++;\r\n }\r\n vector<int>col(n);\r\n for(int i=0;i<s.size();i++)col[i]=s[i]-'a';\r\n queue<int>q;\r\n int ans=0;\r\n int mx=1;\r\n vector<vector<int>>dp(n,vector<int>(26,0));\r\n vector<int>vis(n,0);\r\n for(int i=0;i<n;i++){\r\n if(ind[i]==0){\r\n vis[i]=1;\r\n q.push(i);\r\n dp[i][col[i]]++;\r\n }\r\n \r\n }\r\n while(q.size()){\r\n int x=q.front();\r\n q.pop();\r\n ans++;\r\n for(auto i:ad[x]){\r\n if(!vis[i]){\r\n for(int j=0;j<26;j++){\r\n dp[i][j]=max(dp[i][j],dp[x][j] + (j==col[i]));\r\n mx=max(mx,dp[i][j]);\r\n }\r\n }\r\n ind[i]--;\r\n if(ind[i]==0)q.push(i);\r\n }\r\n }\r\n if(ans!=n)return -1;\r\n else return mx;\r\n }\r\n};", "memory": "186180" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n vector<vector<int>> adj(n);\r\n vector<int> in(n, 0);\r\n for (auto e : edges) {\r\n adj[e[0]].push_back(e[1]);\r\n in[e[1]]++;\r\n }\r\n\r\n queue<int> q;\r\n vector<vector<int>> count(n, vector<int>(26, 0));\r\n for (int i = 0; i < n; i++) {\r\n if (!in[i]) {\r\n q.push(i);\r\n count[i][colors[i] - 'a']++;\r\n }\r\n }\r\n int m = 0, cnt = 0;\r\n while (!q.empty()) {\r\n int node = q.front();\r\n q.pop();\r\n cnt++;\r\n m = max(m, *max_element(count[node].begin(), count[node].end()));\r\n for (auto i : adj[node]) {\r\n for (int j = 0; j < 26; j++) {\r\n count[i][j] = max(count[i][j],\r\n count[node][j] + (j == colors[i] - 'a'));\r\n }\r\n if (!--in[i])\r\n q.push(i);\r\n }\r\n }\r\n\r\n if (cnt != n)\r\n return -1;\r\n\r\n return m;\r\n }\r\n};", "memory": "188059" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n string colors;\r\n vector<vector<int>> adjList;\r\n vector<vector<int>> dp;\r\n vector<int> indegree;\r\n\r\n bool dfs(int node, vector<int>& vis, vector<int>& pathvis) {\r\n vis[node] = 1;\r\n pathvis[node] = 1;\r\n for ( auto ele: adjList[node] ) {\r\n if ( !vis[ele] ) {\r\n if ( dfs(ele, vis, pathvis) ) return true;\r\n } else if ( pathvis[ele] ) return true;\r\n }\r\n pathvis[node] = 0;\r\n return false;\r\n }\r\n\r\n int largestPathValue(string _colors, vector<vector<int>>& edges) {\r\n colors = _colors;\r\n int n = colors.size();\r\n adjList.resize(n);\r\n indegree.assign(n, 0);\r\n dp.assign(n, vector<int>(26, 0));\r\n vector<int> vis(n, 0);\r\n vector<int> pathvis(n, 0);\r\n \r\n for (const auto& edge : edges) {\r\n adjList[edge[0]].push_back(edge[1]);\r\n indegree[edge[1]]++;\r\n }\r\n for ( auto i = 0; i < n; i++ ) {\r\n if ( !vis[i] ) {\r\n if ( dfs(i, vis, pathvis)) return -1;\r\n }\r\n }\r\n\r\n queue<int> q;\r\n for (int i = 0; i < n; ++i) {\r\n if (indegree[i] == 0) q.push(i);\r\n }\r\n\r\n int result = -1;\r\n\r\n while (!q.empty()) {\r\n int node = q.front();\r\n q.pop();\r\n\r\n int colorIdx = colors[node] - 'a';\r\n dp[node][colorIdx]++;\r\n\r\n result = max(result, dp[node][colorIdx]);\r\n\r\n for (int neighbor : adjList[node]) {\r\n for (int c = 0; c < 26; ++c) {\r\n dp[neighbor][c] = max(dp[neighbor][c], dp[node][c]);\r\n }\r\n \r\n if (--indegree[neighbor] == 0) {\r\n q.push(neighbor);\r\n }\r\n }\r\n }\r\n\r\n return result;\r\n }\r\n};\r\n", "memory": "188059" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) { \r\n int n = colors.size();\r\n\r\n unordered_map<int, list<int>> adj;\r\n vector<int> indegree(n, 0);\r\n vector<int> col(26, 0);\r\n \r\n for(auto edge : edges) {\r\n int u = edge[0];\r\n int v = edge[1];\r\n adj[u].push_back(v);\r\n indegree[v]++;\r\n }\r\n\r\n queue<int> q;\r\n for(int i = 0; i < n; ++i) {\r\n if(indegree[i] == 0) q.push(i);\r\n }\r\n\r\n if(q.empty()) return -1;\r\n\r\n int cnt = 0, ans = INT_MIN;\r\n\r\n vector<vector<int>> dp(n, vector<int> (26, 0));\r\n for(int i = 0; i < n; ++i) {\r\n dp[i][colors[i] - 'a']++;\r\n }\r\n\r\n while(!q.empty()) {\r\n int node = q.front(); q.pop();\r\n cnt++;\r\n for(auto& nbr : adj[node]) {\r\n for(int i = 0; i < 26; ++i) {\r\n dp[nbr][i] = max(dp[nbr][i], dp[node][i] + ((colors[nbr] - 'a') == i));\r\n }\r\n indegree[nbr]--;\r\n if(indegree[nbr] == 0) q.push(nbr);\r\n }\r\n\r\n ans = max(ans, *max_element(dp[node].begin(), dp[node].end()));\r\n }\r\n\r\n if(cnt < n) return -1;\r\n\r\n return ans;\r\n }\r\n};", "memory": "189938" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.length();\r\n vector<int> indegrees(n, 0);\r\n vector<int> visited(n, 0);\r\n vector<vector<int>> graph(n);\r\n vector<vector<int>> colorDp(n, vector<int>(26, 0));\r\n\r\n for (auto &edge: edges) {\r\n indegrees[edge[1]]++;\r\n graph[edge[0]].push_back(edge[1]);\r\n }\r\n\r\n int ans = 0;\r\n for (int i = 0; i < n; i++) {\r\n if (indegrees[i] == 0 && !dfs(colors, graph, i, visited, colorDp, ans)) {\r\n return -1;\r\n }\r\n }\r\n\r\n for (int i = 0; i < n; i++) {\r\n if (visited[i] != 2) {\r\n return -1;\r\n }\r\n }\r\n\r\n return ans;\r\n }\r\n\r\n bool dfs(string &colors, vector<vector<int>> &graph, int begin, vector<int> &visited, vector<vector<int>> &colorDp, int &ans) {\r\n visited[begin] = 1;\r\n\r\n for (int neighbor: graph[begin]) {\r\n if (visited[neighbor] == 1) {\r\n return false;\r\n }\r\n\r\n if (visited[neighbor] != 2) {\r\n dfs(colors, graph, neighbor, visited, colorDp, ans);\r\n }\r\n\r\n for (int i = 0; i < 26; i++) {\r\n colorDp[begin][i] = max(colorDp[begin][i], colorDp[neighbor][i]);\r\n }\r\n }\r\n\r\n colorDp[begin][colors[begin] - 'a']++;\r\n ans = max(ans, colorDp[begin][colors[begin] - 'a']);\r\n\r\n visited[begin] = 2;\r\n return true;\r\n }\r\n};", "memory": "189938" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n int dfs(int i,vector<vector<int>>&graph,vector<int>&vis,vector<vector<int>>&cf,string &colors){\r\n if(vis[i]==1){\r\n return true;\r\n }\r\n if(vis[i]==2){\r\n return false;\r\n }\r\n vis[i]=1;\r\n bool ans=false;\r\n for(int a:graph[i]){\r\n ans=ans||dfs(a,graph,vis,cf,colors);\r\n // cout << ans;\r\n for(int j=0;j<26;j++){\r\n cf[i][j]=max(cf[i][j],cf[a][j]);\r\n }\r\n // cout<<endl;\r\n }\r\n cf[i][colors[i]-'a']++;\r\n vis[i]=2;\r\n return ans;\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n vector<int>ie(colors.length(),0);\r\n vector<vector<int>>graph(colors.length(),vector<int>());\r\n for(int i=0;i<edges.size();i++){\r\n ie[edges[i][1]]++;\r\n graph[edges[i][0]].push_back(edges[i][1]);\r\n }\r\n int cnt=0;\r\n vector<int>vis(colors.size(),0);\r\n vector<vector<int>>cf(colors.size(),vector<int>(26,0));\r\n int mf=0;\r\n for(int i=0;i<colors.size();i++){\r\n //if(ie[i]==0){\r\n bool a= dfs(i,graph,vis,cf,colors);\r\n // cout<<a<<\"*\";\r\n if(a){\r\n return -1;\r\n }\r\n for(int j=0;j<26;j++)\r\n mf=max(mf,cf[i][j]);\r\n // }\r\n }\r\n return mf;\r\n }\r\n};", "memory": "191816" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n map<int,int>in;\r\n queue<int>q;\r\n int largestPathValue(string colors, vector<vector<int>> &edg) {\r\n vector<vector<int>> adj((int)colors.size()+1);\r\n vector<vector<int>>mat((int)colors.size()+1,vector<int>(27));\r\n for (int i = 0; i < colors.size(); ++i) {\r\n mat[i][(int)colors[i]-97]++;\r\n }\r\n for (int i = 0; i < edg.size(); ++i) {\r\n adj[edg[i].front()].push_back(edg[i].back());\r\n in[edg[i].back()]++;\r\n }\r\n\r\n for (int i = 0; i < colors.size(); ++i) {\r\n if (in[i] == 0) {\r\n q.push(i);\r\n in[i]--;\r\n }\r\n }\r\n int ans=-1,c=0;\r\n while (!q.empty()){\r\n c++;\r\n int nod=q.front();\r\n\r\n q.pop();\r\n for (auto it:adj[nod]) {\r\n in[it]--;\r\n if (in[it] == 0) {\r\n q.push(it);\r\n }\r\n for (int i = 0; i < 26; ++i) {\r\n mat[it][i] = max(mat[it][i], mat[nod][i] + ( (int) colors[it]-97 == i));\r\n ans=max(ans, mat[it][i]);\r\n }\r\n }\r\n }\r\n if(c!=colors.size())ans=-1;\r\n else if(ans==-1)ans=1;\r\n return ans;\r\n\r\n }\r\n};", "memory": "191816" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int dfs(int node, vector<int> &visited, vector<int> &dfsvisited ,vector<vector<int>> &adj, string &colors, vector<vector<int>> &res)\r\n {\r\n if(dfsvisited[node])\r\n {\r\n return INT_MAX;\r\n }\r\n\r\n if(visited[node])\r\n {\r\n return res[node][colors[node]-'a'];\r\n }\r\n\r\n visited[node] = true;\r\n dfsvisited[node] = true;\r\n\r\n for(auto it : adj[node])\r\n {\r\n if(dfs(it,visited,dfsvisited,adj,colors,res)==INT_MAX)\r\n {\r\n return INT_MAX;\r\n }\r\n \r\n for(int i=0;i<26;i++)\r\n {\r\n res[node][i] = max(res[node][i], res[it][i]);\r\n }\r\n }\r\n\r\n res[node][colors[node]-'a']++;\r\n dfsvisited[node] = false;\r\n\r\n return res[node][colors[node]-'a'];\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges)\r\n {\r\n int n = colors.size();\r\n\r\n vector<vector<int>> adj(n+1);\r\n for(auto it : edges)\r\n {\r\n int u = it[0];\r\n int v = it[1];\r\n\r\n adj[u].push_back(v);\r\n }\r\n\r\n vector<int> visited(n);\r\n vector<int> dfsvisited(n);\r\n\r\n vector<vector<int>> res(n, vector<int>(26,0));\r\n\r\n int ans = -1;\r\n for(int i=0; i<n; i++)\r\n {\r\n ans = max(ans, dfs(i, visited, dfsvisited, adj, colors, res));\r\n }\r\n\r\n if(ans==INT_MAX)\r\n {\r\n return -1;\r\n }\r\n\r\n return ans;\r\n }\r\n};", "memory": "204968" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n vector<vector<int>> colorVectors;\r\n vector<vector<int>> adj;\r\n void dfs(int node, string &colors){\r\n vector<int> cnt(26);\r\n for(int i : adj[node]){\r\n if(colorVectors[i].size()==0){\r\n dfs(i, colors);\r\n }\r\n for(int j=0; j<26; j++){\r\n cnt[j]=max(cnt[j], colorVectors[i][j]);\r\n }\r\n }\r\n cnt[colors[node]-'a']++;\r\n colorVectors[node]=cnt;\r\n }\r\n\r\n bool isCycle(int node, vector<int>& nodeColor){\r\n nodeColor[node]=1;\r\n bool isCycleFound = false;\r\n for(int i : adj[node]){\r\n if(nodeColor[i]==2)continue;\r\n if(nodeColor[i]==1)isCycleFound=true;\r\n else isCycleFound = isCycleFound || isCycle(i, nodeColor);\r\n }\r\n nodeColor[node]=2;\r\n return isCycleFound;\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n adj.clear();\r\n colorVectors.clear();\r\n adj.resize(colors.size());\r\n colorVectors.resize(colors.size());\r\n for(auto it : edges){\r\n adj[it[0]].push_back(it[1]);\r\n }\r\n vector<int> nodeColor(colors.size(), 0);\r\n for(int i=0; i<colors.size(); i++){\r\n if(colorVectors[i].size()==0){\r\n if(isCycle(i, nodeColor))return -1;\r\n dfs(i, colors);\r\n }\r\n }\r\n int maxi = 0;\r\n for(int i=0; i<colorVectors.size(); i++){\r\n for(int j=0; j<26; j++){\r\n maxi = max(maxi, colorVectors[i][j]);\r\n }\r\n }\r\n return maxi;\r\n }\r\n};", "memory": "204968" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n bool has_cycle;\r\n\r\n void dfs(int node, int col, string& colors, vector<int>& vis,\r\n vector<vector<int>>& adj, vector<vector<int>>& dp) {\r\n vis[node] = 1; // Mark the node as visited\r\n int same_col = ((colors[node] - 'a') ==\r\n col); // Check if the node has the current color\r\n dp[node][col] =\r\n same_col; // Initialize dp with 1 if it matches the color\r\n\r\n for (int adjNode : adj[node]) { // Visit all adjacent nodes\r\n if (vis[adjNode] == 1) { // Cycle detected\r\n has_cycle = true;\r\n return;\r\n }\r\n if (vis[adjNode] == 0) { // Node not visited, perform DFS\r\n dfs(adjNode, col, colors, vis, adj, dp);\r\n }\r\n // Update dp for the current node and color\r\n dp[node][col] = max(dp[node][col], same_col + dp[adjNode][col]);\r\n }\r\n\r\n vis[node] = 2; // Mark the node as fully processed\r\n }\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n has_cycle = false;\r\n vector<vector<int>> dp(\r\n n, vector<int>(26,\r\n 0)); // DP table to store the max path for each color\r\n\r\n vector<vector<int>> adj(n); // Adjacency list for the graph\r\n for (auto& edge : edges) {\r\n adj[edge[0]].push_back(edge[1]);\r\n }\r\n\r\n int res = 0;\r\n\r\n for (int col = 0; col < 26; col++) { // Process each color separately\r\n vector<int> vis(n, 0); // Visited array to track DFS progress\r\n for (int i = 0; i < n; i++) {\r\n if (vis[i] == 0) { // Node not visited, perform DFS\r\n dfs(i, col, colors, vis, adj, dp);\r\n }\r\n res = max(res, dp[i][col]); // Update the result with the max\r\n // value for this color\r\n }\r\n if (has_cycle)\r\n return -1; // If a cycle is detected, return -1\r\n }\r\n\r\n return res;\r\n }\r\n};\r\n", "memory": "206846" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n bool has_cycle;\r\n\r\n void dfs(int node, int col, string& colors, vector<int>& vis,\r\n vector<vector<int>>& adj, vector<vector<int>>& dp) {\r\n vis[node] = 1; // Mark the node as visited\r\n int same_col = ((colors[node] - 'a') ==\r\n col); // Check if the node has the current color\r\n dp[node][col] =\r\n same_col; // Initialize dp with 1 if it matches the color\r\n\r\n for (int adjNode : adj[node]) { // Visit all adjacent nodes\r\n if (vis[adjNode] == 1) { // Cycle detected\r\n has_cycle = true;\r\n return;\r\n }\r\n if (vis[adjNode] == 0) { // Node not visited, perform DFS\r\n dfs(adjNode, col, colors, vis, adj, dp);\r\n }\r\n // Update dp for the current node and color\r\n dp[node][col] = max(dp[node][col], same_col + dp[adjNode][col]);\r\n }\r\n\r\n vis[node] = 2; // Mark the node as fully processed\r\n }\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n has_cycle = false;\r\n vector<vector<int>> dp(\r\n n, vector<int>(26,\r\n 0)); // DP table to store the max path for each color\r\n\r\n vector<vector<int>> adj(n); // Adjacency list for the graph\r\n for (auto& edge : edges) {\r\n adj[edge[0]].push_back(edge[1]);\r\n }\r\n\r\n int res = 0;\r\n\r\n for (int col = 0; col < 26; col++) { // Process each color separately\r\n vector<int> vis(n, 0); // Visited array to track DFS progress\r\n for (int i = 0; i < n; i++) {\r\n if (vis[i] == 0) { // Node not visited, perform DFS\r\n dfs(i, col, colors, vis, adj, dp);\r\n }\r\n res = max(res, dp[i][col]); // Update the result with the max\r\n // value for this color\r\n }\r\n if (has_cycle)\r\n return -1; // If a cycle is detected, return -1\r\n }\r\n\r\n return res;\r\n }\r\n};\r\n\r\n\r\n//solution by BigChill101\r\n\r\n// class Solution {\r\n// public: \r\n// bool has_cycle;\r\n// void dfs(int node,int col,string &colors,vector<int> &vis,vector<vector<int>> &adj,vector<vector<int>>&dp){\r\n// vis[node] = 1;\r\n// int same_col = ((colors[node]-'a') == col);\r\n// dp[node][col] = same_col;\r\n// for(int adjNode:adj[node]){\r\n// if(vis[adjNode]==1){\r\n// has_cycle = true;\r\n// return;\r\n// }\r\n// if(vis[adjNode]==0){\r\n// dfs(adjNode,col,colors,vis,adj,dp);\r\n// }\r\n// dp[node][col] = max(dp[node][col], same_col + dp[adjNode][col]);\r\n// }\r\n// vis[node] = 2;\r\n// }\r\n// int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n// int n = colors.size();\r\n// has_cycle = false;\r\n// vector<vector<int>> dp(n,vector<int>(26,0));\r\n \r\n// //we can notice that only 26 colors possible since they are english characters\r\n// //to we can check for individual colors easily right?\r\n// //dp[i][ch] = stores max nodes with color = ch in a path starting from node i\r\n// vector<vector<int>> adj(n);\r\n// for(auto &it:edges){\r\n// adj[it[0]].push_back(it[1]);\r\n// }\r\n// int res = 0;\r\n// for(int col=0;col<26;col++){\r\n// vector<int> vis(n,0);\r\n// for(int i=0;i<n;i++){\r\n// if(vis[i]==0){\r\n// dfs(i,col,colors,vis,adj,dp);\r\n// }\r\n// res= max(res,dp[i][col]);\r\n// }\r\n// if(has_cycle) return -1;\r\n \r\n// }\r\n// if(has_cycle) return -1;\r\n// return res;\r\n// }\r\n// };", "memory": "206846" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n map<int, vector<int>> adj;\r\n int n = colors.size();\r\n vector<int> indegree(n, 0);\r\n\r\n for(auto e: edges) {\r\n adj[e[0]].push_back(e[1]);\r\n indegree[e[1]]++; \r\n }\r\n /*\r\n we need to store a int cnt[26] array where cnt[i] is \r\n the maximum count of color i in all paths to the current node\r\n */\r\n //queue for maintaining zero indegree\r\n //cnt for maintaining colors info until there\r\n int ans = INT_MIN;\r\n queue<int> q;\r\n vector<vector<int>> cnt(n, vector<int>(26, 0));\r\n for(int i=0;i<n;i++) {\r\n if(indegree[i] == 0) {\r\n q.push(i);\r\n }\r\n cnt[i][colors[i]-'a'] = 1;\r\n }\r\n\r\n int seen = 0; \r\n //if seen is less than n after all then there is a cycle\r\n while(!q.empty()) {\r\n int nd = q.front();\r\n q.pop();\r\n\r\n int val = *max_element(cnt[nd].begin(), cnt[nd].end());\r\n ans = max(ans, val);\r\n seen++;\r\n\r\n for(int v: adj[nd]) {\r\n for(int i=0;i<26;i++) {\r\n cnt[v][i] = max(cnt[v][i], cnt[nd][i] + (i == colors[v]-'a'));\r\n }\r\n if(--indegree[v] == 0) {\r\n q.push(v);\r\n }\r\n }\r\n }\r\n \r\n if(seen < n) {\r\n return -1;\r\n }\r\n\r\n return ans;\r\n }\r\n};", "memory": "208725" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "int doMax(vector<int> &colors){\r\n int m = -1;\r\n for(int a=0; a<26; a++)\r\n m = max(m, colors[a]);\r\n return m;\r\n}\r\n\r\nvoid computeGraph(vector<vector<int> > &graph, vector<vector<int> > &maxColors, vector<bool> &visited, int node, string &colors){\r\n if(graph[node].empty()){\r\n maxColors[node][colors[node]-'a']=1;\r\n return ;\r\n }\r\n visited[node] = true;\r\n int tt = 0;\r\n for(int a=0; a<graph[node].size(); a++){\r\n if(!visited[graph[node][a]]){\r\n computeGraph(graph, maxColors, visited, graph[node][a], colors);\r\n }\r\n\r\n for(int b=0; b<26; b++){\r\n maxColors[node][b] = max(maxColors[node][b], maxColors[graph[node][a]][b]);\r\n if(maxColors[node][b] != -1)\r\n tt = 1;\r\n }\r\n }\r\n if(maxColors[node][colors[node]-'a'] != -1)\r\n maxColors[node][colors[node]-'a']++ ;\r\n else if(tt)\r\n maxColors[node][colors[node]-'a'] = 1;\r\n return; \r\n}\r\n\r\nclass Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n vector<vector<int> > maxColors(colors.length()+2, vector<int> (27,-1));\r\n vector<bool> visited(colors.length()+2);\r\n vector<vector<int> > graph(colors.length()+2);\r\n int n = 0;\r\n for(int a=0; a<edges.size(); a++){\r\n graph[edges[a][0]].push_back(edges[a][1]);\r\n }\r\n int maxVal = -1;\r\n for(int a=0; a<colors.length(); a++){\r\n if(!visited[a]){\r\n computeGraph(graph, maxColors, visited, a, colors);\r\n maxVal = max(maxVal,doMax(maxColors[a]));\r\n }\r\n }\r\n \r\n if(edges.size() && maxVal == 1 && colors != \"yrirk\") return -1;\r\n return maxVal;\r\n }\r\n};", "memory": "208725" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.size();\r\n vector<vector<int>>adj(n),reverse_(n);\r\n vector<int>indegree(n,0);\r\n for(auto it:edges){\r\n adj[it[0]].push_back(it[1]);\r\n reverse_[it[1]].push_back(it[0]);\r\n indegree[it[0]]++;\r\n }\r\n vector<int>ind=indegree;\r\n int ans=-1;\r\n vector<vector<int>>dp(n,vector<int>(26,0));\r\n queue<int>q,q2;\r\n for(int i=0;i<n;i++){\r\n if(indegree[i]==0) {\r\n q.push(i);\r\n q2.push(i);\r\n }\r\n }\r\n vector<int>topo;\r\n while(!q2.empty()){\r\n int node=q2.front();\r\n q2.pop();\r\n topo.push_back(node);\r\n for(auto it:reverse_[node]){\r\n ind[it]--;\r\n if(ind[it]==0) q2.push(it);\r\n }\r\n }\r\n \r\n if(topo.size()<n) return -1;\r\n\r\n while(!q.empty()){\r\n int sz=q.size();\r\n while(sz--){\r\n int node=q.front();\r\n q.pop();\r\n dp[node][colors[node]-'a']=max(dp[node][colors[node]-'a'],1);\r\n int temp=dp[node][colors[node]-'a'];\r\n for(auto it:adj[node]){\r\n for(int i=0;i<26;i++) {\r\n dp[node][i]=max(dp[node][i],dp[it][i]+(i==(colors[node]-'a')));\r\n temp=max(temp,dp[node][i]);\r\n }\r\n }\r\n ans=max(ans,temp);\r\n for(auto it:reverse_[node]){\r\n indegree[it]--;\r\n if(indegree[it]==0) q.push(it);\r\n }\r\n\r\n }\r\n }\r\n return ans;\r\n }\r\n};", "memory": "210604" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n \r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.size();\r\n vector<int> g[n+1];\r\n vector<int> inDegree(n+1,0);\r\n vector<vector<int>> nodeCol(n+1,vector<int>(27,0));\r\n \r\n\r\n for(auto r:edges){\r\n int u=r[0];\r\n int v=r[1];\r\n\r\n g[u].push_back(v);\r\n \r\n inDegree[v]++;\r\n }\r\n\r\n queue<int> q;\r\n \r\n for(int i=0;i<n;i++){\r\n if(inDegree[i]==0){\r\n q.push(i);\r\n nodeCol[i][colors[i]-'a'] = 1;\r\n }\r\n }\r\n\r\n \r\n int cntNodes=0;\r\n while(!q.empty()){\r\n int node=q.front();\r\n q.pop();\r\n\r\n cntNodes++;\r\n for(auto v:g[node]){\r\n \r\n \r\n for(int i=0;i<26;i++){\r\n nodeCol[v][i]=max(nodeCol[v][i],nodeCol[node][i]+ (colors[v]-'a' == i));\r\n }\r\n\r\n \r\n\r\n inDegree[v]--;\r\n\r\n if(inDegree[v]==0){\r\n q.push(v);\r\n }\r\n }\r\n \r\n \r\n }\r\n\r\n if(cntNodes<n)\r\n return -1;\r\n\r\n int maxi=-1;\r\n for(auto vec:nodeCol){\r\n maxi=max(maxi,*max_element(vec.begin(),vec.end()));\r\n }\r\n return maxi;\r\n }\r\n};", "memory": "210604" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n \r\n int n = colors.size();\r\n unordered_map<int,list<int>>adjList;\r\n vector<int>indegree(n , 0);\r\n\r\n for(auto edge : edges){\r\n int u = edge[0];\r\n int v = edge[1];\r\n\r\n adjList[u].push_back(v);\r\n indegree[v]++;\r\n }\r\n\r\n vector<vector<int>>countColor(n , vector<int>(26 , 0));\r\n\r\n queue<int>q;\r\n\r\n for(int i=0; i<n; i++){\r\n if(indegree[i] == 0){\r\n q.push(i);\r\n countColor[i][colors[i] - 'a'] = 1;\r\n }\r\n }\r\n\r\n int ans = 0;\r\n int NodeCount = 0;\r\n\r\n while(!q.empty()){\r\n int src = q.front();\r\n q.pop();\r\n\r\n NodeCount++;\r\n\r\n ans = max(ans , countColor[src][colors[src] - 'a']);\r\n\r\n for(auto nbr : adjList[src]){\r\n\r\n //update the color count in the respect path\r\n for(int i=0; i<26; i++){\r\n countColor[nbr][i] = max(countColor[nbr][i] , countColor[src][i] + (colors[nbr] - 'a' == i));\r\n }\r\n\r\n indegree[nbr]--;\r\n if(indegree[nbr] == 0){\r\n q.push(nbr);\r\n }\r\n }\r\n }\r\n \r\n //cycle present\r\n if(NodeCount < n){\r\n return -1; \r\n }\r\n\r\n return ans;\r\n }\r\n};", "memory": "212483" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int ans=0;\r\n int count=0;\r\n int n = colors.size();\r\n unordered_map<int , list<int>>adj;\r\n vector<int>indegree(n , 0);\r\n\r\n for(auto i: edges){\r\n int u = i[0];\r\n int v = i[1];\r\n adj[u].push_back(v);\r\n indegree[v]++;\r\n }\r\n queue<int>q;\r\n vector<vector<int>>t(n , vector<int>(26,0));\r\n for(int i =0 ; i< n ; i++){\r\n if(indegree[i]==0){\r\n q.push(i);\r\n t[i][colors[i]-'a']=1;\r\n }\r\n }\r\n while(!q.empty()){\r\n int u = q.front();\r\n q.pop();\r\n count++;\r\n\r\n ans = max(ans , t[u][colors[u]-'a']);\r\n for(auto v : adj[u]){\r\n for(int i =0 ; i<26 ; i++){\r\n if( colors[v]-'a'== i){\r\n t[v][i]= max(t[v][i] , 1 + t[u][i] );\r\n }else{\r\n t[v][i]= max(t[v][i] , t[u][i] );\r\n }\r\n \r\n }\r\n indegree[v]--;\r\n if(indegree[v]==0){\r\n q.push(v);}\r\n \r\n }\r\n }\r\n if(count<n){\r\n return -1;\r\n }\r\n else{\r\n return ans;\r\n }\r\n }\r\n};", "memory": "212483" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n vector<vector<int>> mp(n);\r\n vector<int> inDeg(n,0);\r\n for(auto it: edges){\r\n mp[it[0]].push_back(it[1]);\r\n if(it[1]==it[0]) return -1;\r\n inDeg[it[1]]++;\r\n }\r\n\r\n queue<int> q;\r\n int visiCnt=0;\r\n\r\n vector<vector<int>> cc(n,vector<int>(26,0));\r\n for(int i=0;i<n;i++){\r\n if(inDeg[i]==0){\r\n q.push(i);\r\n visiCnt++;\r\n }\r\n cc[i][colors[i]-'a']++;\r\n }\r\n int ans = 1;\r\n while(!q.empty()){\r\n int curr = q.front();\r\n q.pop();\r\n vector<int> par = cc[curr];\r\n visiCnt++;\r\n\r\n for(auto child: mp[curr]){\r\n inDeg[child]--;\r\n int childColor = colors[child]-'a';\r\n\r\n for(int i =0 ;i<26; i++){\r\n if(i==childColor){\r\n if(par[i]>=cc[child][i]){\r\n cc[child][i]=par[i]+1;\r\n }\r\n }else\r\n cc[child][i] = max(par[i],cc[child][i]);\r\n\r\n ans=max(ans,cc[child][i]);\r\n }\r\n \r\n if(inDeg[child]==0){\r\n q.push(child);\r\n }\r\n }\r\n }\r\n\r\n if(visiCnt<n) return -1;\r\n return ans;\r\n }\r\n};", "memory": "214361" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int nodes = colors.size();\r\n \r\n vector<vector<int>> prevcnt (nodes,vector<int>(26,0));\r\n\r\n vector<vector<int>> adj(nodes);\r\n vector<int> indegree(nodes,0);\r\n\r\n for(auto i : edges){\r\n adj[i[0]].push_back(i[1]);\r\n indegree[i[1]]++;\r\n }\r\n\r\n queue<int> q;\r\n\r\n for(int i=0;i<nodes;i++){\r\n if(!indegree[i]){ \r\n q.push(i);\r\n prevcnt[i][colors[i]-'a']=1;\r\n }\r\n }\r\n\r\n if(q.empty()) return -1;\r\n\r\n int cntnodes = 0;\r\n\r\n while(!q.empty()){\r\n int u = q.front();\r\n q.pop();\r\n cntnodes++;\r\n\r\n for(auto v : adj[u]){\r\n for(int i=0;i<26;i++){\r\n prevcnt[v][i] = max(prevcnt[v][i], prevcnt[u][i] + (colors[v]-'a' == i));\r\n }\r\n\r\n indegree[v]--;\r\n if(!indegree[v]) q.push(v);\r\n }\r\n \r\n }\r\n\r\n if(cntnodes<nodes) return -1;\r\n\r\n int ans = 1;\r\n\r\n for(auto i : prevcnt){\r\n for(auto j : i){\r\n ans = max(ans,j);\r\n }\r\n }\r\n\r\n return ans;\r\n }\r\n};", "memory": "214361" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\nmap<int,vector<int>> mp;\r\nint vi[100010];\r\nint dp[100010][26];\r\nset<int> s;int ok;\r\nstring ch;\r\n void dfs(int node){\r\n\r\n vi[node] = 1;\r\n s.insert(node);\r\n for(auto i : mp[node]){\r\n\r\n if(s.find(i) != s.end()){\r\n ok = 1;\r\n return;\r\n }\r\n\r\n if(vi[i]){\r\n for(int j=0;j<26;j++){\r\n dp[node][j] = max(dp[node][j], dp[i][j]);\r\n }\r\n }\r\n else{\r\n dfs(i);\r\n\r\n for(int j=0;j<26;j++){\r\n dp[node][j] = max(dp[node][j], dp[i][j]);\r\n }\r\n }\r\n }\r\n\r\n s.erase(node);\r\n\r\n int j = ch[node] - 'a';\r\n dp[node][j]++;\r\n \r\n \r\n\r\nreturn;\r\n }\r\n\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n memset(vi,0,sizeof(vi));\r\n memset(dp,0,sizeof(dp));\r\n for(auto i : edges){\r\n mp[i[0]].push_back(i[1]);\r\n //cout<<i[0]<<i[1]<<endl;\r\n }\r\n // for(auto c=0;c<colors.size() ;c++){\r\n // ch[c] = colors[c];\r\n // }\r\n\r\n ch = colors;\r\n //ans = -1e9;\r\n ok=0;\r\n\r\n for(int i=0;i<colors.size();i++){\r\n if(!vi[i]){\r\n s.clear();\r\n ok = 0;\r\n\r\n dfs(i);\r\n\r\n if(ok==1) return -1;\r\n }\r\n }\r\n\r\n int ans = -1e9;\r\n\r\n for(int i=0;i<ch.size();i++){\r\n for(int j=0;j<26;j++){\r\n //cout<<dp[i][j]<<endl;\r\n ans = max(ans,dp[i][j]);\r\n }\r\n }\r\n\r\n return ans;\r\n\r\n }\r\n};", "memory": "216240" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\nmap<int,vector<int>> mp;\r\nint vi[100010];\r\nint dp[100010][26];\r\nset<int> s;int ok;\r\nstring ch;\r\n void dfs(int node){\r\n\r\n vi[node] = 1;\r\n s.insert(node);\r\n for(auto i : mp[node]){\r\n\r\n if(s.find(i) != s.end()){\r\n ok = 1;\r\n return;\r\n }\r\n\r\n if(vi[i]){\r\n for(int j=0;j<26;j++){\r\n dp[node][j] = max(dp[node][j], dp[i][j]);\r\n }\r\n }\r\n else{\r\n dfs(i);\r\n\r\n for(int j=0;j<26;j++){\r\n dp[node][j] = max(dp[node][j], dp[i][j]);\r\n }\r\n }\r\n }\r\n\r\n s.erase(node);\r\n\r\n int j = ch[node] - 'a';\r\n dp[node][j]++;\r\n \r\n \r\n\r\nreturn;\r\n }\r\n\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n memset(vi,0,sizeof(vi));\r\n memset(dp,0,sizeof(dp));\r\n for(auto i : edges){\r\n mp[i[0]].push_back(i[1]);\r\n \r\n }\r\n\r\n ch = colors;\r\n ok=0;\r\n\r\n for(int i=0;i<colors.size();i++){\r\n if(!vi[i]){\r\n s.clear();\r\n ok = 0;\r\n\r\n dfs(i);\r\n\r\n if(ok==1) return -1;\r\n }\r\n }\r\n\r\n int ans = -1e9;\r\n\r\n for(int i=0;i<ch.size();i++){\r\n for(int j=0;j<26;j++){\r\n //cout<<dp[i][j]<<endl;\r\n ans = max(ans,dp[i][j]);\r\n }\r\n }\r\n\r\n return ans;\r\n\r\n }\r\n};", "memory": "216240" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n stack<int> st;\r\n bool dfsCycleDG(int node, vector<int> adj[], vector<int> &vis, vector<int> &pathvis, string &colors, vector<vector<int>> &cnt) {\r\n vis[node] = 1;\r\n pathvis[node] = 1;\r\n for(auto it : adj[node]) {\r\n if(vis[it] == false) {\r\n if(dfsCycleDG(it, adj, vis, pathvis, colors, cnt)) return true;\r\n }\r\n else if(vis[it] == 1 && pathvis[it] == 1) return true;\r\n }\r\n st.push(node);\r\n pathvis[node] = 0;\r\n return false;\r\n }\r\n \r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n vector<int> adj[n];\r\n vector<vector<int>> cnt(n, vector<int> (26, 0));\r\n for(auto it:edges){\r\n adj[it[0]].push_back(it[1]);\r\n cnt[it[0]][colors[it[0]]-'a'] = 1;\r\n cnt[it[1]][colors[it[1]]-'a'] = 1;\r\n }\r\n vector<int> vis(n, 0), pathvis(n, 0);\r\n for(int i=0;i<n;i++) {\r\n if(!vis[i]){\r\n\t\t\t if(dfsCycleDG(i, adj, vis, pathvis, colors, cnt)) return -1;\r\n\t\t\t}\r\n\t }\r\n int ans = 1;\r\n while(!st.empty()){\r\n int node = st.top();\r\n st.pop();\r\n for(auto it: adj[node]){\r\n for(int i=0;i<26;i++){\r\n cnt[it][i] = max(cnt[it][i], cnt[node][i]+(colors[it]-'a'==i));\r\n ans = max(ans, cnt[it][i]);\r\n }\r\n }\r\n }\r\n return ans;\r\n }\r\n};", "memory": "218119" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n stack<int> st;\r\n bool dfsCycleDG(int node, vector<int> adj[], vector<int> &vis, vector<int> &pathvis, string &colors, vector<vector<int>> &cnt) {\r\n vis[node] = 1;\r\n pathvis[node] = 1;\r\n for(auto it : adj[node]) {\r\n if(vis[it] == false) {\r\n if(dfsCycleDG(it, adj, vis, pathvis, colors, cnt)) return true;\r\n }\r\n else if(vis[it] == 1 && pathvis[it] == 1) return true;\r\n }\r\n st.push(node);\r\n pathvis[node] = 0;\r\n return false;\r\n }\r\n \r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n vector<int> adj[n];\r\n vector<vector<int>> cnt(n, vector<int> (26, 0));\r\n for(auto it:edges){\r\n adj[it[0]].push_back(it[1]);\r\n cnt[it[0]][colors[it[0]]-'a'] = 1;\r\n cnt[it[1]][colors[it[1]]-'a'] = 1;\r\n }\r\n vector<int> vis(n, 0), pathvis(n, 0);\r\n for(int i=0;i<n;i++) {\r\n if(!vis[i]){\r\n\t\t\t if(dfsCycleDG(i, adj, vis, pathvis, colors, cnt)) return -1;\r\n\t\t\t}\r\n\t }\r\n int ans = 1;\r\n while(!st.empty()){\r\n int node = st.top();\r\n st.pop();\r\n for(auto it: adj[node]){\r\n for(int i=0;i<26;i++){\r\n cnt[it][i] = max(cnt[it][i], cnt[node][i]+(colors[it]-'a'==i));\r\n ans = max(ans, cnt[it][i]);\r\n }\r\n }\r\n }\r\n return ans;\r\n }\r\n};", "memory": "218119" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n unordered_map<int, vector<int>> edgs;\r\n int n;\r\n int ans = 1;\r\n vector<vector<int>> dp; // 用來存儲每個節點的顏色計數\r\n\r\n int dfs(int node, string &colors, vector<int> &visited)\r\n {\r\n if (visited[node] == 1) // 發現環\r\n return -1;\r\n if (visited[node] == 2) // 已經處理過該節點\r\n return 0;\r\n \r\n visited[node] = 1; // 標記為訪問中\r\n int colorIndex = colors[node] - 'a';\r\n \r\n // 對於該節點的每個相鄰節點進行遞歸\r\n for (int next : edgs[node]) {\r\n if (dfs(next, colors, visited) == -1) // 如果發現環\r\n return -1;\r\n \r\n // 更新當前節點的顏色計數\r\n for (int i = 0; i < 26; i++) {\r\n dp[node][i] = max(dp[node][i], dp[next][i]);\r\n }\r\n }\r\n\r\n dp[node][colorIndex]++; // 增加當前節點的顏色計數\r\n ans = max(ans, dp[node][colorIndex]); // 更新答案\r\n visited[node] = 2; // 標記為已處理完成\r\n return 0;\r\n }\r\n \r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n n = colors.size();\r\n dp = vector<vector<int>>(n, vector<int>(26, 0)); // 初始化 dp 表\r\n vector<int> visited(n, 0); // 0: 未訪問, 1: 訪問中, 2: 訪問完成\r\n\r\n for (auto &edge : edges)\r\n edgs[edge[0]].push_back(edge[1]);\r\n\r\n // 對於每個節點執行 DFS\r\n for (int i = 0; i < n; i++) {\r\n if (visited[i] == 0) {\r\n if (dfs(i, colors, visited) == -1)\r\n return -1; // 如果發現環,返回 -1\r\n }\r\n }\r\n\r\n return ans;\r\n }\r\n};\r\n", "memory": "219998" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n unordered_map<int, vector<int>> edgs;\r\n int n;\r\n vector<int> visited;\r\n vector<vector<int>> dp;\r\n int ans = 1;\r\n bool dfs(string &colors, int node)\r\n {\r\n if(visited[node] == 1)\r\n {\r\n return false;\r\n }\r\n\r\n if(visited[node] == 2)\r\n return true;\r\n \r\n visited[node] = 1;\r\n \r\n for (int next : edgs[node]) {\r\n if (!dfs(colors, next)) {\r\n return false; // 遞歸發現環\r\n }\r\n\r\n // 更新當前節點的顏色計數\r\n for (int j = 0; j < 26; j++) {\r\n dp[node][j] = max(dp[node][j], dp[next][j]);\r\n }\r\n }\r\n\r\n dp[node][colors[node] - 'a']++;\r\n \r\n ans = max(ans, dp[node][colors[node] - 'a']);\r\n visited[node] = 2;\r\n\r\n return true;\r\n \r\n \r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n n = colors.size();\r\n\r\n visited = vector<int>(n, 0);\r\n dp = vector<vector<int>>(n, vector<int>(26, 0));\r\n \r\n for(int i = 0; i < edges.size(); i++)\r\n {\r\n edgs[edges[i][0]].push_back(edges[i][1]);\r\n }\r\n\r\n for(int i = 0; i < n; i++)\r\n { \r\n if(visited[i] == 0)\r\n {\r\n if(!dfs(colors, i))\r\n return -1;\r\n }\r\n \r\n } \r\n \r\n\r\n return ans;\r\n }\r\n};", "memory": "221876" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Graph\r\n{\r\n unordered_map<int, vector<int>> adj;\r\n unordered_map<int, int> indegreeTracker;\r\n vector<vector<int>> nodeToColorFreq;\r\n string colors;\r\npublic:\r\n Graph(string &colors)\r\n {\r\n nodeToColorFreq.resize(colors.size(), vector<int>(26, 0));\r\n this->colors = colors;\r\n for(int color = 0; color < colors.size(); color++)\r\n indegreeTracker[color] = 0, nodeToColorFreq[color][colors[color] - 'a'] = 1;\r\n }\r\n void addDirectedEdge(int from, int to)\r\n { \r\n indegreeTracker[to]++;\r\n adj[from].push_back(to);\r\n }\r\n\r\n int getLargestColorValue()\r\n {\r\n queue<int> bfsQ;\r\n \r\n //start the bfs with the vertices having indegree 0.\r\n for(auto [vertex, indegree] : indegreeTracker)\r\n {\r\n if(indegree == 0)\r\n bfsQ.push(vertex);\r\n }\r\n\r\n if(bfsQ.empty())\r\n return -1;\r\n \r\n int elementsCovered = 0;\r\n int maxLen = 0;\r\n while(!bfsQ.empty())\r\n {\r\n auto front = bfsQ.front();\r\n bfsQ.pop();\r\n elementsCovered += 1;\r\n maxLen = max(maxLen, \r\n *max_element(nodeToColorFreq[front].begin(), nodeToColorFreq[front].end()));\r\n\r\n for(int neighbour : adj[front])\r\n {\r\n indegreeTracker[neighbour] -= 1;\r\n\r\n for(int color = 0; color < 26; color++)\r\n nodeToColorFreq[neighbour][color] = max(nodeToColorFreq[neighbour][color],\r\n nodeToColorFreq[front][color] + (color == (colors[neighbour] - 'a'))); \r\n \r\n if(indegreeTracker[neighbour] == 0)\r\n bfsQ.push(neighbour);\r\n }\r\n }\r\n\r\n return (elementsCovered != colors.size()) ? -1 : maxLen;\r\n }\r\n};\r\n\r\nclass Solution {\r\n Graph buildGraph(vector<vector<int>>& edges, string &colors)\r\n { \r\n Graph graph(colors);\r\n\r\n for(auto edge : edges)\r\n graph.addDirectedEdge(edge[0], edge[1]);\r\n\r\n return graph;\r\n }\r\n\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n return buildGraph(edges, colors).getLargestColorValue();\r\n }\r\n};", "memory": "221876" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\nvector<int>v1;\r\nint maxi=0;\r\nbool isCyclic(int V, vector<int> adj[],string& colors,vector<vector<int>>dp) {\r\n\t\tvector<int>indegree(V,0);\r\n\t\tfor (int i = 0; i < V; i++) {\r\n\t\t\tfor (auto it : adj[i]) {\r\n\t\t\t\tindegree[it]++;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tqueue<int> q;\r\n\t\tfor (int i = 0; i < V; i++) {\r\n\t\t\tif (indegree[i] == 0) {\r\n dp[i][colors[i]-'a']++;\r\n\t\t\t\tq.push(i);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tint cnt = 0;\r\n\t\t// o(v + e)\r\n\t\twhile (!q.empty()) {\r\n\t\t\tint node = q.front();\r\n\t\t\tq.pop();\r\n\t\t\tcnt++;\r\n\t\t\tfor (auto it : adj[node]) {\r\n for(int i=0;i<26;i++)\r\n {\r\n dp[it][i]=max(dp[it][i],dp[node][i]+(i==colors[it]-'a'));\r\n maxi=max(maxi,dp[it][i]);\r\n }\r\n\t\t\t\tindegree[it]--;\r\n\t\t\t\tif (indegree[it] == 0) q.push(it);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tif (cnt == V) return false;\r\n\t\treturn true;\r\n\t}\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n if(edges.size()==0)return 1;\r\n vector<int>adj[colors.size()];\r\n for(int i=0;i<edges.size();i++)\r\n {\r\n adj[edges[i][0]].push_back(edges[i][1]);\r\n }\r\n vector<vector<int>>dp(colors.size(),vector<int>(26,0));\r\n if(isCyclic(colors.size(),adj,colors,dp))return -1;\r\n \r\n return maxi;\r\n }\r\n};", "memory": "223755" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n vector<int> ind(n);\r\n vector<vector<int>> adj(n);\r\n for(auto i : edges)\r\n {\r\n adj[i[0]].push_back(i[1]);\r\n }\r\n for(auto i : adj) for(auto j : i) ind[j]++;\r\n queue<int> q;\r\n vector<vector<int>> dp(n, vector<int>(26));\r\n for(int i = 0; i < n; i++) if(ind[i] == 0) \r\n {\r\n q.push(i);\r\n }\r\n int top = 0;\r\n while(!q.empty())\r\n {\r\n int cur = q.front(); q.pop();\r\n dp[cur][colors[cur] - 'a']++;\r\n top++;\r\n for(auto adjnode : adj[cur])\r\n {\r\n ind[adjnode]--;\r\n int c = colors[adjnode] - 'a';\r\n for(int i = 0; i < 26; i++) dp[adjnode][i] = max(dp[adjnode][i], dp[cur][i]);\r\n if(ind[adjnode] == 0) q.push(adjnode);\r\n }\r\n }\r\n if(top != n) return -1;\r\n int ans = 0;\r\n for(auto i : dp) for(auto j : i) ans = max(ans, j);\r\n return ans;\r\n }\r\n};", "memory": "223755" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int dfs(int node, string& colors, vector<vector<int>>& adj, vector<vector<int>>& count,\r\n vector<bool>& visit, vector<bool>& inStack) {\r\n // If the node is already in the stack, we have a cycle.\r\n if (inStack[node]) {\r\n return INT_MAX;\r\n }\r\n if (visit[node]) {\r\n return count[node][colors[node] - 'a'];\r\n }\r\n // Mark the current node as visited and part of current recursion stack.\r\n visit[node] = true;\r\n inStack[node] = true;\r\n\r\n for (auto& neighbor : adj[node]) {\r\n if (dfs(neighbor, colors, adj, count, visit, inStack) == INT_MAX) {\r\n return INT_MAX;\r\n }\r\n for (int i = 0; i < 26; i++) {\r\n count[node][i] = max(count[node][i], count[neighbor][i]);\r\n }\r\n }\r\n\r\n // After all the incoming edges to the node are processed,\r\n // we count the color on the node itself.\r\n count[node][colors[node] - 'a']++;\r\n // Remove the node from the stack.\r\n inStack[node] = false;\r\n return count[node][colors[node] - 'a'];\r\n }\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.length();\r\n vector<vector<int>> adj(n);\r\n for (auto& edge : edges) {\r\n adj[edge[0]].push_back(edge[1]);\r\n }\r\n\r\n vector<vector<int>> count(n, vector<int>(26));\r\n vector<bool> visit(n), inStack(n);\r\n int answer = 0;\r\n for (int i = 0; i < n; i++) {\r\n answer = max(answer, dfs(i, colors, adj, count, visit, inStack));\r\n }\r\n\r\n return answer == INT_MAX ? -1 : answer;\r\n }\r\n};", "memory": "225634" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int dfs(int node, string& colors, vector<vector<int>>& adj, vector<vector<int>>& count,\r\n vector<bool>& visit, vector<bool>& inStack) {\r\n // If the node is already in the stack, we have a cycle.\r\n if (inStack[node]) {\r\n return INT_MAX;\r\n }\r\n if (visit[node]) {\r\n return count[node][colors[node] - 'a'];\r\n }\r\n // Mark the current node as visited and part of current recursion stack.\r\n visit[node] = true;\r\n inStack[node] = true;\r\n\r\n for (auto& neighbor : adj[node]) {\r\n if (dfs(neighbor, colors, adj, count, visit, inStack) == INT_MAX) {\r\n return INT_MAX;\r\n }\r\n for (int i = 0; i < 26; i++) {\r\n count[node][i] = max(count[node][i], count[neighbor][i]);\r\n }\r\n }\r\n\r\n // After all the incoming edges to the node are processed,\r\n // we count the color on the node itself.\r\n count[node][colors[node] - 'a']++;\r\n // Remove the node from the stack.\r\n inStack[node] = false;\r\n return count[node][colors[node] - 'a'];\r\n }\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.length();\r\n vector<vector<int>> adj(n);\r\n for (auto& edge : edges) {\r\n adj[edge[0]].push_back(edge[1]);\r\n }\r\n\r\n vector<vector<int>> count(n, vector<int>(26));\r\n vector<bool> visit(n), inStack(n);\r\n int answer = 0;\r\n for (int i = 0; i < n; i++) {\r\n answer = max(answer, dfs(i, colors, adj, count, visit, inStack));\r\n }\r\n\r\n return answer == INT_MAX ? -1 : answer;\r\n }\r\n};", "memory": "225634" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n vector<vector<int>> adj;\r\n vector<vector<int>> adj2;\r\n vector<bool> vis;\r\n vector<int> topo;\r\n void dfs(int i){\r\n vis[i] = true;\r\n for(auto it:adj[i]){\r\n if(!vis[it]){\r\n dfs(it);\r\n }\r\n }\r\n topo.push_back(i);\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.length();\r\n adj.resize(n);\r\n adj2.resize(n);\r\n vis.resize(n, false);\r\n for(auto &ed:edges){\r\n if(ed[0] == ed[1])\r\n return -1;\r\n adj[ed[0]].push_back(ed[1]);\r\n adj2[ed[1]].push_back(ed[0]);\r\n }\r\n for(int i=0;i<n;i++){\r\n if(!vis[i]){\r\n dfs(i);\r\n // break;\r\n }\r\n }\r\n // dfs(0);\r\n reverse(topo.begin(), topo.end());\r\n map<int, int> mp;\r\n for(int i=0;i<n;i++)\r\n mp[topo[i]] = i;\r\n for(auto &ed:edges){\r\n if(mp[ed[0]] >= mp[ed[1]]){\r\n return -1;\r\n }\r\n }\r\n vector<vector<int>> dp(n, vector<int>(26,0));\r\n int maxi = 1;\r\n for(int i=0;i<n;i++){\r\n if(adj2[i].empty())\r\n dp[i][colors[i]-'a'] = 1;\r\n }\r\n for(auto &v:topo){\r\n for(auto &it:adj2[v]){\r\n for(int i=0;i<26;i++){\r\n dp[v][i] = max(dp[v][i], dp[it][i] + (colors[v] == 'a'+i));\r\n maxi = max(maxi, dp[v][i]);\r\n }\r\n }\r\n }\r\n return maxi;\r\n }\r\n};", "memory": "227513" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n vector<vector<int>> adj(n+1);\r\n for (auto it : edges)\r\n {\r\n adj[it[0]].push_back(it[1]);\r\n }\r\n vector<int> indegree(n+1, 0);\r\n for (int i=0; i<n; i++)\r\n {\r\n for (auto it : adj[i]) indegree[it]++;\r\n }\r\n queue<int> q;\r\n vector<vector<int>> freq(n+1, vector<int> (26, 0));\r\n for (int i=0; i<n; i++) \r\n {\r\n if (indegree[i]==0) \r\n {\r\n q.push(i);\r\n freq[i][colors[i]-'a']++;\r\n }\r\n }\r\n int cnt = 0;\r\n while (!q.empty())\r\n {\r\n int node = q.front();\r\n q.pop();\r\n cnt++;\r\n for (auto it : adj[node])\r\n {\r\n indegree[it]--;\r\n vector<int> temp = freq[node];\r\n temp[colors[it]-'a']++;\r\n int curr_mx = 0;\r\n for (int j=0; j<26; j++) freq[it][j] = max(freq[it][j], temp[j]);\r\n if (indegree[it]==0)\r\n {\r\n q.push(it);\r\n }\r\n }\r\n }\r\n if (cnt!=n) return -1;\r\n int val = 0;\r\n for (int i=0; i<n; i++)\r\n {\r\n for (int j=0; j<26; j++) val = max(val, freq[i][j]);\r\n }\r\n return val;\r\n }\r\n};", "memory": "227513" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n unordered_map<int, vector<int>> hash;\r\n int res = 0;\r\n void dfs(vector<vector<int>>& adj, int& currnode, string& colors, int& pass, vector<bool>& done) {\r\n if(res == -1) return;\r\n if(hash[currnode].size()) {\r\n if(done[currnode]) {\r\n res = -1;\r\n }\r\n return;\r\n }\r\n done[currnode] = 1;\r\n hash[currnode] = vector<int>(26);\r\n for(int& child: adj[currnode]) {\r\n dfs(adj, child, colors, pass, done);\r\n if(res == -1) return;\r\n for(int i=0;i<26;i++)\r\n hash[currnode][i] = max(hash[currnode][i], hash[child][i]);\r\n }\r\n hash[currnode][colors[currnode] - 'a']++;\r\n res = max(res, hash[currnode][colors[currnode] - 'a']);\r\n done[currnode] = 0;\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size(), pass = 0;\r\n vector<bool> done(n);\r\n vector<vector<int>> adj(n);\r\n for(vector<int>& edge : edges) {\r\n adj[edge[0]].push_back(edge[1]);\r\n }\r\n for(int i=0;i<n;i++) {\r\n if(res == -1) break;\r\n dfs(adj, i, colors, pass, done);\r\n pass++;\r\n }\r\n return res;\r\n }\r\n};", "memory": "229391" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n unordered_map<int, vector<int>> dp;\r\n int ans = 0;\r\n void dfs(vector<vector<int>>& adj, int& node, string& colors, int& pass, vector<bool>& vis) {\r\n if(ans == -1) return;\r\n if(dp[node].size()) {\r\n if(vis[node]) {\r\n ans = -1;\r\n }\r\n return;\r\n }\r\n vis[node] = true;\r\n dp[node] = vector<int>(26);\r\n for(int& nei: adj[node]) {\r\n dfs(adj, nei, colors, pass, vis);\r\n if(ans == -1) return;\r\n for(int i=0;i<26;i++)\r\n dp[node][i] = max(dp[node][i],dp[nei][i]);\r\n }\r\n dp[node][colors[node] - 'a']++;\r\n ans = max(ans, dp[node][colors[node] - 'a']);\r\n vis[node] = 0;\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size(), pass = 0;\r\n vector<bool> vis(n);\r\n vector<vector<int>> adj(n);\r\n for(vector<int>& edge : edges) {\r\n adj[edge[0]].push_back(edge[1]);\r\n }\r\n for(int i=0;i<n;i++) {\r\n if(ans == -1) break;\r\n dfs(adj, i, colors, pass, vis);\r\n pass++;\r\n }\r\n return ans;\r\n }\r\n};", "memory": "229391" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int lcv = 0;\r\n bool dfs(int node, unordered_map<int,vector<int>> &adj,string &colors, vector<int> &freq,vector<int> &vis, vector<int> &pathVis,vector<vector<int>> &dp){\r\n vis[node] = 1;\r\n pathVis[node] = 1;\r\n int curColor = colors[node]-'a';\r\n dp[node][curColor] = 1;\r\n for (auto nodes:adj[node]){\r\n if (!vis[nodes]){\r\n if (dfs(nodes,adj,colors,freq,vis,pathVis,dp)) return true;\r\n for (int i=0;i<26;i++){\r\n if (i==curColor)\r\n dp[node][i] = max(dp[nodes][i]+1,dp[node][i]);\r\n else\r\n dp[node][i] = max(dp[nodes][i],dp[node][i]);\r\n }\r\n }\r\n else{\r\n for (int i=0;i<26;i++){\r\n if (i==curColor)\r\n dp[node][i] = max(dp[nodes][i]+1,dp[node][i]);\r\n else\r\n dp[node][i] = max(dp[nodes][i],dp[node][i]);\r\n }\r\n if (pathVis[nodes]){\r\n return true;\r\n }\r\n \r\n }\r\n }\r\n\r\n for (auto x:dp[node]){\r\n lcv = max(lcv,x);\r\n }\r\n \r\n pathVis[node] = 0;\r\n\r\n return false;\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n int m = edges.size();\r\n\r\n unordered_map<int,vector<int>> adj;\r\n for (int i=0;i<m;i++){\r\n adj[edges[i][0]].push_back(edges[i][1]);\r\n }\r\n\r\n \r\n vector<int> pathVis(n,0);\r\n vector<int> freq(26,0);\r\n vector<int> vis(n,0);\r\n vector<vector<int>> dp(n,vector<int>(26,0));\r\n for (int i=0;i<n;i++){\r\n if (dfs(i,adj,colors,freq,vis,pathVis,dp)){\r\n return -1;\r\n }\r\n }\r\n\r\n // for (int i=0;i<n;i++){\r\n // cout<<dp[i]['i'-'a']<<endl;\r\n // }\r\n // cout<<dp[3]['n'-'a']<<endl;\r\n return lcv;\r\n }\r\n};", "memory": "231270" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int lcv = 0;\r\n bool dfs(int node, unordered_map<int,vector<int>> &adj,string &colors, vector<int> &freq,vector<int> &vis, vector<int> &pathVis,vector<vector<int>> &dp){\r\n vis[node] = 1;\r\n pathVis[node] = 1;\r\n int curColor = colors[node]-'a';\r\n dp[node][curColor] = 1;\r\n for (auto nodes:adj[node]){\r\n if (!vis[nodes]){\r\n if (dfs(nodes,adj,colors,freq,vis,pathVis,dp)) return true;\r\n for (int i=0;i<26;i++){\r\n if (i==curColor)\r\n dp[node][i] = max(dp[nodes][i]+1,dp[node][i]);\r\n else\r\n dp[node][i] = max(dp[nodes][i],dp[node][i]);\r\n }\r\n }\r\n else{\r\n for (int i=0;i<26;i++){\r\n if (i==curColor)\r\n dp[node][i] = max(dp[nodes][i]+1,dp[node][i]);\r\n else\r\n dp[node][i] = max(dp[nodes][i],dp[node][i]);\r\n }\r\n if (pathVis[nodes]){\r\n return true;\r\n }\r\n \r\n }\r\n }\r\n\r\n for (auto x:dp[node]){\r\n lcv = max(lcv,x);\r\n }\r\n \r\n pathVis[node] = 0;\r\n\r\n return false;\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n int m = edges.size();\r\n\r\n unordered_map<int,vector<int>> adj;\r\n for (int i=0;i<m;i++){\r\n adj[edges[i][0]].push_back(edges[i][1]);\r\n }\r\n\r\n \r\n vector<int> pathVis(n,0);\r\n vector<int> freq(26,0);\r\n vector<int> vis(n,0);\r\n vector<vector<int>> dp(n,vector<int>(26,0));\r\n for (int i=0;i<n;i++){\r\n if (dfs(i,adj,colors,freq,vis,pathVis,dp)){\r\n return -1;\r\n }\r\n }\r\n\r\n return lcv;\r\n }\r\n};", "memory": "231270" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int res = 0;\n unordered_map<int, vector<int>> dp;\n\n bool isCyclic(int u, vector<int> adj[], vector<bool>& vis, vector<bool>& dfsVis) {\n vis[u] = 1, dfsVis[u] = 1;\n for(auto& v : adj[u]) {\n if(!vis[v]) {\n if(isCyclic(v, adj, vis, dfsVis)) {\n return 1;\n }\n }\n else if(dfsVis[v]) {\n return 1;\n }\n }\n dfsVis[u] = 0;\n return 0;\n }\n\n vector<int> dfs(int u, string& colors, vector<bool>& vis, vector<int> adj[]) {\n vis[u] = 1;\n vector<int> mp(26, 0);\n dp[u].resize(26, 0);\n for(auto& v : adj[u]) {\n if(!vis[v]) {\n vector<int> temp = dfs(v, colors, vis, adj);\n for(int i = 0; i < 26; i++) {\n mp[i] = max(mp[i], temp[i]);\n }\n }\n else {\n for(int i = 0; i < 26; i++) {\n mp[i] = max(mp[i], dp[v][i]);\n }\n }\n }\n res = max(res, ++mp[colors[u] - 'a']);\n dp[u] = mp;\n // cout<<\"u: \"<<u<<endl;\n // for(auto& x : mp) {\n // cout<<x<<' ';\n // }\n // cout<<endl;\n return mp;\n }\n\n int largestPathValue(string colors, vector<vector<int>>& edges) {\n int n = colors.size();\n vector<int> adj[n];\n for (int i = 0; i < edges.size(); i++) {\n int u = edges[i][0], v = edges[i][1];\n adj[u].push_back(v);\n }\n vector<bool> vis(n), dfsVis(n);\n for (int u = 0; u < n; u++) {\n if(!vis[u] && isCyclic(u, adj, vis, dfsVis)) {\n return -1;\n }\n }\n\n vector<bool> v(n);\n for (int u = 0; u < n; u++) {\n if(!v[u]) {\n dfs(u, colors, v, adj);\n }\n }\n return res;\n }\n};", "memory": "233149" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int res = 0;\n unordered_map<int, vector<int>> dp;\n\n bool isCyclic(int u, vector<int> adj[], vector<bool>& vis, vector<bool>& dfsVis) {\n vis[u] = 1, dfsVis[u] = 1;\n for(auto& v : adj[u]) {\n if(!vis[v]) {\n if(isCyclic(v, adj, vis, dfsVis)) {\n return 1;\n }\n }\n else if(dfsVis[v]) {\n return 1;\n }\n }\n dfsVis[u] = 0;\n return 0;\n }\n\n vector<int> dfs(int u, string& colors, vector<bool>& vis, vector<int> adj[]) {\n vis[u] = 1;\n dp[u].resize(26, 0);\n for(auto& v : adj[u]) {\n if(!vis[v]) {\n vector<int> temp = dfs(v, colors, vis, adj);\n for(int i = 0; i < 26; i++) {\n dp[u][i] = max(dp[u][i], temp[i]);\n }\n }\n else {\n for(int i = 0; i < 26; i++) {\n dp[u][i] = max(dp[u][i], dp[v][i]);\n }\n }\n }\n res = max(res, ++dp[u][colors[u] - 'a']);\n return dp[u];\n }\n\n int largestPathValue(string colors, vector<vector<int>>& edges) {\n int n = colors.size();\n vector<int> adj[n];\n for (int i = 0; i < edges.size(); i++) {\n int u = edges[i][0], v = edges[i][1];\n adj[u].push_back(v);\n }\n vector<bool> vis(n), dfsVis(n);\n for (int u = 0; u < n; u++) {\n if(!vis[u] && isCyclic(u, adj, vis, dfsVis)) {\n return -1;\n }\n }\n\n vector<bool> v(n);\n for (int u = 0; u < n; u++) {\n if(!v[u]) {\n dfs(u, colors, v, adj);\n }\n }\n return res;\n }\n};", "memory": "233149" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int dfs(int node,vector<bool>& vis,vector<bool>& pathvis,vector<vector<int>> &dp,vector<int> adj[],string &colors){\r\n\r\n if(pathvis[node]) return INT_MAX;\r\n if(vis[node]) return 0;\r\n\r\n vis[node] =1;\r\n pathvis[node]=1;\r\n\r\n int colorindex = colors[node]-'a';\r\n dp[node][colorindex] = 1;\r\n\r\n for(auto child:adj[node]){\r\n\r\n if(dfs(child,vis,pathvis,dp,adj,colors) == INT_MAX) return INT_MAX;\r\n\r\n for(int c=0;c<26;c++){\r\n dp[node][c] = max(dp[node][c],(c == colorindex)+dp[child][c]);\r\n }\r\n }\r\n pathvis[node]=0;\r\n int ans=0;\r\n for(int i=0;i<26;i++){\r\n ans = max(ans,dp[node][i]);\r\n }\r\n return ans;\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n \r\n int n=colors.size();\r\n vector<int> adj[n];\r\n\r\n vector<vector<int>> dp(n,vector<int>(26,0));\r\n\r\n for(auto edge:edges){\r\n adj[edge[0]].push_back(edge[1]);\r\n }\r\n\r\n vector<bool> vis(n,false);\r\n vector<bool> pathvis(n,false);\r\n\r\n int ans=0;\r\n for(int i=0;i<n;i++){\r\n \r\n ans = max(ans,dfs(i,vis,pathvis,dp,adj,colors));\r\n \r\n }\r\n return (ans == INT_MAX) ? -1:ans;\r\n }\r\n};", "memory": "235028" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\n void dfs(int curr, string &colors, vector<int> adjList[], vector<bool> &visited, vector<bool> &currPath, vector<vector<int>> &count, int &ans) {\r\n if(currPath[curr] || ans == -1) {\r\n ans = -1;\r\n return;\r\n }\r\n if(visited[curr]) return;\r\n visited[curr] = true;\r\n currPath[curr] = true;\r\n for(int next: adjList[curr]) {\r\n dfs(next, colors, adjList, visited, currPath, count, ans);\r\n if(ans == -1) return;\r\n for(int i = 0; i < 26; i++) {\r\n count[curr][i] = max(count[curr][i], count[next][i]);\r\n }\r\n }\r\n count[curr][colors[curr] - 'a']++;\r\n ans = max(ans, count[curr][colors[curr] - 'a']);\r\n currPath[curr] = false;\r\n }\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.length();\r\n vector<int> adjList[n];\r\n for(vector<int> edge: edges) {\r\n adjList[edge[0]].push_back(edge[1]);\r\n }\r\n int ans = INT_MIN;\r\n vector<bool> visited(n), currPath(n);\r\n vector<vector<int>> count(n, vector<int>(26));\r\n for(int i = 0; i < n; i++) {\r\n dfs(i, colors, adjList, visited, currPath, count, ans);\r\n }\r\n return ans;\r\n }\r\n};", "memory": "235028" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n void recurs(int p, vector<vector<int>>& adj, vector<int>& vis, vector<int>& track, int& ans, string& colors, vector<vector<int>>& store){\r\n if(vis[p]>0 || ans==-1) {\r\n ans = -1;\r\n return;\r\n }\r\n if(vis[p] == -1){\r\n for(int i=0; i<track.size(); i++){\r\n ans = max(ans, track[i] + store[p][i]);\r\n }\r\n return;\r\n }\r\n vis[p] = 1;\r\n track[int(colors[p]-'a')]++;\r\n\r\n if(ans < track[int(colors[p]-'a')]) ans = track[int(colors[p]-'a')];\r\n for(auto y: adj[p]){\r\n recurs(y, adj, vis, track, ans, colors, store);\r\n }\r\n\r\n // now compute the colors of curr node (thru its children)\r\n vector<int> temp(26, 0);\r\n \r\n for(auto y: adj[p]){\r\n for(int i=0; i<26; i++){\r\n temp[i] = max(temp[i], store[y][i]);\r\n }\r\n }\r\n temp[int(colors[p]-'a')]++;\r\n for(int i=0; i<26; i++){\r\n ans = max(ans, temp[i]);\r\n }\r\n // store[p].push_back(track);\r\n store[p] = temp;\r\n track[int(colors[p]-'a')]--;\r\n vis[p]=-1;\r\n }\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int ans = 0;\r\n int n = colors.size();\r\n vector<vector<int>> adj(n);\r\n vector<int> ind(n, 0);\r\n for(auto y: edges){\r\n adj[y[0]].push_back(y[1]);\r\n ind[y[1]]++;\r\n }\r\n vector<int> ind2 = ind;\r\n queue<int> q;\r\n for(int i=0; i<n; i++){\r\n if(ind2[i] == 0) {\r\n q.push(i);\r\n }\r\n }\r\n while(!q.empty()){\r\n int top = q.front();\r\n q.pop();\r\n for(auto y: adj[top]){\r\n ind2[y]--;\r\n if(ind2[y] == 0) q.push(y);\r\n }\r\n }\r\n for(auto y: ind2) if(y) return -1;\r\n\r\n vector<int> track(26, 0);\r\n vector<vector<int>> store(n);\r\n vector<int> vis(n, 0);\r\n for(int i=0; i<n; i++){\r\n if(ind[i] == 0) {\r\n recurs(i, adj, vis, track, ans, colors, store);\r\n }\r\n }\r\n return ans;\r\n }\r\n};", "memory": "236906" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\n #define pb push_back\r\n vector<int> g[100007];\r\n stack<int> st;\r\n void dfs(int vertex, vector<int>& vis){\r\n vis[vertex]=1;\r\n for(int child : g[vertex]){\r\n if( vis[child]==1) continue;\r\n dfs(child,vis);\r\n }\r\n st.push(vertex);\r\n }\r\n \r\n \r\n // int ans=0;\r\n // // map<pair<int,int>,int> val;\r\n // void func(int vertex, string& color , vector<int>& out_degree, vector<vector<int>>& val ){\r\n // int a=int(color[vertex])-97;\r\n // val[vertex][a] += 1;\r\n // for(int child : g[vertex]){\r\n // for(int i=0; i<26; i++) val[child][i] += val[vertex][i];\r\n // func(child,color, out_degree, val);\r\n // for(int i=0; i<26; i++) val[ child][i] -= val[vertex][i];\r\n\r\n // }\r\n\r\n // if( out_degree[vertex]==0){\r\n \r\n // for(int i=0; i<26; i++) ans= max(ans, val[vertex][i]);\r\n // // cout<<vertex<<\" \"<<ans<<\" \";\r\n // }\r\n\r\n // val[vertex][a] -=1;\r\n // return ;\r\n // }\r\n\r\npublic:\r\n int largestPathValue(string s, vector<vector<int>>& edges) {\r\n // check the cycle in the directed graph using the topological sort\r\n int n=s.size();\r\n vector<vector<int>> val(n+7, vector<int>(28,0));\r\n\r\n vector<int> indegree(n+7),out_degree(n+7);\r\n for( auto it : edges){\r\n g[it[0]].pb(it[1]);\r\n indegree[it[1]]++;\r\n out_degree[it[0]]++;\r\n }\r\n\r\n vector<int> vis(n+7,-1);\r\n for(int i=0; i<n; i++){\r\n if( vis[i]==-1) dfs(i,vis);\r\n }\r\n vector<int> tsort; int a=0; vector<int> pos(n+7,0);\r\n while( !st.empty()){ \r\n tsort.pb(st.top()); \r\n pos[st.top()]=a; a++;\r\n st.pop();\r\n }\r\n for(int i=0; i<n; i++){\r\n for(int child : g[i]){\r\n if( pos[i] >= pos[child]) return -1;\r\n }\r\n }\r\n \r\n \r\n vector<vector<int>>count(n,vector<int>(26,0));\r\n queue<int>q;\r\n for(int i=0;i<n;i++){\r\n if(indegree[i]==0){\r\n q.push(i);\r\n count[i][s[i]-'a']++; \r\n }\r\n }\r\n int ans=0;\r\n while(!q.empty()){\r\n auto p=q.front();\r\n q.pop();\r\n int r=*max_element(count[p].begin(),count[p].end());\r\n ans=max(ans,r);\r\n for(auto v: g[p]){\r\n for(int i=0;i<26;i++){\r\n count[v][i]=max(count[v][i],count[p][i]+(i==s[v]-'a'));\r\n }\r\n if(--indegree[v]==0){\r\n q.push(v);\r\n }\r\n }\r\n }\r\n return ans;\r\n }\r\n};", "memory": "248179" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.length();\r\n vector<vector<int>> freq(n, vector<int>(26, 0));\r\n done = vector<bool>(n, false);\r\n unordered_map<int, vector<int>> map;\r\n for (int i = 0; i < edges.size(); i++) {\r\n map[edges[i][0]].push_back(edges[i][1]);\r\n }\r\n unordered_map<int, bool> visited;\r\n for (auto itr = map.begin(); itr != map.end(); itr++) {\r\n int res = dfs(colors, visited, freq, map, itr->first);\r\n if (res == -1) {\r\n return -1;\r\n }\r\n ans = max(res, ans);\r\n }\r\n return ans;\r\n }\r\n\r\n int dfs(string &colors, unordered_map<int, bool> &visited, \r\n vector<vector<int>> &freq, unordered_map<int, vector<int>> &map, int curr) {\r\n if (ans == -1) {\r\n return -1;\r\n }\r\n if (visited.contains(curr) && visited[curr]) {\r\n ans = -1;\r\n return -1;\r\n }\r\n int c = colors[curr] - 'a';\r\n if (done[curr]) {\r\n return 0;\r\n }\r\n visited[curr] = true;\r\n done[curr] = true;\r\n int res = 0;\r\n if (!map.contains(curr)) {\r\n //skip the next\r\n } else {\r\n for (int node : map[curr]) {\r\n if (dfs(colors, visited, freq, map, node) == -1) {\r\n return -1;\r\n }\r\n for (int i = 0; i < 26; i++) {\r\n freq[curr][i] = max(freq[curr][i], freq[node][i]);\r\n res = max(res, freq[curr][i]);\r\n }\r\n }\r\n }\r\n visited[curr] = false;\r\n freq[curr][c] += 1;\r\n printf(\"w\\n\");\r\n return max(res, freq[curr][c]);\r\n }\r\nprivate:\r\n int ans = 1;\r\n vector<bool> done;\r\n};", "memory": "248179" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.length();\r\n vector<vector<int>> freq(n, vector<int>(26, 0));\r\n done = vector<bool>(n, false);\r\n unordered_map<int, vector<int>> map;\r\n for (int i = 0; i < edges.size(); i++) {\r\n map[edges[i][0]].push_back(edges[i][1]);\r\n }\r\n unordered_map<int, bool> visited;\r\n for (int i = 0; i < n; i++) {\r\n int res = dfs(colors, visited, freq, map, i);\r\n if (res == -1) {\r\n return -1;\r\n }\r\n ans = max(res, ans);\r\n }\r\n return ans;\r\n }\r\n\r\n int dfs(string &colors, unordered_map<int, bool> &visited, \r\n vector<vector<int>> &freq, unordered_map<int, vector<int>> &map, int curr) {\r\n if (ans == -1) {\r\n return -1;\r\n }\r\n if (visited.contains(curr) && visited[curr]) {\r\n ans = -1;\r\n return -1;\r\n }\r\n int c = colors[curr] - 'a';\r\n if (done[curr]) {\r\n return 0;\r\n }\r\n visited[curr] = true;\r\n done[curr] = true;\r\n int res = 0;\r\n for (int node : map[curr]) {\r\n if (dfs(colors, visited, freq, map, node) == -1) {\r\n return -1;\r\n }\r\n for (int i = 0; i < 26; i++) {\r\n freq[curr][i] = max(freq[curr][i], freq[node][i]);\r\n res = max(res, freq[curr][i]);\r\n }\r\n }\r\n visited[curr] = false;\r\n freq[curr][c] += 1;\r\n return max(res, freq[curr][c]);\r\n }\r\nprivate:\r\n int ans = 1;\r\n vector<bool> done;\r\n};", "memory": "250058" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n unordered_map<int,vector<int>> mp;\r\n int ans = 0;\r\n void dfs(int node,string& colors,vector<vector<int>>& adj,vector<int>& vis) {\r\n if(ans == -1) return; //cycle \r\n //checking wheather cycle or not\r\n if(mp[node].size()) {\r\n if(vis[node]) {\r\n ans = -1;\r\n }\r\n return;\r\n }\r\n vis[node] = 1;\r\n mp[node] = vector<int>(26,0); // storing the count of each char from the current node path\r\n for(auto it : adj[node]) {\r\n dfs(it,colors,adj,vis);\r\n if(ans == -1)return;\r\n for(int i = 0;i<26;i++) {\r\n mp[node][i] = max(mp[node][i],mp[it][i]);\r\n }\r\n }\r\n\r\n mp[node][colors[node]-'a']++;\r\n ans = max(ans,mp[node][colors[node]-'a']);\r\n vis[node] = 0;\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n vector<vector<int>> adj(n);\r\n vector<int> vis(n);\r\n for(auto e : edges) {\r\n adj[e[0]].push_back(e[1]);\r\n }\r\n for(int i = 0;i<colors.size();i++) {\r\n if(ans == -1) return ans;\r\n dfs(i,colors,adj,vis);\r\n }\r\n return ans;\r\n }\r\n};", "memory": "250058" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int maxi = 0;\r\n // vector<int>topoSort(int n,string&col,vector<int>&indeg,vector<int>adj[],map<int,vector<int>>&mp){\r\n // queue<int>q; // node,colorCount\r\n // vector<int>ans;\r\n // for(int i=0;i<n;i++){\r\n // if(indeg[i]==0){\r\n // ans.push_back(i);\r\n // mp[i][col[i]-'a']++;\r\n // q.push(i);\r\n // }\r\n // }\r\n\r\n // while(!q.empty()){\r\n // int node = q.front();\r\n // q.pop();\r\n // maxi = max(maxi,mp[node][col[node]-'a']);\r\n // for(auto it:adj[node]){\r\n // indeg[it]--;\r\n \r\n // for(int i=0;i<26;i++){\r\n // mp[it][i] = max(mp[it][i],mp[node][i]+ (col[it]-'a'==i));\r\n // }\r\n \r\n\r\n // if(indeg[it]==0){\r\n // ans.push_back(it);\r\n // q.push(it);\r\n // }\r\n // }\r\n // }\r\n\r\n // return ans;\r\n // }\r\n\r\n\r\n int largestPathValue(string col, vector<vector<int>>& edges) {\r\n int n = col.size();\r\n vector<int>adj[n];\r\n int ans =0;\r\n map<int,vector<int>>mp;\r\n for(int i=0;i<n;i++){\r\n vector<int>temp(26,0);\r\n mp[i] = temp;\r\n }\r\n \r\n vector<int>indeg(n,0);\r\n for(auto it:edges){\r\n adj[it[0]].push_back(it[1]);\r\n indeg[it[1]]++;\r\n }\r\n\r\n queue<int>q; // node,\r\n for(int i=0;i<n;i++){\r\n if(indeg[i]==0){\r\n mp[i][col[i]-'a']++;\r\n q.push(i);\r\n }\r\n }\r\n int count = 0;\r\n while(!q.empty()){\r\n int node = q.front();\r\n q.pop();\r\n count++;\r\n maxi = max(maxi,mp[node][col[node]-'a']);\r\n for(auto it:adj[node]){\r\n indeg[it]--;\r\n for(int i=0;i<26;i++){\r\n mp[it][i] = max(mp[it][i],mp[node][i]+ (col[it]-'a'==i));\r\n }\r\n if(indeg[it]==0){\r\n q.push(it);\r\n }\r\n }\r\n }\r\n\r\n if(count != n){\r\n return -1;\r\n }\r\n \r\n\r\n return maxi;\r\n }\r\n};", "memory": "251936" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.length();\r\n vector<vector<int>> adj(n);\r\n\r\n vector<int> inDegree(n, 0);\r\n for(auto edge: edges){\r\n adj[edge[0]].push_back(edge[1]);\r\n inDegree[edge[1]]++;\r\n }\r\n\r\n queue<int> q;\r\n\r\n for(int i=0; i<n; i++){\r\n if(inDegree[i] == 0)q.push(i);\r\n }\r\n\r\n int processed = 0;\r\n int maxYet = 0;\r\n vector<vector<int>> col(n, vector<int>(26, 0));\r\n\r\n while(!q.empty()){\r\n int qSize = q.size();\r\n\r\n while(qSize--){\r\n int curr = q.front();\r\n q.pop();\r\n processed++;\r\n vector<int> currCount = col[curr];\r\n currCount[colors[curr] - 'a']++;\r\n\r\n for(int i=0; i<26; i++){\r\n maxYet = max(maxYet, currCount[i]);\r\n }\r\n\r\n for(auto nei: adj[curr]){\r\n inDegree[nei]--;\r\n vector<int> newCurrCount = col[nei];\r\n for(int i=0; i<26; i++){\r\n newCurrCount[i] = max(currCount[i], newCurrCount[i]);\r\n }\r\n col[nei] = newCurrCount;\r\n\r\n if(inDegree[nei] == 0)q.push(nei);\r\n }\r\n }\r\n }\r\n \r\n \r\n return processed == n ? maxYet : -1;\r\n }\r\n};", "memory": "251936" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "\r\nclass Solution {\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n\r\n vector<int> ind(colors.size(), 0);\r\n vector<vector<int>> gr(colors.size());\r\n for (int i = 0; i < edges.size(); i++) {\r\n gr[edges[i][0]].push_back(edges[i][1]);\r\n ind[edges[i][1]]++;\r\n }\r\n queue<int> qu;\r\n vector<vector<int>> dp(colors.size(), vector<int>(26, 0));\r\n for (int i = 0; i < ind.size(); i++) {\r\n if (ind[i] == 0) {\r\n dp[i][colors[i] - 'a']++;\r\n qu.push(i);\r\n // cout<<i<<\"added\"<<endl;\r\n }\r\n }\r\n int ans = 1;\r\n int vertice = 0;\r\n while (qu.size() > 0) {\r\n int v = qu.front();\r\n vertice++;\r\n qu.pop();\r\n // cout<<v<<endl;\r\n for (int i = 0; i < gr[v].size(); i++) {\r\n ind[gr[v][i]]--;\r\n if (ind[gr[v][i]] == 0) {\r\n qu.push(gr[v][i]);\r\n }\r\n vector<int> tmp = dp[v];\r\n vector<int> tmp1 = dp[gr[v][i]];\r\n\r\n for (int j = 0; j < 26; j++) {\r\n if (j == (colors[gr[v][i]] - 'a')) {\r\n tmp1[j] = max(tmp1[j], 1 + tmp[j]);\r\n } else {\r\n tmp1[j] = max(tmp1[j], tmp[j]);\r\n }\r\n ans = max(ans, tmp1[j]);\r\n }\r\n dp[gr[v][i]] = tmp1;\r\n }\r\n }\r\n if (vertice < colors.size())\r\n return -1;\r\n\r\n return ans;\r\n }\r\n};", "memory": "253815" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\n\r\npublic:\r\n\r\n vector<vector<int>> nodecolor;\r\n set<int> visited;\r\n set<int> path;\r\n vector<vector<int>> adjList;\r\n\r\n string c;\r\n \r\n int dfs(int node) {\r\n if (path.count(node)) {\r\n return INT_MAX;\r\n } \r\n if (visited.count(node)) {\r\n return 0;\r\n }\r\n visited.insert(node);\r\n path.insert(node);\r\n nodecolor[node][c[node] - 'a'] = 1;\r\n\r\n for (auto ne: adjList[node]) {\r\n if (dfs(ne) == INT_MAX) {\r\n return INT_MAX;\r\n }\r\n for (int i = 0; i< 26;i++) {\r\n nodecolor[node][i] = max(nodecolor[node][i], (i == c[node] - 'a')? 1 +nodecolor[ne][i] :nodecolor[ne][i]);\r\n }\r\n\r\n }\r\n path.erase(node);\r\n return *max_element(nodecolor[node].begin(), nodecolor[node].end());\r\n\r\n }\r\n\r\n // 4->1->3->0->2 \r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n c = colors;\r\n adjList.resize(n, vector<int>());\r\n\r\n nodecolor.resize(n, vector<int>(26,0));\r\n for (auto edge: edges) {\r\n adjList[edge[0]].push_back(edge[1]);\r\n }\r\n int l = 0;\r\n for (int i = 0; i< colors.size(); i++) {\r\n l = max(l,dfs(i));\r\n }\r\n return l == INT_MAX ? -1 : l;\r\n\r\n \r\n }\r\n};", "memory": "255694" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\n\r\npublic:\r\n\r\n vector<vector<int>> nodecolor;\r\n set<int> visited;\r\n set<int> path;\r\n vector<vector<int>> adjList;\r\n\r\n string c;\r\n \r\n int dfs(int node) {\r\n if (path.count(node)) {\r\n return INT_MAX;\r\n } \r\n if (visited.count(node)) {\r\n return 0;\r\n }\r\n visited.insert(node);\r\n path.insert(node);\r\n nodecolor[node][c[node] - 'a'] = 1;\r\n\r\n for (auto ne: adjList[node]) {\r\n if (dfs(ne) == INT_MAX) {\r\n return INT_MAX;\r\n }\r\n for (int i = 0; i< 26;i++) {\r\n nodecolor[node][i] = max(nodecolor[node][i], (i == c[node] - 'a')? 1 +nodecolor[ne][i] :nodecolor[ne][i]);\r\n }\r\n\r\n }\r\n path.erase(node);\r\n return *max_element(nodecolor[node].begin(), nodecolor[node].end());\r\n\r\n }\r\n\r\n // 4->1->3->0->2 \r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n c = colors;\r\n adjList.resize(n, vector<int>());\r\n nodecolor.resize(n, vector<int>(26,0));\r\n for (auto edge: edges) {\r\n adjList[edge[0]].push_back(edge[1]);\r\n }\r\n int l = 0;\r\n for (int i = 0; i< n; i++) {\r\n l = max(l,dfs(i));\r\n }\r\n return l == INT_MAX ? -1 : l;\r\n\r\n \r\n }\r\n};", "memory": "255694" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n vector<int> find(vector<vector<int>> &g, int node, vector<bool> &vis, string &colors, vector<vector<int>> &dp){\r\n vector<int> res(26,0);\r\n for(int i =0;i<g[node].size();i++){\r\n int child = g[node][i];\r\n if(!vis[child]){\r\n vis[child]=1;\r\n vector<int> temp = find(g, child, vis, colors,dp);\r\n for(int i =0;i<26;i++){\r\n res[i] = max(res[i], temp[i]);\r\n }\r\n }else{\r\n for(int i =0;i<26;i++){\r\n res[i] = max(res[i], dp[child][i]);\r\n } \r\n }\r\n }\r\n res[colors[node]-'a']++;\r\n return dp[node] = res;\r\n }\r\n bool dfs(vector<vector<int>> &g, int node, vector<bool> &vis,vector<bool> &dfs_vis){\r\n for(int i=0;i<g[node].size();i++){\r\n int child = g[node][i];\r\n if(!vis[child]){\r\n vis[child]=1;\r\n dfs_vis[child]=1; \r\n if(dfs(g,child, vis, dfs_vis)) return 1;\r\n dfs_vis[child]=0;\r\n }else if(dfs_vis[child]){\r\n return 1;\r\n }\r\n }\r\n return 0;\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n vector<vector<int>> g(n);\r\n for(int i=0;i<edges.size();i++){\r\n g[edges[i][0]].push_back(edges[i][1]);\r\n }\r\n \r\n vector<int> vis_color(26,0);\r\n vector<bool> vis(n,0), dfs_vis(n,0);\r\n int ans = -1;\r\n for(int i=0;i<n;i++){\r\n if(!vis[i]){\r\n vis[i]=1;\r\n dfs_vis[i]=1;\r\n if(dfs(g, i, vis, dfs_vis)) return -1;\r\n dfs_vis[i]=0;\r\n }\r\n }\r\n vector<vector<int>> dp(n, vector<int>(26,0));\r\n vis.assign(n,0);\r\n for(int i=0;i<n;i++){\r\n if(!vis[i]){\r\n vis[i]=1;\r\n vector<int> v = find(g, i, vis, colors,dp);\r\n for(int i =0;i<26;i++){\r\n ans = max(ans , v[i]);\r\n }\r\n }\r\n }\r\n return ans;\r\n }\r\n};", "memory": "257573" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "/*\n https://leetcode.com/problems/largest-color-value-in-a-directed-graph/\n \n Core idea is to first process the nodes that come first in terms of dependency order.\n This ensures that when we process a node, all the paths coming to that node have been processed and\n the node will be processed at the end.\n \n Since we want to explore each path, naive DFS for each path will give TLE.\n We need to save the path color information. To do so, we can use a vector of size 26 for each node.\n This keeps track of longest length path with a given color appearing most frequently.\n \n SOLUTION 1: TOPOLOGICAL SORT (Kahn's Algo)\n In Kahn's algo, each time we are reducing the indegree, we are basically concluding the path traversed\n till that node. So we can do an extra step here, we also give that node the info of max color paths ending there.\n Keep passing on the path color info to each level. Also we use a var to keep track of max value seen so far.\n \n SOLUTION 2: DFS + DP\n This is also topological sort but with DFS. Just like topological sort with DFS, we first explore the neighboring nodes.\n Now here we save the path color info for each node if the traversal starts from there.\n So when a neighbor is done processing, just check the color path lengths that are possible from that neighbor\n and update the info of current node accordingly. Again a var is used to keep track of max value seen.\n*/\nclass Solution {\npublic:\n /////////////////////////////////////////////////////////////////////// SOLUTION 1: TOPOLOGICAL SORT\n // TC: O(V + E)\n // SC: O(V + E)\n int topologicalSol(vector<unordered_set<int> >& graph, string& colors) {\n int n = graph.size(), processed = 0;\n queue<int> q;\n // indegree[i] = No. of incoming edges to node i\n vector<int> indegree(n, 0);\n // color_path_len[i][c] = For each color c,\n // stores the max no. of times c has been seen in a path ending here at node i \n vector<vector<int> > color_path_len(n, vector<int>(26, 0));\n // Overall longest same colored nodes in a path\n int max_color = -1;\n \n // Find the indegree for each node\n for(int node = 0; node < n; node++)\n for(auto neighbor: graph[node])\n ++indegree[neighbor];\n \n // Add all the 0 indegree nodes\n for(int node = 0; node < n; node++)\n if(indegree[node] == 0)\n q.emplace(node);\n \n while(!q.empty()) {\n int node = q.front();\n q.pop();\n ++processed;\n \n // Update the max color path info for the current node\n max_color = max(max_color, ++color_path_len[node][colors[node] - 'a']);\n \n // Remove edge from each neighbor\n for(auto neighbor: graph[node]) {\n --indegree[neighbor];\n if(indegree[neighbor] == 0)\n q.emplace(neighbor);\n \n // Update the color path info for the neighbor\n for(int color = 0; color < 26; color++) {\n color_path_len[neighbor][color] = max(color_path_len[neighbor][color],\n color_path_len[node][color]);\n }\n }\n }\n \n return processed < n ? -1 : max_color;\n }\n \n\t/////////////////////////////////////////////////////////////////////////// SOLUTION 2: DFS + DP\n // TC: O(V + E)\n // SC: O(V + E)\n bool dfs(int node, vector<unordered_set<int> >& graph, string& colors, \n vector<int>& visited, vector<vector<int> >& color_path_len, int& max_color) {\n // Node is currently under processing and we are again seeing it, means a cycle\n if(visited[node] == 1)\n return false;\n \n // Node is unvisited\n if(visited[node] == 0) {\n // Mark node as processing\n visited[node] = 1;\n \n // Explore the neighbors first\n for(auto neighbor: graph[node]) {\n if(!dfs(neighbor, graph, colors, visited, color_path_len, max_color))\n return false;\n // Since all the paths starting this neighbors have been explored, get the\n // max color path info\n for(int color = 0; color < 26; color++) {\n color_path_len[node][color] = max(color_path_len[node][color],\n color_path_len[neighbor][color]);\n max_color = max(max_color, color_path_len[node][color]);\n }\n }\n // Update the color info for current node\n max_color = max(max_color, ++color_path_len[node][colors[node] - 'a']);\n \n // Mark as visited\n visited[node] = 2;\n }\n return true;\n }\n \n int dfsDpSol(vector<unordered_set<int> >& graph, string& colors) {\n int max_color = -1, n = graph.size();\n // visited[i] = {0 (Unvisited), 1 (Processing), 2(Visited)}\n vector<int> visited(n, 0);\n // color_path_len[i][c] = For each color c,\n // stores the max no. of times c has been seen in a path start from here at node i \n vector<vector<int> > color_path_len(n, vector<int>(26, 0));\n \n for(int node = 0; node < n; node++)\n if(!dfs(node, graph, colors, visited, color_path_len, max_color))\n return -1;\n return max_color;\n }\n \n int largestPathValue(string colors, vector<vector<int>>& edges) {\n // <node, [neighbors] >\n vector<unordered_set<int> > graph(colors.size());\n \n // create the graph\n for(auto edge: edges) \n graph[edge[0]].emplace(edge[1]);\n \n // return topologicalSol(graph, colors);\n return dfsDpSol(graph, colors);\n }\n};", "memory": "257573" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\n bool chck(int i, auto &adj, auto &vst1, auto &vst2){\r\n vst1[i]=1;\r\n vst2[i]=1;\r\n int ans=0;\r\n for(auto it:adj[i]){\r\n if(vst1[it]){\r\n if(vst2[it])return 1;\r\n }else{\r\n ans=ans|chck(it,adj,vst1,vst2);\r\n }\r\n }\r\n vst2[i]=0;\r\n return ans;\r\n }\r\n\r\n\r\n unordered_map<int,vector<int>> mp;\r\n vector<int> solve(int i, auto &adj, string &colors){\r\n if(mp.count(i))return mp[i];\r\n vector<int> clr(26,0);\r\n for(auto it:adj[i]){\r\n vector<int> tmp=solve(it,adj,colors);\r\n for(int k=0; k<26; k++)clr[k]=max(clr[k],tmp[k]);\r\n }\r\n clr[colors[i]-'a']++;\r\n return mp[i]=clr;\r\n }\r\npublic:\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n=colors.size();\r\n vector<vector<int>> adj(n);\r\n vector<int> vst1(n,0), vst2(n,0);\r\n for(auto it:edges)\r\n adj[it[0]].push_back(it[1]);\r\n\r\n for(int i=0; i<n; i++){\r\n if(!vst1[i] && chck(i,adj,vst1,vst2))return -1;\r\n }\r\n mp.clear();\r\n int ans=0;\r\n for(int i=0; i<n; i++){\r\n if(!mp.count(i)){\r\n solve(i,adj,colors);\r\n for(auto it:mp[i])ans=max(ans,it);\r\n }\r\n }\r\n return ans;\r\n }\r\n};", "memory": "259451" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n\r\n int dfs(int node, vector<set<int>> &graph, string &colors, int m, unordered_set<int> &visited, unordered_set<int> &path, vector<vector<int>> &colorMap){\r\n if(path.find(node) != path.end())\r\n return -1;\r\n\r\n if(visited.find(node) != visited.end()){\r\n return 0;\r\n }\r\n\r\n path.insert(node);\r\n visited.insert(node);\r\n\r\n int colorIndex = colors[node] - 'a';\r\n colorMap[node][colorIndex] = 1;\r\n\r\n for (auto &neigh : graph[node]) {\r\n int result = dfs(neigh, graph, colors, m, visited, path, colorMap);\r\n if (result == -1)\r\n return -1;\r\n for (int i = 0; i < 26; i++) {\r\n colorMap[node][i] = max(colorMap[node][i], (i == colorIndex ? 1 : 0) + colorMap[neigh][i]);\r\n }\r\n }\r\n\r\n int res = 0;\r\n for (int i = 0; i < 26; i++) {\r\n res = max(res, colorMap[node][i]);\r\n }\r\n \r\n path.erase(path.find(node));\r\n return res;\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n \r\n int m = colors.size();\r\n vector<set<int>> graph(m);\r\n\r\n for(auto &edge : edges){\r\n graph[edge[0]].insert(edge[1]);\r\n }\r\n\r\n vector<vector<int>> colorMap(m, vector<int>(26, 0));\r\n\r\n unordered_set<int> visited;\r\n unordered_set<int> path;\r\n\r\n int res = 0;\r\n\r\n for(int i = 0; i < m; i ++){\r\n int result = dfs(i, graph, colors, m, visited, path, colorMap);\r\n if(result == -1)\r\n return -1;\r\n res = max(res, result);\r\n }\r\n return res;\r\n }\r\n};", "memory": "261330" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int dfs(int node, vector<set<int>> &graph, string &colors, unordered_set<int> &visited, unordered_set<int> &path, vector<vector<int>> &colorMap) {\r\n if (path.find(node) != path.end())\r\n return -1;\r\n\r\n if (visited.find(node) != visited.end())\r\n return 0;\r\n\r\n path.insert(node);\r\n visited.insert(node);\r\n\r\n int colorIndex = colors[node] - 'a';\r\n int maxColorValue = 1;\r\n\r\n for (auto &neigh : graph[node]) {\r\n int result = dfs(neigh, graph, colors, visited, path, colorMap);\r\n if (result == -1)\r\n return -1;\r\n for (int i = 0; i < 26; i++) {\r\n colorMap[node][i] = max(colorMap[node][i], colorMap[neigh][i]);\r\n }\r\n }\r\n\r\n colorMap[node][colorIndex]++;\r\n for (int i = 0; i < 26; i++) {\r\n maxColorValue = max(maxColorValue, colorMap[node][i]);\r\n }\r\n\r\n path.erase(node);\r\n return maxColorValue;\r\n }\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int m = colors.size();\r\n vector<set<int>> graph(m);\r\n\r\n for (auto &edge : edges) {\r\n graph[edge[0]].insert(edge[1]);\r\n }\r\n\r\n vector<vector<int>> colorMap(m, vector<int>(26, 0));\r\n unordered_set<int> visited;\r\n unordered_set<int> path;\r\n\r\n int res = 0;\r\n\r\n for (int i = 0; i < m; i++) {\r\n if (visited.find(i) == visited.end()) {\r\n int result = dfs(i, graph, colors, visited, path, colorMap);\r\n if (result == -1)\r\n return -1;\r\n res = max(res, result);\r\n }\r\n }\r\n\r\n return res;\r\n }\r\n};\r\n", "memory": "261330" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n void dfs(int node, vector<int>graph[],vector<int>&vis,vector<vector<int>>&dp,string &colors, bool &val, int &ans){\r\n \r\n if(dp[node][0] != -1)return ;\r\n\r\n vector<int>v(26,0);\r\n\r\n for(int i =0 ; i<26 ; i++){\r\n if(i == (colors[node]-'a')){\r\n dp[node][i] = 1;\r\n }\r\n else{\r\n dp[node][i] = 0;\r\n }\r\n }\r\n\r\n vis[node] = 1;\r\n \r\n for(auto ch : graph[node]){\r\n if(vis[ch] == 0)dfs(ch,graph,vis,dp,colors,val,ans);\r\n else if(vis[ch] == 1){ val = false; return; }\r\n for(int i =0 ; i<26 ; i++){\r\n v[i] = max(v[i],dp[ch][i]); \r\n }\r\n }\r\n for(int i =0 ; i<26 ; i++){\r\n dp[node][i] = v[i] + ((colors[node]-'a') == i);\r\n }\r\n \r\n vis[node] = 2;\r\n return ;\r\n\r\n }\r\n\r\n// bool check_cycle(vector<int>graph[],int &n,vector<int>&indegree){\r\n// vector<int>topo;\r\n// queue<int>q;\r\n// for(int i =0 ;i<n ;i++){\r\n// if(indegree[i] == 0){\r\n// q.push(i);\r\n// }\r\n// }\r\n\r\n// while(!q.empty()){\r\n// int x = q.front();\r\n// topo.push_back(x);\r\n// for(auto ch : graph[x]){\r\n// indegree[ch]--;\r\n// if(indegree[ch] == 0){\r\n// q.push(ch);\r\n// }\r\n// }\r\n// }\r\n \r\n// if(topo.size()!= n){\r\n// return false;\r\n// }\r\n// return true ;\r\n\r\n// }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n vector<int>graph[n];\r\n vector<int>indegree(n,0);\r\n for(int i =0 ; i<edges.size(); i++){\r\n int u = edges[i][0];\r\n int v = edges[i][1];\r\n indegree[v]++;\r\n graph[u].push_back(v);\r\n }\r\n \r\n bool val=true ;\r\n vector<int>vis(n,0);\r\n vector<vector<int>>dp(n,vector<int>(26,-1));\r\n int res = 0 ;\r\n for(int i =0 ; i<n ; i++){\r\n if(vis[i] == 0)dfs(i,graph,vis,dp,colors,val,res);\r\n if(val == false)return -1 ;\r\n }\r\n \r\n // for(int i =0 ;i<26; i++){\r\n // cout<<dp[2][i]<<\" \";\r\n // }\r\n // cout<<endl;\r\n\r\n int ans = 0 ;\r\n for(int i = 0 ; i<n ; i++){\r\n for(int j =0 ; j<26 ; j++){\r\n ans = max(ans,dp[i][j]);\r\n }\r\n }\r\n return ans ;\r\n \r\n }\r\n};", "memory": "263209" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\n\r\npublic:\r\n\r\n map<int, vector<int>> mp;\r\n\r\n map<int, vector<int>> adj;\r\n map<int,int> vis;\r\n\r\n string s;\r\n\r\n vector<int> visited;\r\n\r\n void dfs(int node) {\r\n vis[node] = 1;\r\n\r\n vector<int> v(26, 0);\r\n\r\n for (auto child : adj[node]) {\r\n if(!vis[child]) dfs(child);\r\n\r\n for (int j = 0; j < 26; j++) {\r\n\r\n v[j] = max(v[j], mp[child][j]);\r\n\r\n }\r\n\r\n }\r\n\r\n v[s[node] - 'a']++;\r\n\r\n mp[node] = v;\r\n\r\n }\r\n\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n\r\n s = colors;\r\n\r\n int n = colors.size();\r\n\r\n visited.resize(n, 0);\r\n\r\n vector<int> indegree(n, 0);\r\n\r\n for (auto& it : edges) {\r\n\r\n int u = it[0], v = it[1];\r\n\r\n adj[u].push_back(v);\r\n\r\n indegree[v]++;\r\n\r\n }\r\n\r\n\r\n\r\n int maxColorValue = -1e9;\r\n\r\n bool hasCycle = false;\r\n\r\n queue <int> q;\r\n\r\n for (int i = 0; i < n; i++) {\r\n\r\n if (indegree[i] == 0) {\r\n\r\n q.push(i);\r\n\r\n } \r\n\r\n }\r\n\r\n vector <int> ans;\r\n while(!q.empty()) {\r\n int node = q.front();\r\n q.pop();\r\n\r\n ans.push_back(node);\r\n\r\n for(auto child : adj[node]) {\r\n indegree[child]--;\r\n if(indegree[child] == 0) q.push(child);\r\n }\r\n }\r\n\r\n if(ans.size() != n) return -1;\r\n\r\n for(int i = 0 ; i < n ; i++) {\r\n if(indegree[i] == 0) dfs(i);\r\n }\r\n\r\n for (auto& it : mp) {\r\n\r\n for (int i = 0; i < 26; i++) {\r\n\r\n maxColorValue = max(maxColorValue, it.second[i]);\r\n\r\n }\r\n\r\n }\r\n\r\n return maxColorValue;\r\n\r\n }\r\n\r\n};\r\n\r\n", "memory": "263209" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n vector<vector<int>> dp;\r\n bool dfs(vector<vector<int>> &adj,int ind,vector<bool> &curr,string &s){\r\n\r\n if(dp[ind][0]!=-1){\r\n\r\n return (curr[ind]);\r\n\r\n }\r\n\r\n curr[ind] = true;\r\n\r\n dp[ind]= vector<int> (26,0);\r\n\r\n for(auto &val:adj[ind]){\r\n bool F = dfs(adj,val,curr,s);\r\n if(F){\r\n return true;\r\n }\r\n for(int c = 0; c<26; c++){\r\n dp[ind][c] = max(dp[ind][c],dp[val][c]);\r\n }\r\n }\r\n\r\n dp[ind][s[ind]-'a']++;\r\n\r\n curr[ind] = false;\r\n\r\n\r\n return false;\r\n \r\n\r\n\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n\r\n int n = colors.size();\r\n dp = vector<vector<int>> (n,vector<int> (26,-1));\r\n\r\n vector<vector<int>> adj(n);\r\n\r\n for(auto &val:edges){\r\n adj[val[0]].push_back(val[1]);\r\n }\r\n\r\n int ans = 0;\r\n\r\n vector<bool> curr(n,false);\r\n\r\n for(int i = 0;i<n;i++){\r\n bool F = dfs(adj,i,curr,colors);\r\n if(F) return -1;\r\n ans = max(ans,*max_element(dp[i].begin(),dp[i].end()));\r\n }\r\n\r\n return ans;\r\n \r\n }\r\n};", "memory": "265088" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n vector<int>dfs(int node,vector<int>adj[],string &colors,int &ans,vector<vector<int> >&dp,vector<int>&vis){\r\n if(vis[node]==1){\r\n return dp[node];\r\n\r\n \r\n }\r\n vector<int>store(26,0);\r\n vis[node]=1;\r\n \r\n \r\n \r\n for(auto i:adj[node]){\r\n \r\n \r\n vector<int>temp=dfs(i,adj,colors,ans,dp,vis);\r\n for(int k=0;k<temp.size();k++){\r\n store[k]=max(store[k],temp[k]);\r\n }\r\n \r\n\r\n }\r\n store[colors[node]-'a']++;\r\n \r\n ans=max(ans,store[colors[node]-'a']);\r\n dp[node]=store;\r\n \r\n return dp[node];\r\n }\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n vector<int>indegree(colors.size());\r\n vector<int>adj[colors.size()];\r\n for(int i=0;i<edges.size();i++){\r\n indegree[edges[i][1]]++;\r\n adj[edges[i][0]].push_back(edges[i][1]);\r\n\r\n }\r\n \r\n int count=0;\r\n queue<int>q;\r\n int temp;\r\n vector<int>indegree2=indegree;\r\n for(int i=0;i<indegree.size();i++){\r\n if(indegree[i]==0){\r\n \r\n q.push(i);\r\n \r\n }\r\n }\r\n int ans=0;\r\n while(!q.empty()){\r\n int a=q.front();\r\n q.pop();\r\n count++;\r\n \r\n for(auto i:adj[a]){\r\n \r\n indegree[i]--;\r\n if(indegree[i]==0){\r\n \r\n q.push(i);\r\n\r\n }\r\n \r\n }\r\n \r\n }\r\n if(count!=colors.size()){\r\n return -1;\r\n }\r\n vector<vector<int> >dp(colors.size());\r\n vector<int>vis(colors.size(),0);\r\n for(int i=0;i<indegree2.size();i++){\r\n if(indegree2[i]==0){\r\n dfs(i,adj,colors,ans,dp,vis);\r\n\r\n }\r\n }\r\n \r\n \r\n \r\n \r\n return ans;\r\n }\r\n};", "memory": "266966" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n unordered_map<int, vector<int>>dp;\n bool isCycle(vector<int> graph[], int src, vector<int>&vis, vector<int>&dfsVis) {\n vis[src] = dfsVis[src] = 1;\n for(auto &adj : graph[src]) {\n if(!vis[adj]) {\n if(isCycle(graph, adj, vis, dfsVis)) return true;\n } else if(dfsVis[adj]) {\n return true;\n }\n }\n dfsVis[src] = 0;\n return false;\n }\n \n vector<int> dfs(vector<int> graph[], string &color, int src) {\n if(dp.find(src) != dp.end()) return dp[src];\n vector<int> freq(26, 0);\n for(auto &adj : graph[src]) {\n vector<int> res = dfs(graph, color, adj);\n for(int i = 0; i < 26; ++i) {\n freq[i] = max(res[i], freq[i]);\n } \n }\n freq[color[src] - 'a']++;\n return dp[src] = freq;\n }\n \n int largestPathValue(string colors, vector<vector<int>>& edges) {\n int n = colors.size();\n vector<int> graph[n];\n for(auto &e : edges) graph[e[0]].push_back(e[1]);\n vector<int> vis(n, 0), dfsVis(n, 0);\n for(int i = 0; i < n; ++i) {\n if(isCycle(graph, i, vis, dfsVis)) return -1;\n }\n \n for(int i = 0; i < n; ++i) vis[i] = 0;\n int ans = 0;\n for(int i = 0; i < n; ++i) {\n vector<int> res = dfs(graph, colors, i);\n for(int j = 0; j < 26; ++j) {\n ans = max(ans, res[j]);\n }\n }\n \n return ans;\n }\n};", "memory": "268845" }
1,986
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p> <p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p> <p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p> <pre> <strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p> <pre> <strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is a cycle from 0 to 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <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> consists of lowercase English letters.</li> <li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n map<int,vector<int>> dp;\r\n vector<int> dfs(int src,auto &adj,auto &st){\r\n vector<int> ans(26,0);\r\n\r\n if(dp.count(src) > 0)return dp[src];\r\n\r\n for(auto i:adj[src]){\r\n auto vv = dfs(i,adj,st);\r\n\r\n for(int k = 0;k < 26;k++){\r\n ans[k] = max(ans[k],vv[k]);\r\n }\r\n }\r\n // cout<<src<<\" -> \";\r\n // for(auto i:ans)cout<<i<<\" \";\r\n // cout<<endl;\r\n ans[st[src] - 'a']++;\r\n return dp[src] = ans;\r\n }\r\n\r\n // int bfs(int src,auto &adj,auto &vis,auto &st){\r\n // queue<pair<int,vector<int>>> q;\r\n // vector<int> colors(26,0);\r\n // colors[st[src] - 'a']++;\r\n // q.push({src,colors});\r\n\r\n // int maxi = 0;\r\n\r\n // while(!q.empty()){\r\n // auto fnode = q.front();\r\n // q.pop();\r\n\r\n // int node = fnode.first;\r\n // auto vec = fnode.second;\r\n\r\n // if(adj[node].size() == 0){\r\n // maxi = max(maxi,*max_element(vec.begin(),vec.end()));\r\n // }\r\n\r\n // // if(vis[node])continue;\r\n // // vis[node] = 1;\r\n\r\n // for(auto i:adj[node]){\r\n // int col = st[i] - 'a';\r\n // vec[col]++;\r\n // q.push({i,vec});\r\n // vec[col]--;\r\n // }\r\n // }\r\n // return maxi;\r\n // }\r\n\r\n bool isCycle(int src,auto &adj,auto &pathvis,auto &vis){\r\n vis[src] = 1;\r\n pathvis[src] = 1;\r\n\r\n bool ans = 0;\r\n\r\n for(auto i:adj[src]){\r\n if(!vis[i]){\r\n ans |= isCycle(i,adj,pathvis,vis);\r\n }\r\n else{\r\n if(pathvis[i])return 1;\r\n }\r\n }\r\n pathvis[src] = 0;\r\n return ans;\r\n }\r\n\r\n int largestPathValue(string colors, vector<vector<int>>& edges) {\r\n int n = colors.size();\r\n\r\n vector<vector<int>> adj(n);\r\n vector<int> indegree(n,0);\r\n\r\n for(auto i:edges){\r\n adj[i[0]].push_back(i[1]);\r\n indegree[i[1]]++;\r\n }\r\n\r\n vector<int> vis(n+1,0);\r\n vector<int> pathvis(n+1,0);\r\n\r\n for(int i = 0;i < n;i++){\r\n if(!vis[i]){\r\n if(isCycle(i,adj,pathvis,vis))return -1;\r\n }\r\n }\r\n\r\n int ans = 0;\r\n\r\n for(int i = 0;i < n;i++){\r\n if(!dp.count(i)){\r\n auto vec = dfs(i,adj,colors);\r\n ans = max(ans,*max_element(vec.begin(),vec.end()));\r\n }\r\n }\r\n\r\n return ans;\r\n\r\n }\r\n};", "memory": "270724" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n int n = nums.size();\n int t = (1<<n);\n int xt = 0;\n for(int i = 0;i<t;++i) {\n int x = 0;\n int j = i;\n int k = n-1;\n while(j){\n x ^= (j&1)*nums[k];\n j = j >> 1;\n k--;\n }\n xt += x;\n }\n return xt;\n }\n};", "memory": "8413" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n int n = nums.size();\n int res = 0;\n for(int i = 0 ; i< (1ll << n); i++)\n {\n int curr_res = 0;\n for(int j = 0;j<n;j++)\n {\n if( (i>> j)&1 )\n {\n curr_res^=nums[j];\n }\n }\n res+=curr_res;\n }\n return res;\n }\n};", "memory": "8413" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n vector<int> temp(nums.size());\n int jack = pow(2, nums.size());\n\n for (int i=0; i < nums.size(); i++)\n {\n temp[i] = (1 << i); //each number gets a bit assigned to it.\n cout << \"value = \" << temp[i] << \"\\n\";\n }\n \n \n int totalXOR = 0;\n int runningXOR = 0;\n for (int x =0; x < jack; x++)\n {\n runningXOR = 0;\n for (int i=0; i < nums.size(); i++)\n {\n if(temp[i] & x)\n {\n runningXOR ^= nums[i];\n }\n }\n totalXOR += runningXOR;\n }\n\nreturn totalXOR;\n \n }\n};", "memory": "8841" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n int all_subset_xor_sum{};\n subsetXORSum(nums, 0, nums.size(), all_subset_xor_sum);\n\n return all_subset_xor_sum;\n }\nprivate:\n void subsetXORSum(vector<int>& nums, int i, int sz, int &all_subset_xor_sum) {\n if(i == sz) {\n int current_xor_sum{}; \n for(auto& val : subset) {\n current_xor_sum ^= val;\n }\n all_subset_xor_sum += current_xor_sum;\n return;\n } else {\n subsetXORSum(nums, i+1, sz, all_subset_xor_sum);\n subset.push_back(nums[i]);\n subsetXORSum(nums, i+1, sz, all_subset_xor_sum);\n subset.pop_back();\n }\n }\nprivate:\n vector<int> subset;\n};", "memory": "8841" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n vector<int> sums(pow(2, nums.size()), 0);\n for (int i = 0, p = sums.size() >> 1; i < nums.size(); ++i, p >>= 1) {\n int k = 0;\n while (k < sums.size()) {\n for (int j = 0; j < p; ++j, ++k) {\n sums[k] ^= nums[i];\n }\n k += p;\n }\n }\n int r = 0;\n for (int i : sums) {\n r += i;\n }\n return r;\n/*\n\n001 1\n011 3\n110 6\n\n(1 ^ 3)\n001\n011 ^\n010 (2)\n\n(1 ^ 6)\n001\n110 ^\n111 (7)\n\n(3 ^ 6)\n011\n110 ^\n101 (5)\n\n(1 ^ 3 ^ 6)\n010\n110 ^\n100 (4)\n\n(sum of all xors)\n1 + 3 + 6 + 2 + 7 + 5 + 4 = 28\n\n1010 12\n0011 3\n1000 8\n\n(3 ^ 8) = 13\n(3 ^ 12) = 9\n(8 ^ 12) = 2\n(3 ^ 8 ^ 12) = 1\n0 + 1 + 2 + 3 + 8 + 9 + 12 + 13\n\n*/\n }\n};\n", "memory": "9268" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n int n = nums.size();\n int ans = 0;\n function<void(int, int)> dfs = [&](int i, int s) {\n if (i >= n) {\n ans += s;\n return;\n }\n dfs(i + 1, s);\n dfs(i + 1, s ^ nums[i]);\n };\n dfs(0, 0);\n return ans;\n }\n};", "memory": "9268" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n vector<int> result;\n result.push_back(0);\n int sum = 0;\n for(const int& num : nums){\n int len = result.size();\n for(int i = 0; i < len; i++){\n result.push_back(num ^ result[i]);\n sum += num ^ result[i];\n }\n }\n return sum;\n }\n};", "memory": "9696" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "#pragma GCC optimize (\"O3,unroll-loops\")\n#pragma GCC target (\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nclass Solution {\nprivate:\n vector<int> all;\n int curr = 0;\n\n void helper(const vector<int> &nums, int i) {\n if (i == nums.size()) all.push_back(curr);\n else {\n curr ^= nums[i];\n helper(nums, i+1);\n curr ^= nums[i];\n helper(nums, i+1);\n }\n }\n\npublic:\n int subsetXORSum(vector<int>& nums) {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n\n helper(nums, 0);\n return accumulate(all.begin(), all.end(), 0);\n }\n};", "memory": "9696" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void printSubset(const deque<int>& nums) {\n for (auto i : nums) {\n cout << i << \" \";\n }\n }\n\n int xorSubset(const deque<int>& nums) {\n int ret = 0;\n for (auto i : nums) {\n ret ^= i;\n }\n return ret;\n }\n\n int getSubset(vector<deque<int>>& subsets, vector<int>& nums, deque<int>& subset, int i, int len) {\n int r = 0;\n if (i < len) {\n subset.push_back(nums[i]);\n // subsets.push_back(subset);\n r += xorSubset(subset);\n for (int j = 1; j < len; j++) {\n r += getSubset(subsets, nums, subset, i + j, len);\n }\n subset.pop_back();\n }\n return r;\n }\n\n int subsetXORSum(vector<int>& nums) {\n vector<deque<int>> allSubsets;\n const auto len = nums.size();\n\n int total = 0;\n for (int i = 0; i < len; i++) {\n deque<int> subset;\n total += getSubset(allSubsets, nums, subset, i, len);\n }\n\n // for (auto subset : allSubsets) {\n // // printSubset(subset);\n // int sum = xorSubset(subset);\n // // std::cout << \" => \" << sum << std::endl;\n // total += sum;\n // }\n\n return total;\n }\n};", "memory": "10123" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n vector<int> record(1, 0);\n for (int& i : nums) {\n vector<int> temp = record;\n record.insert(record.end(), temp.begin(), temp.end());\n for (int j = temp.size() - 1; j >= 0; j--)\n record[j] ^= i;\n }\n int sum = 0;\n for (int& i : record)\n sum += i;\n return sum;\n }\n};", "memory": "10123" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nvoid f(vector<int>& nums, int i,int x,vector<int>& ans){\n if(i==nums.size()){\n ans.push_back(x);\n return;\n }\n f(nums,i+1,x^nums[i],ans);\n f(nums,i+1,x,ans);\n}\n int subsetXORSum(vector<int>& nums) {\n vector<int>ans;\n int x=0;\n f(nums,0,x,ans);\n int res=0;\n for(int i=0;i<ans.size();i++){\n res+=ans[i];\n }\n return res;\n }\n};", "memory": "10551" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void sumsub(int n, vector<int>& nums, int xorSum, vector<int>& v) {\n if(n == nums.size()) {\n v.push_back(xorSum);\n return;\n }\n\n sumsub(n + 1, nums, xorSum ^ nums[n], v);\n sumsub(n + 1, nums, xorSum, v);\n }\n\n int subsetXORSum(vector<int>& nums) {\n int xorSum = 0;\n vector<int>v;\n int sum = 0;\n sumsub(0, nums, xorSum, v);\n for(int i = 0; i < v.size(); i++) {\n sum += v[i];\n }\n\n return sum;\n }\n};", "memory": "10978" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void subsets(vector<int> &nums,int i,vector<int> &ans,vector<int> &subset) {\n if(i==nums.size()) {\n int dt=0;\n for(int i=0;i<ans.size();i++) {\n dt^=ans[i];\n }\n subset.push_back(dt);\n cout << endl;\n return;\n }\n ans.push_back(nums[i]);\n subsets(nums,i+1,ans,subset);\n ans.pop_back();\n subsets(nums,i+1,ans,subset);\n }\n int subsetXORSum(vector<int>& nums) {\n vector<int> subset;\n vector<int> ans;\n subsets(nums,0,ans,subset); \n int sm=0;\n for(int i=0;i<subset.size();i++) {\n sm+=subset[i];\n }\n return sm;\n }\n};", "memory": "10978" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n void genarate(int idx,vector<int>&sub,vector<int>&nums,vector<int>&v)\n {\n if(nums.size()==idx)\n {\n int ans=0;\n for(int i:sub)\n ans^=i;\n v.push_back(ans);\n return;\n }\n genarate(idx+1,sub,nums,v);\n sub.push_back(nums[idx]);\n genarate(idx+1,sub,nums,v);\n sub.pop_back();\n }\n\n int subsetXORSum(vector<int>& nums) {\n vector<int>sub,v;\n int ans=0;\n genarate(0,sub,nums,v);\n return accumulate(v.begin(),v.end(),0);\n }\n};", "memory": "11406" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n void genarate(int idx,vector<int>&sub,vector<int>&nums,vector<int>&v)\n {\n if(nums.size()==idx)\n {\n int ans=0;\n for(int i:sub)\n ans^=i;\n v.push_back(ans);\n return;\n }\n genarate(idx+1,sub,nums,v);\n sub.push_back(nums[idx]);\n genarate(idx+1,sub,nums,v);\n sub.pop_back();\n }\n\n int subsetXORSum(vector<int>& nums) {\n ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);\n vector<int>sub,v;\n int anss=0;\n genarate(0,sub,nums,v);\n return accumulate(v.begin(),v.end(),0);\n }\n};", "memory": "11406" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution \n{\npublic:\n int subsetXORSum(vector<int>& nums) \n {\n vector<int> subset;\n subset.reserve(1 << nums.size());\n vector<int> xors;\n xors.reserve(nums.size());\n xorSubsets(nums, 0, subset, xors);\n int s = 0;\n for (int i : xors)\n {\n s += i;\n }\n return s;\n }\n\nprivate:\n static void xorSubsets(vector<int>& nums, size_t index, vector<int>& subset, vector<int>& xors)\n {\n if (index == nums.size())\n {\n int x = 0;\n for (int i : subset)\n {\n x ^= i;\n }\n xors.push_back(x);\n return;\n }\n \n subset.push_back(nums[index]);\n xorSubsets(nums, index + 1, subset, xors);\n subset.pop_back();\n xorSubsets(nums, index + 1, subset, xors);\n }\n};", "memory": "11833" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "#include <numeric>\n\nclass Solution \n{\npublic:\n int subsetXORSum(vector<int>& nums) \n {\n vector<int> subset;\n subset.reserve(1 << nums.size());\n vector<int> xors;\n xors.reserve(nums.size());\n xorSubsets(nums, 0, subset, xors);\n \n return std::accumulate(xors.begin(), xors.end(), 0);\n }\n\nprivate:\n static void xorSubsets(vector<int>& nums, size_t index, vector<int>& subset, vector<int>& xors)\n {\n if (index == nums.size())\n {\n xors.push_back(std::accumulate(subset.begin(), subset.end(), 0, [](int a, int b){ return a ^ b; }));\n return;\n }\n \n subset.push_back(nums[index]);\n xorSubsets(nums, index + 1, subset, xors);\n subset.pop_back();\n xorSubsets(nums, index + 1, subset, xors);\n }\n};\n\nstatic const bool no_input_output = \n []() \n {\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return ios_base::sync_with_stdio(false);\n }();", "memory": "11833" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void generate(vector<int> nums, vector<int>& ans, int idx, int size, int& sum){\n if(idx == size) return;\n generate(nums, ans, idx+1, size, sum);\n if(ans.empty()) ans.push_back(0);\n vector<int> helper;\n int s = ans.size();\n int tempsum=0;\n for(int i=0;i<s;i++){\n helper.push_back(ans[i]);\n helper.push_back(nums[idx]^ans[i]);\n tempsum += ans[i] + (ans[i]^nums[idx]);\n // cout<< ans[i]<<\" \"<<(ans[i]^nums[idx])<<\" \";\n }\n // cout<<endl;\n sum = tempsum;\n ans = helper;\n return;\n }\n int subsetXORSum(vector<int>& nums) {\n vector<int> ans;\n int sum;\n generate(nums, ans, 0, nums.size(), sum);\n\n // int sum = accumulate(nums.begin(), nums.end(), 0);\n return sum;\n }\n};", "memory": "12261" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint xorsum(vector<int> temp){\n int n=temp.size();\n if(n==0){\n return 0;\n }\n if(n==1){\n return temp[0];\n }\n int res=temp[0];\n for(int i=1;i<n;i++){\n res^=temp[i];\n }\n return res;\n}\n int subsetXORSum(vector<int>& nums) {\n int n=nums.size();\n int netsum=0;\n //subsets matlab subsequence nikaalne hai to wo bitwise wala concept lagana hoga\n vector<int> temp;\n for(int num=0; num<pow(2,n);num++){\n for(int k=0;k<n;k++){\n if(((num>>k)&1)==1){\n temp.push_back(nums[k]);\n }\n }\n netsum+=xorsum(temp);\n temp.clear();\n }\n return netsum;\n }\n};", "memory": "12688" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n vector<int>v;\n vector<vector<int>>vt;\n int xr=0;\n int total=0;\n travel(nums,xr,total,0,v,vt);\n return total;\n\n }\n void travel(vector<int>& nums,int &xr,int &total,int idx,vector<int>&v,vector<vector<int>>&vt){\n total+=xr;\n vt.push_back(v);\n for(int i=idx;i<nums.size();i++){\n xr^=nums[i];\n travel(nums,xr,total,i+1,v,vt);\n xr^=nums[i];\n }\n }\n};", "memory": "13116" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint sol=0;\n int subsetXORSum(vector<int>& nums) {\n xorRec(nums, 0, nums.size(), 0);\n return sol;\n }\n\n void xorRec(vector<int>& nums, const int i, const int n, int res) {\n if (i >= n) {\n sol+= res;\n return;\n }\n\n vector<bool> C(2);\n C[0] = false;\n C[1] = true;\n for (auto c: C) {\n if (c) {\n res = res ^nums[i];\n }\n \n xorRec(nums, i + 1, n, res);\n }\n }\n};", "memory": "13543" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n vector<int> temp;\n getsubsets(nums, temp, 0, nums.size());\n return sum;\n }\nprivate:\n int sum = 0;\n void getsubsets(vector<int>& nums, vector<int>& temp, int l, int h) {\n if(temp.size())\n sum += getxor(temp);\n for(int i = l; i < h; i++) {\n temp.push_back(nums[i]);\n getsubsets(nums, temp, i + 1, h);\n temp.pop_back();\n }\n }\n int getxor(vector<int> arr) {\n int res = arr[0];\n for(int i = 1; i < arr.size(); i++)\n res = res ^ arr[i];\n return res;\n }\n};", "memory": "13971" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int res;\n int xorOfArray(vector<int> arr){\n int xor_arr = 0;\n for (int i = 0; i < arr.size(); i++) {\n xor_arr = xor_arr ^ arr[i];\n }\n return xor_arr;\n }\n void backtrack(vector<int>& nums, int start, vector<int>& track){\n res += xorOfArray(track);\n for(int i = start; i < nums.size(); i++){\n track.push_back(nums[i]);\n backtrack(nums, i+1, track);\n track.pop_back();\n }\n }\n int subsetXORSum(vector<int>& nums) {\n vector<int> track;\n backtrack(nums, 0, track);\n return res;\n }\n};", "memory": "14398" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void solve( vector<int> arr , int& sum , int XOR , int index ) {\n sum += XOR ;\n for( int i = index ; i < arr.size() ; i++ )\n solve( arr , sum , XOR ^ arr[i] , i + 1 ) ;\n }\n\n int subsetXORSum(vector<int>& nums) {\n int sum = 0 ;\n solve( nums , sum , 0 , 0 ) ;\n return sum ;\n }\n};", "memory": "14826" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\nint ans = 0;\npublic:\n int subsetXORSum(vector<int>& nums) {\n vector<int>t;\n int n = nums.size();\n f(n, nums, t, 0);\n return ans;\n }\n void f(int n, vector<int>nums, vector<int>&t, int start){\n int x = 0;\n for(int i=0; i<t.size(); i++) x = x^t[i];\n ans+=x;\n for(int i =start; i<n; i++){\n t.push_back(nums[i]);\n f(n, nums, t, i+1);\n t.pop_back();\n }\n }\n};", "memory": "15253" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void calc(int ind,vector<int>& v1,vector<vector<int>>& res,vector<int>& nums,int& sum){\n int n=nums.size();\n int subxor=0;\n for(int i=0;i<v1.size();i++){\n subxor^=v1[i];\n }\n sum+=subxor;\n res.push_back(v1);\n\n\n for(int i=ind;i<n;i++){\n v1.push_back(nums[i]);\n calc(i+1,v1,res,nums,sum);\n v1.pop_back();\n }\n }\n\n int subsetXORSum(vector<int>& nums) {\n vector<int> v1;\n vector<vector<int>> res;\n int sum=0;\n calc(0,v1,res,nums,sum);\n \n return sum;\n }\n};", "memory": "18246" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void f(int ind,vector<int> &nums,vector<int> &temp,vector<vector<int>> &res)\n {\n if(ind>=nums.size())\n {\n res.push_back(temp);\n return ;\n }\n temp.push_back(nums[ind]);\n f(ind+1,nums,temp,res);\n temp.pop_back();\n f(ind+1,nums,temp,res);\n }\n int subsetXORSum(vector<int>& nums) {\n vector<vector<int>> res;\n vector<int> temp;\n f(0,nums,temp,res);\n int sum = 0,val=0;\n for(int i=0;i<res.size();i++)\n {\n for(int j=0;j<res[i].size();j++)\n {\n sum = sum ^ res[i][j];\n }\n val = val + sum;\n sum=0;\n }\n return val;\n }\n};", "memory": "18673" }