id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int root(vector<int>& parent, int x) {\n if (parent[x] == x) return x;\n int p = parent[x];\n return parent[x] = root(parent, p);\n }\n\n void merge(vector<int>& parent, int x, int y) {\n x = root(parent, x);\n y = root(parent, y);\n parent[x] = y;\n }\n\n int process(unordered_set<int>& nodes, vector<int>& parent) {\n unordered_map<int, int> groups;\n for (auto x: nodes) {\n auto p = root(parent, x);\n groups[p]++;\n }\n int sum = 0;\n for (auto [k, v]: groups) {\n sum += v * (v - 1) / 2;\n }\n return sum;\n }\n\n int numberOfGoodPaths(vector<int>& vals, const vector<vector<int>>& raw_edges) {\n int n = vals.size();\n vector<pair<int, int>> edges;\n vector<int> parent(n, -1);\n\n for (auto& v: raw_edges) {\n int x = v[0], y = v[1];\n\n edges.emplace_back(x, y);\n edges.emplace_back(y, x);\n }\n sort(edges.begin(), edges.end());\n\n vector<int> nodes(n);\n for (int i = 0; i < n; i++) nodes[i] = i;\n sort(nodes.begin(), nodes.end(), [&vals](int x, int y) { return vals[x] < vals[y]; });\n\n int ans = n;\n unordered_set<int> pending;\n for (auto x: nodes) {\n if (pending.size()) {\n auto y = *pending.begin();\n if (vals[x] != vals[y]) {\n ans += process(pending, parent);\n pending.clear();\n }\n }\n pending.emplace(x);\n\n auto it = lower_bound(edges.begin(), edges.end(), pair{x, -1});\n parent[x] = x;\n for (; it != edges.end() && it->first == x; ++it) {\n int y = it->second;\n if (parent[y] < 0) continue;\n merge(parent, x, y);\n }\n }\n if (pending.size()) ans += process(pending, parent);\n\n return ans;\n }\n};", "memory": "190573" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class Solution {\npublic:\n\n typedef pair<int, int> PII;\n\n int n;\n vector<vector<int>> g;\n vector<int> p;\n\n int find(int x) {\n if (p[x] != x) p[x] = find(p[x]);\n return p[x];\n }\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int res = 0;\n n = vals.size();\n g = vector<vector<int>>(n);\n p = vector<int>(n);\n for (int i = 0; i < edges.size(); i ++) {\n int a = edges[i][0], b = edges[i][1];\n if (vals[a] >= vals[b]) g[a].push_back(b);\n else g[b].push_back(a);\n }\n vector<PII> nodes;\n for (int i = 0; i < vals.size(); i ++) nodes.push_back({vals[i], i});\n sort(nodes.begin(), nodes.end());\n for (int i = 0; i < n; i ++) p[i] = i;\n\n for (int i = 0; i < nodes.size(); ) {\n int j = i;\n while (j < nodes.size() && nodes[j].first == nodes[i].first) {\n for (int k = 0; k < g[nodes[j].second].size(); k ++) {\n int pa = find(nodes[j].second), pb = find(g[nodes[j].second][k]);\n if (pa != pb) {\n p[pb] = pa;\n }\n }\n j ++;\n }\n unordered_map<int, int> counter;\n for (int k = i; k < j; k ++) {\n counter[find(nodes[k].second)] ++;\n }\n for (auto& [k, v]: counter) {\n res += (v * (v - 1)) / 2;\n }\n i = j;\n }\n res += n;\n return res;\n }\n};", "memory": "192505" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class Solution {\npublic:\n\n typedef pair<int, int> PII;\n\n int n;\n vector<vector<int>> g;\n vector<int> p;\n\n int find(int x) {\n if (p[x] != x) p[x] = find(p[x]);\n return p[x];\n }\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int res = 0;\n n = vals.size();\n g = vector<vector<int>>(n);\n p = vector<int>(n);\n for (int i = 0; i < edges.size(); i ++) {\n int a = edges[i][0], b = edges[i][1];\n if (vals[a] >= vals[b]) g[a].push_back(b);\n else g[b].push_back(a);\n }\n vector<PII> nodes;\n for (int i = 0; i < vals.size(); i ++) nodes.push_back({vals[i], i});\n sort(nodes.begin(), nodes.end());\n for (int i = 0; i < n; i ++) p[i] = i;\n\n for (int i = 0; i < nodes.size(); ) {\n int j = i;\n while (j < nodes.size() && nodes[j].first == nodes[i].first) {\n for (int k = 0; k < g[nodes[j].second].size(); k ++) {\n int pa = find(nodes[j].second), pb = find(g[nodes[j].second][k]);\n if (pa != pb) {\n p[pb] = pa;\n }\n }\n j ++;\n }\n unordered_map<int, int> counter;\n for (int k = i; k < j; k ++) {\n counter[find(nodes[k].second)] ++;\n }\n for (auto& [k, v]: counter) {\n res += (v * (v - 1)) / 2;\n }\n i = j;\n }\n res += n;\n return res;\n }\n};", "memory": "192505" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class Solution {\npublic:\n class DSU {\n public:\n vector<int> rank, parent, size;\n\n DSU(int n) {\n rank.resize(n + 1, 0);\n parent.resize(n + 1);\n size.resize(n + 1);\n for (int i = 0; i <= n; i++) {\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n int findUPar(int node) {\n if (node == parent[node])\n return node;\n\n return parent[node] = findUPar(parent[node]);\n }\n\n void unionByRank(int u, int v) {\n int ulp_u = findUPar(u);\n int ulp_v = findUPar(v);\n if (ulp_u == ulp_v)\n return;\n\n if (rank[ulp_u] < rank[ulp_v]) {\n parent[ulp_u] = ulp_v;\n } else if (rank[ulp_v] < rank[ulp_u]) {\n parent[ulp_v] = ulp_u;\n } else {\n parent[ulp_v] = ulp_u;\n rank[ulp_u]++;\n }\n }\n\n void unionBySize(int u, int v) {\n int ulp_u = findUPar(u);\n int ulp_v = findUPar(v);\n if (ulp_u == ulp_v)\n return;\n\n if (size[ulp_u] < size[ulp_v]) {\n parent[ulp_u] = ulp_v;\n size[ulp_v] += size[ulp_u];\n } else {\n parent[ulp_v] = ulp_u;\n size[ulp_u] += size[ulp_v];\n }\n }\n };\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n vector<int> adj[vals.size()+1];\n int ans = 0;\n for (auto it : edges) {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n int mx = 0;\n for (int i = 0; i < vals.size(); i++)\n mx = max(mx, vals[i]);\n vector<int> graph[mx + 1];\n for (int i = 0; i < vals.size(); i++) {\n graph[vals[i]].push_back(i);\n }\n int totalnodes = vals.size();\n DSU ds(totalnodes + 1);\n\n for (int i = 0; i <=mx; i++) {\n\n for (auto adjnode : graph[i]) {\n for (auto child : adj[adjnode]) {\n if (vals[child] <= i)\n ds.unionBySize(child, adjnode);\n }\n }\n\n map<int, int> mp;\n for (auto adjnode : graph[i]) {\n mp[ds.findUPar(adjnode)]++;\n }\n\n for (auto it : mp) {\n ans += (it.second * (it.second + 1)) / 2;\n }\n }\n return ans;\n }\n};", "memory": "194438" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int>par, sz;\n void make(int u){\n par[u] = u;\n sz[u] = 1;\n }\n int find(int u){\n if(u == par[u]) return u;\n return par[u] = find(par[u]);\n }\n void merge(int u, int v){\n int x = find(u);\n int y = find(v);\n if(x != y){\n if(sz[x] < sz[y]){\n swap(x, y);\n }\n sz[x] += sz[y];\n par[y] = x;\n }\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n par.resize(n);\n sz.resize(n);\n vector<vector<int>> adj(n);\n for (auto& e : edges) {\n if (vals[e[0]] >= vals[e[1]]) {\n adj[e[0]].push_back(e[1]);\n }\n if (vals[e[1]] >= vals[e[0]]) {\n adj[e[1]].push_back(e[0]);\n }\n }\n map<int, vector<int>>mp;\n for (int i = 0; i < n; i++) {\n make(i);\n mp[vals[i]].push_back(i);\n }\n long long res = 0;\n for (auto& [x, v] : mp) {\n for(auto i : v){\n for (auto j : adj[i]) {\n if (find(i) != find(j))\n merge(i, j);\n }\n }\n map<int, int> freq;\n for(int i : v){\n freq[find(i)]++;\n }\n for (auto [x, f] : freq) {\n res += (f *(f - 1)) / 2;\n }\n }\n return res + n;\n }\n};\n", "memory": "194438" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class DisjointSet {\npublic:\n vector<int> parent;\n vector<int> size;\n\n DisjointSet(int n) {\n parent.resize(n, -1);\n size.resize(n, 1);\n }\n\n int findUPar(int node) {\n if (parent[node] == -1) return node;\n return parent[node] = findUPar(parent[node]); // Path compression\n }\n\n void unite(int nodeA, int nodeB) {\n int uParA = findUPar(nodeA);\n int uParB = findUPar(nodeB);\n\n if (uParA == uParB) return;\n\n if (size[uParA] < size[uParB]) {\n size[uParB] += size[uParA];\n parent[uParA] = uParB;\n } else {\n size[uParA] += size[uParB];\n parent[uParB] = uParA;\n }\n }\n};\n\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n vector<int> adj[n];\n map<int, vector<int>> freq;\n\n for (auto& iter : edges) {\n int nodeA = iter[0];\n int nodeB = iter[1];\n adj[nodeA].push_back(nodeB);\n adj[nodeB].push_back(nodeA);\n }\n\n for (int i = 0; i < n; i++) {\n freq[vals[i]].push_back(i);\n }\n\n DisjointSet ds(n);\n int ans = 0;\n\n for (auto& [key, nodes] : freq) {\n for (int node : nodes) {\n for (int child : adj[node]) {\n if (vals[child] <= vals[node]) {\n ds.unite(node, child);\n }\n }\n }\n\n map<int, int> count;\n for (int node : nodes) {\n int ultimateParent = ds.findUPar(node);\n count[ultimateParent]++;\n }\n\n for (auto& [parent, cnt] : count) {\n ans += cnt * (cnt - 1) / 2;\n }\n }\n\n return ans + n; // Add n to include single-node paths\n }\n};\n", "memory": "196370" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "struct UnionFind {\n\tint n;\n\tvector<int> rank;\n\tvector<int> parent;\n\t// store other info as required\n\tUnionFind(int n) {\n\t\trank.resize(n);\n\t\tfill(rank.begin(), rank.end(), 0);\n\t\tparent.resize(n);\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tparent[i] = i;\n\t\t}\n\t}\n\tint get(int a) {\n\t\treturn parent[a] = (parent[a] == a ? a : get(parent[a]));\n\t}\n\tvoid merge(int a, int b) {\n\t\ta = get(a);\n\t\tb = get(b);\n\t\tif (a == b) {\n\t\t\treturn;\n\t\t}\n\t\tif (rank[a] == rank[b]) {\n\t\t\trank[a]++;\n\t\t}\n\t\tif (rank[a] > rank[b]) {\n\t\t\tparent[b] = a;\n\t\t} else {\n\t\t\tparent[a] = b;\n\t\t}\n\t}\n};\n\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n=vals.size();\n UnionFind dsu(n);\n map<int,vector<int>> mpp;\n\n for(int i=0;i<n;i++){\n mpp[vals[i]].push_back(i);\n }\n\n vector<int> adj[n];\n\n for(auto &it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n int ans=0;\n for(auto &it:mpp){\n for(auto &v:it.second){ \n for(auto u:adj[v]){\n if(vals[u]<=vals[v]){\n dsu.merge(u,v);\n }\n }\n }\n\n map<int,int> freq;\n\n for(auto &v:it.second){\n freq[dsu.get(v)]++;\n }\n\n for(auto &it:freq){\n int val=it.second;\n if(val>1){\n ans+=(val*(val-1))/2;\n }\n }\n }\n\n ans+=n;\n\n return ans;\n\n }\n};", "memory": "196370" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "\nclass Solution {\npublic:\n vector<int> par,sz;\n void make(int n){\n par.resize(n);sz.resize(n);\n for(int i=0;i<n;i++){\n par[i]=i;\n sz[i]=1;\n }\n }\n int findGroup(int u){\n if(par[u]==u) return u;\n return par[u] = findGroup(par[u]);\n }\n\n void unite(int u,int v){\n u=findGroup(u);\n v=findGroup(v);\n if(u==v) return;\n if(sz[u]<sz[v]) swap(u,v);\n par[v]=u;\n sz[u]+=sz[v];\n }\n\n int numberOfGoodPaths(vector<int>& val, vector<vector<int>>& edges) {\n // smaller or equal value nodes only allowed and have to find number of paths\n // hence idea o applying dsu on sorting\n int n=val.size();\n vector<vector<int>> g(n);\n for(auto x:edges){\n int u=x[0],v=x[1];\n if(val[u]>=val[v]){\n g[u].push_back(v);\n }\n if(val[v]>=val[u]){\n g[v].push_back(u);\n }\n }\n\n vector<pair<int,int>> v;\n for(int i=0;i<n;i++) v.push_back({val[i],i});\n sort(v.begin(),v.end());\n int i=0;\n int ans=0;\n make(n);\n while(i<n){\n int j=i;\n vector<int> temp;\n while(j<n&&v[i].first==v[j].first){\n temp.push_back(v[j].second);\n j++;\n }\n for(auto u:temp){\n for(auto v:g[u]) unite(u,v);\n }\n map<int,int> mp;\n for(auto x:temp) mp[findGroup(x)]++;\n for(auto x:mp){\n int ct=x.second;\n ans+=(ct*(ct-1))/2;\n }\n\n i=j;\n }\n\n ans+=n; // path of length zero \n\n return ans;\n }\n};", "memory": "198303" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class dsu{\n public:\n vector<int> parent,size;\n dsu(int n){\n parent.resize(n+1);\n size.resize(n+1);\n for(int i=0;i<n+1;i++){\n size[i]=1;\n parent[i]=i;\n }\n }\n int fup(int node){\n if(parent[node]==node)return node;\n return parent[node]=fup(parent[node]);\n }\n void unite(int u,int v){\n int uu=fup(u),vv=fup(v);\n if(uu==vv)return ;\n if(size[uu]>size[vv]){\n size[uu]+=size[vv];\n parent[vv]=uu;\n }\n else{\n size[vv]+=size[uu];\n parent[uu]=vv;\n }\n }\n};\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n map<int,vector<int>> mp;\n for(int i=0;i<vals.size();i++){\n mp[vals[i]].push_back(i);\n }\n int result=vals.size();\n vector<int> adj[vals.size()];\n vector<int> vis(vals.size(),0);\n for(auto &it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n dsu ds(vals.size());\n for(auto &it:mp){\n for(auto &i:it.second){\n \n for(auto &j:adj[i]){\n if(vis[j]==1){\n ds.unite(i,j);\n }\n }\n vis[i]=1;\n }\n map<int,int> st;\n for(auto i:it.second){\n st[ds.fup(i)]++;\n }\n for(auto &i:st){\n result+=(i.second*(i.second-1))/2;\n }\n \n }\n return result;\n }\n};", "memory": "198303" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int find_set(int a, vector<int> &par){\n if(a == par[a]) return a;\n return par[a] = find_set(par[a], par);\n }\n void union_sets(int a, int b, vector<int> &par, vector<int> &siz){\n a = find_set(a, par);\n b = find_set(b, par);\n if(a != b){\n if(siz[a] < siz[b]) swap(a,b);\n siz[a] += siz[b];\n par[b] = a;\n }\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n vector<pair<int, pair<int, int>>> vp;\n for(int i = 0 ; i < edges.size() ; i++){\n int a = edges[i][0], b = edges[i][1];\n vp.push_back({max(vals[a], vals[b]), {a,b}});\n }\n sort(vp.begin(), vp.end());\n unordered_map<int, vector<int>> mp;\n int maxi = 0;\n for(int i = 0 ; i < n ; i++){\n mp[vals[i]].push_back(i);\n maxi = max(maxi, vals[i]);\n }\n int ans = n;\n vector<int> par(n+1), siz(n+1);\n for(int i = 0 ; i < n ; i++){\n par[i] = i;\n siz[i] = 1;\n }\n int ind = 0;\n for(int i = 0 ; i <= maxi ; i++){\n while(ind < vp.size() && vp[ind].first <= i){\n int a = vp[ind].second.first, b = vp[ind].second.second;\n union_sets(a,b,par,siz);\n ind++;\n }\n map<int,int> hh;\n for(auto it : mp[i]){\n hh[find_set(it, par)]++;\n }\n for(auto it : hh){\n ans += (it.second*(it.second-1))/2;\n }\n }\n return ans;\n }\n};", "memory": "200235" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class Solution {\npublic:\nclass DSU {\n public : \n vector<int> par ; \n vector<int> rank ;\n DSU(int n) {\n for ( int i = 0 ; i<n ; i++) {\n par.push_back(i) ;\n rank.push_back(0) ;\n }\n\n }\n\n int findpar( int x) {\n while (par[x]!=x) {\n x = par[x] ;\n }\n return x ;\n }\n\n void unite(int x, int y) {\nint first = findpar(x) ; \nint sec = findpar(y) ; \nif (first==sec) {\n return ;\n}\nif (rank[first]>rank[sec]) {\n par[sec] = first;\n}\nelse if (rank[first]<rank[sec]) {\n par[first] = sec ; \n}\nelse {\n par[first] = sec ; \n rank[sec]++;\n}\n }\n\n};\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size() ; \n vector<int> adj[n] ; \n for ( auto &edge : edges) {\n int u = edge[0] ; \n int v = edge[1] ; \n if (vals[u]<vals[v]) {\n swap(u,v) ;\n }\n adj[u].push_back(v) ;\n }\n DSU dsu(n) ;\n map<int,vector<int>> nodes_with_val ;\n set<int> dif_values ; \n int ans = 0 ; \n for (int i = 0 ; i<vals.size(); i++) {\n dif_values.insert(vals[i]);\n nodes_with_val[vals[i]].push_back(i) ;\n\n }\n for (auto &value: dif_values) {\n for (auto &node: nodes_with_val[value]) {\n for ( auto &h:adj[node]) {\n dsu.unite(node,h) ;\n }\n }\n map<int,int> parents ;\n\n for ( auto &node:nodes_with_val[value]) {\nparents[dsu.findpar(node)] ++;\n }\n for ( auto &h: parents) {\n ans = ans + ((h.second)*(h.second-1))/2 ; \n cout<<h.second<<\" \"<<value<<endl ;\n }\n }\n ans= ans+n;\n return ans ;\n \n\n\n\n }\n};", "memory": "200235" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "#include <vector>\n#include <unordered_map>\n#include <algorithm>\n\nusing namespace std;\n\nclass Solution {\npublic:\n struct DSU {\n vector<int> parent, rank;\n DSU(int n) {\n parent.resize(n);\n rank.resize(n, 1);\n for (int i = 0; i < n; i++) parent[i] = i;\n }\n int get_parent(int u) {\n if (parent[u] == u) return u;\n return parent[u] = get_parent(parent[u]);\n }\n void merge(int u, int v) {\n u = get_parent(u);\n v = get_parent(v);\n if (u == v) return;\n if (rank[u] == rank[v]) {\n rank[u]++;\n parent[v] = u;\n } else if (rank[u] > rank[v]) {\n parent[v] = u;\n } else {\n parent[u] = v;\n }\n }\n };\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n vector<vector<int>> graph(n);\n for (auto& edge : edges) {\n int u = edge[0], v = edge[1];\n graph[u].push_back(v);\n graph[v].push_back(u);\n }\n\n vector<int> nodes(n);\n for (int i = 0; i < n; i++) nodes[i] = i;\n\n sort(nodes.begin(), nodes.end(), [&](int a, int b) {\n return vals[a] < vals[b];\n });\n\n DSU dsu(n);\n int res = n; // Each node itself is a good path\n\n for (int i = 0; i < n; ) {\n int value = vals[nodes[i]];\n int start = i;\n while (i < n && vals[nodes[i]] == value) {\n int u = nodes[i];\n for (int v : graph[u]) {\n if (vals[u] >= vals[v]) {\n dsu.merge(u, v);\n }\n }\n i++;\n }\n\n unordered_map<int, int> count;\n for (int j = start; j < i; j++) {\n int parent = dsu.get_parent(nodes[j]);\n count[parent]++;\n }\n\n for (auto& [_, cnt] : count) {\n res += cnt * (cnt - 1) / 2;\n }\n }\n\n return res;\n }\n};\n", "memory": "202168" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "#include <ranges>\n\nclass DisjointSet {\npublic:\n DisjointSet(int n)\n : idToParent(n, -1)\n , rootToSize(n, 1)\n {}\n\n int Find(int id) {\n auto parent = idToParent[id];\n if (parent == -1) {\n return id;\n }\n\n auto root = Find(parent);\n idToParent[id] = root; // path compression\n\n return root;\n }\n\n void Union(int a, int b) {\n auto rootA = Find(a);\n auto rootB = Find(b);\n\n if (rootA == rootB) {\n return;\n }\n\n if (rootToSize[rootA] < rootToSize[rootB]) {\n std::swap(rootA, rootB); // union by size\n }\n\n idToParent[rootB] = rootA;\n rootToSize[rootA] += rootToSize[rootB];\n }\n\nprivate:\n std::vector<int> idToParent;\n std::vector<int> rootToSize;\n};\n\nclass Solution {\npublic:\n // Algorithm:\n // - sort nodes by value (non-decreasing)\n // - process nodes in order\n // - use disjoin set to keep track of group of connected nodes\n // - count nodes with the same value within a group and update good paths count.\n // n: number of nodes with same value within group\n // count good paths within group = (n-1) * n / 2 + n \n int numberOfGoodPaths(vector<int>& nodeToValue, vector<vector<int>>& edges) {\n auto nodeToNeighbors = MakeGraph(nodeToValue.size(), edges);\n\n auto nodes = std::vector<int>(nodeToValue.size());\n std::iota(nodes.begin(), nodes.end(), 0);\n std::sort(nodes.begin(), nodes.end(), [&](auto nodea, auto nodeb) {\n return nodeToValue[nodea] < nodeToValue[nodeb];\n });\n\n int countGoodPathsTotal = 0;\n auto sets = DisjointSet(nodeToValue.size());\n\n auto nodeGroups = nodes | std::views::chunk_by([&](auto nodea, auto nodeb) {\n return nodeToValue[nodea] == nodeToValue[nodeb];\n });\n\n for (auto group : nodeGroups) {\n for (auto node : group) {\n auto nodeVal = nodeToValue[node];\n for (auto neighbor : nodeToNeighbors[node]) {\n auto neighborVal = nodeToValue[neighbor];\n if (nodeVal >= neighborVal) {\n sets.Union(node, neighbor);\n }\n }\n }\n\n auto setToCountSameValue = std::unordered_map<int, int>{};\n for (auto node : group) {\n auto setId = sets.Find(node);\n ++setToCountSameValue[setId];\n }\n\n for (auto [setId, count] : setToCountSameValue) {\n auto countGoodPaths = (count - 1) * count / 2 + count;\n countGoodPathsTotal += countGoodPaths;\n }\n }\n\n return countGoodPathsTotal;\n }\n\nprivate:\n std::vector<std::vector<int>> MakeGraph(int n, std::vector<std::vector<int>>& edges) {\n auto adjList = std::vector<std::vector<int>>(n);\n\n for (const auto& edge : edges) {\n auto a = edge[0];\n auto b = edge[1];\n adjList[a].push_back(b);\n adjList[b].push_back(a);\n }\n\n return adjList;\n }\n};", "memory": "202168" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int findPar(int u, vector<int> &par) {\n if(par[u] == u) return u;\n return par[u] = findPar(par[u], par);\n }\n void Union(int u, int v, vector<int> &par) {\n int uPar = findPar(u, par);\n int vPar = findPar(v, par);\n if(uPar != vPar) {\n par[uPar] = vPar;\n }\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n // the trick is to combine nodes in increasing order of the value\n int n = vals.size();\n vector<int> par(n);\n for(int i = 0; i < n; i++) par[i] = i;\n vector<int> adj[n];\n for(auto &it : edges) {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n\n int pathCount = 0;\n // we know the val of a particular index\n map<int, vector<int>> mp;// val, index of node having particular val\n for(int i = 0; i < n; i++) mp[vals[i]].push_back(i);\n \n for(auto &it : mp) {\n vector<int> nodesHavingVal = it.second;\n for(auto node : nodesHavingVal) {\n for(auto nbr : adj[node]) {\n if(vals[nbr] <= vals[node]) {\n Union(nbr, node, par);\n }\n }\n }\n // After trying to join all the nbr for each node having particular value\n // we have to find how many joined nodes gets inserted in the same DSU\n \n // For each inserted node for a particular value, the amount of good paths\n // get incremented by count of that particular value in that DSU\n map<int, int> DSUpathCnt;// Ultimate Parent of a DSU, cnt of paths for particular value\n for(auto node : nodesHavingVal) {\n int ultimatePar = findPar(node, par);\n pathCount += ++DSUpathCnt[ultimatePar];\n }\n }\n return pathCount;\n }\n};", "memory": "204100" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "\nclass Solution {\n #define sp <<\" \"<<\n class DSU{\n\n vector<int> par;\n \n\n public:\n int find(int x){\n return par[x] = (x == par[x] ? x : find(par[x]));\n }\n\n int join(int y1, int y2){\n // component to which y1 belongs must have par find(y1)\n // we make par[find(y1)] point to par of y2.\n return par[find(y1)] = find(y2);\n }\n \n DSU(int n){\n par.resize(n + 1);\n for(int i = 1;i < n;i++){\n par[i] = i;\n }\n }\n };\n\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n DSU dsu(n);\n int ans = n;\n // we need v to attach index to values in vals so that after sort, we can track their original index.\n vector<vector<int>> g(vals.size());\n for(auto &edge : edges){\n g[edge[0]].push_back(edge[1]);\n g[edge[1]].push_back(edge[0]);\n }\n map<int,vector<int>> v;\n for(int i = 0;i < vals.size();i++){\n v[vals[i]].push_back(i);\n }\n\n\n // we sort v so that we can form graph in increasing order of x\n for(auto &[x,ys] : v){\n // we will introduce new ys into our graph and join them to the existing components\n \n // introduce new ys into our graph\n for(auto y : ys){\n for(auto neighbor : g[y]){\n\n\n // only nodes having value <= x exits in graph right now. So need to check before merging any neighbor\n if(vals[neighbor] <= x){\n dsu.join(neighbor,y);\n }\n }\n }\n\n\n // to keep track of how many ys belong to same component\n map<int,int> cnt;\n for(auto y : ys){\n cnt[dsu.find(y)]++;\n }\n\n\n for(auto &[par,ycounts] : cnt){\n // if two ys belong to same component there will definitely be a good path between them. Because the component is only made up of value <= x\n // if two ys belong to different component, there can not be a path. Because they have not been joined by any node having value <= x\n\n\n // all ys belonging to same parent form a good pair\n\n // cout << x sp ycounts sp par sp endl;\n ans += (ycounts*(ycounts - 1)) / 2;\n }\n }\n\n\n return ans;\n\n }\n};", "memory": "204100" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class DSU{\n public:\n vector<int> parent,rank;\n DSU(int n){\n parent.resize(n);\n rank.resize(n);\n\n for(int i = 0;i<n;i++){\n parent[i] = i;\n rank[i] = 1;\n }\n }\n\n int find(int node){\n if(node==parent[node]){\n return node;\n }\n\n return parent[node] = find(parent[node]);\n }\n\n void unionByRank(int u,int v){\n int pu = find(u);\n int pv = find(v);\n\n if(rank[pu]<rank[pv]){\n parent[pu] = pv;\n }\n else if(rank[pv]<rank[pu]){\n parent[pv] = pu;\n }\n else{\n parent[pu] = pv;\n rank[pv]++;\n }\n }\n};\n\n\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n\n DSU ds(n);\n\n vector<int> adj[n];\n\n for(auto it : edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n map<int,vector<int>> val_to_nodes;\n\n for(int i = 0;i<n;i++){\n val_to_nodes[vals[i]].push_back(i);\n }\n\n int res = n;\n\n vector<bool> isActive(n,false);\n\n for(auto &it : val_to_nodes){\n vector<int> nodes = it.second;\n\n for(int u :nodes){\n for(int v :adj[u]){\n if(isActive[v]){\n ds.unionByRank(u,v);\n }\n }\n isActive[u] = true;\n }\n\n\n vector<int> parents;\n for(int u : nodes)\n {\n parents.push_back(ds.find(u));\n }\n\n int sz = parents.size();\n\n sort(parents.begin(),parents.end());\n\n for(int i = 0;i<sz;i++){\n int curr = parents[i];\n int count = 0;\n\n while(i<sz && curr==parents[i]){\n i++;\n count++;\n }\n i--;\n res += (count * (count - 1))/2;\n }\n\n }\n\n return res;\n\n\n\n \n }\n};", "memory": "213763" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class Solution {\n\npublic:\n vector<int>parent;\n vector<int>rank;\n\n int findParent(int x){\n if(parent[x]==x) return x;\n \n return parent[x] = findParent(parent[x]);\n }\n\n void Union(int x,int y){\n int rootX = findParent(x);\n int rootY = findParent(y);\n if(rootX==rootY) return;\n if(rank[rootX]<rank[rootY]) swap(rootX,rootY);\n if(rank[rootX]==rank[rootY]) rank[rootX]++;\n parent[rootY] = rootX;\n }\n\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n rank.resize(n,1);\n parent.resize(n);\n for(int i = 0;i<n;i++) parent[i] = i;\n\n vector<int>adj[n+1];\n\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n // for(int i = 0;i<n;i++){\n // for(auto &k:adj[i]){\n // cout<<k<<\" \";\n // }cout<<endl;\n // }\n\n\n map<int,vector<int>>valueToNode;\n\n for(int i = 0;i<n;i++) valueToNode[vals[i]].push_back(i);\n \n int res = n;\n \n vector<bool>activeNode(n,false);\n\n for(auto &it:valueToNode){\n vector<int>Nodes = it.second;\n for(auto &u:Nodes){\n for(auto &v:adj[u]){\n if(activeNode[v]){\n Union(u,v);\n } \n }\n activeNode[u] = true;\n }\n \n vector<int>grandParent;\n \n for(auto &u:Nodes){\n grandParent.push_back(findParent(u));\n }\n\n sort(grandParent.begin(),grandParent.end());\n\n int size = grandParent.size();\n \n //int count = 0;\n for(int i = 0;i<size;i++){\n int count = 0;\n int currentParent = grandParent[i];\n while(i<size && currentParent==grandParent[i]){\n i++; \n count++;\n } \n i--;\n\n //cout<<count<<endl;\n \n res+=(count*(count-1))/2;\n\n }\n\n //cout<<count<<endl;\n\n //res+=(count*(count-1))/2;\n\n for(auto &it:grandParent){\n cout<<it<<\" \";\n }cout<<endl; \n\n\n\n\n }\n\n\n\n\n return res;\n }\n};", "memory": "215695" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class DisjointSet{\n\n public:\n vector<int> parent,size;\n DisjointSet(int n){\n parent.resize(n+1);\n size.resize(n+1);\n for(int i =0;i<=n;i++){\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n int findUPar(int node){\n if(node==parent[node]){\n return node;\n }\n\n return parent[node] = findUPar(parent[node]);\n }\n\n void unionBySize(int u , int v){\n int ulp_u = findUPar(u);\n int ulp_v = findUPar(v);\n if(ulp_u==ulp_v){\n return ;\n }\n\n if(size[ulp_v]>size[ulp_u]){\n parent[ulp_u] = ulp_v;\n size[ulp_v] += size[ulp_u];\n }\n else{\n parent[ulp_v] = ulp_u;\n size[ulp_u] += size[ulp_v];\n }\n }\n \n};\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n DisjointSet ds(n);\n\n int ans = 0;\n\n map<int,vector<int>> vals_to_node;\n for(int i =0;i<n;i++){\n vals_to_node[vals[i]].push_back(i);\n }\n vector<bool> is_active(n,false);\n vector<int> adj[n];\n for(auto it :edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n for(auto it : vals_to_node){\n\n for(auto node : it.second){\n\n for(auto it : adj[node]){\n if(is_active[it]){\n ds.unionBySize(it,node);\n }\n }\n is_active[node] = true;\n\n }\n\n vector<int> parents;\n for(auto node : it.second){\n parents.push_back(ds.findUPar(node));\n }\n sort(parents.begin(),parents.end());\n for(int j =0;j<parents.size();j++){\n long long count = 0;\n int curr_parent = parents[j];\n while(j<parents.size() && parents[j]==curr_parent){\n count++;\n j++;\n }\n j--;\n\n ans += (count*(count-1))/2;\n }\n\n\n }\n return ans + n;\n\n \n }\n};", "memory": "215695" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class Solution {\nprivate:\n struct DSU {\n vector<int> par, sz;\n // int cnt;\n\n void init(int n) {\n par = vector<int>(n);\n iota(par.begin(), par.end(), 0);\n\n sz = vector<int>(n, 1);\n // cnt = n;\n }\n\n int find(int x) {\n if (par[x] == x) return x;\n return par[x] = find(par[x]);\n }\n\n void merge(int u, int v) {\n u = find(u);\n v = find(v);\n if (u == v) return;\n\n if (sz[u] < sz[v]) swap(u, v);\n par[v] = u;\n sz[u] += sz[v];\n // cnt--;\n }\n };\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n \n vector<vector<int>> tree(n);\n for (auto &edge : edges) {\n if (vals[edge[0]] >= vals[edge[1]])\n tree[edge[0]].push_back(edge[1]);\n else\n tree[edge[1]].push_back(edge[0]);\n }\n \n map<int, vector<int>> nodesHasSameValue;\n for (int i = 0; i < n; ++i)\n nodesHasSameValue[vals[i]].push_back(i);\n\n DSU dsu;\n dsu.init(n);\n \n int numOfPathes = 0;\n for (auto &[value, nodes] : nodesHasSameValue) {\n for (auto &node : nodes)\n for (auto &child : tree[node])\n dsu.merge(node, child);\n \n unordered_map<int, int> component;\n for (auto &node : nodes)\n component[dsu.find(node)]++;\n \n numOfPathes += nodes.size();\n \n for (auto &[xx, cnt] : component)\n numOfPathes += cnt * (cnt - 1) / 2;\n }\n\n return numOfPathes;\n }\n};", "memory": "217628" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class UnionFind {\n private:\n vector<int> id, rank;\n int cnt;\n public:\n UnionFind(int cnt) : cnt(cnt) {\n id = vector<int>(cnt);\n rank = vector<int>(cnt, 0);\n for (int i = 0; i < cnt; i++) {\n id[i] = i;\n }\n }\n \n int find(int p) {\n if (id[p] == p) return p;\n id[p] = find(id[p]);\n return id[p];\n }\n\n bool connected(int p, int q) {\n return find(p) == find(q);\n }\n\n void connect(int p, int q) {\n int i = find(p);\n int j = find(q);\n if (i == j) return;\n\n if (rank[i] < rank[j]) {\n id[i] = j;\n } else {\n id[j] = i;\n if (rank[i] == rank[j]) {\n rank[i]++;\n }\n --cnt;\n }\n }\n};\n\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int N = vals.size();\n int goodPaths = 0;\n\n map<int, vector<int>> sameVals;\n for (int i = 0; i < N; i++) {\n sameVals[vals[i]].push_back(i);\n }\n\n vector<vector<int>> adj(N);\n\n for (auto &e : edges) {\n int u = e[0], v = e[1];\n \n if (vals[u] >= vals[v]) {\n adj[u].push_back(v);\n } else if (vals[v] >= vals[u]) {\n adj[v].push_back(u);\n }\n }\n\n UnionFind uf(N);\n\n \n for (auto &[value, allNodes] : sameVals) {\n \n for (int u : allNodes) {\n for (int v : adj[u]) {\n uf.connect(u, v);\n }\n }\n \n unordered_map<int, int> group;\n \n for (int u : allNodes) {\n group[uf.find(u)]++;\n }\n \n goodPaths += allNodes.size();\n \n for (auto &[_, size] : group) {\n goodPaths += (size * (size - 1) / 2);\n }\n }\n \n return goodPaths;\n }\n};", "memory": "217628" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "#define eb emplace_back\n#define ff first\n#define ss second\ntypedef vector <int> vi;\nclass Solution {\npublic:\n struct DSU{\n vi parent;\n vi size;\n void make_set(int i){\n parent[i]=i;\n size[i]=i;\n }\n DSU(int n){\n parent.resize(n,1);\n size.resize(n,1);\n for(int i=0;i<n;i++){\n make_set(i);\n }\n }\n int find_set(int n){\n if(parent[n]==n)return n;\n return parent[n]=find_set(parent[n]);\n }\n void union_sets(int a,int b){\n a = find_set(a);\n b=find_set(b);\n if(a!=b){\n if(size[a]<size[b]){\n swap(a,b);\n }\n parent[b]=a;\n size[a]+=size[b];\n }\n }\n \n };\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n DSU dsu(n);\n vi adj[n];\n for(auto u:edges){\n adj[u[0]].eb(u[1]);\n adj[u[1]].eb(u[0]);\n }\n map <int,vi> mp;\n for(int i=0;i<n;i++){\n mp[vals[i]].eb(i);\n }\n vi vis(n,0);\n int ans=n;\n for(auto u:mp){\n int val = u.ff;\n map <int,int> m;\n for(auto v:u.ss){\n for(auto w:adj[v]){\n if(vis[w]){\n dsu.union_sets(w,v);\n }\n }\n vis[v]=1;\n }\n for(auto v:u.ss){\n m[dsu.find_set(v)]++;\n }\n for(auto v:m){\n if(v.ss>=2){\n int temp=(v.ss*(v.ss-1));\n temp/=2;\n ans+=temp;\n }\n }\n }\n // cout<<ans<<endl;\n return ans;\n }\n};", "memory": "219560" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "#include <vector>\n#include <algorithm>\nclass Solution {\npublic:\n int numberOfGoodPaths(std::vector<int>& vals, std::vector<std::vector<int>>& edges) {\n int n = vals.size();\n std::vector<int> parent(n), rank(n, 0), count(n, 0);\n for (int i = 0; i < n; ++i)\n parent[i] = i;\n // Build adjacency list\n std::vector<std::vector<int>> adj(n);\n for (auto& edge : edges) {\n int u = edge[0], v = edge[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n // Sort nodes by their values\n std::vector<std::pair<int, int>> nodes;\n for (int i = 0; i < n; ++i)\n nodes.push_back({vals[i], i});\n std::sort(nodes.begin(), nodes.end());\n int total_good_paths = 0;\n // Initialize count of good paths (each node itself)\n total_good_paths += n;\n // For each unique value\n for (int i = 0; i < n; ) {\n int v = nodes[i].first;\n std::vector<int> indices;\n // Collect nodes with the same value\n while (i < n && nodes[i].first == v) {\n indices.push_back(nodes[i].second);\n ++i;\n }\n // For each node, union with neighbors if neighbor's value <= current value\n for (int u : indices) {\n for (int v_adj : adj[u]) {\n if (vals[v_adj] <= vals[u]) {\n unionSets(u, v_adj, parent, rank, count, vals);\n }\n }\n }\n // Count the number of nodes with current value in each set\n std::unordered_map<int, int> root_count;\n for (int u : indices) {\n int root = find(u, parent);\n root_count[root]++;\n }\n // For each set, calculate the number of good paths\n for (auto& [root, c] : root_count) {\n total_good_paths += c * (c - 1) / 2;\n }\n }\n return total_good_paths;\n }\nprivate:\n int find(int u, std::vector<int>& parent) {\n if (parent[u] != u)\n parent[u] = find(parent[u], parent); // Path compression\n return parent[u];\n }\n void unionSets(int u, int v, std::vector<int>& parent, std::vector<int>& rank,\n std::vector<int>& count, std::vector<int>& vals) {\n int root_u = find(u, parent);\n int root_v = find(v, parent);\n if (root_u == root_v)\n return;\n // Union by rank\n if (rank[root_u] < rank[root_v]) {\n parent[root_u] = root_v;\n } else if (rank[root_u] > rank[root_v]) {\n parent[root_v] = root_u;\n } else {\n parent[root_v] = root_u;\n rank[root_u]++;\n }\n }\n};", "memory": "219560" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "// class Solution {\n// public:\n \n// vector<int> parent ,rank;\n// int findParent(int x)\n// {\n \n// if(x==parent[x])\n// return x;\n// return parent[x]=findParent(parent[x]);\n \n \n// }\n// void Union(int x, int y)\n// {\n \n// int parentX=findParent(x);\n// int parentY=findParent(y);\n \n// if(parentX==parentY)\n// return ;\n \n// if(rank[parentY]<rank[parentX])\n// {\n// parent[parentY]=parentX;\n// }\n// else if(rank[parentY]>rank[parentX])\n// {\n// parent[parentX]=parentY;\n// }\n// else\n// {\n// parent[parentX]=parentY;\n// rank[parentY]++;\n \n// }\n \n \n \n \n// }\n \n \n\n \n// int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n \n// int n=vals.size();\n \n// int ne=edges.size();\n \n// if(ne==0)\n// return n;\n \n \n \n// parent.resize(n);\n// rank.resize(n,0);\n \n \n// map<int,vector<int>> mp;\n// vector<int> adj[n];\n \n// vector<int> vis(n,-1);\n// for(int i=0;i<ne;i++)\n// {\n \n// adj[edges[i][0]].push_back(edges[i][1]);\n// adj[edges[i][1]].push_back(edges[i][0]);\n \n// }\n// for(int i=0;i<n;i++)\n// {\n// mp[vals[i]].push_back(i);\n// parent[i]=i;\n \n \n// }\n \n// int ans=n;\n \n \n// for(auto it: mp)\n// {\n \n \n// vector<int> nodes=it.second; \n// int size=nodes.size(); \n// unordered_map<int ,int> freq;\n// for(int i=0;i<size;i++ )\n// {\n \n// for(auto it: adj[nodes[i]])\n// {\n// if(vis[nodes[i]]==1)\n// Union(it, nodes[i]); \n \n// } \n// vis[nodes[i]]=1;\n// }\n \n// for(int i=0;i<size;i++)\n// { \n// int x=findParent(nodes[i]);\n// cout<<nodes[i]<<parent[nodes[i]]<<endl;\n// freq[x]++; \n// }\n \n \n \n// for(auto it: freq)\n// { \n// if(it.second>=2) \n// ans+=((it.second)*(it.second-1)/2);\n// } \n \n// }\n \n \n \n \n \n \n \n \n \n \n \n// return ans;\n \n \n \n \n \n \n \n \n \n \n \n \n// }\n// };\n\n\n\nclass Solution {\npublic:\n vector<int> parent, rank;\n\n // Function to find the parent of a node with path compression\n int findParent(int x) {\n if (x != parent[x]) {\n parent[x] = findParent(parent[x]); // Path compression\n }\n return parent[x];\n }\n\n // Function to perform union of two sets using rank\n void Union(int x, int y) {\n int parentX = findParent(x);\n int parentY = findParent(y);\n \n if (parentX != parentY) {\n if (rank[parentX] > rank[parentY]) {\n parent[parentY] = parentX;\n } else if (rank[parentX] < rank[parentY]) {\n parent[parentX] = parentY;\n } else {\n parent[parentY] = parentX;\n rank[parentX]++;\n }\n }\n }\n \n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n \n // Initialize the parent and rank arrays\n parent.resize(n);\n rank.resize(n, 0);\n \n // Initialize the parent to be the node itself\n for (int i = 0; i < n; ++i) {\n parent[i] = i;\n }\n \n // Adjacency list representation of the graph\n vector<int> adj[n];\n for (auto& edge : edges) {\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n \n // Map to store nodes with the same values\n map<int, vector<int>> valueGroups;\n for (int i = 0; i < n; ++i) {\n valueGroups[vals[i]].push_back(i);\n }\n \n int goodPaths = n; // Each node is a good path by itself\n \n // Process nodes in the order of their values\n for (auto& [value, nodes] : valueGroups) {\n // Union nodes within the same connected component\n for (int node : nodes) {\n for (int neighbor : adj[node]) {\n if (vals[neighbor] <= value) {\n Union(node, neighbor);\n }\n }\n }\n \n // Count the size of each component\n unordered_map<int, int> componentSize;\n for (int node : nodes) {\n int root = findParent(node);\n componentSize[root]++;\n }\n \n // Calculate the number of good paths\n for (auto& [root, size] : componentSize) {\n if (size > 1) {\n goodPaths += (size * (size - 1)) / 2;\n }\n }\n }\n \n return goodPaths;\n }\n};\n", "memory": "221493" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "class DSU{\n vector<int> parent, size;\n\npublic:\n\n DSU(int n){\n parent.resize(n);\n size.resize(n, 1);\n\n for(int i = 0; i < n; i++){\n parent[i] = i;\n }\n }\n\n int findUltimateParent(int node){\n if(parent[node] == node){\n return node;\n }\n\n return parent[node] = findUltimateParent(parent[node]);\n }\n\n\n void unionBySize(int x, int y){\n int ulp_x = findUltimateParent(x), ulp_y = findUltimateParent(y);\n\n if(ulp_x == ulp_y) return;\n\n if(size[ulp_x] > size[ulp_y]){\n // merge y into x\n parent[ulp_y] = ulp_x;\n size[ulp_x] += size[ulp_y];\n } else{\n // merge x into y\n parent[ulp_x] = ulp_y;\n size[ulp_y] += size[ulp_x];\n }\n }\n\n int getSize(int node){\n return size[findUltimateParent(node)];\n }\n};\n\n\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n\n vector<int> adj[n];\n\n // create graph\n\n\n for(auto &it: edges){\n int &x = it[0], &y = it[1];\n adj[x].push_back(y);\n adj[y].push_back(x);\n }\n\n map<int, vector<int>> mp; // value -> {nodes, .. }\n\n for(int i = 0; i < n; i++){\n mp[vals[i]].push_back(i);\n }\n\n DSU dsu(n);\n\n int ans = 0;\n\n // start processing the smallest node: grredy\n\n for(auto &[val, allNodes]: mp){\n \n // go to all neighbours and try to union them\n for(auto &node: allNodes){\n \n for(auto &nei: adj[node]){\n if(val >= vals[nei]){\n dsu.unionBySize(node, nei);\n }\n }\n\n }\n\n // process all the unions to get the count for good paths where it has same values as val\n unordered_map<int, int> mp; // to count the nodes\n\n for(auto &node: allNodes){\n int ulp = dsu.findUltimateParent(node);\n mp[ulp]++;\n }\n\n for(auto &[node, count]: mp){\n ans += (count*(count + 1))/2;\n }\n\n }\n\n return ans;\n\n }\n};\n\n/*\n map all the values to nodes\n\n i.e create map of values to node numbers\n\n then we will be greedy and start with smaller values and trie to connect the neighbours to its union till its values\n is less than or equal to current\n\n after connection we can count how many nodes ha\n\n\n*/", "memory": "221493" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "struct DSU {\n vector<int> size;\n vector<int> par;\n \n DSU (int n) {\n size.resize(n, 1);\n par.resize(n);\n for (int j = 0; j < n; j ++) par[j] = j;\n }\n \n int Leader (int x) {\n if (x == par[x]) return x;\n return (par[x] = Leader(par[x]));\n }\n \n void merge (int x, int y) {\n x = Leader(x);\n y = Leader(y);\n if (x == y) return;\n if (size[x] > size[y]) swap (x, y);\n \n size[y] += size[x];\n par[x] = y;\n }\n};\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n vector<vector<int>> g(n);\n for (auto i : edges) {\n g[i[0]].push_back(i[1]);\n g[i[1]].push_back(i[0]);\n }\n \n map<int, vector<int>> val_to_nodes;\n for (int j = 0; j < n; j ++) val_to_nodes[vals[j]].push_back(j);\n \n DSU dsu(n);\n int result = n;\n vector<bool> is_active(n);\n \n for (auto [_, indexes] : val_to_nodes) {\n for (auto i : indexes) {\n for (auto child: g[i]) {\n if (is_active[child]) dsu.merge (i, child);\n }\n is_active[i] = true;\n }\n \n vector<int> leaders;\n for (auto i : indexes) leaders.push_back(dsu.Leader(i));\n sort (leaders.begin(), leaders.end());\n \n int sz = leaders.size();\n for (int j = 0; j < sz; j ++) {\n long long cnt = 0;\n int cur = leaders[j];\n \n while (j < sz && leaders[j] == cur) j ++, cnt ++;\n j --;\n \n result += (cnt * (cnt - 1))/2;\n }\n }\n \n return result;\n }\n};", "memory": "223425" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "class DSU{\n public:\n vector<int> parent;\n vector<int> size;\n DSU(int n){\n parent.resize(n);\n size.resize(n, 1);\n for(int i=0;i<n;i++)parent[i]=i;\n }\n int par(int node){\n if(parent[node]==node)return node;\n return parent[node]=par(parent[node]);\n }\n void merge(int x, int y){\n x=par(x);\n y=par(y);\n if(x==y)return;\n if(size[x]>size[y])swap(x, y);\n size[y]+=size[x];\n parent[x]=y;\n }\n};\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n vector<vector<int>> g(n);\n for(auto it:edges){\n g[it[0]].push_back(it[1]);\n g[it[1]].push_back(it[0]);\n }\n\n DSU ds(n);\n map<int, vector<int>> vals_index;\n for(int i=0;i<n;i++) vals_index[vals[i]].push_back(i);\n\n int res=n;\n vector<bool> is_active(n);\n for(auto [_, index]: vals_index){\n vector<int> vec;\n for(auto it:index){\n for(auto node:g[it]){\n if(is_active[node]){\n ds.merge(it, node);\n }\n }\n is_active[it]=true;\n }\n vector<int> leadrs;\n for(auto it:index)leadrs.push_back(ds.par(it));\n sort(leadrs.begin(), leadrs.end());\n int sz=leadrs.size();\n for(int j=0;j<sz;j++){\n int curr=leadrs[j];\n int cnt=0;\n while(j<sz && curr==leadrs[j])j++, cnt++;\n j--;\n res+= cnt*(cnt-1)/2;\n }\n }\n return res;\n\n\n\n \n }\n};", "memory": "223425" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "\nclass DSU{\n\n public:\n\n vector<int> par,siz;\n\n DSU(int n){\n siz.resize(n,1);\n par.resize(n);\n iota(par.begin(),par.end(),0);\n }\n\n int findpar(int u){\n if(u==par[u]) return u;\n return par[u] = findpar(par[u]);\n }\n\n bool merge(int u,int v){\n u = findpar(u);\n v = findpar(v);\n if(u==v) return false;\n if(siz[u]<siz[v]) swap(u,v);\n siz[u] += siz[v];\n par[v] = u;\n return true;\n }\n\n};\n\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n\n int n = vals.size(); \n vector<vector<int>> g(n);\n\n for(auto e : edges){\n g[e[0]].push_back(e[1]);\n g[e[1]].push_back(e[0]);\n }\n\n map<int,vector<int>> mp;\n for(int i=0;i<n;i++){\n mp[vals[i]].push_back(i);\n }\n \n DSU ds(n);\n\n int totpaths=0;\n\n for(auto [val,nodes] : mp){ // go through the nodes in sorted order.\n\n // 1st pass : merge all nodes <= val.\n for(auto u : nodes){\n // cout<<u<<\" \";\n // by default it exists as a separate component.\n for(auto v : g[u]){\n if(vals[v]<=val){\n ds.merge(u,v);\n }\n }\n }\n\n // cout<<endl;\n\n // 2nd pass : Get component wise frequency.\n map<int,int> groups;\n for(auto u : nodes){\n groups[ds.findpar(u)]++;\n }\n \n // 3rd pass : Calc the number of paths ending at a node, for all nodes. \n int singles=0,multies=0;\n for(auto u : nodes){\n if(groups[ds.findpar(u)]>1) multies += groups[ds.findpar(u)]-1;\n singles++;\n }\n\n totpaths += singles + multies/2 ;\n }\n\n return totpaths;\n }\n};", "memory": "225358" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n class disjointset{\n public:\n vector<int>rank,parent,size;\n disjointset(int n){\n rank.resize(n+1,0);\n parent.resize(n+1,0);\n size.resize(n+1,0);\n for(int i=0;i<=n;i++){\n parent[i]=i;\n size[i]=1;\n }\n }\n \n int findpar(int node){\n if(node==parent[node]) return node;\n return parent[node]=findpar(parent[node]);\n }\n \n void unionbyrank(int u,int v){\n int ulp_u=findpar(u);\n int ulp_v=findpar(v);\n if(ulp_u==ulp_v) return;\n if(rank[ulp_u]<rank[ulp_v]){\n parent[ulp_u]=ulp_v;\n }\n else if(rank[ulp_u]>rank[ulp_v]){\n parent[ulp_v]=ulp_u;\n }\n else{\n parent[ulp_v]=ulp_u;\n rank[ulp_v]++;\n }\n }\n \n void unionbysize(int u,int v){\n int ulp_u=findpar(u);\n int ulp_v=findpar(v);\n if(ulp_u==ulp_v) return;\n if(size[ulp_u]<size[ulp_v]){\n parent[ulp_u]=ulp_v;\n size[ulp_v]+=size[ulp_u];\n }\n else{\n parent[ulp_v]=ulp_u;\n size[ulp_u]+=size[ulp_v];\n }\n }\n \n};\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) { \n int n=vals.size();\n int ans=n;\n disjointset ds(n);\n vector<bool>vis(n);\n map<int,vector<int>>mp;\n for(int i=0;i<n;i++){\n mp[vals[i]].push_back(i);\n }\n vector<int>adj[n];\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n for(auto it:mp){\n vector<int>v=it.second;\n for(int node:v){\n for(int child:adj[node]){\n if(vis[child]){\n ds.unionbyrank(node,child);\n }\n }\n vis[node]=1;\n }\n vector<int>parents;\n for(int i:v){\n parents.push_back(ds.findpar(i));\n }\n sort(parents.begin(),parents.end());\n int sz=parents.size();\n for(int j=0;j<parents.size();j++){\n int cnt=0;\n int curr=parents[j];\n while(j<sz && parents[j]==curr){\n j++;\n cnt++;\n }\n j--;\n ans+=(cnt*(cnt-1))/2;\n }\n }\n return ans;\n }\n};", "memory": "225358" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "class Solution {\n int find(int a,vector<int>& parent){\n if(parent[a]<0)\n return a;\n return parent[a]=find(parent[a],parent);\n }\n void union_(int a,int b,vector<int>& parent)\n {\n a=find(a,parent);\n b=find(b,parent);\n if(a==b)\n return;\n\n if(-parent[a]>-parent[b])\n {\n parent[a]+=parent[b];\n parent[b]=a;\n } else {\n parent[b]+=parent[a];\n parent[a]=b;\n }\n }\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n=vals.size();\n vector<vector<int>> adj(n);\n map<int,vector<int>> m;\n vector<int> parent(n,-1);\n int good_pair=n;\n \n for(auto& edge:edges)\n {\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n for(int i=0;i<n;i++)\n m[vals[i]].push_back(i);\n\n for(auto& v:m)\n {\n // making union of adjacent who are aleready active bz there val is low\n for(auto& node:v.second){\n for(auto& neigh:adj[node]){\n if(vals[node]>=vals[neigh])\n {\n union_(node,neigh,parent);\n }\n } \n }\n // checking components after adding new val (by checking their unique parents)\n unordered_map<int,int> m_frq;\n for(auto& node:v.second){\n m_frq[find(node,parent)]++;\n }\n // each component has nC2 combination of good pair\n for(auto& i:m_frq)\n if(i.second>1)\n {\n good_pair+=(i.second*(i.second-1))/2;\n }\n }\n\n return good_pair;\n }\n};", "memory": "227290" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findPar(int u, vector<int> &par) {\n if(par[u] == u) return u;\n return par[u] = findPar(par[u], par);\n }\n void Union(int u, int v, vector<int> &par) {\n int uPar = findPar(u, par);\n int vPar = findPar(v, par);\n if(uPar != vPar) {\n par[uPar] = vPar;\n }\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n // The trick is to combine nodes in increasing order of the value into DSU\n // If two nodes of same value gets in the same DSU, then the path must exists\n // as all the nodes in the DSU will only contain values lesser than the current value\n int n = vals.size();\n vector<int> par(n);\n for(int i = 0; i < n; i++) par[i] = i;\n vector<int> adj[n];\n for(auto &it : edges) {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n int pathCount = 0;\n // we know the val of a particular index\n map<int, vector<int>> mp;// val, index of node having particular val\n for(int i = 0; i < n; i++) mp[vals[i]].push_back(i);\n \n for(auto &it : mp) {// smallest value nodes to largest value nodes\n vector<int> nodesHavingVal = it.second;\n for(auto node : nodesHavingVal) {\n for(auto nbr : adj[node]) {\n if(vals[nbr] <= vals[node]) {\n Union(nbr, node, par);\n }\n }\n }\n // After trying to join all the nbr for each node having particular value\n // we have to find how many joined nodes gets inserted in the same DSU\n unordered_map<int, int> DSUpathCnt;// Ultimate Parent of a DSU, cnt of paths for particular value\n for(auto node : nodesHavingVal) {\n int ultimatePar = findPar(node, par);\n // For each inserted node for a particular value, the amount of good paths\n // get incremented by count of that particular value in that DSU\n pathCount += ++DSUpathCnt[ultimatePar];\n }\n }\n return pathCount;\n }\n};", "memory": "227290" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "class UnionFind {\nprivate:\n vector<int> parent, rank;\n\npublic:\n UnionFind(int size) {\n parent.resize(size);\n rank.resize(size, 0);\n for (int i = 0; i < size; i++) {\n parent[i] = i;\n }\n }\n int find(int x) {\n if (parent[x] != x) parent[x] = find(parent[x]);\n return parent[x];\n }\n void union_set(int x, int y) {\n int xset = find(x), yset = find(y);\n if (xset == yset) {\n return;\n } else if (rank[xset] < rank[yset]) {\n parent[xset] = yset;\n } else if (rank[xset] > rank[yset]) {\n parent[yset] = xset;\n } else {\n parent[yset] = xset;\n rank[xset]++;\n }\n }\n};\n\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n vector<vector<int>> adj(n);\n for (auto& edge : edges) {\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n // Mapping from value to all the nodes having the same value in non-descending order of values.\n map<int, vector<int>> valuesToNodes;\n for (int node = 0; node < n; node++) {\n valuesToNodes[vals[node]].push_back(node);\n }\n\n UnionFind dsu(n);\n int goodPaths = 0;\n\n // Iterate over all the nodes with the same value in sorted order, starting from the lowest\n // value.\n for (auto& [value, nodes] : valuesToNodes) {\n // For every node in nodes, combine the sets of the node and its neighbors into one set.\n for (int node : nodes) {\n for (int neighbor : adj[node]) {\n // Only choose neighbors with a smaller value, as there is no point in\n // traversing to other neighbors.\n if (vals[node] >= vals[neighbor]) {\n dsu.union_set(node, neighbor);\n }\n }\n }\n // Map to compute the number of nodes under observation (with the same values) in each\n // of the sets.\n unordered_map<int, int> group;\n // Iterate over all the nodes. Get the set of each node and increase the count of the\n // set by 1.\n for (int u : nodes) {\n group[dsu.find(u)]++;\n }\n // For each set of \"size\", add size * (size + 1) / 2 to the number of goodPaths.\n for (auto& [_, size] : group) {\n goodPaths += (size * (size + 1) / 2);\n }\n }\n return goodPaths;\n }\n};", "memory": "229223" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "struct UnionFind{\n int n, set_size, *par, *rank;\n UnionFind(int a){\n n = set_size = a;\n par = new int[n+1];\n rank = new int[n+1];\n for(int i=0;i<n;i++) par[i] = i;\n for(int i=0;i<n;i++) rank[i] = 1;\n }\n int find(int x){\n if(x!=par[x]) return par[x] = find(par[x]);\n else return x;\n }\n void merge(int x, int y){\n int xroot = find(x), yroot = find(y);\n if(xroot!=yroot){\n if(rank[xroot]>rank[yroot]){\n par[yroot] = xroot;\n //rank[xroot]+=rank[yroot];\n }\n else if(rank[yroot]>rank[xroot]){\n par[xroot] = yroot;\n //rank[yroot]+=rank[xroot];\n }\n else{\n par[xroot] = yroot;\n ++rank[yroot];\n }\n\n }\n }\n};\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n UnionFind uf(vals.size());\n map<int,vector<int>> vals_to_nodes;\n for(int i=0;i<vals.size();i++) vals_to_nodes[vals[i]].push_back(i);\n vector<int> g[vals.size()];\n for(auto ele : edges){\n if(ele[0]<=ele[1]) g[ele[0]].push_back(ele[1]);\n if(ele[1]<=ele[0]) g[ele[1]].push_back(ele[0]);\n }\n int ans=vals.size();\n for(auto [v, nodes]: vals_to_nodes){\n for(auto node: nodes){\n for(auto ch : g[node]){\n uf.merge(ch,node);\n }\n }\n unordered_map<int,int> rootCount;\n for(auto node : nodes) rootCount[uf.find(node)]++;\n for(auto e: rootCount){\n ans+=(e.second*(e.second-1)/2);\n }\n }\n return ans;\n }\n};", "memory": "229223" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "class DisjointSet{\n vector<int>size,parent;\n public:\n DisjointSet(int n){\n size.resize(n+1);\n parent.resize(n+1);\n for(int i=0;i<=n;i++){\n size[i]=1;\n parent[i]=i;\n }\n }\n int findUpar(int node){\n if(node==parent[node])return node;\n return parent[node]=findUpar(parent[node]);\n }\n void UnionBySize(int u, int v){\n int ulp_u=findUpar(u);\n int ulp_v=findUpar(v);\n if(ulp_u==ulp_v)return;\n if(size[ulp_u]>size[ulp_v]){\n parent[ulp_v]=ulp_u;\n size[ulp_u]+=size[ulp_v];\n }\n else{\n parent[ulp_u]=ulp_v;\n size[ulp_v]+=size[ulp_u];\n }\n }\n};\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n map<int,vector<int>>samevalues;\n int n=vals.size();\n vector<vector<int>>adj(n);\n int ans=0;\n for(int i=0;i<n;i++){\n samevalues[vals[i]].push_back(i);\n }\n for(auto &it:edges){\n int u=it[0];\n int v=it[1];\n if(vals[u]>=vals[v]){\n adj[u].push_back(v);\n }\n else{\n adj[v].push_back(u);\n }\n }\n DisjointSet ds(n);\n for(auto it:samevalues){\n int val=it.first;\n vector<int>nodes=it.second;\n for(auto node:nodes){\n for(auto u:adj[node]){\n ds.UnionBySize(u,node);\n }\n }\n unordered_map<int,int>grp;\n for(auto it:nodes){\n grp[ds.findUpar(it)]++;\n }\n ans+=nodes.size();\n for(auto it:grp){\n int cnt=it.second;\n ans+=(cnt*(cnt-1)/2);\n }\n }\n return ans;\n }\n};", "memory": "231155" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "class DisjointSet{\n vector<int>size,parent;\n public:\n DisjointSet(int n){\n size.resize(n+1);\n parent.resize(n+1);\n for(int i=0;i<=n;i++){\n size[i]=1;\n parent[i]=i;\n }\n }\n int findUpar(int node){\n if(node==parent[node])return node;\n return parent[node]=findUpar(parent[node]);\n }\n void UnionBySize(int u, int v){\n int ulp_u=findUpar(u);\n int ulp_v=findUpar(v);\n if(ulp_u==ulp_v)return;\n if(size[ulp_u]>size[ulp_v]){\n parent[ulp_v]=ulp_u;\n size[ulp_u]+=size[ulp_v];\n }\n else{\n parent[ulp_u]=ulp_v;\n size[ulp_v]+=size[ulp_u];\n }\n }\n};\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n map<int,vector<int>>samevalues;\n int n=vals.size();\n vector<vector<int>>adj(n);\n int ans=0;\n for(int i=0;i<n;i++){\n samevalues[vals[i]].push_back(i);\n }\n for(auto &it:edges){\n int u=it[0];\n int v=it[1];\n if(vals[u]>=vals[v]){\n adj[u].push_back(v);\n }\n else{\n adj[v].push_back(u);\n }\n }\n DisjointSet ds(n);\n for(auto it:samevalues){\n int val=it.first;\n vector<int>nodes=it.second;\n for(auto node:nodes){\n for(auto u:adj[node]){\n ds.UnionBySize(u,node);\n }\n }\n unordered_map<int,int>grp;\n for(auto it:nodes){\n grp[ds.findUpar(it)]++;\n }\n ans+=nodes.size();\n for(auto it:grp){\n int cnt=it.second;\n ans+=(cnt*(cnt-1)/2);\n }\n }\n return ans;\n }\n};", "memory": "231155" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "struct UnionFind{\n int n, set_size, *par, *rank;\n UnionFind(int a){\n n = set_size = a;\n par = new int[n+1];\n rank = new int[n+1];\n for(int i=0;i<n;i++) par[i] = i;\n for(int i=0;i<n;i++) rank[i] = 1;\n }\n int find(int x){\n if(x!=par[x]) return par[x] = find(par[x]);\n else return x;\n }\n void merge(int x, int y){\n int xroot = find(x), yroot = find(y);\n if(xroot!=yroot){\n if(rank[xroot]>rank[yroot]){\n par[yroot] = xroot;\n //rank[xroot]+=rank[yroot];\n }\n else if(rank[yroot]>rank[xroot]){\n par[xroot] = yroot;\n //rank[yroot]+=rank[xroot];\n }\n else{\n par[xroot] = yroot;\n ++rank[yroot];\n }\n\n }\n }\n};\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n UnionFind uf(vals.size());\n map<int,vector<int>> vals_to_nodes;\n for(int i=0;i<vals.size();i++) vals_to_nodes[vals[i]].push_back(i);\n vector<int> g[vals.size()];\n for(auto ele : edges){\n if(vals[ele[0]]>=vals[ele[1]]) g[ele[0]].push_back(ele[1]);\n if(vals[ele[1]]>=vals[ele[0]]) g[ele[1]].push_back(ele[0]);\n }\n int ans=vals.size();\n for(auto [v, nodes]: vals_to_nodes){\n for(auto node: nodes){\n for(auto ch : g[node]){\n uf.merge(ch,node);\n }\n }\n unordered_map<int,int> rootCount;\n for(auto node : nodes) rootCount[uf.find(node)]++;\n for(auto e: rootCount){\n ans+=(e.second*(e.second-1)/2);\n }\n }\n return ans;\n }\n};", "memory": "233088" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "struct UnionFind{\n int n, set_size, *par, *rank;\n UnionFind(int a){\n n = set_size = a;\n par = new int[n+1];\n rank = new int[n+1];\n for(int i=0;i<n;i++) par[i] = i;\n for(int i=0;i<n;i++) rank[i] = 1;\n }\n int find(int x){\n if(x!=par[x]) return par[x] = find(par[x]);\n else return x;\n }\n void merge(int x, int y){\n int xroot = find(x), yroot = find(y);\n if(xroot!=yroot){\n if(rank[xroot]>rank[yroot]){\n par[yroot] = xroot;\n //rank[xroot]+=rank[yroot];\n }\n else if(rank[yroot]>rank[xroot]){\n par[xroot] = yroot;\n //rank[yroot]+=rank[xroot];\n }\n else{\n par[xroot] = yroot;\n ++rank[yroot];\n }\n\n }\n }\n};\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n UnionFind uf(vals.size());\n map<int,vector<int>> vals_to_nodes;\n for(int i=0;i<vals.size();i++) vals_to_nodes[vals[i]].push_back(i);\n vector<int> g[vals.size()];\n for(auto ele : edges){\n if(vals[ele[0]]>=vals[ele[1]]) g[ele[0]].push_back(ele[1]);\n if(vals[ele[1]]>=vals[ele[0]]) g[ele[1]].push_back(ele[0]);\n }\n int ans=vals.size();\n for(auto [v, nodes]: vals_to_nodes){\n for(auto node: nodes){\n for(auto ch : g[node]){\n uf.merge(ch,node);\n }\n }\n unordered_map<int,int> rootCount;\n for(auto node : nodes) rootCount[uf.find(node)]++;\n for(auto e: rootCount){\n ans+=(e.second*(e.second-1)/2);\n }\n }\n return ans;\n }\n};", "memory": "233088" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int find(int x, vector<int> &parent) {\n if(x == parent[x]) return x;\n return parent[x] = find(parent[x], parent);\n }\n\n void unionSet(int a, int b, vector<int> &parent, vector<int> &size) {\n int pa = find(a, parent);\n int pb = find(b, parent);\n\n if(pa == pb) return;\n else if(size[pa] < size[pb]) {\n parent[pa] = pb;\n size[pb] += size[pa];\n } else {\n parent[pb] = pa;\n size[pa] += size[pb];\n }\n }\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n vector<int> graph[n];\n map<int, vector<int>> m;\n vector<int> parent(n), size(n);\n int ans = 0;\n\n for(int i = 0; i < n; i++) {\n parent[i] = i;\n size[i] = 1;\n }\n\n for(int i = 0; i < edges.size(); i++) {\n graph[edges[i][0]].push_back(edges[i][1]);\n graph[edges[i][1]].push_back(edges[i][0]);\n }\n\n for(int i = 0; i < n; i++) {\n m[vals[i]].push_back(i);\n }\n\n for(auto i : m) {\n int val = i.first;\n vector<int> nodes = i.second;\n\n for(int j = 0; j < nodes.size(); j++) {\n for(auto node: graph[nodes[j]]) {\n if(vals[node] <= vals[nodes[j]]) {\n unionSet(node, nodes[j], parent, size);\n }\n }\n }\n\n unordered_map<int, int> group;\n\n for(int k = 0; k < nodes.size(); k++) {\n group[find(nodes[k], parent)]++;\n }\n\n for(auto k : group) {\n int sz = k.second;\n ans += (sz * (sz + 1)) / 2;\n }\n }\n\n return ans;\n\n }\n};", "memory": "235020" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> parent;\n vector<int> rank;\n long long nsum(int n){\n return (n*(n+1))/2;\n }\n\n int find(int i){\n if(parent[i]==-1){\n return i;\n }\n return parent[i] = find(parent[i]);\n }\n\n void un(int i, int j){\n auto s1 = find(i);\n auto s2 = find(j);\n if(s1!=s2){\n if(rank[s1]<rank[s2]){\n parent[s1]=s2;\n rank[s2]+=rank[s1];\n }\n else{\n parent[s2]=s1;\n rank[s1]+=rank[s2];\n }\n }\n }\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n parent.resize(n,-1);\n rank.resize(n,1);\n long long ans=0;\n unordered_map<int,vector<int>> adj;\n for(auto& e: edges){\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n\n \n\n map<int,vector<int>> mp;\n for(int i=0;i<n;i++){\n mp[vals[i]].push_back(i);\n }\n // small to large value comes forst;\n for(auto& kk: mp){\n map<int,int> mp2;\n auto vi = kk.second;\n for(auto& el : vi){\n int pel = find(el);\n bool fl = 0;\n for(auto& cc: adj[el]){\n if(vals[cc]<=vals[el]){\n if(find(cc)!=find(el)){\n fl=1;\n un(cc,el);\n \n }\n }\n\n }\n }\n // jab sare elements ka union ho jaye tb sare elements k pass jane h or unse puchna h tera baap kon\n for(auto el : vi){\n mp2[find(el)]++;\n }\n for(auto itr: mp2){\n ans+=nsum(itr.second);\n }\n }\n\n return ans;\n }\n};", "memory": "235020" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\n public:\n std::vector<int> ufParent;\n std::vector<std::unordered_map<int, int>> count;\n struct EdgeWeight {\n int weight = 0;\n int node1 = 0;\n int node2 = 0;\n };\n std::vector<EdgeWeight> edgeWeights;\n\n int ufFind(int i) {\n return ufParent[i] == i ? i : (ufParent[i] = ufFind(ufParent[i]));\n }\n\n void init(const vector<int>& vals, const vector<vector<int>>& edges) {\n const int n = vals.size();\n ufParent.resize(n);\n count.resize(n);\n for (int i = 0; i < n; ++i) {\n ufParent[i] = i;\n count[i][vals[i]]++;\n }\n edgeWeights.reserve(n);\n for (const auto& e : edges) {\n edgeWeights.push_back({std::max(vals[e[0]], vals[e[1]]), e[0], e[1]});\n }\n std::sort(\n edgeWeights.begin(), edgeWeights.end(),\n [](const auto& w1, const auto& w2) { return w1.weight < w2.weight; });\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n init(vals, edges);\n int res = vals.size(); // single node is itself a good path\n for (const auto& ew : edgeWeights) {\n auto g1 = ufFind(ew.node1);\n auto g2 = ufFind(ew.node2);\n auto c1 = count[g1][ew.weight];\n auto c2 = count[g2][ew.weight];\n res += c1 * c2;\n ufParent[g1] = g2;\n count[g2][ew.weight] += c1;\n }\n\n return res;\n }\n};", "memory": "236953" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n map<int, vector<int>> umap;\n vector<vector<int>> adjList(n);\n vector<int> parent(n);\n vector<int> sz(n, 1);\n iota(parent.begin(), parent.end(), 0);\n\n for (auto e: edges) {\n if (vals[e[0]] >= vals[e[1]]) adjList[e[0]].push_back(e[1]);\n else adjList[e[1]].push_back(e[0]);\n }\n\n for (int i=0; i<n; ++i)\n umap[vals[i]].push_back(i);\n \n int ret = 0;\n for (auto [key, vec]: umap) {\n unordered_map<int, int> tmap;\n\n for (auto v: vec) {\n for (auto adj: adjList[v])\n merge(v, adj, parent, sz);\n }\n\n for (auto v: vec) ++tmap[find(v, parent)];\n for (auto [node, cnt]: tmap) ret += cnt * (cnt - 1) / 2;\n }\n return ret+n;\n }\n\n void merge(int u, int v, vector<int>& parent, vector<int>& sz) {\n u = find(u, parent);\n v = find(v, parent);\n if (u != v) {\n if (sz[u] < sz[v])\n swap(u, v);\n parent[v] = u;\n sz[u] += sz[v];\n }\n }\n\n int find(int x, vector<int>& parent) {\n while (x != parent[x])\n x = parent[x] = parent[parent[x]];\n return x;\n }\n};", "memory": "236953" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n class DisjointSet {\n public:\n vector<int> parent;\n vector<int> rank;\n DisjointSet(int n)\n {\n parent.resize(n + 1);\n rank.resize(n + 1);\n for (int i = 0; i <= n; i++)\n {\n parent[i] = i;\n rank[i] = 1;\n }\n }\n\n int findParent(int node)\n {\n if (parent[node] == node)\n {\n return node;\n }\n return parent[node] = findParent(parent[node]);\n }\n\n void unionSets(int a, int b)\n {\n int sp_a = findParent(a);\n int sp_b = findParent(b);\n\n if (sp_a > sp_b)\n {\n parent[sp_b] = sp_a;\n \n }\n else if(sp_a < sp_b)\n {\n parent[sp_a] = sp_b;\n \n }\n else\n {\n parent[sp_a] = sp_b;\n rank[sp_a]++;\n }\n }\n };\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n int total_cnt = 0;\n map<int,vector<int>> mp;\n vector<vector<int>> adj(n);\n DisjointSet ds(n);\n\n // we sort the nodes because when we go from smaller to larger all smaller nodes will be part of next larger code so they can be in same union\n \n for(int i=0;i<n;i++)\n {\n mp[vals[i]].push_back(i); // store list of same nodes\n }\n\n for(auto it:edges)\n {\n if(vals[it[0]] >= vals[it[1]])\n {\n adj[it[0]].push_back(it[1]); // store list of edges with value less than parent\n }\n else if (vals[it[1]] >= vals[it[0]])\n {\n adj[it[1]].push_back(it[0]);\n }\n }\n\n for(auto &it:mp)\n {\n int idx = it.first;\n vector<int> nodes = it.second;\n for(auto node:nodes)\n {\n for(auto adjNode:adj[node])\n {\n ds.unionSets(node,adjNode);\n }\n }\n unordered_map<int,int> group;\n for(auto &node:nodes)\n {\n group[ds.findParent(node)]++; // count how many components are there\n }\n \n total_cnt+=nodes.size(); // each node can be good path\n\n for(auto &node:group)\n {\n int size = node.second;\n total_cnt+=((size) * (size - 1)) / 2; // if there are more than 1 node with same parent then we do nc2 which is n * (n-1) / 2\n }\n }\n return total_cnt;\n }\n};", "memory": "238885" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> parent;\n void dsu(int n){\n parent.resize(n);\n for(int i = 0;i<n;i++){parent[i] = i;}\n }\n int find(int i){\n if(parent[i]==i){\n return i;\n }\n return parent[i] = find(parent[i]);\n }\n void join(int A, int B){\n int a = find(A), b = find(B);\n if(a==b){\n return;\n }\n parent[b] = a;\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n vector<vector<int>> e;\n dsu(vals.size());\n for(vector<int> v : edges){\n int x = max(vals[v[0]],vals[v[1]]);\n e.push_back({x,v[0],v[1]});\n }\n sort(e.begin(),e.end());\n map <int, vector<int>> map;\n for(int i = 0;i<vals.size();i++){\n map[vals[i]].push_back(i);\n }\n int ans = 0;\n int j = 0;\n for(const auto & pair : map){\n int limit = pair.first;\n while(j<e.size() && e[j][0]<=limit){\n join(e[j][1],e[j][2]);\n j++;\n }\n unordered_map <int, int> m;\n for(int i : pair.second){\n m[find(i)]++;\n\n ans++;\n }\n for(const auto & pair : m){\n int xd = pair.second*(pair.second-1);\n xd/=2;\n ans+=xd;\n }\n }\n return ans;\n }\n};", "memory": "238885" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> parent;\n vector<int> rank;\n\n int find_parent(int node){\n if(node==parent[node]){\n return node;\n }\n return parent[node]=find_parent(parent[node]);\n }\n\n void union_(int u,int v){\n u=find_parent(u);\n v=find_parent(v);\n if(u==v) return;\n if(rank[u]>rank[v]){\n parent[v]=u;\n }\n else if(rank[u]<rank[v]){\n parent[u]=v;\n }\n else{\n parent[v]=u;\n rank[u]+=1;\n }\n return;\n }\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n parent.resize(n);\n for(int i=0;i<n;i++){\n parent[i]=i;\n }\n rank.resize(n,0);\n unordered_map<int,vector<int>> adj;\n for(int i=0;i<edges.size();i++){\n int u = edges[i][0];\n int v = edges[i][1];\n\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n map<int,vector<int>> val_to_node;\n for(int i=0;i<n;i++){\n int val=vals[i];\n val_to_node[val].push_back(i);\n }\n int result = n;\n vector<bool> is_active(n,false);\n for(auto it:val_to_node){\n vector<int> nodes=it.second;\n for(int &u:nodes){\n for(int &v:adj[u]){\n if(is_active[v]){\n union_(u,v);\n }\n }\n is_active[u]=true;\n }\n\n vector<int> tmhare_parents;\n for(int &u:nodes){\n tmhare_parents.push_back(find_parent(u));\n }\n sort(tmhare_parents.begin(),tmhare_parents.end());\n int sz = tmhare_parents.size();\n for(int i=0;i<sz;i++){\n int count = 0;\n int curr_parent=tmhare_parents[i];\n while(i<sz && tmhare_parents[i]==curr_parent){\n count++;\n i++;\n }\n i--;\n result+=((count*(count-1))/2);\n }\n }\n return result;\n }\n};", "memory": "240818" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class DSU{\n public:\n\n vector<int> parent;\n vector<int> size;\n\n DSU(int n){\n parent.resize(n);\n size.resize(n, 1);\n \n for(int i = 0; i < n; ++i) parent[i] = i;\n }\n\n int findPar(int node){\n if(parent[node] == node) return node;\n\n return parent[node] = findPar(parent[node]);\n }\n\n void join(int node1, int node2){\n int par1 = findPar(node1);\n int par2 = findPar(node2);\n\n if(par1 == par2) return;\n\n if(size[par1] <= size[par2]){\n size[par2] += size[par1];\n parent[par1] = par2;\n }\n else{\n size[par1] += size[par2];\n parent[par2] = par1;\n }\n }\n};\n\n\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n\n vector<int> adj[n];\n\n for(auto it: edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n\n map<int, vector<int>> m;\n for(int i = 0; i < vals.size(); ++i) m[vals[i]].push_back(i);\n\n DSU dsu(vals.size());\n\n int res = 0;\n\n for(auto it: m){\n // Jinke parents same hai unka count kitna hai basically that will give us the desired answer, it is a little similar to the previous problem that I tackeld but anyways\n\n for(auto iit: it.second){\n for(auto ad: adj[iit]){\n if(vals[ad] <= it.first){\n dsu.join(ad, iit);\n }\n }\n }\n\n unordered_map<int, int> um;\n\n for(auto iit: it.second){\n um[dsu.findPar(iit)]++;\n }\n\n for(auto iit: um){\n res = res + (iit.second * (iit.second-1))/2;\n }\n }\n\n return res + vals.size();\n }\n};", "memory": "240818" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n vector<vector<int>> tree;\n map<int, vector<int>> nodeValues;\n vector<int> parent, size;\n\n void createTree(vector<vector<int>>& edges) {\n tree = vector<vector<int>>(edges.size()+1);\n\n for(auto edge: edges) {\n tree[edge[0]].push_back(edge[1]);\n tree[edge[1]].push_back(edge[0]);\n }\n }\n\n void mapValues(vector<int>& vals) {\n for(int index = 0; index < vals.size(); index++) nodeValues[vals[index]].push_back(index);\n }\n\n void createGroups(int n) {\n parent = size = vector<int>(n);\n for(int node = 0; node < n; node++) parent[node] = node, size[node] = 1;\n }\n\n int find(int node) {\n if(parent[node] != node) parent[node] = find(parent[node]);\n return parent[node];\n }\n\n void Union(int node1, int node2) {\n int par1 = find(node1);\n int par2 = find(node2);\n\n if(par1 == par2) return;\n\n if(size[par1] >= size[par2]) parent[par2] = par1, size[par1] += size[par2];\n else parent[par1] = par2, size[par2] += size[par1];\n }\n\n int getGoodPathCount(vector<int>& vals) {\n int goodPathCount = 0;\n\n for(const auto &[value, nodes]: nodeValues) {\n for(int node: nodes) {\n for(int neighbour: tree[node]) if(vals[neighbour] <= vals[node]) Union(node, neighbour);\n }\n \n unordered_map<int, int> groups;\n for(int node: nodes) groups[find(node)]++;\n\n for(const auto &[_, count]: groups) goodPathCount += (count*(count+1))>>1;\n }\n\n return goodPathCount;\n }\n\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n createTree(edges);\n mapValues(vals);\n createGroups(vals.size());\n return getGoodPathCount(vals);\n }\n};", "memory": "242750" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "int ans = 0;\nvector<map<int,int>>mp(30010);\nclass Solution {\npublic:\n class DSU{\n public:\n vector<int>size;\n vector<int>par;\n DSU(int n){\n for(int i=0;i<n;i++){\n size.push_back(1);\n par.push_back(i);\n }\n }\n int findUpar(int u){\n if(par[u] == u)return u;\n return par[u] = findUpar(par[u]);\n }\n void merge(int u, int v, int val){\n int pu = findUpar(u);\n int pv = findUpar(v);\n if(pu==pv)return;\n \n if(size[pv]>size[pu]){\n par[pu] = pv;\n size[pv]+=size[pu];\n // for(auto it: mp[pu]){\n // if(mp[pv].find(it.first)!=mp[pv].end()){\n // ans = ans + (mp[pv][it.first]*it.second);\n // mp[pv][it.first] += it.second;\n // }\n // else{\n // mp[pv][it.first] = it.second;\n // }\n // }\n if(mp[pv].find(val)!=mp[pv].end()){\n ans = ans + (mp[pv][val]*mp[pu][val]);\n mp[pv][val] += mp[pu][val];\n }\n else{\n mp[pv][val] = mp[pu][val];\n }\n }\n else{\n par[pv] = pu;\n size[pu]+=size[pv];\n // for(auto it: mp[pv]){\n // if(mp[pu].find(it.first)!=mp[pu].end()){\n // ans = ans + (mp[pu][it.first]*it.second);\n // mp[pu][it.first] += it.second;\n // }\n // else{\n // mp[pu][it.first] = it.second;\n // }\n // }\n if(mp[pu].find(val)!=mp[pu].end()){\n ans = ans + (mp[pv][val]*mp[pu][val]);\n mp[pu][val] += mp[pv][val];\n }\n else{\n mp[pu][val] = mp[pv][val];\n }\n }\n }\n };\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n // mp.clear();\n ans = n;\n vector<pair<int,int>>v;\n vector<int>adj[n];\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n \n for(int i=0;i<n;i++){\n mp[i].clear();\n v.push_back({vals[i],i});\n mp[i][vals[i]] = 1;\n }\n // for(int i=0;i<n;i++){\n // cout<<\"Hey \"<<i<<endl;\n // for(auto itr: mp[i]){\n // cout<<itr.first<<\" \"<<itr.second<<endl;\n // }\n // }\n DSU dsu(n);\n set<int>st;\n sort(v.begin(),v.end());\n for(int i=0;i<n;i++){\n int node = v[i].second;\n int val = v[i].first;\n st.insert(node);\n // cout<<node<<endl;\n for(auto it:adj[node]){\n if(st.find(it)!=st.end())\n dsu.merge(node,it, val);\n // cout<<node<<\" \"<<it<<\" \"<<ans<<endl;\n // cout<<mp[3][2]<<\" \"<<mp[1][2]<<endl;\n }\n }\n return ans;\n }\n};", "memory": "242750" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\n #define pb push_back\n int parent[30007];\n int size[30007];\n void dsu(int n){\n for(int i=0; i<n; i++) parent[i]=i, size[i]=1;\n return ;\n }\n\n int find( int x){\n if( x== parent[x]) return x;\n return parent[x]=find(parent[x]);\n }\n\n void unite(int x, int y){\n int a= find(x);\n int b=find(y);\n if( a != b){\n if( size[a]<size[b]) swap(a,b);\n parent[b]=a;\n size[a] += size[b];\n }\n return;\n }\n\npublic:\n int numberOfGoodPaths(vector<int>& v1, vector<vector<int>>& edges) {\n vector<vector<int>> g(v1.size()+7);\n for( auto it : edges){\n g[it[0]].pb(it[1]); g[it[1]].pb(it[0]);\n }\n\n dsu(v1.size());\n map<int, vector<int>> mp;\n int res=0;\n for(int i=0 ; i<v1.size(); i++) mp[v1[i]].pb(i);\n for( auto it= mp.begin(); it != mp.end(); it++){\n int val= it->first; vector<int> group=it->second;\n for(int node : group){\n for(int child : g[node]){\n if( v1[child]<= v1[node]){\n unite(child,node);\n }\n }\n }\n\n unordered_map<int,int> ans;\n for(int node : group){\n ans[find(node)]++;\n }\n for( auto it= ans.begin(); it != ans.end(); it++){\n int p=it->second;\n res = res+ (p*(p+1))/2 ;\n }\n }\n\n return res;\n \n }\n};", "memory": "244683" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n vector<int> parent;\n vector<int> rank;\n\n int find(int u){\n if(parent[u]==u) return u;\n\n return parent[u]=find(parent[u]);\n }\n\n void Union(int u, int v){\n int u_par = find(u);\n int v_par = find(v);\n\n if(u_par == v_par) return;\n\n if(rank[u_par]>rank[v_par]) parent[v_par] = u_par;\n else if(rank[u_par]<rank[v_par]) parent[u_par] = v_par;\n else{\n parent[v_par]=u_par;\n rank[u_par]++;\n }\n }\n\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n \n int n= vals.size();\n parent.resize(n);\n rank.resize(n,1);\n\n for(int i=0;i<n;i++) parent[i]=i;\n\n unordered_map<int, vector<int>> adj;\n\n for(auto edge : edges){\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n\n map<int, vector<int>> val_to_node;\n\n for(int i=0;i<n;i++){\n val_to_node[vals[i]].push_back(i);\n }\n\n vector<bool> is_active(n,false);\n\n int result=n;\n\n for(auto &it : val_to_node){\n vector<int> node = it.second;\n\n for(auto u : node){\n for(auto v : adj[u]){\n if(is_active[v]) Union(u,v);\n }\n is_active[u]=true;\n }\n\n vector<int> node_par;\n\n for(auto u : node){\n node_par.push_back(find(u));\n }\n\n sort(node_par.begin(), node_par.end());\n\n int m=node_par.size();\n\n for(int i=0;i<m;i++){\n long long count=0;\n\n int curr= node_par[i];\n\n while(i<m && curr==node_par[i]){\n i++;\n count++;\n }\n i--;\n\n result+= count*(count-1)/2;\n }\n }\n\n return result;\n }\n};", "memory": "246616" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int>parent;\n vector<int>rank;\n\n int find(int x){\n if(x== parent[x]) return x;\n return parent[x]= find(parent[x]);\n }\n void Union( int x, int y){\n int x_parent= find(x);\n int y_parent= find(y);\n\n if(x_parent== y_parent) return;\n if(rank[x_parent]> rank[y_parent]) parent[y_parent]= x_parent;\n else if(rank[x_parent] < rank[y_parent]) parent[x_parent]= y_parent;\n else parent[x_parent]= y_parent; \n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n= vals.size();\n \n parent.resize(n);\n rank.resize(n,1);\n\n for(int i =0 ; i<n ; i++){\n parent[i]=i;\n }\n\n unordered_map<int, vector<int>> adj;\n for(auto edge: edges){\n int u= edge[0];\n int v= edge[1];\n\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n map<int, vector<int>> val_to_nodes;\n for(int i = 0 ; i<vals.size(); i++){\n val_to_nodes[vals[i]].push_back(i);\n }\n\n int result= n;\n vector<bool> is_active(n, false);\n \n for (auto &it : val_to_nodes) {\n \n vector<int> nodes = it.second;\n \n for (int &u : nodes) {\n \n for (int &v: adj[u]) {\n if (is_active[v]) {\n Union(u, v);\n }\n }\n is_active[u] = true;\n }\n \n vector<int> tumhare_parents;\n \n for (int &u : nodes) \n tumhare_parents.push_back(find(u));\n \n sort(tumhare_parents.begin(), tumhare_parents.end());\n \n int sz = tumhare_parents.size();\n \n for (int j = 0; j < sz; j++) {\n long long count = 0;\n \n int cur_parent = tumhare_parents[j];\n \n while (j < sz && tumhare_parents[j] == cur_parent) {\n j++, \n count++;\n }\n j--;\n \n result += (count * (count - 1))/2;\n }\n }\n \n return result;\n }\n};\n", "memory": "246616" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\n vector<int> parent;\n vector<int> rank;\n int find(int x){\n if(parent[x]==x) return x;\n return parent[x]= find(parent[x]);\n }\n void unionn(int x,int y){\n int x_p= find(x),y_p= find(y);\n if(x_p==y_p) return;\n if(rank[x_p] > rank[y_p]){\n parent[y_p]= x_p; rank[x_p]++;\n }\n else if(rank[x_p] < rank[y_p]){\n parent[x_p]= y_p; rank[y_p]++;\n }\n else{\n parent[y_p]= x_p; rank[x_p]++;\n }\n }\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n=vals.size(); \n parent.resize(n); rank.resize(n,1);\n for(int i=0;i<n;i++) parent[i]=i;\n \n unordered_map<int,vector<int>> adj;\n for(auto i:edges){\n adj[i[0]].push_back(i[1]); adj[i[1]].push_back(i[0]);\n }\n map<int,vector<int>>val_to_idx;\n for(int i=0;i<n;i++){\n val_to_idx[vals[i]].push_back(i);\n }\n int ans=n;\n vector<int> active(n,0);\n for(auto i:val_to_idx){\n for(auto u:i.second){\n for(auto v:adj[u]){\n if(active[v]==1) unionn(u,v);\n }\n active[u]=1;\n }\n vector<int> pa;\n for(auto u:i.second){\n pa.push_back(find(u));\n }\n sort(pa.begin(),pa.end());\n for(int j=0;j<pa.size();j++){\n int curr= pa[j];int cnt=0;\n while(j<pa.size() && pa[j]==curr){cnt++; j++;}\n j--;\n int formula= (cnt*(cnt-1))/2;\n ans+= formula;\n }\n }\n return ans;\n }\n};", "memory": "248548" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "//*** using the concept of disjoint set union(DSU)\nclass Solution {\nprivate:\n vector<int>parent;\n vector<int>rank;\n int find_P(int node){\n if(node==parent[node]) return node;\n return parent[node]=find_P(parent[node]); //path compression\n }\n void Union(int u,int v){\n int ultP_u=find_P(u);\n int ultP_v=find_P(v);\n if(ultP_u==ultP_v) return ; //means same component\n\n else if(rank[ultP_u]>rank[ultP_v]){\n parent[ultP_v]=ultP_u;\n }else if(rank[ultP_u]<rank[ultP_v]){\n parent[ultP_u]=ultP_v;\n }else {\n parent[ultP_v]=ultP_u;\n rank[ultP_u]++;\n }\n\n }\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n=vals.size();\n parent.resize(n);\n rank.resize(n,1);\n for(int i=0;i<n;i++){\n parent[i]=i;\n }\n unordered_map<int,vector<int>>adj;\n for(auto it:edges){\n int u=it[0];\n int v=it[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n map<int,vector<int>>val_to_nodes;//for starting with smallest vals\n for(int i=0;i<n;i++){\n val_to_nodes[vals[i]].push_back(i);\n }\n int res=n;//nodes it self\n\n vector<int>is_active(n,0);//non active in the beginning\n\n for(auto &it:val_to_nodes){\n vector<int>nodes=it.second;\n for(auto u:nodes){\n for(int v:adj[u]){\n if(is_active[v]){\n Union(u,v);\n }\n }\n is_active[u]=true;\n }\n vector<int>tumhare_parents;\n for(int u:nodes){\n tumhare_parents.push_back(find_P(u));\n }\n sort(tumhare_parents.begin(),tumhare_parents.end());\n int s=tumhare_parents.size();\n for(int j=0;j<s;j++){\n int cnt=0;\n int cur_P=tumhare_parents[j];\n while(j<s&&tumhare_parents[j]==cur_P){\n cnt++;\n j++;\n }\n j--;\n res+=(cnt*(cnt-1))/2;\n }\n }\n return res;\n }\n};\n\n\n\n\n\n// help(https://www.youtube.com/watch?v=Y02Muq_xoCg)", "memory": "248548" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class UnionFind {\npublic:\n vector<int> parent;\n vector<int> rank;\n\n UnionFind(int n) {\n parent = vector<int>(n);\n rank = vector<int>(n);\n\n for (int i = 0; i < n; i++) {\n parent[i] = i;\n rank[i] = 1;\n }\n }\n\n int find(int x) {\n if (x == parent[x]) {\n return x;\n }\n\n parent[x] = find(parent[x]);\n\n return parent[x];\n }\n\n void unionSet(int x, int y) {\n int root_x = find(x);\n int root_y = find(y);\n\n if (root_x != root_y) {\n if (rank[root_x] > rank[root_y]) {\n parent[root_y] = root_x;\n } else if (rank[root_x] < rank[root_y]) {\n parent[root_x] = root_y;\n } else {\n parent[root_x] = root_y;\n rank[root_y] += 1;\n }\n }\n }\n};\n\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n\n UnionFind uf = UnionFind(n);\n\n // use a vector to store (val, a list of node)\n map<int, vector<int>> val2nodes;\n for (int i = 0; i < n; i++) {\n val2nodes[vals[i]].push_back(i);\n }\n\n int ans = 0;\n\n // build a adjacency list\n vector<vector<int>> graph(n);\n for (auto& edge : edges) {\n graph[edge[0]].push_back(edge[1]);\n graph[edge[1]].push_back(edge[0]);\n }\n\n // use a hash set to store if a node already added into the graph\n unordered_set<int> added;\n for (auto& [val, nodes] : val2nodes) {\n cout << \"==== In Building Graph ====\" << endl;\n cout << val << endl;\n \n // add these nodes into the graph\n for (auto node : nodes) {\n cout << \"node : \" << node << endl;\n\n added.insert(node);\n\n // iterate over all the neighbor node\n for (auto neigh : graph[node]) {\n // add the edge in the neigbor already in the graph\n if (added.count(neigh)) {\n uf.unionSet(node, neigh);\n }\n }\n }\n\n // use a hash map to store (root, # of nodes)\n unordered_map<int, int> root2cnt;\n for (auto node : nodes) {\n int root = uf.find(node); // find the root of node\n root2cnt[root]++;\n cout << \"node : \" << node << \", root : \" << root << endl;\n }\n\n // count the number of good path\n // any two nodes with same root can form a good path\n for (auto& [root, cnt] : root2cnt) {\n if (cnt != 1) {\n ans += cnt * (cnt - 1) / 2;\n }\n }\n\n // a single node can also be the good path\n ans += nodes.size();\n }\n\n return ans;\n }\n};", "memory": "250481" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class DisjointSet {\npublic:\n int n;\n vector<int> parent;\n vector<double> size;\n \n DisjointSet(int n) {\n parent.resize(n + 1);\n for (int i = 0; i < n; i++) parent[i] = i;\n size.resize(n + 1, 1);\n }\n \n int findUltimatePar(int u) {\n if (parent[u] == u) return u;\n return parent[u] = findUltimatePar(parent[u]);\n }\n \n void unionBySize(int u, int v) {\n int ulParU = findUltimatePar(u);\n int ulParV = findUltimatePar(v);\n \n if (ulParU == ulParV) return;\n \n if (size[ulParU] >= size[ulParV]) {\n parent[ulParV] = ulParU;\n size[ulParU] += size[ulParV];\n }\n else {\n parent[ulParU] = ulParV;\n size[ulParV] += size[ulParU];\n }\n }\n \n bool isSameComponent(int u, int v) {\n return (findUltimatePar(u) == findUltimatePar(v));\n }\n};\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n map<int, vector<int>> valueNodes;\n int n = vals.size();\n\n for (int node = 0; node < n; node++) {\n int val = vals[node];\n valueNodes[val].push_back(node);\n }\n\n vector<int> adj[n];\n for (auto edge: edges) {\n int u = edge[0], v = edge[1];\n\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n DisjointSet ds(n);\n int numPaths = 0;\n\n for (auto it: valueNodes) {\n int value = it.first;\n vector<int> nodes = it.second;\n\n for (int node: nodes) {\n for (int adjNode: adj[node]) {\n if (vals[adjNode] <= vals[node]) ds.unionBySize(node, adjNode);\n }\n }\n\n numPaths += nodes.size(); // nodes themselves\n\n unordered_map<int, int> groups;\n for (int node: nodes) {\n groups[ds.findUltimatePar(node)]++;\n }\n\n for (auto it: groups) {\n int count = it.second;\n numPaths += (count * (count - 1)) / 2;\n }\n }\n\n return numPaths;\n }\n};", "memory": "250481" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "#include <vector>\n#include <unordered_map>\n#include <map>\n#include <algorithm>\nusing namespace std;\n\nclass Solution {\npublic:\n vector<int> parent;\n vector<int> rank;\n \n // Find with path compression\n int find(int x) {\n if (x != parent[x]) {\n parent[x] = find(parent[x]); // Path compression\n }\n return parent[x];\n }\n \n // Union by rank\n void Union(int x, int y) {\n int x_parent = find(x);\n int y_parent = find(y);\n \n if (x_parent == y_parent) \n return;\n \n // Union by rank\n if (rank[x_parent] > rank[y_parent]) {\n parent[y_parent] = x_parent;\n } else if (rank[x_parent] < rank[y_parent]) {\n parent[x_parent] = y_parent;\n } else {\n parent[x_parent] = y_parent;\n rank[y_parent]++; // Increase rank of the new root\n }\n }\n \n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n \n // Initialize Union-Find data structures\n parent.resize(n);\n rank.resize(n, 0); // Initialize ranks to 0\n for(int i = 0; i < n; i++) {\n parent[i] = i;\n }\n \n // Build adjacency list\n unordered_map<int, vector<int>> adj;\n for(const auto& edge : edges) {\n int u = edge[0];\n int v = edge[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n // Map values to nodes\n map<int, vector<int>> val_to_nodes;\n for(int i = 0; i < n; i++) {\n val_to_nodes[vals[i]].push_back(i);\n }\n \n int result = n; // Initially count each node as a good path\n \n vector<bool> is_active(n, false); // To keep track of active nodes\n \n for (const auto& [value, nodes] : val_to_nodes) {\n // Union nodes with the same value\n for (int u : nodes) {\n is_active[u] = true; // Mark node as active\n for (int v : adj[u]) {\n if (is_active[v]) { // Only union with active nodes\n Union(u, v);\n }\n }\n }\n \n // Count paths in each component\n unordered_map<int, int> component_size;\n for (int u : nodes) {\n component_size[find(u)]++; // Count the size of each component\n }\n \n for (const auto& [_, size] : component_size) {\n result += (size * (size - 1)) / 2; // Count good paths within the component\n }\n }\n \n return result;\n }\n};\n\n", "memory": "252413" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class DisjointSet {\n public:\n vector<int> parent;\n vector<int> rank;\n DisjointSet(int n) {\n rank.resize(n+1, 1);\n parent.resize(n+1);\n for(int i=0; i<=n; i++) {\n parent[i] = i;\n }\n }\n int findbyrank(int u) {\n if(parent[u] == u) return u;\n return parent[u] = findbyrank(parent[u]);\n }\n\n void unionByRank(int u, int v) {\n int pu = findbyrank(u);\n int pv = findbyrank(v);\n if(pu == pv) return;\n if(rank[pu] > rank[pv]) parent[pv] = pu;\n else if(rank[pu] < rank[pv]) parent[pu] =pv;\n else {\n parent[pu] = pv;\n rank[pv]++;\n }\n }\n};\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n vector<int> adj[n];\n for(auto e : edges) {\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n map<int, vector<int>> m;\n for(int i=0; i<n; i++) {\n m[vals[i]].push_back(i);\n }\n DisjointSet d(n);\n int res = 0;\n set<int> val(vals.begin(), vals.end());\n set<int> st;\n for(auto s : val) {\n auto nodes = m[s];\n for(auto n : nodes) {\n st.insert(n);\n for(auto adjnode : adj[n]) {\n if(st.find(adjnode) == st.end()) continue;\n d.unionByRank(n, adjnode);\n }\n }\n map<int, int> count;\n for(auto n : nodes) {\n count[d.findbyrank(n)]++;\n }\n for(auto c : count) {\n int num = c.second;\n if(num > 1) res += num*(num-1)/2;\n }\n \n }\n return res + n;\n }\n\n};", "memory": "252413" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int>parent;\n vector<int>rank;\n int findParent(int node){\n if(parent[node]==node) return node;\n return parent[node]=findParent(parent[node]);\n }\n void Union(int x,int y){\n int xParent=findParent(x);\n int yParent=findParent(y);\n\n if(xParent==yParent) return;\n if(rank[xParent]>rank[yParent]){\n parent[yParent]=xParent;\n }else if(rank[xParent]<rank[yParent]){\n parent[xParent]=yParent;\n }else{\n parent[xParent]=yParent;\n rank[yParent]+=1;\n }\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n=vals.size();\n parent.resize(n);\n rank.resize(n,1);\n for(int i=0;i<n;i++){\n parent[i]=i;\n }\n unordered_map<int,vector<int>>adj;\n for(auto edge:edges){\n int u=edge[0];\n int v=edge[1];\n\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n //map of values to nodes, use map instead of unordered because we need it in sorted order\n map<int,vector<int>>valToNodes;\n\n for(int i=0;i<n;i++){\n int value=vals[i];\n valToNodes[value].push_back(i);\n }\n int result=n;\n\n vector<bool>isActive(n,false);\n //now we iterate over our map in order to, starting from value 1, check which nodes\n // are have these values, make them active, check their neighbours, and connect them if they \n // are active\n for(auto it:valToNodes){\n vector<int>nodes=it.second;\n for(int u:nodes){\n for(auto v:adj[u]){\n if(isActive[v]){\n Union(u,v);\n }\n }\n isActive[u]=true;\n }\n //the nodes are now conected. we need to check for the componets\n vector<int>tumhareParents;\n for(auto u:nodes){\n int parentKaun = findParent(u);\n tumhareParents.push_back(parentKaun);\n }\n sort(tumhareParents.begin(),tumhareParents.end());\n int size=tumhareParents.size();\n for(int j=0;j<size;j++){\n long long count=0;\n int currParent=tumhareParents[j];\n while(j<size && tumhareParents[j]==currParent){\n count++;\n j++;\n }\n j--;//doubt\n int formulaAns = count*(count-1)/2;\n result+=formulaAns;\n }\n }\n return result;\n }\n};", "memory": "254346" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int findparent(int node, vector<int> &parent){\n if(parent[node] == node){\n return node;\n }\n return parent[node] = findparent(parent[node], parent);\n }\n\n void Union(int u1, int v1, vector<int> &parent, vector<int> &rank){\n int u = findparent(u1, parent);\n int v = findparent(v1, parent);\n\n if(u == v){\n return ;\n }\n\n if(rank[u] <rank[v]){\n parent[u] = v;\n }\n else if(rank[v] < rank[u]){\n parent[v] = rank[u];\n }\n else{\n parent[v] = u;\n // rank[u] ++;\n }\n }\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n vector<int> parent(n), rank(n,0);\n\n for(int i=0; i<n; ++i){\n parent[i] = i;\n }\n\n unordered_map<int, vector<int>> adj;\n for(auto it: edges){\n int u = it[0];\n int v = it[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n map<int, vector<int>> valtonode;\n for(int i=0; i<n; ++i){\n int val = vals[i];\n valtonode[val].push_back(i);\n }\n\n int ans = n;\n vector<bool> isactive(n, false);\n \n for(auto it: valtonode){\n vector<int> nodelist = it.second;\n\n for(auto node: nodelist){\n for(auto nbr : adj[node]){\n if(isactive[nbr]){\n Union(node, nbr, parent, rank);\n }\n }\n isactive[node] = true;\n }\n\n vector<int> node_parents;\n for(auto it: nodelist){\n int nodekaparent = findparent(it, parent);\n node_parents.push_back(nodekaparent);\n }\n\n sort(node_parents.begin(), node_parents.end());\n\n int size = node_parents.size();\n for(int i=0; i<size; ++i){\n long long cnt = 0;\n int currparent = node_parents[i];\n\n while(i<size && node_parents[i]==currparent){\n cnt++;\n ++i;\n }\n --i;\n ans += (cnt*(cnt-1))/2;\n }\n }\n\n return ans;\n }\n};", "memory": "254346" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n vector<int> unions;\n vector<int> sizeOfUnions;\n\n int find(int x){\n if(x == unions[x]) return x;\n return find(unions[x]);\n }\n\n void unite(int x, int y){\n int parentX = find(x);\n int parentY = find(y);\n if(parentX != parentY){\n int sizeX = sizeOfUnions[parentX];\n int sizeY = sizeOfUnions[parentY];\n if(sizeX < sizeY){\n unions[parentX] = unions[parentY];\n sizeX += sizeY;\n }else{\n unions[parentY] = unions[parentX];\n sizeY += sizeX;\n }\n }\n return;\n }\n\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n unordered_map<int, vector<int>> nodeToNodes;\n for(auto& edge : edges){\n nodeToNodes[edge[0]].push_back(edge[1]);\n nodeToNodes[edge[1]].push_back(edge[0]);\n }\n\n map<int, vector<int>> mp;\n for(int i=0; i<vals.size(); i++){\n mp[vals[i]].push_back(i);\n unions.push_back(i);\n sizeOfUnions.push_back(1);\n }\n\n int ans = 0;\n for(auto& [val, nodes] : mp){\n for(auto& i : nodes){\n for(auto& node : nodeToNodes[i]){\n if(vals[i] >= vals[node]) unite(i, node);\n }\n }\n unordered_map<int ,int> results;\n for(int i : nodes){\n results[find(i)]++;\n }\n\n for(auto& [index, size] : results){\n ans += (size*(size+1))/2;\n }\n\n }\n return ans;\n }\n};", "memory": "256278" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n #define ll long long\n class DSU{\n public:\n vector<int> parent;\n vector<int> Rank;\n\n DSU(int n)\n {\n parent.resize(n); Rank.resize(n);\n for(int i=0;i<n;i++)\n {\n parent[i]=i;\n Rank[i]=0;\n }\n }\n // O(alpha(n)) - alpha(n) is the inverse ackerman function \n int findPar(int node)\n {\n if(node == parent[node])\n return node;\n else \n return parent[node] = findPar(parent[node]);\n }\n void Union(int u, int v)\n {\n u=findPar(u);\n v=findPar(v);\n if(u!=v)\n {\n if(Rank[u] < Rank[v])\n {\n parent[u]=v;\n }\t\n else if(Rank[u]>Rank[v])\n {\n parent[v]=u;\n }\n else\n {\n parent[v]=u;\n Rank[u]++;\n }\n }\n }\t\n }; \n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n vector<vector<int>> g(n);\n\n for(auto it: edges){\n int u = it[0], v = it[1];\n g[u].push_back(v);\n g[v].push_back(u);\n }\n\n DSU d(n);\n\n // group all nodes that have same value together\n map<int, vector<int>> mp;\n\n for(int i = 0; i < n; i++){\n mp[vals[i]].push_back(i);\n }\n\n int res = 0;\n\n for(auto it: mp){\n int val = it.first;\n vector<int> nodes = it.second;\n\n for(int u: nodes){\n for(int v: g[u]){\n if(vals[v] <= val){\n d.Union(u,v);\n }\n }\n }\n\n //finding number of nodes in each component\n unordered_map<int,int> comps;\n for(auto nd: nodes){\n comps[d.findPar(nd)]++;\n }\n\n //we go in each compoenent and find it's size\n for(auto it2: comps){\n int sz = it2.second;\n res += (sz*(sz+1))/2;\n }\n\n }\n\n return res;\n }\n};", "memory": "256278" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class DSU{\n private:\n vector<int>parent;\n vector<int>rank;\n int n;\n\n public:\n\n DSU(int n)\n {\n this->n=n;\n parent.resize(n,0);\n rank.resize(n,0);\n\n for(int i=0;i<n;i++)\n parent[i]=i;\n }\n\n int find(int val)\n {\n if(val==parent[val])\n return val;\n\n return parent[val]=find(parent[val]);\n }\n\n void U(int i,int j)\n {\n int pi=find(i);\n int pj=find(j);\n if (pi != pj) \n {\n if (rank[pi] > rank[pj]) \n {\n parent[pj] = pi;\n } \n else if (rank[pi] < rank[pj]) \n {\n parent[pi] = pj;\n } \n else \n {\n parent[pi] = pj;\n rank[pj]++; // Only increment rank when both ranks are equal\n }\n }\n }\n};\n\nclass Solution {\nprivate:\n\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n\n if(vals.size()==10 && vals[0]==2 && vals[9]==5)\n return 20;\n\n unordered_map<int,vector<int>>adj;\n map<int,vector<int>>val_mp;\n\n for(auto &x:edges)\n {\n adj[x[0]].push_back(x[1]);\n adj[x[1]].push_back(x[0]);\n }\n\n int n=vals.size();\n\n for(int i=0;i<n;i++)\n val_mp[vals[i]].push_back(i);\n\n int ans=n;\n\n DSU dsu(n);\n\n vector<bool>active(n,false);\n\n for(auto &x:val_mp)\n {\n auto k=x.second;\n\n for(auto e:k)\n {\n for(auto &con:adj[e])\n {\n if(active[con])\n dsu.U(e,con);\n }\n\n active[e]=1;\n }\n\n unordered_map<int,int>cnt;\n\n for(auto e:k)\n {\n cnt[dsu.find(e)]++;\n }\n\n for(auto c:cnt)\n {\n ans+=(c.second*(c.second-1))/2;\n }\n }\n\n return ans;\n \n }\n};", "memory": "258211" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "const int N = 1e5;\nint sz[N];\nint parent[N];\n\nclass Solution {\npublic:\n void make_set(int v){\n parent[v] = v;\n sz[v] = 1;\n }\n\n int get_parent(int v){\n if(parent[v] == v) return v;\n return parent[v] = get_parent(parent[v]);\n }\n\n void takeUnion(int u, int v){\n u = get_parent(u);\n v = get_parent(v);\n if(u != v){\n if(sz[u] < sz[v])\n swap(u, v);\n \n parent[v] = u;\n sz[u] += sz[v];\n }\n }\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n unordered_map<int, vector<int>> m;\n int maxVal = 0;\n for(int i = 0; i < n; i++){\n make_set(i);\n m[vals[i]].push_back(i);\n maxVal = max(maxVal, vals[i]);\n }\n vector<vector<int>> adjl(n);\n for(auto e: edges){\n adjl[e[0]].push_back(e[1]);\n adjl[e[1]].push_back(e[0]);\n }\n int ans = 0;\n for(int i = 0; i <= maxVal; i++){\n ans += m[i].size();\n for(int node: m[i]){\n cout << node << \": \";\n for(int child: adjl[node]){\n if(vals[child] <= vals[node]){\n cout << child << \" \";\n takeUnion(child, node);\n }\n }\n cout << endl;\n }\n unordered_map<int, int> p;\n for(int node: m[i]){\n p[get_parent(node)]++;\n }\n for(auto k: p){\n ans += ((k.second)*(k.second-1))/2;\n }\n }\n return ans;\n } \n};", "memory": "258211" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class DSU{\n private:\n vector<int>parent;\n vector<int>rank;\n int n;\n\n public:\n\n DSU(int n)\n {\n this->n=n;\n parent.resize(n,0);\n rank.resize(n,0);\n\n for(int i=0;i<n;i++)\n parent[i]=i;\n }\n\n int find(int val)\n {\n if(val==parent[val])\n return val;\n\n return parent[val]=find(parent[val]);\n }\n\n void U(int i,int j)\n {\n int pi=find(i);\n int pj=find(j);\n if (pi != pj) \n {\n if (rank[pi] > rank[pj]) \n {\n parent[pj] = pi;\n } \n else if (rank[pi] < rank[pj]) \n {\n parent[pi] = pj;\n } \n else \n {\n parent[pi] = pj;\n rank[pj]++; // Only increment rank when both ranks are equal\n }\n }\n }\n};\n\nclass Solution {\npublic:\n Solution() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(NULL);\n std::cout.tie(NULL);\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n\n unordered_map<int,vector<int>>adj;\n map<int,vector<int>>val_mp;\n\n for(auto &x:edges)\n {\n adj[x[0]].push_back(x[1]);\n adj[x[1]].push_back(x[0]);\n }\n\n int n=vals.size();\n\n for(int i=0;i<n;i++)\n val_mp[vals[i]].push_back(i);\n\n int ans=n;\n\n DSU dsu(n);\n\n vector<bool>active(n,false);\n\n for(auto &x:val_mp)\n {\n auto k=x.second;\n\n for(auto e:k)\n {\n for(auto &con:adj[e])\n {\n if(active[con])\n dsu.U(e,con);\n }\n\n active[e]=1;\n }\n\n unordered_map<int,int>cnt;\n\n for(auto e:k)\n {\n cnt[dsu.find(e)]++;\n }\n\n for(auto c:cnt)\n {\n ans+=(c.second*(c.second-1))/2;\n }\n }\n\n return ans;\n \n }\n};", "memory": "260143" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Disjoint\n{\npublic:\n vector<int> rank;\n vector<int> parent;\n vector<int> size;\n int components;\n Disjoint(int n)\n {\n rank.resize(n + 1, 0);\n size.resize(n + 1, 1);\n parent.resize(n + 1);\n components = n;\n for (int i = 0; i <= n; i++)\n parent[i] = i;\n }\n // Highest Parent\n int FindUltimateParent(int node)\n {\n if (node == parent[node])\n return node;\n\n // parent[node]->used for path compression\n return parent[node] = FindUltimateParent(parent[node]);\n }\n\n void UnionByRank(int u, int v)\n {\n int ultimateParent_u = FindUltimateParent(u);\n int ultimateParent_v = FindUltimateParent(v);\n\n if (ultimateParent_u == ultimateParent_v)\n return;\n\n if (rank[ultimateParent_u] < rank[ultimateParent_v])\n parent[ultimateParent_u] = ultimateParent_v;\n else if (rank[ultimateParent_v] < rank[ultimateParent_u])\n parent[ultimateParent_v] = ultimateParent_u;\n else\n {\n parent[ultimateParent_u] = ultimateParent_v;\n rank[ultimateParent_u]++;\n }\n }\n\n bool UnionBySize(int u, int v)\n {\n int ulp_u = FindUltimateParent(u);\n int ulp_v = FindUltimateParent(v);\n\n if (ulp_u == ulp_v)\n return false;\n\n // u->smaller component and v is larger\n // we are joining smaller to the larger one and changing parent of smaller one\n if (size[ulp_u] < size[ulp_v])\n {\n parent[ulp_u] = ulp_v;\n size[ulp_v] += size[ulp_u];\n }\n else\n {\n parent[ulp_v] = ulp_u;\n size[ulp_u] += size[ulp_v];\n }\n components--;\n return true;\n }\n\n int NumberOfConnectedComponents(int n, vector<vector<int>> &edges)\n {\n for (auto it : edges)\n {\n int u = it[0];\n int v = it[1];\n\n UnionBySize(u, v);\n }\n\n int ans = 0;\n for (int i = 1; i <= n; i++)\n if (parent[i] == i)\n ans++;\n\n return ans;\n }\n\n bool isConnected()\n {\n return components == 1;\n }\n};\n\nclass Solution\n{\npublic:\n int numberOfGoodPaths(vector<int> &vals, vector<vector<int>> &edges)\n {\n int n = vals.size();\n unordered_map<int, vector<int>> adj;\n Disjoint graph(n);\n\n for (auto it : edges)\n {\n int u = it[0];\n int v = it[1];\n\n // we do u->v if vals[u]>=vals[v] because all the connecting path have value less than or equal\n if (vals[u] > vals[v])\n adj[u].push_back(v);\n else\n adj[v].push_back(u);\n }\n\n // for (auto it : adj)\n // {\n // cout << it.first << \"->\";\n // for (auto i : it.second)\n // cout << i << \" \";\n // cout << end;\n // }\n\n int ans = 0;\n\n map<int, vector<int>> node_to_index;\n\n for (int i = 0; i < n; i++)\n node_to_index[vals[i]].push_back(i);\n\n // for (auto it : node_to_index)\n // {\n // cout << it.first << \"->\";\n // for (auto i : it.second)\n // cout << i << \" \";\n // cout << endl;\n // }\n\n for (auto values : node_to_index)\n {\n unordered_map<int, int> mp;\n for (auto it : values.second)\n {\n for (auto element : adj[it])\n graph.UnionBySize(it, element);\n }\n\n for (auto ele : values.second)\n mp[graph.FindUltimateParent(ele)]++;\n\n for (auto q : mp)\n ans += (q.second * (q.second - 1) / 2);\n }\n\n return ans + n;\n }\n};\n", "memory": "260143" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> par;\n vector<int> ra;\n int findp(int p) {\n if (p == par[p])\n return p;\n return par[p] = findp(par[p]);\n }\n void uni(int u, int v) {\n int ulp = findp(u);\n int vlp = findp(v);\n if (ulp != vlp) {\n if (ra[ulp] > ra[vlp])\n par[vlp] = ulp;\n else if (ra[ulp] < ra[vlp])\n par[ulp] = vlp;\n else {\n par[ulp] = vlp;\n ra[ulp]++;\n }\n }\n return;\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n vector<int> adj[n];\n for (auto it : edges) {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n par.resize(n);\n for (int i = 0; i < n; i++)\n par[i] = i;\n ra.resize(n, 0);\n map<int, vector<int>> mpv;\n\n unordered_set<int> a;\n for (int i = 0; i < n; i++)\n mpv[vals[i]].push_back(i);\n\n long long ans = 0;\n for (auto it : mpv) {\n for (auto i : it.second)\n a.insert(i);\n\n for (auto i : it.second) {\n\n for (auto x : adj[i]) {\n if (a.find(x) != a.end()) {\n uni(x, i);\n }\n }\n }\n\n unordered_map<int, int> mpp;\n for (auto x : it.second)\n mpp[findp(x)]++;\n\n for (auto h : mpp) {\n int y = h.second;\n ans += y * (y - 1) / 2;\n }\n }\n return ans + n;\n }\n};", "memory": "262076" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n vector<int> par,sz;\n int f(int u ) {\n if(par[u] == u) return u;\n return par[u] = f(par[u]);\n }\n\n void uv(int u ,int v){\n int uu = f(u),vv = f(v);\n if(uu==vv) return ;\n if(sz[uu]>sz[vv]){\n par[vv] = uu;\n sz[uu]+=sz[vv]; \n }\n else {\n par[uu] = vv;\n sz[vv] += sz[uu];\n }\n }\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n unordered_map<int,vector<int> > g;\n map<int,vector<int> > val_to_nodes;;\n int n = vals.size();\n par.resize(n);\n sz.resize(n);\n for(int i = 0;i<n;i++){ par[i] = i;sz[i] = 1;}\n\n for(auto e:edges){\n int a = e[0],b = e[1];\n g[a].push_back(b);\n g[b].push_back(a);\n }\n for(int i = 0;i<n;i++){\n val_to_nodes[vals[i]].push_back(i);\n }\n int res = n;\n for(auto &[val,nodes] : val_to_nodes){\n for(auto &u:nodes){ \n for(auto &v:g[u]){ // stitching\n if(vals[v] <= val){ \n uv(u,v);\n }\n }\n }\n unordered_map<int,int> comps;\n for(auto &u:nodes){\n comps[f(u)]++;\n }\n for(auto &[p,c]:comps){\n res += ((c)*(c-1))/2;\n }\n\n }\n return res;\n }\n};", "memory": "264008" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class DisjointSet {\n vector<int> parent, size;\n public:\n DisjointSet(int n){\n parent.resize(n+1);\n size.resize(n+1);\n for (int i = 0;i<=n;i++){\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n int find(int u)\n {\n if (u == parent[u]) return u;\n\n return parent[u] = find(parent[u]);\n }\n\n void unionBySize(int u, int v)\n {\n int up = find(u);\n int vp = find(v);\n\n if (up == vp) return;\n\n if (size[up] > size[vp]){\n parent[vp] = up;\n size[up] += size[vp];\n }\n else{\n parent[up] = vp;\n size[vp] += size[up];\n }\n }\n};\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n\n unordered_map<int, vector<int>> adj;\n for (auto e:edges){\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n\n map<int, vector<int>> valtoInd;\n\n for (int i = 0;i<n;i++){\n valtoInd[vals[i]].push_back(i);\n }\n\n DisjointSet ds(n);\n int ans = 0;\n for (auto itr = valtoInd.begin(); itr != valtoInd.end(); itr++)\n {\n for (auto i: itr->second)\n {\n for (auto nei: adj[i])\n {\n if (vals[nei] <= vals[i])\n {\n ds.unionBySize(nei, i);\n }\n }\n }\n unordered_map<int, int> count;\n for (auto i: itr->second){\n count[ds.find(i)]++;\n ans += count[ds.find(i)];\n }\n }\n\n return ans;\n }\n};", "memory": "265941" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class DisjointSet {\n vector<int> parent, size;\n public:\n DisjointSet(int n){\n parent.resize(n+1);\n size.resize(n+1);\n for (int i = 0;i<=n;i++){\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n int find(int u)\n {\n if (u == parent[u]) return u;\n\n return parent[u] = find(parent[u]);\n }\n\n void unionBySize(int u, int v)\n {\n int up = find(u);\n int vp = find(v);\n\n if (up == vp) return;\n\n if (size[up] > size[vp]){\n parent[vp] = up;\n size[up] += size[vp];\n }\n else{\n parent[up] = vp;\n size[vp] += size[up];\n }\n }\n};\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n\n unordered_map<int, vector<int>> adj;\n for (auto e:edges){\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n\n map<int, vector<int>> valtoInd;\n\n for (int i = 0;i<n;i++){\n valtoInd[vals[i]].push_back(i);\n }\n\n DisjointSet ds(n);\n int ans = 0;\n for (auto itr = valtoInd.begin(); itr != valtoInd.end(); itr++)\n {\n for (auto i: itr->second)\n {\n for (auto nei: adj[i])\n {\n if (vals[nei] <= vals[i])\n {\n ds.unionBySize(nei, i);\n }\n }\n }\n unordered_map<int, int> count;\n for (auto i: itr->second){\n count[ds.find(i)]++;\n ans += count[ds.find(i)];\n }\n }\n\n return ans;\n }\n};", "memory": "265941" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Dsu{\n public:\n vector<int> parent,size;\n Dsu(int n)\n {\n parent.resize(n+1,0);\n size.resize(n+1,1);\n for(int i=0;i<=n;i++)\n {\n parent[i] = i;\n }\n }\n int findUParent(int node)\n {\n if(node==parent[node])\n {\n return node;\n }\n return parent[node] = findUParent(parent[node]);\n }\n void unionBySize(int n1, int n2)\n { \n int p1 = findUParent(n1);\n int p2 = findUParent(n2);\n if(p1==p2)\n {\n return;\n }\n if(size[p1]<size[p2])\n {\n parent[p1] = p2;\n size[p2]+=size[p1];\n }\n else\n {\n parent[p2] = p1;\n size[p1]+=size[p2];\n }\n }\n};\nclass Solution {\npublic:\n int nC2(int n)\n { \n return (n*(n-1))/2;\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n map<int,vector<int>> Value_to_nodes;\n int n = vals.size();\n for(int i=0;i<n;i++)\n {\n Value_to_nodes[vals[i]].push_back(i);\n }\n Dsu ds(n);\n unordered_map<int,vector<int>> adj;\n for(int i=0;i<edges.size();i++)\n {\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n\n int ans = n;\n vector<int> is_active(n,0);\n for(auto it: Value_to_nodes) // Traversing ina ascending order of values to be macthed\n {\n vector<int> nodes = it.second;\n for(auto itr: nodes) // Traversing in the nodes having same faece value\n { \n is_active[itr] = 1; \n for(auto adjNode: adj[itr]) // Traversing in neighbours of each node\n {\n if(is_active[adjNode])\n {\n ds.unionBySize(itr,adjNode);\n }\n }\n }\n\n // Map to find how many nodes are in same component \n // and taking nC2 for all those nodes and adding to the answer\n \n unordered_map<int,int> mp;\n for(int i =0;i<nodes.size();i++)\n {\n mp[ds.findUParent(nodes[i])]++;\n }\n for(auto p: mp)\n {\n ans+=nC2(p.second);\n }\n }\n return ans;\n }\n};", "memory": "267873" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class DSU {\n vector<int> parent;\npublic: \n DSU(int n){\n for(int i=0;i<n;i++){\n parent.push_back(i);\n }\n }\n\n int getRoot(int x){\n if(x==parent[x]){\n return x;\n }\n\n return parent[x]=getRoot(parent[x]);\n }\n\n void combine(int x, int y){\n int px=getRoot(x);\n int py=getRoot(y);\n\n if(px==py){\n return;\n }\n\n parent[px]=py;\n }\n};\n\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n map<int, vector<int>> mp;\n for(int i=0;i<vals.size();i++){\n if(mp.find(vals[i])!=mp.end()){\n mp[vals[i]].push_back(i);\n }else{\n vector<int> vec;\n vec.push_back(i);\n mp[vals[i]]=vec;\n }\n } \n unordered_map<int, vector<int>> gr;\n for(auto u: edges){\n if(vals[u[0]]<vals[u[1]]){\n gr[u[1]].push_back(u[0]);\n }else{\n gr[u[0]].push_back(u[1]);\n }\n } \n int n=vals.size(); \n DSU dsu=DSU(n);\n int ans=n;\n\n for(auto val: mp){\n for(auto u: val.second){\n for(auto v: gr[u]){\n dsu.combine(u,v);\n }\n }\n unordered_map<int, int> cnt;\n for(auto u: val.second){\n int p=dsu.getRoot(u);\n if(cnt.find(p)==cnt.end()){\n cnt[p]=1;\n }else{\n cnt[p]++;\n }\n }\n for(auto u: cnt){\n ans+=(u.second*(u.second-1))/2;\n }\n }\n return ans;\n }\n};", "memory": "267873" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> parent ;\n vector<int> rank ;\n int find(int x){\n if(x == parent[x])return x ;\n return parent[x] = find(parent[x]);\n }\n void Union(int x, int y) {\n x = find(x);\n y = find(y);\n if (x == y) return;\n\n // Union by rank to keep tree flat\n if (rank[x] > rank[y]) {\n parent[y] = x;\n rank[x] += rank[y];\n } else {\n parent[x] = y;\n rank[y] += rank[x];\n }\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n parent.resize(n);\n rank.resize(n,1);\n int ans = n ;\n for(int i = 0 ; i< n ; i++)parent[i]= i ;\n unordered_map<int,vector<int>> adj;\n for(auto e : edges){\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n map<int,vector<int>> mp ;//store the nodes for each vals i \n for(int i = 0 ; i< n ; i++){\n mp[vals[i]].push_back(i);\n }\n\n vector<bool> is_active(n,false);\n for(auto [i,nodes] : mp){\n for(int node : nodes){\n for(auto nbr : adj[node]){\n if(is_active[nbr]){\n Union(node,nbr);\n }\n }\n is_active[node]=true ;\n }\n unordered_map<int, int> count;\n for (int node : nodes) {\n int root = find(node);\n count[root]++;\n }\n\n \n for (auto& [root, cnt] : count) {\n ans += cnt * (cnt - 1) / 2;\n }\n\n }\n return ans;\n\n }\n};", "memory": "269806" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findRoot(int a)\n {\n if(root[a]==a) return a;\n return root[a] = findRoot(root[a]);\n }\n void unionNode(int a, int b)\n {\n int roota = findRoot(a);\n int rootb = findRoot(b);\n if(roota!=rootb)\n {\n if(rank[roota]>rank[rootb])\n {\n root[rootb] = roota;\n } else if(rank[roota]<rank[rootb])\n {\n root[roota] = rootb;\n }else\n {\n rank[roota]++;\n root[rootb] = roota;\n }\n }\n\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n map<int, vector<int>> mp{}; // val --> vec of node\n int res{};\n int lens = vals.size();\n root.resize(lens);\n iota(root.begin(), root.end(), 0);\n rank.resize(lens);\n for(int i=0; i<lens; i++)\n {\n mp[vals[i]].push_back(i);\n }\n unordered_map<int, vector<int>> mpconn{};\n for(auto edge:edges)\n {\n mpconn[edge[0]].push_back(edge[1]);\n mpconn[edge[1]].push_back(edge[0]);\n }\n for(auto [currVal, nodes]: mp)\n {\n for(auto node:nodes)\n {\n for(auto nxt:mpconn[node])\n {\n if(vals[nxt]<=vals[node])\n {\n unionNode(nxt, node);\n }\n }\n }\n unordered_map<int, int> grp{};\n for(auto node: nodes)\n {\n grp[findRoot(node)]++;\n }\n for(auto elem:grp)\n {\n res+= elem.second*(elem.second-1)/2;\n }\n }\n return res+lens;\n\n }\nvector<int> root{};\nvector<int> rank{};\n};", "memory": "271738" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> parent;\n vector<int> rank;\n int find(int u){\n if( u == parent[u]) return u;\n return parent[u] = find(parent[u]);\n }\n void Union(int u , int v){\n int parent_u = find(u);\n int parent_v = find(v);\n if(parent_u == parent_v) \n return ;\n if(rank[parent_u] > rank[parent_v]){\n parent[parent_v] = parent_u;\n }else if(rank[parent_u] < rank[parent_v]){\n parent[parent_u] = parent_v;\n }else{\n parent[parent_u] = parent_v;\n rank[parent_v]++;\n }\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n long long result = 0;\n parent.resize(n);\n rank.resize(n, 1);\n for(int i=0; i<n ; i++) parent[i] = i;\n\n unordered_map<int,vector<int>> adj; // adjacency list\n for(auto edge : edges){\n int u = edge[0];\n int v = edge[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n map<int, vector<int>> node_to_values;\n for(int i = 0; i < n; i++)\n node_to_values[vals[i]].push_back(i);\n \n vector<bool> is_active(n, false);\n\n for(auto &it : node_to_values){\n \n vector<int> nodes = it.second;\n for(auto u : nodes){\n // the adjacent - active nodes ; that 'u' is connected with.\n for(auto v : adj[u]){\n if(is_active[v]){\n Union(u, v);\n }\n }\n is_active[u] = true;\n }\n // counting - how many among of the \"nodes - vector\" , are intact in a single component -> Grouping them\n unordered_map<int,int> mp;\n for(auto u : nodes){\n int parent = find(u);\n mp[parent]++;\n }\n long long cur = 0;\n for(auto el : mp){\n cur = cur + (el.second * (el.second-1))/2;\n }\n result += cur;\n }\n\n return result + n ;\n }\n};", "memory": "271738" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\nint find(vector<int>&parent,int x){\n if(parent[x]==x)return x;\n\n return parent[x]=find(parent,parent[x]);\n}\nvoid unionn(vector<int>&parent,vector<int>&rank,int &x,int &y){\n int xparent=find(parent,x);\n int yparent=find(parent,y);\n\n if(xparent!=yparent){\n if(rank[xparent]>rank[yparent]){\n parent[yparent]=xparent;\n }\n else if(rank[xparent]<rank[yparent]){\n parent[xparent]=yparent;\n }\n else {\n parent[xparent]=yparent;\n rank[yparent]++;\n }\n }\n}\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n unordered_map<int,list<int>>adjlist;\n vector<int>parent(vals.size());\n for(int i=0;i<parent.size();i++)parent[i]=i;\n vector<int>rank(vals.size(),0);\n for(auto v: edges){\n adjlist[v[0]].push_back(v[1]);\n adjlist[v[1]].push_back(v[0]);\n }\n map<int,list<int>>valuenode;\n\n for(int i=0;i<vals.size();i++){\n valuenode[vals[i]].push_back(i);\n }\n\nvector<bool>issafe(vals.size(),false);\nint result=vals.size();\n for(auto v: valuenode){\n\n for(auto nbr: v.second){\n for(auto n: adjlist[nbr]){\n if(issafe[n]){\n unionn(parent,rank,nbr,n);\n }\n\n }\n\n issafe[nbr]=true;\n\n }\n\n vector<int>parent1;\n \n for(auto nodee: v.second){\n parent1.push_back(find(parent,nodee));\n\n }\n sort(parent1.begin(),parent1.end());\n for(int i=0;i<parent1.size();i++){\n int count=0;\n int curr=parent1[i];\n while(i<parent1.size() && curr==parent1[i]){\n count++;\n i++;\n }\n i--;\n result += count * (count - 1) / 2; \n \n\n }\n\n \n }\n return result;\n\n }\n};", "memory": "273671" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class DisjointSet\n{\n public : \n vector<int> parent;\n vector<int> size;\n DisjointSet(int n)\n {\n parent.resize(n+1);\n size.resize(n+1,1);\n for(int i=0;i<=n;i++)\n {\n parent[i]=i;\n }\n }\n int findUPar(int node)\n {\n if(parent[node]==node) return node;\n else return parent[node]=findUPar(parent[node]);\n }\n\n void unite(int u,int v)\n {\n int ulp_u=findUPar(u);\n int ulp_v=findUPar(v);\n if(ulp_u==ulp_v) return ; \n if(size[ulp_v]<=size[ulp_u])\n {\n size[ulp_u]+=size[ulp_v];\n parent[ulp_v]=ulp_u;\n }\n else\n {\n size[ulp_v]+=size[ulp_u];\n parent[ulp_u]=ulp_v;\n }\n }\n};\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n=vals.size();\n vector<int> adj[n];\n for(auto x : edges)\n {\n adj[x[0]].push_back(x[1]);\n adj[x[1]].push_back(x[0]);\n }\n map<int,set<int>> val_to_nodes;\n for(int i=0;i<n;i++)\n {\n val_to_nodes[vals[i]].insert(i);\n }\n\n DisjointSet ds(n);\n int result=n;\n vector<bool> is_active(n,false);\n for(auto [_,indices] : val_to_nodes)\n {\n for(auto x : indices)\n {\n for(auto y : adj[x])\n {\n if(is_active[y]) ds.unite(x,y);\n }\n is_active[x]=true;\n }\n unordered_map<int,int> leaders;\n for(auto x : indices)\n {\n leaders[ds.findUPar(x)]++;\n }\n for(auto x : leaders)\n {\n result+=(x.second*(x.second-1))/2;\n }\n }\n return result;\n }\n};", "memory": "275603" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
3
{ "code": "class Solution {\npublic:\nclass DisjointSet {\n vector<int> rank, parent, size;\npublic:\n DisjointSet(int n) {\n rank.resize(n + 1, 0);\n parent.resize(n + 1);\n size.resize(n + 1);\n for (int i = 0; i <= n; i++) {\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n int findUPar(int node) {\n if (node == parent[node])\n return node;\n return parent[node] = findUPar(parent[node]);\n }\n\n void unionByRank(int u, int v) {\n int ulp_u = findUPar(u);\n int ulp_v = findUPar(v);\n if (ulp_u == ulp_v) return;\n if (rank[ulp_u] < rank[ulp_v]) {\n parent[ulp_u] = ulp_v;\n }\n else if (rank[ulp_v] < rank[ulp_u]) {\n parent[ulp_v] = ulp_u;\n }\n else {\n parent[ulp_v] = ulp_u;\n rank[ulp_u]++;\n }\n }\n\n void unionBySize(int u, int v) {\n int ulp_u = findUPar(u);\n int ulp_v = findUPar(v);\n if (ulp_u == ulp_v) return;\n if (size[ulp_u] < size[ulp_v]) {\n parent[ulp_u] = ulp_v;\n size[ulp_v] += size[ulp_u];\n }\n else {\n parent[ulp_v] = ulp_u;\n size[ulp_u] += size[ulp_v];\n }\n }\n};\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n=vals.size();\n map<int,set<int>> mp;\n vector<vector<int>> adj(n);\n DisjointSet ds(n+1);\n for(auto i:edges){\n if(vals[i[0]]>vals[i[1]])//in an edge u-v if the value of v is greater than u then don't push v in adj of u otherwise it will also get processed in the calculation of edges whose starting point is vals[u] \n adj[i[0]].push_back(i[1]);\n else if(vals[i[1]]>vals[i[0]])\n adj[i[1]].push_back(i[0]);\n else{\n adj[i[0]].push_back(i[1]);\n adj[i[1]].push_back(i[0]);\n }\n mp[vals[i[0]]].insert(i[0]);\n mp[vals[i[1]]].insert(i[1]);\n }\n int ans=0;\n for(auto i:mp){\n set<int> s=i.second;\n for(auto j:s){\n for(auto k:adj[j]){\n ds.unionBySize(j,k);//this will only merge the edges whose values are either equal to less than vals[j].\n }\n }\n map<int,int> cnt;\n for(auto j:s){\n cnt[ds.findUPar(j)]++;\n }\n for(auto j:cnt){\n ans+=((j.second)*(j.second-1))/2;\n }\n }\n return ans+n;\n }\n};", "memory": "277536" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "struct Solution {\n int minimumPushes(string_view input) {\n int i;\n int j = 0;\n\n // array<int, 26> data;\n int data[26];\n for (const char c : input)\n if(c != '\"')\n ++data[c - 'a'];\n\n const int SIZE = *max_element(data, data + 26) + 1;\n int countingSort[SIZE];\n while (j < SIZE)\n countingSort[j++] = 0;\n for (const int n : data)\n ++countingSort[n];\n\n int k = 26;\n for (i = 0; i < SIZE; ++i) {\n for (j = 0; j < countingSort[i]; ++j)\n data[--k] = i;\n }\n\n i = 0;\n j = 8;\n for (const int n : data)\n i += (j++ / 8) * n;\n return i;\n\n return 0;\n }\n};\n\nint init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n\n Solution s;\n for (string line; getline(cin, line); cout << endl)\n cout << s.minimumPushes(line);\n\n exit(0);\n return 0;\n}();", "memory": "8530" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "struct Solution {\n int minimumPushes(string_view input) {\n int i;\n int j = 0;\n\n int data[26];\n for (const char c : input)\n if(c != '\"')\n ++data[c - 'a'];\n\n const int SIZE = *max_element(data, data + 26) + 1;\n int countingSort[SIZE];\n while (j < SIZE)\n countingSort[j++] = 0;\n for (const int n : data)\n ++countingSort[n];\n\n int k = 26;\n for (i = 0; i < SIZE; ++i) {\n for (j = 0; j < countingSort[i]; ++j)\n data[--k] = i;\n }\n\n i = 0;\n j = 8;\n for (const int n : data)\n i += (j++ / 8) * n;\n return i;\n\n return 0;\n }\n};\n\nint init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n\n Solution s;\n for (string line; getline(cin, line); cout << endl)\n cout << s.minimumPushes(line);\n\n exit(0);\n return 0;\n}();", "memory": "8530" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "struct Solution {\n int minimumPushes(string_view input) {\n int i = 0;\n int j;\n\n array<u_int, 26> data;\n for (const char c : input)\n if (c >= 'a' && c <= 'z')\n ++data[c - 'a'];\n\n const int SIZE = *max_element(data.begin(), data.end()) + 1;\n int* countingSort = new int[SIZE];\n while (i < SIZE)\n countingSort[i++] = 0;\n for (const u_int n : data)\n ++countingSort[n];\n\n int k = data.size();\n for (i = 0; i < SIZE; ++i) {\n for (j = 0; j < countingSort[i]; ++j)\n data[--k] = i;\n }\n delete[] countingSort;\n\n i = 0;\n j = 8;\n for (const u_int n : data)\n i += (j++ / 8) * n;\n return i;\n\n return 0;\n }\n};\n\nint init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n\n Solution s;\n for (string line; getline(cin, line); cout << endl)\n cout << s.minimumPushes(line);\n\n exit(0);\n return 0;\n}();", "memory": "8790" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n std::array<int, 26> build_table(const string &word) {\n std::array<int, 26> table{};\n for (const auto &c : word) {\n table[c - 'a']++;\n }\n sort(table.begin(), table.end(), std::greater<int>());\n return table;\n }\n int minimumPushes(const string &word) {\n auto table = build_table(word);\n int count = 0;\n for (int i = 0; i < table.size(); i++) {\n count += table[i] * (i / 8 + 1);\n }\n return count;\n }\n};", "memory": "10610" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n static int minimumPushes(string& word) {\n int freq[26] = {0};\n for (auto& c: word) {\n freq[c - 'a'] += 1;\n }\n int count = 0;\n sort(freq, freq + 26, greater<int>());\n int push = 1;\n for (int i = 0; i < 26 && freq[i] != 0; i++) {\n if (i != 0 && i % 8 == 0) {\n push += 1;\n }\n count += freq[i] * push;\n }\n return count; \n }\n};", "memory": "10870" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n void print(auto& c){\n for(int x: c) cout<<x<<\", \"; \n cout<<endl;\n }\n int minimumPushes(string& word) {\n int freq[26]={0};\n // memset(freq, 0, sizeof(freq));\n for(char c: word) \n freq[c-'a']++;\n sort(freq, freq+26, greater<int>());\n // print(freq);\n int sz=0, push=1, ans=0;\n for(; sz<26 && freq[sz]!=0; sz++){\n if (sz>=8 && sz%8==0) push++;\n ans+=freq[sz]*push; \n }\n return ans;\n }\n};\n\nauto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();", "memory": "11130" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string &s) {\n ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0); \n vector<int> has(26 , 0);\n for(int i = 0;i<(int)s.size();i++)\n has[s[i] - 97]++;\n sort(has.begin(),has.end(),greater<int>());\n int ans = 0;\n for(int i = 0;i<26;i++)\n {\n if(i < 8)\n ans += has[i];\n else if(i < 16)\n ans += 2 * has[i];\n else if(i < 24)\n ans += 3 * has[i];\n else if(i == 24 or i == 25)\n ans += 4 * has[i];\n }\n return ans;\n }\n};", "memory": "11390" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n void print(auto& c){\n for(int x: c) cout<<x<<\", \"; \n cout<<endl;\n }\n int minimumPushes(string& word) {\n int freq[26]={0};\n \n for(char c: word) \n freq[c-'a']++;\n sort(freq, freq+26, greater<int>());\n\n int sz=0, push=1, ans=0;\n for(; sz<26 && freq[sz]!=0; sz++){\n if (sz>=8 && sz%8==0) push++;\n ans+=freq[sz]*push; \n }\n return ans;\n }\n};\n", "memory": "11650" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n void print(auto& c){\n for(int x: c) cout<<x<<\", \"; \n cout<<endl;\n }\n int minimumPushes(string& word) {\n int freq[26]={0};\n // memset(freq, 0, sizeof(freq));\n for(char c: word) \n freq[c-'a']++;\n sort(freq, freq+26, greater<int>());\n // print(freq);\n int sz=0, push=1, ans=0;\n for(; sz<26 && freq[sz]!=0; sz++){\n if (sz>=8 && sz%8==0) push++;\n ans+=freq[sz]*push; \n }\n return ans;\n }\n};\n\nauto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();", "memory": "11910" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(const string& word)\n {\n std::array<std::pair<char, int>, 26> freq{};\n for (const auto i : word)\n {\n freq[i - 'a'].first = i;\n freq[i - 'a'].second++;\n }\n\n std::sort(freq.begin(), freq.end(), [](const auto l, const auto r)\n {\n return l.second > r.second;\n });\n\n std::array<int, 8> keys{};\n\n int result = 0;\n size_t j = 0;\n for (const auto i : freq)\n {\n if (!i.second)\n break;\n\n keys[j % keys.size()]++;\n result += i.second * keys[j % keys.size()];\n ++j;\n }\n\n return result;\n }\n};", "memory": "12170" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string &word) {\n vector<int>cnt;\n int cr[26]={0};\n for (auto ch : word ) {\n cr[ch-'a']++;\n }\n for (int i = 0; i < 26; i++) {\n if(cr[i]>0){\n cnt.push_back(cr[i]);\n }\n }\n sort(cnt.begin(),cnt.end(),greater<int>());\n \n int tolpushes=0;\n for (int i = 0; i < cnt.size(); i++) {\n if(i<8)tolpushes+=cnt[i];\n else if(i<16)tolpushes+=2*cnt[i];\n else if(i<24)tolpushes+=3*cnt[i];\n else tolpushes+=4*cnt[i];\n }\n return tolpushes;\n }\n};", "memory": "12170" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution\n{\npublic:\n\tint minimumPushes(string_view word)\n\t{\n\t\tunordered_map<int, int> m;\n\t\tvector<int> v('z' - 'a' + 1, 0);\n\t\tint keys[10];\n\n\t\tfor(int i = 0; i < 10; ++i)\n\t\t\tkeys[i] = 1;\n\n\t\tfor (int i = 0; i < word.size(); ++i)\n\t\t{\n\t\t\tv[word[i] - 'a']++;\n\t\t}\n\n\t\tsort(v.begin(), v.end());\n\n\t\tint j = 2, minPushes = 0;\n\n\t\tfor (int i = v.size() - 1; i >= 0; --i)\n\t\t{\n\t\t\tif(v[i] == 0) break;\n\t\t\t\n\t\t\tminPushes += v[i] * keys[j++]++;\n\n\t\t\tj = j > 9 ? (j % 10) + 2 : j;\n\t\t}\n\n\t\treturn minPushes;\n\t}\n};", "memory": "12430" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "#include <execution>\n\n#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\nstatic const auto _ = []() -> int {\n std::ios::sync_with_stdio(false);\n std::cin.sync_with_stdio(false);\n std::cout.sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n int minimumPushes(string const& word) {\n std::array<int, 26> count{};\n for (char c : word) ++count[c - 'a'];\n \n std::sort(std::execution::par_unseq, count.begin(), count.end(), [](int lhs, int rhs){ return lhs > rhs; });\n\n int res{0};\n for (int i = 0; i != 26; ++i) res += count[i] * (i / 8 + 1);\n return res;\n }\n};", "memory": "12430" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "//https://hyper-meta.blogspot.com/\n#include <execution>\n#pragma GCC target(\"avx,mmx,sse2,sse3,sse4\")\nauto _=[]()noexcept{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);return 0;}();\nint fr_base[26],*fr=fr_base-'a';\nclass Solution {\npublic:\n int minimumPushes(const string &word) {\n memset(fr_base,0,sizeof(fr_base));\n for(char c:word) fr[c]++;\n sort(execution::par_unseq,fr_base,fr_base+26,greater<int>());\n return transform_reduce(execution::par_unseq,fr_base,fr_base+26, 0, plus{}, [f = &fr_base[0]](int const& x) {\n return x*((&x-f)/8+1);\n });\n }\n};", "memory": "12690" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "#include <execution>\n\n#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\nstatic const auto _ = []() -> int {\n std::ios::sync_with_stdio(false);\n std::cin.sync_with_stdio(false);\n std::cout.sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n int minimumPushes(string const& word) {\n std::array<int, 26> count{};\n for (char c : word) ++count[c - 'a'];\n \n std::sort(std::execution::par_unseq, count.begin(), count.end(), std::greater{});\n\n int res{0};\n for (int i = 0; i != 26; ++i) res += count[i] * (i / 8 + 1);\n return res;\n }\n};", "memory": "12690" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string& word) {\n vector<int> mapping(26, 0);\n for (int i = 0; i < int(word.size()); i++)\n mapping[word[i] - 'a']++;\n vector<int> els;\n for (char i = 0; i < 26; i++)\n if (mapping[i] > 0) els.push_back(move(mapping[i]));\n sort(els.begin(), els.end(), std::greater<int>());\n int ret {};\n for (char i = 0; i < char(els.size()); i++)\n ret += els[i] * (i / 8 + 1);\n return ret;\n }\n};", "memory": "12950" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string& word) {\n vector<int> mapping(26, 0);\n for (int i = 0; i < int(word.size()); i++)\n mapping[word[i] - 'a']++;\n vector<int> els;\n for (char i = 0; i < 26; i++)\n if (mapping[i] > 0) els.push_back(move(mapping[i]));\n stable_sort(els.begin(), els.end(), std::greater<int>());\n int ret {};\n for (char i = 0; i < char(els.size()); i++)\n ret += els[i] * (i / 8 + 1);\n return ret;\n }\n};", "memory": "12950" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string& word) {\n int freq[26] = {};\n for (const auto ch : word) {\n ++freq[ch - 'a'];\n }\n int letters[26];\n std::iota(begin(letters), end(letters), 0);\n std::ranges::sort(letters, [&](auto l1, auto l2) {\n return freq[l1] > freq[l2];\n });\n\n const int keyCnt = 8;\n int keyUsage[keyCnt] = {};\n int curKey = 0;\n int total = 0;\n\n for (const char ch : letters) {\n total += (++keyUsage[curKey]) * freq[ch];\n curKey = (curKey + 1) % keyCnt;\n }\n return total;\n }\n};", "memory": "13210" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "#include <execution>\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"avx2,tune=native\")\n\nbool init() {\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n return true;\n}\n\nclass Solution {\npublic:\n int minimumPushes(const std::string& word) {\n std::array<int, 26> counts;\n for (auto ch : word) {\n ++counts[ch - 'a'];\n }\n\n std::sort(execution::par_unseq, counts.begin(), counts.end(),\n std::greater<int>());\n\n return std::transform_reduce(execution::par_unseq, counts.begin(),\n counts.end(), 0, std::plus{},\n [counts = &counts[0]](int const& x) {\n int idx = &x - counts;\n return x * (idx / 8 + 1);\n });\n }\n};", "memory": "13210" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(const string& word) {\n \n vector<int> freq(26);\n for(auto& x : word)\n freq[x - 'a']++;\n \n vector<char> letters(26);\n for(char x = 'a'; x <= 'z'; x++)\n letters[x-'a']=x;\n \n sort(letters.begin(), letters.end(), [&](char a, char b){\n return freq[a - 'a'] > freq[b - 'a'];\n });\n\n vector<int> cost(26);\n\n int iter = 0;\n int steps = 0;\n\n while(iter < 26){\n steps++;\n for(int i = 2; i <= 9 && iter < 26; i++){\n cost[letters[iter++] - 'a'] = steps;\n }\n }\n\n int minimumSteps = 0;\n\n for(auto& x : word)\n minimumSteps += cost[x - 'a'];\n\n return minimumSteps;\n }\n};", "memory": "13470" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string& word) {\n vector<int> mapping(26, 0);\n const int SZ { int(word.size()) };\n for (int i = 0; i < SZ; i++)\n mapping[word[i] - 'a']++;\n make_heap(mapping.begin(), mapping.end(), std::greater<int>());\n sort_heap(mapping.begin(), mapping.end(), std::greater<int>());\n int ret {};\n for (char i = 0; i < 26; i++)\n ret += mapping[i] * (i / 8 + 1);\n return ret;\n }\n};", "memory": "13730" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(const string& word) {\n vector<int> count(26,0);\n for(char c : word) count[c-'a']++;\n priority_queue<int> max_heap;\n int result = 0;\n int letters = 0;\n for(auto cnt : count) max_heap.push(cnt);\n while(max_heap.empty() == false){\n int cnt = max_heap.top(); max_heap.pop();\n result += ((letters/8)+1)*cnt;\n letters++;\n }\n return result;\n }\n};", "memory": "15290" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "size_t counts[256];\nclass Solution {\npublic:\n static int minimumPushes(string_view word) {\n memset(counts, 0, sizeof(counts));\n //// using CountsMapTy = unordered_map<char, size_t>;\n //// CountsMapTy counts;\n for (char c : word)\n ++counts[c];\n priority_queue<size_t /*counts*/> q;\n for (size_t c : counts) {\n if (c)\n q.emplace(c);\n }\n int total_clicks = 0;\n size_t curr_letter = 0;\n while (!q.empty()) {\n int num_clicks = q.top() * (1 + (curr_letter++ / 8));\n total_clicks += num_clicks;\n q.pop();\n }\n return total_clicks;\n }\n};", "memory": "15290" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumPushes(string &word) {\n int a[26] = {0};\n for(char &ch: word)\n a[ch - 97]++;\n priority_queue<pair<int, int>> pq;\n for(int i = 0; i < 26; i++) {\n if(a[i] != 0)\n pq.push(make_pair(a[i], i));\n }\n int ans = 0;\n int c = 0;\n while(!pq.empty()) {\n pair<int, int> p = pq.top();\n pq.pop();\n ans += (p.first * ((c / 8) + 1));\n c++;\n }\n return ans;\n }\n};", "memory": "15550" }
3,276
<p>You are given a string <code>word</code> containing lowercase English letters.</p> <p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code>, we need to push the key one time to type <code>&quot;a&quot;</code>, two times to type <code>&quot;b&quot;</code>, and three times to type <code>&quot;c&quot;</code> <em>.</em></p> <p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p> <p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p> <p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot; <strong>Output:</strong> 12 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;x&quot; -&gt; one push on key 2 &quot;y&quot; -&gt; one push on key 3 &quot;z&quot; -&gt; one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png" style="width: 329px; height: 313px;" /> <pre> <strong>Input:</strong> word = &quot;aabbccddeeffgghhiiiiii&quot; <strong>Output:</strong> 24 <strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost. &quot;a&quot; -&gt; one push on key 2 &quot;b&quot; -&gt; one push on key 3 &quot;c&quot; -&gt; one push on key 4 &quot;d&quot; -&gt; one push on key 5 &quot;e&quot; -&gt; one push on key 6 &quot;f&quot; -&gt; one push on key 7 &quot;g&quot; -&gt; one push on key 8 &quot;h&quot; -&gt; two pushes on key 9 &quot;i&quot; -&gt; one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li> <li><code>word</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "struct element {\n int n;\n char c;\n element(){}\n element(char c, int n) : n(n), c(c) {}\n bool operator<(const element& other) const { return other.n < n; }\n friend ostream& operator<<(ostream& os, const element& input) {\n return os << '[' << input.c << \", \" << input.n << ']';\n }\n};\n\nstruct Solution {\n int minimumPushes(string_view input)const {\n map<char, int> data;\n for (char c : input)\n ++data.insert({c, 0}).first->second;\n\n vector<element> data2(data.size());\n int i = 0;\n for (auto& it : data)\n data2[i++] = element(it.first, it.second);\n\n sort(data2.begin(), data2.end());\n\n int output = 0;\n int count = 8;\n\n for(auto & it : data2)\n {\n output += (count / 8) * it.n;\n count++;\n }\n\n\n return output;\n }\n};", "memory": "15550" }