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[... | 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 ... |
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[... | 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 ... |
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[... | 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 ... |
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[... | 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 ... |
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[... | 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){\... |
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[... | 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]); // ... |
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[... | 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\... |
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[... | 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... |
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[... | 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... |
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[... | 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... |
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[... | 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 ... |
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[... | 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]... |
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[... | 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 ... |
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[... | 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... |
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[... | 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 ... |
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[... | 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 retu... |
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[... | 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 ... |
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[... | 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 ... |
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[... | 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... |
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[... | 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 ... |
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[... | 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.resiz... |
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[... | 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[... |
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[... | 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 ... |
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[... | 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... |
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[... | 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 ... |
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[... | 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])... |
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[... | 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,in... |
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[... | 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 ... |
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[... | 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... |
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[... | 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... |
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[... | 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]... |
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[... | 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 p... |
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[... | 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])retur... |
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[... | 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])retur... |
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[... | 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 p... |
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[... | 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 p... |
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[... | 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, p... |
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[... | 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... |
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[... | 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 : (ufParen... |
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[... | 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(), pare... |
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[... | 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... |
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[... | 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(in... |
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[... | 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 ... |
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[... | 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 ret... |
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[... | 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(e... |
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[... | 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);\... |
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[... | 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 ... |
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[... | 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_p... |
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[... | 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_... |
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[... | 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... |
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[... | 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 ... |
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[... | 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 ... |
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[... | 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... |
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[... | 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(p... |
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[... | 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(... |
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[... | 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... |
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[... | 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 ... |
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[... | 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 != pa... |
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[... | 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 ... |
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[... | 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 }... |
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[... | 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... |
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[... | 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 }... |
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[... | 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 ... |
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[... | 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 ... |
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[... | 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 ... |
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[... | 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[... |
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[... | 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[... |
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[... | 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... |
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[... | 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 combi... |
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[... | 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 // ... |
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[... | 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(r... |
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[... | 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 =... |
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[... | 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 ... |
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[... | 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(p... |
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[... | 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 ... |
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>["a",&... | 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 + ... |
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>["a",&... | 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 countingSor... |
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>["a",&... | 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())... |
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>["a",&... | 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 m... |
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>["a",&... | 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 && f... |
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>["a",&... | 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, fr... |
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>["a",&... | 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... |
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>["a",&... | 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 in... |
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>["a",&... | 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, fr... |
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>["a",&... | 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()... |
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>["a",&... | 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 }\... |
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>["a",&... | 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\... |
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>["a",&... | 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... |
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>["a",&... | 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(... |
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>["a",&... | 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... |
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>["a",&... | 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(mo... |
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>["a",&... | 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(mo... |
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>["a",&... | 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 l... |
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>["a",&... | 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<i... |
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>["a",&... | 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 ... |
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>["a",&... | 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 so... |
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>["a",&... | 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(ma... |
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>["a",&... | 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 prior... |
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>["a",&... | 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 ... |
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>["a",&... | 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 << ']... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.