id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = (int)edges.size();\n int maxi = -1;\n for(int i=0;i<n;i++){\n maxi = max({maxi,edges[i][0],edges[i][1]});\n }\n vector<int>hash(maxi+1);\n for(int i=0;i<n;i++){\n hash[edges[i][0]]++;\n hash[edges[i][1]]++;\n }\n int ans;\n for(int i=0;i<maxi+1;i++){\n // cout<<i<<\" \"<<hash[i]<<endl;\n if(hash[i]==maxi-1){\n ans = i;\n break;\n }\n }\n // cout<<maxi<<endl;\n return ans;\n }\n};", "memory": "32524" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = edges.size();\n vector<int>totalEdges(n+2,0);\n\n for(int i=0;i<n;i++){\n\n totalEdges[edges[i][0]]++;\n totalEdges[edges[i][1]]++;\n }\n\n for(int i=1;i<=n+2;i++){\n\n if(totalEdges[i]==n) return i;\n }\n\n return 0;\n }\n};", "memory": "32524" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n bitset<100000> visited=0;\n for(auto e: edges)\n {\n int u = e[0];\n int v = e[1];\n if(visited[u])\n return u;\n if(visited[v])\n return v;\n visited[u]=visited[v]=1;\n }\n return -1;\n }\n};", "memory": "33653" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n vector<int>v(2*edges.size()+1,0);\n for(int i=0;i<edges.size();i++){\n \n v[(edges[i])[0]]+=1;\n v[(edges[i])[1]]+=1;\n }\n int m =0;\n for(int i=0;i<v.size();i++){\n m = max(m,v[i]);\n }\n int index=0;\n for(int i=0;i<v.size();i++){\n if(v[i]==m){\n index = i;\n break;\n }\n }\n return index;\n }\n};", "memory": "34781" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) \n {\n vector<int> mp(2*edges.size()+1, 0);\n\n for(int i = 0; i < edges.size(); i++)\n {\n for(int j=0; j<2; j++)\n {\n mp[edges[i][j]]++;\n }\n }\n\n int temp=0, ans=0;\n for(int i=0; i<mp.size(); i++)\n {\n if(temp < mp[i])\n {\n temp = mp[i];\n ans = i;\n }\n }\n\n return ans;\n\n }\n\n \n};", "memory": "35910" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n vector<int> arr(edges.size()*2);\n int k=0;\n for(int i=0;i<edges.size();i++){\n for(int j=0;j<2;j++){\n arr[k]=edges[i][j];\n k++;\n }\n } \n for(int i=0;i<2;i++){\n int count=1;\n for(int j=2;j<arr.size();j++){\n if(arr[i]==arr[j]){\n count++;\n }\n }\n cout<<count;\n if(count==arr.size()/2){\n return arr[i];\n }\n }\n return 0;\n }\n};", "memory": "37039" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "#include <vector>\n\nclass Solution {\npublic:\n int findCenter(std::vector<std::vector<int>>& edges) {\n int n = edges.size()+1;\n vector<int> inDegree(n + 1, 0);\n vector<int> outDegree(n + 1, 0);\n\n for (const auto& edge : edges) {\n outDegree[edge[0]]++;\n outDegree[edge[1]]++;\n inDegree[edge[0]]++;\n inDegree[edge[1]]++;\n }\n\n for (int i = 1; i <= n + 1; i++) {\n cout<<outDegree[i]<<endl;\n if (outDegree[i] == n-1) {\n return i;\n }\n }\n\n return -1;\n }\n};", "memory": "38168" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n vector<int> arr;\n for (int i=0; i<edges.size()+1; ++i) {\n arr.push_back(0);\n }\n\n for (int i=0; i<edges.size(); ++i) {\n for (int j=0; j<2; ++j) {\n\n if (arr[edges[i][j]-1]==1) {\n return edges[i][j];\n }\n\n arr[edges[i][j]-1] += 1;\n }\n }\n return -1;\n }\n};\n\n/*\nn is edges.size()+1\narray of size n\n\n*/", "memory": "39296" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n vector<int> edge[n+1];\n for(int i=0;i<2;i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n\n if(i==0)\n {\n edge[u].push_back(v);\n edge[v].push_back(u);\n }\n\n if(i==1)\n {\n if(edge[u].size()>0)\n return u;\n else\n return v;\n }\n \n }\n return -1;\n }\n};", "memory": "40425" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n vector<int> arr;\n for (int i=0; i<edges.size()+1; ++i) {\n arr.push_back(0);\n }\n\n for (int i=0; i<edges.size(); ++i) {\n for (int j=0; j<2; ++j) {\n for (int a=0; a<edges.size()+1; ++a) {\n cout << arr[a] << \" \";\n }\n cout << endl;\n if (arr[edges[i][j]-1]==1) {\n return edges[i][j];\n }\n //arr[edges[i][j]-1] = arr[edges[i][j]-1] + 1;\n cout << \"current node: \" << edges[i][j] << endl;\n arr[edges[i][j]-1] += 1;\n }\n cout << endl;\n }\n return -1;\n }\n};\n\n/*\nn is edges.size()+1\narray of size n\n\n*/", "memory": "41554" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = edges.size();\n n++;\n vector<int> adj[n+1];\n for(auto it:edges)\n {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n if(adj[it[0]].size()>1) return it[0];\n if(adj[it[1]].size()>1) return it[1];\n }\n /*for(int node = 1;node<=n;node++)\n {\n if(adj[node].size()==n-1) return node;\n } */\n return 0;\n }\n};", "memory": "42683" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n=edges.size()+1;\n vector<vector<int>>v(2,vector<int>(n+1,0));\n for(int i=0;i<edges.size();i++)\n {\n v[0][edges[i][0]]++;\n v[1][edges[i][1]]++;\n }\n for(int i=0;i<v[0].size();i++)\n {\n if(v[0][i]+v[1][i]==n-1)\n return i;\n }\n return 0;\n }\n};", "memory": "43811" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n vector<int> ans;\n for(int i=0 ; i<edges.size() ; i++){\n ans.push_back(edges[i][0]);\n ans.push_back(edges[i][1]);\n }\n int x;\n sort(ans.begin() , ans.end());\n for(int i=0 ; i<2*edges.size()-1 ; i++){\n if(ans[i] == ans[i+1]){\n x = ans[i];\n }\n }\n return x;\n }\n};", "memory": "44940" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n=edges.size()+1;\n vector<int> v[n];\n int i;\n vector<int> indegree(n+1,0);\n vector<int> outdegree(n+1,0);\n for(const auto &vect:edges)\n {\n int a=vect[0];\n int b=vect[1];\n indegree[a]+=1;\n indegree[b]+=1;\n } \n for(i=1;i<=n;i++)\n {\n cout<<indegree[i]<<\" \";\n if(indegree[i]==n-1)\n return i;\n }\n return -1;\n }\n};", "memory": "46069" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "#include<vector>\n#include<algorithm>\nclass Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n vector<int> arr;\n for(int i=0;i<edges.size();i++){\n arr.push_back(edges[i][0]);\n arr.push_back(edges[i][1]);\n } \n for(int i=0;i<2;i++){\n int count=1;\n for(int j=2;j<arr.size();j++){\n if(arr[i]==arr[j]){\n count++;\n }\n }\n cout<<count;\n if(count==arr.size()/2){\n return arr[i];\n }\n }\n return 0;\n }\n};", "memory": "47198" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "#include<vector>\n#include<algorithm>\nclass Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n vector<int> arr;\n for(int i=0;i<edges.size();i++){\n arr.insert(arr.end(),edges[i].begin(),edges[i].end());\n } \n for(int i=0;i<2;i++){\n int count=1;\n for(int j=2;j<arr.size();j++){\n if(arr[i]==arr[j]){\n count++;\n }\n }\n cout<<count;\n if(count==arr.size()/2){\n return arr[i];\n }\n }\n return 0;\n }\n};", "memory": "48326" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n vector<int> adj[edges.size()*2]; \n for(auto v: edges) {\n adj[v[0]].push_back(v[1]);\n adj[v[1]].push_back(v[0]);\n if(adj[v[0]].size() > 1) {\n return v[0];\n }\n if(adj[v[1]].size() > 1) {\n return v[1];\n }\n }\n return -1;\n }\n};", "memory": "49455" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n \n int n = edges.size();\n vector<int> indeg(n+1);\n\n for(int i=0;i<n;i++)\n {\n indeg[edges[i][0]-1]++;\n indeg[edges[i][1]-1]++;\n }\n\n vector<pair<int, int>> vp;\n for(int i=0;i<n+1;i++)\n {\n vp.push_back({indeg[i], i+1});\n }\n\n sort(vp.begin(), vp.end());\n\n return vp[vp.size()-1].second;\n }\n};", "memory": "50584" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int maxno=0;\n map<int,int> mpp;\n int n=edges.size();\n int num;\n for(int i=0; i<n; i++){\n for(int j=0; j<2; j++){\n mpp[edges[i][j]]++;\n if(mpp[edges[i][j]] > maxno){\n maxno=mpp[edges[i][j]];\n num=edges[i][j];\n if(maxno>n/2){\n return num;\n }\n }\n }\n }\n return num;\n \n }\n};", "memory": "51713" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\nint count(vector<int>v,int x){\n int c=0;\n for(int i=0;i<v.size();i++){\n if(v[i]==x)\n c++;\n }\n return c;\n}\n int findCenter(vector<vector<int>>& edges) {\n vector<int>v;\n for(int i=0;i<edges.size();i++){\n for(int j=0;j<edges[0].size();j++){\n v.push_back(edges[i][j]);\n }\n }\n for(int i=0;i<v.size();i++){\n int w=count(v,v[i]);\n if(w==edges.size())\n return v[i];\n }\n return -1;\n \n }\n};", "memory": "52841" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n vector<bool> visited(1000000);\n for(auto e: edges) {\n int v = e[0], w = e[1];\n if(visited[v]) return v;\n if(visited[w]) return w;\n visited[v]=visited[w] = true;\n }\n\n return -1;\n }\n};", "memory": "53970" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n\n vector<int> firstSubVector = edges[0];\n int n=firstSubVector[0];\n int m = firstSubVector[1];\n\n int count_m=0;\n int count_n=0;\n for (auto it:edges)\n {\n if(it[0]==n) count_n++;\n if(it[1]==m) count_m++;\n }\n\n if(count_m > count_n)return m;\n else return n;\n }\n};", "memory": "55099" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int condidate1 =edges[0][0] ,condidate2 = edges[0][1];\n int nbC1 = 0 , nbC2 = 0;\n for(auto u: edges){\n if((u[0] == condidate1 ) || (u[1]==condidate1)){\n nbC1 ++;\n }\n if((u[0] == condidate2 ) || (u[1]==condidate2)){\n nbC2 ++;\n }\n\n }\n if(nbC1 == edges.size()) return condidate1;\n else return condidate2;\n }\n};", "memory": "56228" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int one=edges[0][0],second=edges[0][1];\n int c1=0,c2=0;\n\n for(auto i:edges)\n {\n if(i[0]==one || i[1]==one)c1++;\n else if (i[1]==second || i[0]==one)c2++;\n }\n\n if(c1==edges.size())return one;\n\n return second;\n\n }\n};", "memory": "57356" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n //n*(n+1)/2-(n-1)*k=S\n // get n (len edges+1)\n unsigned long long n=edges.size()+1;\n // get S, su of all the values\n unsigned long long S=0;\n for(auto e: edges){\n S+=e[0];\n S+=e[1];\n }\n return (S-n*(n+1)/2)/(n-2);\n }\n};", "memory": "58485" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n //n*(n+1)/2-(n-1)*k=S\n // get n (len edges+1)\n unsigned long n=edges.size()+1;\n // get S, su of all the values\n unsigned long S=0;\n for(auto e: edges){\n S+=e[0];\n S+=e[1];\n }\n return (S-n*(n+1)/2)/(n-2);\n }\n};", "memory": "58485" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = edges.size();\n n++;\n vector<int>deg(n,0);\n\n for(auto it:edges){\n int u = it[0]-1;\n int v = it[1]-1;\n deg[u]++;\n deg[v]++;\n if(deg[u]==n-1) return u+1;\n if(deg[v]==n-1) return v+1;\n }\n return 0;\n \n }\n};", "memory": "66386" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n vector<int> degree(edges.size()+2,0);\n for(auto i:edges)\n {\n degree[i[0]]++;\n degree[i[1]]++;\n } \n int ans=0;\n for(int i=0;i<edges.size()+2;i++)\n {\n if(degree[ans]<degree[i])\n ans=i;\n }\n return ans;\n }\n};", "memory": "66386" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = edges.size();\n vector<int> cnt(n+1, 0);\n for(auto i: edges) {\n cnt[i[1]-1]++;\n cnt[i[0]-1]++;\n }\n for(int i = 0; i <= n; i++) {\n if(cnt[i] == n) {\n return i+1;\n }\n }\n return -1;\n }\n};", "memory": "67515" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n=edges.size();\n vector<int>In(n+2),Out(n+2);\n\n for(auto it:edges){\n In[it[0]]+=1;\n In[it[1]]+=1;\n // Out[it[0]]+=1;\n // Out[it[1]]+=1;\n }\n int ans=-1;\n for(int i=0;i<=n+1;i++){\n if(In[i]==n){ans=i;break;}\n }\n\n return ans;\n }\n};", "memory": "68644" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n\n int base = 10;\n int exponent = 5;\n long max_size = pow(base,exponent);\n std::vector<int> matrix[max_size+1];\n\n int v = 0;\n int e = 0;\n int max_edges = 0;\n int max_node = 0;\n for (int i = 0; i < edges.size(); i++) {\n v = edges.at(i).at(0);\n e = edges.at(i).at(1);\n\n matrix[v].push_back(e);\n if (matrix[v].size() > max_edges) {\n max_edges = matrix[v].size();\n max_node = v;\n }\n matrix[e].push_back(v);\n if (matrix[e].size() > max_edges) {\n max_edges = matrix[e].size();\n max_node = e;\n }\n\n }\n\n return max_node;\n \n }\n};", "memory": "69773" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = edges.size();\n vector<int> adjList[n+2];\n for(int i = 0; i < n; i++){\n int u = edges[i][0];\n int v = edges[i][1];\n adjList[u].push_back(v);\n adjList[v].push_back(u);\n }\n for(int i = 1; i <= n+1; i++){\n if(adjList[i].size() == n) return i;\n }\n return 0;\n }\n};", "memory": "70901" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = edges.size()+1;\n vector<int> adj[n+1];\n for(int i=0;i<edges.size();i++)\n {\n int a = edges[i][0];\n int b = edges[i][1];\n\n adj[a].push_back(b);\n adj[b].push_back(a);\n }\n\n for(int i=1;i<=n;i++)\n {\n if(adj[i].size() == n-1)\n {\n return i;\n }\n }\n\n return -1;\n }\n};", "memory": "72030" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int edgesLen = edges.size();\n vector<int> adj[edgesLen+2];\n\n for(int i = 0; i < edgesLen; i++){\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 = 1; i <= edgesLen+1; i++){\n if(adj[i].size() == edgesLen){\n return i;\n }\n }\n\n return -1;\n }\n};", "memory": "73159" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n vector<int> adj[edges.size()+1];\n for(int i=0;i<edges.size();i++){\n adj[edges[i][0]-1].push_back(edges[i][1]-1);\n adj[edges[i][1]-1].push_back(edges[i][0]-1);\n }\n int ans;\n for(int i=0;i<=edges.size();i++){\n if(adj[i].size()==edges.size()){\n ans=i+1;\n break;\n }\n }\n return ans;\n\n }\n};", "memory": "74288" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n \n unordered_map<int,int> mp;\n\n for(int i = 0; i < edges.size(); ++i){\n mp[edges[i][0]]++;\n mp[edges[i][1]]++;\n }\n\n for(const auto& x : mp){\n if(x.second == edges.size())\n return x.first; \n }\n return 0;\n\n }\n};", "memory": "75416" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n=edges.size();\n unordered_map<int,int>m;\n for(int i=0;i<n;i++){\n m[edges[i][0]]++;\n m[edges[i][1]]++;\n }\n int res;\n int count=INT_MIN;\n for(auto val : m){\n if(val.second>count){\n res=val.first;\n count=val.second;\n }\n }\n return res;\n }\n};", "memory": "75416" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "\n\nclass Solution {\npublic:\n int findCenter(std::vector<std::vector<int>>& edges) {\n std::unordered_map<int, int> counter;\n\n for (const auto& edge : edges) {\n counter[edge[0]]++;\n counter[edge[1]]++;\n }\n\n for (const auto& [key, value] : counter) {\n if (value == edges.size()) {\n return key;\n }\n }\n\n return -1; // In case there is no center found, though it shouldn't happen in a valid star graph\n }\n};\n\n \n \n", "memory": "76545" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n unordered_map<int,int>map;\n for(int i=0; i<edges.size(); i++){\n map[edges[i][0]]++;\n map[edges[i][1]]++;\n }\n int count=0;\n for(auto el: map){\n if(el.second==edges.size()) return el.first;\n }\n return 0;\n }\n};", "memory": "76545" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n unordered_map<int,int> mp;\n for(int i=0;i<edges.size();i++){\n mp[edges[i][0]]++;\n mp[edges[i][1]]++;\n }\n int mxr=0;\n int mxv=0;\n for(auto x: mp){\n if(mxr<x.second){\n mxr=x.second;\n mxv=x.first;\n }\n \n }\n return mxv;\n }\n};", "memory": "77674" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "\n\nclass Solution {\npublic:\n int findCenter(std::vector<std::vector<int>>& edges) {\n std::unordered_map<int, int> counter;\n\n for (const auto& edge : edges) {\n counter[edge[0]]++;\n counter[edge[1]]++;\n }\n\n for (const auto& [key, value] : counter) {\n if (value == edges.size()) {\n return key;\n }\n }\n\n return -1; \n }\n};\n\n \n \n", "memory": "77674" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n unordered_map<int, int> mapa;\n\n for (int i=0; i<edges.size(); ++i){\n int x = edges[i][0];\n int y = edges[i][1];\n\n mapa[x]++;\n mapa[y]++;\n }\n\n for (auto i : mapa){\n if (i.second == mapa.size()-1) return i.first;\n }\n\n return -1;\n }\n};", "memory": "78803" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n=edges.size();\n unordered_map<int,int>mp;\n for(int i=0;i<edges.size();i++){\n mp[edges[i][0]]++;\n mp[edges[i][1]]++;\n }\n for(auto it:mp){\n if(it.second==n){\n return it.first;\n }\n }\n return -1;\n }\n};", "memory": "79931" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "vector<vector<int>> g;\nclass Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n g.clear();\n int n = edges.size();\n g.resize(n+2);\n for(int i=0; i<n; i++){\n g[edges[i][0]].push_back(edges[i][1]);\n g[edges[i][1]].push_back(edges[i][0]);\n }\n // for(int i=1; i<n+2;i++){\n // cout<<i<<\"--\";\n // for(auto v:g[i])\n // {\n // cout<<v<<\" \";\n // }\n // cout<<endl;\n // }\n for(int i=1; i<=n+1; i++){\n if(g[i].size()>=2)\n {\n return i;\n }\n }\n return 0;\n }\n};", "memory": "81060" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "int findMax(vector<vector<int>> mat)\n{\n \n // Initializing max element as INT_MIN\n int maxElement = INT_MIN;\n \n // checking each element of matrix\n // if it is greater than maxElement,\n // update maxElement\n for (int i = 0; i < mat.size(); i++) {\n for (int j = 0; j < 2; j++) {\n if (mat[i][j] > maxElement) {\n maxElement = mat[i][j];\n }\n }\n }\n \n // finally return maxElement\n return maxElement;\n}\n \n\nclass Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int node = findMax(edges);\n \n vector<int> hash(node+1,0);\n for (int i = 0; i < edges.size(); i++) {\n for (int j = 0; j < 2; j++) {\n hash[edges[i][j]]++;\n }\n }\n int t;\n int maxElement = INT_MIN;\n for(int i=0;i<node+1;i++){\n if(hash[i]>maxElement){\n t=i;\n maxElement=hash[i];\n }\n }\n return t;\n \n }\n};", "memory": "82189" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution \n{\npublic:\n int findCenter(vector<vector<int>>& edges) \n {\n int n = edges.size()+1;\n \n vector<int> indeg(n+2,0);\n vector<int> outdeg(n+2,0);\n\n vector<int> adj[n+2];\n\n for(auto &vals:edges)\n {\n adj[vals[0]].push_back(vals[1]);\n adj[vals[1]].push_back(vals[0]);\n ++outdeg[vals[0]];\n ++outdeg[vals[1]];\n } \n for(int i=1;i<=n;++i)\n {\n for(auto &vals:adj[i])\n {\n ++indeg[vals];\n }\n }\n for(int i=1;i<=n;++i)\n {\n cout<<i<<\" \"<<outdeg[i]<<endl;\n }\n for(int i=1;i<=n;++i)\n {\n if(outdeg[i]==n-1)\n {\n return i;\n }\n }\n\n return -1;\n }\n};", "memory": "83318" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n map<int,int>mpp;\n for(int i =0 ;i<edges.size();i++){\n mpp[edges[i][0]]++;\n mpp[edges[i][1]]++;\n\n }\n int nodes = 0 ;\n for(auto it:mpp){\n nodes++;\n }\n for(auto it:mpp){\n if(it.second==nodes-1)return it.first;\n }\n return 1;\n }\n};", "memory": "84446" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n map<int,int> mp;\n for(int i=0;i<edges.size();i++)\n {\n for(int j=0;j<edges[i].size();j++)\n {\n mp[edges[i][j]]++;\n }\n }\n int ans=0;\n int fr=0;\n for(auto freq:mp)\n {\n if(fr<freq.second)\n {\n fr=freq.second;\n ans=max(ans,freq.first);\n }\n }\n return ans;\n }\n};", "memory": "85575" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n map<int, int> m;\n for(int i=0;i<edges.size();i++){\n m[edges[i][0]]++;\n m[edges[i][1]]++;\n }\n for(auto it = m.begin();it!=m.end();it++){\n if(it->second == edges.size()) return it->first;\n }\n return -1;\n }\n};", "memory": "86704" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int ans = -1;\n map<int,int>mp;\n int n = edges.size();\n int m = edges[0].size();\n for(int i =0;i<n;i++){\n for(int j =0;j<m;j++){\n mp[edges[i][j]]++;\n }\n }\n for(auto it:mp){\n if(it.second == n)\n ans = it.first;\n }\n return ans;\n }\n};", "memory": "86704" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& e) {\n map<int,int>m;\n int n = e.size();\n for(int i = 0; i < n; i ++){\n\n m[e[i][0]]++;\n m[e[i][1]]++;\n\n }\n for(int i = 0; i < n; i ++){\n\n if(m[e[i][0]] == n)return e[i][0];\n if(m[e[i][1]] == n)return e[i][1];\n\n }\n return -1;\n }\n};", "memory": "87833" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = edges.size()+1;\n vector<int>in(n+1,0);\n vector<int>adj[n+1];\n for(auto &i : edges)\n {\n in[i[0]]++;\n in[i[1]]++;\n adj[i[0]].push_back(i[1]);\n adj[i[1]].push_back(i[0]);\n }\n queue<int>q;\n for(int i=0;i<=n;i++)\n {\n if(in[i]==1)\n {\n q.push(i);\n }\n }\n while(n>1)\n {\n int sz = q.size();\n n -=sz;\n while(sz--)\n {\n auto it = q.front();\n q.pop();\n for(auto i:adj[it])\n {\n in[i]--;\n if(in[i]==1)\n {\n q.push(i);\n }\n }\n }\n }\n return q.front();\n\n }\n};", "memory": "87833" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n \n int n = edges.size()+1;\n vector<vector<int>> adj(n+1);\n vector<int> indeg(n+1, 0);\n for(int i=0; i<edges.size(); i++){\n int u = edges[i][0];\n int v = edges[i][1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n indeg[u]++;\n indeg[v]++;\n }\n\n for(int i=0; i<=n; i++){\n if(indeg[i] == n-1){\n return i;\n }\n }\n\n return -1;\n }\n};", "memory": "88961" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int numVertices = edges.size() + 1; \n vector<vector<int>> adj(numVertices + 1); \n \n for (int i = 0; i < edges.size(); i++) {\n int u = edges[i][0]; \n int v = edges[i][1]; \n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n vector<int>vis(numVertices+1, 0);\n for(int i=1;i<numVertices+1;i++){\n if(adj[i].size()==numVertices-1){\n return i;\n }\n }\n return -1;\n\n }\n};", "memory": "88961" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = 1;\n for (auto edge: edges) {\n n = max(n, max(edge[0], edge[1]));\n }\n auto points = vector<int>(n + 1, 0);\n for (auto edge: edges) {\n points[edge[0]]++;\n points[edge[1]]++;\n }\n auto star = 1;\n for (int i = 1; i < points.size(); i++ )\n {\n if (points[i] == n - 1) {\n star = i;\n }\n }\n return star;\n }\n};", "memory": "90090" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = edges.size()+1;\n vector<vector<int>> adj(n+1);\n vector<int> vis(n+1,0);\n for(int i=0;i<edges.size();i++){\n int u = edges[i][0];\n int v = edges[i][1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n queue<int> q;\n q.push(1);\n vis[1] = 1;\n while(!q.empty()){\n int node = q.front();\n q.pop();\n int cnt = 0;\n for(int it:adj[node]){\n cnt++;\n if(!vis[it]){\n vis[it]=1;\n q.push(it);\n }\n }\n if(cnt==edges.size()) return node;\n }\n return -1;\n }\n};", "memory": "90090" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& e) {\n\n vector<int>v;\n for(int i=0;i<e.size();i++)\n {\n for(int j=0;j<e[i].size();j++)\n {\n v.push_back(e[i][j]);\n }\n }\n map<int,int>m;\n for(auto i:v)\n {\n m[i]++;\n }\n int maxi=-3;\n int ans=0;\n for(auto i:m)\n {\n if(i.second>maxi)\n {\n ans=i.first;\n maxi=i.second;\n }\n }\n return ans;\n \n }\n};", "memory": "91219" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = edges.size(); \n vector<vector<int>>g(n+2);\n for(auto& it : edges){\n g[it[0]].push_back(it[1]); \n g[it[1]].push_back(it[0]);\n }\n int node = 0;\n for(auto vec : g){\n if(vec.size() == n){\n return node;\n break;\n }\n node++;\n }\n return -1;\n }\n};", "memory": "92348" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = edges.size() + 1; \n vector<vector<int>> ans(n+1);\n for(int i=0;i<edges.size();i++){\n int v=edges[i][0];\n int u=edges[i][1];\n\n ans[u].push_back(v);\n ans[v].push_back(u);\n }\n int i=0;\n for(auto it:ans){\n if(it.size()==edges.size()){\n return i;\n }\n i++;\n }\n return -1;\n }\n};", "memory": "93476" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int maxnode=-1;\n for(auto &it:edges)\n {\n maxnode=max(maxnode,max(it[0],it[1]));\n }\n vector<vector<int>> final(maxnode+1);\n for(int i=0;i<edges.size();i++)\n {\n int x=edges[i][0];\n int y=edges[i][1];\n final[x].push_back(y);\n final[y].push_back(x);\n } \n int cnt=0; \n for(auto it:final)\n {\n cnt++;\n if(it.size()>1)\n {\n return cnt-1;\n }\n }\n return -1;\n }\n};", "memory": "93476" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int n = edges.size()+1;\n vector<int> adj_list[n+1];\n for(auto it:edges)\n {\n adj_list[it[0]].push_back(it[1]);\n adj_list[it[1]].push_back(it[0]);\n }\n \n for(int idx = 0; idx <= n ;idx++)\n {\n if(adj_list[idx].size() == n-1)\n return idx;\n }\n return -1;\n \n }\n\n\n};", "memory": "94605" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n unordered_map<int,int> mpp;\n for (auto it:edges)\n {\n for (int num : it) {\n mpp[num]++; \n }\n }\n\n int n=edges.size();\n int val=0;\n for (auto it:mpp)\n {\n if(it.second == n) val=it.first;\n }\n return val;\n }\n};", "memory": "94605" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n int m=edges.size();\n int n=2*edges.size();\n /*vector<int>li(n,0);\n for(auto i:edges){\n li[i[0]]++;\n li[i[1]]++;\n }*/\n map<int,int>mp;\n for(auto i:edges){\n mp[i[0]]++;\n mp[i[1]]++;\n }\n for(auto i:mp){\n if(i.second==m){\n return i.first;\n }\n }\n return 0;\n }\n};", "memory": "95734" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n \n map<int, int> answer;\n int maxN=0;\n int remember=-1;\n\n for(auto vec : edges) {\n for(auto e : vec) {\n answer[e]++;\n if(edges.size()==answer[e])return e;\n }\n }\n \n\n return maxN;\n \n\n }\n};", "memory": "95734" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n map<int, int> mpp;\n int n =edges.size();\n for(auto edge : edges){\n mpp[edge[0]]++;\n mpp[edge[1]]++;\n }\n for(auto mp:mpp){\n if(mp.second == n){\n return mp.first;\n }\n }\n return 0;\n }\n};", "memory": "96863" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n unordered_map<int, int> freq;\n for(vector<int> edge : edges) {\n int u = edge[0];\n int v = edge[1];\n if(freq.count(u)) {\n freq[u] += 1;\n }\n else {\n freq[u] = 1;\n }\n if(freq.count(v)) {\n freq[v] += 1;\n }\n else {\n freq[v] = 1;\n }\n }\n vector<pair<int, int>> vec(freq.begin(), freq.end());\n sort(vec.begin(), vec.end(), [] (const pair<int, int> &a, const pair<int, int> &b) {\n return a.second > b.second;\n });\n return vec[0].first;\n }\n};", "memory": "96863" }
1,916
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p> <p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/24/star_graph.png" style="width: 331px; height: 321px;" /> <pre> <strong>Input:</strong> edges = [[1,2],[2,3],[4,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &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>1 &lt;= u<sub>i,</sub> v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>The given <code>edges</code> represent a valid star graph.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findCenter(vector<vector<int>>& edges) {\n\n int n = edges.size();\n \n vector<int> arr(1e5 + 1, 0);\n\n for(int i = 0 ;i<n; i++){\n arr[edges[i][0]]++;\n arr[edges[i][1]]++;\n }\n\n for(int i = 0; i<1e5 + 1; i++){\n if(arr[i] == n){\n return i;\n }\n }\n\n return 0;\n\n \n }\n};", "memory": "97991" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n int ans=0, l=k, r=k, mv=nums[k];\n while (l>=0 || r<n) {\n while (l>=0 && nums[l]>=mv) --l;\n while (r<n && nums[r]>=mv) ++r;\n ans=max(ans, mv*(r-l-1));\n \n if (l>=0 && r<n) {\n if (nums[l]>=nums[r]) {\n mv=nums[l];\n --r;\n }\n else {\n mv=nums[r];\n ++l;\n }\n }\n else if (l>=0) {\n mv=nums[l];\n --r;\n }\n else if (r<n) {\n mv=nums[r];\n ++l;\n }\n }\n return ans;\n }\n};\n\nstatic int x=[](){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n return 0;\n}();", "memory": "91600" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
0
{ "code": "class Solution {\n /*\n * TWO POINTER APPROACH\n * Ref: https://leetcode.com/problems/maximum-score-of-a-good-subarray/solutions/1108333/java-c-python-two-pointers/\n */\npublic:\n Solution() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n }\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n int i=k, j=k, res=nums[k], mini=nums[k];\n\n while(i-1>=0 || j+1<n) {\n int left = (i-1>=0) ? nums[i-1] : INT_MIN;\n int right = (j+1<n) ? nums[j+1] : INT_MIN;\n\n if(left >= right) {\n mini = min(mini, left);\n i--;\n res = max(res, mini*(j-i+1));\n } else {\n mini = min(mini, right);\n j++;\n res = max(res, mini*(j-i+1));\n }\n }\n return res;\n }\n};", "memory": "91700" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int i=k,j=k;\n int curr_min=nums[k];\n int ans=nums[k],n=nums.size();\n while(i>0||j<n-1){\n int leftvalue=(i>0)?nums[i-1]:0;\n int rightvalue=(j<n-1)?nums[j+1]:0;\n\n if(leftvalue<rightvalue){\n j++;\n curr_min=min(nums[j],curr_min);\n }else{\n i--;\n curr_min=min(nums[i],curr_min);\n }\n\n ans=max(ans,curr_min*(j-i+1));\n\n }\n\n return ans;\n }\n};\n\nauto init = [](){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();", "memory": "91800" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n \n int n=nums.size();\n\n int i=k;\n int j=k;\n int mini=nums[k];\n int left=nums[k];\n int right=nums[k];\n int res=nums[k];\n while(i>0 || j<n-1){\n \n left=i>0?nums[i-1]:0;\n right=j<n-1?nums[j+1]:0;\n\n if(left>right){\n i--;\n mini=min(mini,nums[i]);\n }\n else{\n j++;\n mini=min(mini,nums[j]);\n }\n\n res=max(res,mini*(j-i+1));\n }\n\n return res;\n }\n};", "memory": "91900" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int res = nums[k];\n int mini = nums[k];\n int i = k;\n int j = k;\n int n = nums.size();\n\n while (i > 0 || j < n - 1) {\n if (i == 0) {\n j++;\n } else if (j == n - 1) {\n i--;\n } else if (nums[i - 1] < nums[j + 1]) {\n j++;\n } else {\n i--;\n }\n\n mini = min(mini, min(nums[i], nums[j]));\n res = max(res, mini * (j - i + 1));\n }\n\n return res;\n }\n};", "memory": "92000" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n int lo = k;\n int hi = k;\n int ans = nums[k];\n int curMin = nums[k];\n\n while (0 < lo && hi < n - 1) {\n if (nums[lo - 1] < nums[hi + 1]) {\n hi++;\n curMin = min(curMin, nums[hi]);\n } else {\n lo--;\n curMin = min(curMin, nums[lo]);\n }\n ans = max(ans, curMin * (hi - lo + 1));\n }\n\n while (0 < lo) {\n lo--;\n curMin = min(curMin, nums[lo]);\n ans = max(ans, curMin * (hi - lo + 1));\n }\n\n while (hi < n - 1) {\n hi++;\n curMin = min(curMin, nums[hi]);\n ans = max(ans, curMin * (hi - lo + 1));\n }\n return ans;\n }\n};", "memory": "92000" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int i=k;\n int j=k;\n int currmin =nums[k];\n int ans = nums[k];\n\n int n = nums.size();\n\n while(i>0 ||j<n-1){\n int leftvalue;\n int rightvalue;\n\n if(i>0){\n leftvalue = nums[i-1];\n }else leftvalue =0;\n\n if(j<n-1) rightvalue = nums[j+1];\n else rightvalue =0;\n\n if(leftvalue<rightvalue){\n j++;\n currmin = min(currmin,nums[j]);\n }\n else{\n i--;\n currmin = min(currmin,nums[i]);\n }\n\n ans = max(ans,currmin*(j-i+1));\n }\n\n return ans;\n }\n};", "memory": "92100" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int left = k, right = k;\n \n int min_val = nums[k];\n int max_score = min_val;\n\n while (left > 0 || right < nums.size() - 1) {\n if (left == 0 || nums[right + 1] > nums[left - 1]) {\n right++;\n } else {\n left--;\n }\n min_val = min({min_val, nums[left], nums[right]});\n max_score = max(max_score, min_val * (right - left + 1));\n }\n \n return max_score;\n }\n};", "memory": "92100" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int left = k , right = k;\n int min_val = nums[k];\n int ans = min_val;\n int n = nums.size();\n\n while(left>0 || right < n-1){\n if(left == 0 || (right<n-1 && nums[right+1]>nums[left-1])){\n right++;\n }else{\n left--;\n }\n min_val = min({min_val , nums[left] , nums[right]});\n ans = max(ans , min_val*(right-left+1));\n }\n return ans;\n }\n};", "memory": "92200" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n int left = k;\n int right = k;\n int ans = nums[k];\n int currMin = nums[k];\n \n while (left > 0 || right < n - 1) {\n if ((left > 0 ? nums[left - 1]: 0) < (right < n - 1 ? nums[right + 1] : 0)) {\n right++;\n currMin = min(currMin, nums[right]);\n } else {\n left--;\n currMin = min(currMin, nums[left]);\n }\n \n ans = max(ans, currMin * (right - left + 1));\n }\n \n return ans;\n }\n};", "memory": "92200" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int res = nums[k];\n\n int left = k, right = k;\n\n int n = nums.size();\n\n int currentMin = nums[k];\n\n while (left > 0 && right < n) {\n res = max(res, (right - left + 1) * currentMin);\n\n left--;\n ++right;\n if (left >= 0 && nums[left] <= nums[right]) {\n ++left;\n } else if (right <= n - 2 && nums[right] <= nums[left]) {\n --right;\n }\n\n left = max(left, 0);\n right = min(right, n - 1);\n currentMin = min(currentMin, min(nums[left], nums[right]));\n }\n\n while (left > 0) {\n currentMin = min(currentMin, nums[left]);\n res = max(res, (right - left + 1) * currentMin);\n left--;\n }\n\n while (right < n) {\n currentMin = min(currentMin, nums[right]);\n res = max(res, (right - left + 1) * currentMin);\n right++;\n }\n return res;\n }\n};", "memory": "92300" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n int left = k;\n int right = k;\n int ans = nums[k];\n int currMin = nums[k];\n \n while (left > 0 || right < n - 1) {\n if ((left > 0 ? nums[left - 1]: 0) < (right < n - 1 ? nums[right + 1] : 0)) {\n right++;\n currMin = min(currMin, nums[right]);\n } else {\n left--;\n currMin = min(currMin, nums[left]);\n }\n \n ans = max(ans, currMin * (right - left + 1));\n }\n \n return ans;\n }\n};", "memory": "92300" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\nint calc_score(int i, int j, int min){\n return min*(j-i+1);\n}\nint maximumScore(vector<int>& nums, int k){\n int i, j, min = nums[k], n = nums.size(), score = nums[k];\n min = nums[k];\n int marr[nums.size()];\n for(i = k; i > -1; i--){\n if(nums[i] <= min) { min = nums[i];}\n marr[i] = min;\n } min = nums[k];\n for(i = k; i < n; i++){\n if(nums[i] <= min) { min = nums[i];}\n marr[i] = min;\n } min = nums[k];\n i = 0;j = n-1;\n while(i <= k && j >= k){\n if( marr[i] < marr[j]) {\n if(score < calc_score(i, j, marr[i])) score = calc_score(i,j,marr[i]); i++; \n }\n if(marr[i] >= marr[j]){\n if(score < calc_score(i,j, marr[j])) score = calc_score(i,j,marr[j]); j--;\n }\n }\n return score;\n}\n\n\n};", "memory": "92600" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int leftMostGTE[100000] = {0};\n int rightMostGTE[100000] = {0};\n\n int maximumScore(vector<int>& nums, int k) {\n for (int i = 0; i < nums.size(); i++) {\n leftMostGTE[i] = i;\n\n if (i == 0) {\n continue;\n }\n\n int tmp = i - 1;\n while (tmp >= 0 && nums[tmp] >= nums[i]) {\n leftMostGTE[i] = leftMostGTE[tmp];\n tmp = leftMostGTE[i] - 1;\n // cout << \"[x] leftMostGTE \" << i << \" \" << leftMostGTE[i] << endl;\n }\n\n // cout << \"leftMostGTE \" << i << \" \" << leftMostGTE[i] << endl;\n }\n\n for (int i = nums.size() - 1; i >= 0; i--) {\n rightMostGTE[i] = i;\n\n if (i == nums.size() - 1) {\n continue;\n }\n\n int tmp = i + 1;\n while (tmp <= nums.size() - 1 && nums[tmp] >= nums[i]) {\n rightMostGTE[i] = rightMostGTE[tmp];\n tmp = rightMostGTE[i] + 1;\n // cout << \"[x] rightMostGTE \" << i << \" \" << rightMostGTE[i] << endl;\n }\n\n // cout << \"rightMostGTE \" << i << \" \" << rightMostGTE[i] << endl;\n }\n\n int ans = 0;\n\n for (int i = 0; i < nums.size(); i++) { \n if (leftMostGTE[i] <= k && rightMostGTE[i] >= k) {\n ans = max(ans, (rightMostGTE[i] - leftMostGTE[i] + 1) * nums[i]);\n }\n }\n\n return ans;\n }\n};", "memory": "93100" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "#pragma GCC optimize(\"O3\")\n\narray<int, 100001> left1, right1 ;\nclass Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int N = nums.size() ;\n // vector<int> left1(N, -1) ;\n // vector<int> right1(N, N) ;\n\n for(int idx = 0 ; idx < N ; ++idx) {\n left1[idx] = idx - 1 ;\n while(left1[idx] != -1 && nums[left1[idx]] >= nums[idx]) {\n left1[idx] = left1[left1[idx]] ; \n }\n }\n\n for(int idx = N-1; idx >= 0 ; --idx) {\n right1[idx] = idx + 1 ;\n while(right1[idx] != N && nums[right1[idx]] >= nums[idx]) {\n right1[idx] = right1[right1[idx]] ;\n }\n }\n\n int result = 0 ;\n for(int idx = 0 ; idx < N ; ++idx) {\n // int left1_idx = left1[idx] ;\n // int righ\n if(left1[idx] < k && k < right1[idx]) {\n\n int delta = right1[idx] - left1[idx] - 1 ;\n result = max(result, delta * nums[idx]) ;\n }\n\n\n }\n\n // for(auto idx : left1) {\n // cout << idx << \" \" ;\n // }\n // cout << endl ;\n\n\n // for(auto idx : right1) {\n // cout << idx << \" \" ;\n // }\n // cout << endl ;\n\n return result ;\n }\n};", "memory": "93200" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& a, int k) {\n\n int n=a.size();\n int mn=a[k],x=1;\n priority_queue<pair<int,pair<int,int>>>q;\n if(k) q.push({a[k-1],{k-1,-1}});\n if(k<n-1) q.push({a[k+1],{k+1,1}});\n\n int ans=a[k];\n while(q.size()){\n auto it=q.top();\n q.pop();\n mn=min(mn,it.first);\n x++;\n ans=max(ans,mn*x);\n int ind=it.second.first+it.second.second;\n if(ind>=0 && ind<n) q.push({a[ind],{ind,it.second.second}});\n\n }\n return ans;\n \n }\n};", "memory": "93300" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n priority_queue<pair<int,int>> pq;\n if(k-1>=0)pq.push({nums[k-1],k-1});\n if(k+1<nums.size())pq.push({nums[k+1],k+1});\n \n int ans=nums[k],cnt=1,mn=nums[k];\n while(!pq.empty()){\n int val, ind;\n tie(val,ind)=pq.top();\n pq.pop();\n int t=min(mn,val);\n if(t*(cnt+1)>=mn*cnt){\n ans=max(ans,t*(cnt+1));\n }\n mn=min(t,mn);\n cnt++;\n int dir=(k>ind)?-1:1;\n ind=ind+dir;\n if(ind<0 || ind>=nums.size())continue;\n pq.push({nums[ind],ind});\n }\n return ans;\n\n }\n};", "memory": "93400" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n\n int n = nums.size();\n\n stack<int> st;\n\n st.push(-1);\n\n int ans = nums[k];\n\n for (int i = 0; i < n; i++) {\n while (st.top() != -1 && nums[st.top()] >= nums[i]) {\n int height = nums[st.top()];\n st.pop();\n int width = i - st.top() - 1;\n\n if (st.top()<k && i>k) {\n ans = max(ans, height * width);\n }\n }\n st.push(i);\n }\n\n while (st.top() != -1) {\n int height = nums[st.top()];\n st.pop();\n int width = n - st.top() - 1;\n\n if (st.top()<k) {\n ans = max(ans, height * width);\n }\n }\n\n return ans;\n }\n};", "memory": "93600" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int score = 0;\n int n = nums.size();\n stack<int> st;\n\n for (int i = 0; i <= n; i++) {\n while (!st.empty() && (i == n || nums[st.top()] >= nums[i])) {\n int mn = nums[st.top()];\n st.pop();\n int left = st.empty() ? -1 : st.top();\n int right = i;\n \n if (left < k && right > k) {\n score = max(score, mn * (right - left - 1));\n }\n }\n st.push(i);\n }\n return score;\n }\n \n};", "memory": "93700" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n // finding the largest rectangle of the histogram\n\n // to store index\n stack<int> stk;\n\n // only compute the are when current element is smaller than stk.top()\n // only take the area range which involves k \n int nMax = 0;\n for( int i = 0; i <= nums.size(); i++ ) {\n int nNum = i < nums.size() ? nums[i] : 0;\n while( !stk.empty() && nNum < nums[stk.top()] ) {\n int ind = stk.top();\n int value = nums[ind];\n stk.pop();\n int leftInd = stk.empty() ? -1 : stk.top();\n\n //printf( \"%d, %d, %d\\n\", i, nNum, leftInd );\n\n if( k >= leftInd + 1 && k <= i - 1 ) {\n int len = i - leftInd - 1; // (range) leftInd+1, i-1\n int area = len * value;\n //printf( \"area: %d, %d, %d\\n\", len, area, value );\n nMax = max( area, nMax );\n }\n }\n\n stk.push(i);\n }\n\n return nMax;\n }\n};", "memory": "93800" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n // finding the largest rectangle of the histogram\n\n // to store index\n stack<int> stk;\n\n // only compute the are when current element is smaller than stk.top()\n // only take the area range which involves k \n int nMax = 0;\n for( int i = 0; i <= nums.size(); i++ ) {\n int nNum = i < nums.size() ? nums[i] : 0;\n while( !stk.empty() && nNum < nums[stk.top()] ) {\n int value = nums[stk.top()];\n stk.pop();\n int leftInd = stk.empty() ? -1 : stk.top();\n\n //printf( \"%d, %d, %d\\n\", i, nNum, leftInd );\n\n if( k >= leftInd + 1 && k <= i - 1 ) {\n int len = i - leftInd - 1; // (range) leftInd+1, i-1\n int area = len * value;\n //printf( \"area: %d, %d, %d\\n\", len, area, value );\n nMax = max( area, nMax );\n }\n }\n\n stk.push(i);\n }\n\n return nMax;\n }\n};", "memory": "93800" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n if(!n)\n return 0;\n stack<int>st;\n int ret = 0;\n for(int i = 0;i<=n;i++){\n int val = i==n?-1:nums[i];\n while(!st.empty() && val < nums[st.top()]){\n int h = nums[st.top()];\n st.pop();\n int l = st.empty()?-1:st.top();\n if(k<i && k>l)\n ret = max(ret, h*(i-l-1));\n }\n st.push(i);\n }\n return ret;\n }\n};", "memory": "93900" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& a, int k) {\n int n = a.size();\n int next[n];\n int prev[n];\n stack<int> s;\n int l, idx, ans = -1 ;\n\n // ONE PASS SOLUTION \n\n for(int i=0; i<n; i++) {\n while(!s.empty() && a[s.top()] > a[i]){\n idx = s.top();\n s.pop();\n l = s.empty() ? 0 : s.top();\n next[idx] = i;\n prev[idx] = l;\n if(a[prev[idx]] < a[idx]) prev[idx]++;\n if(a[next[idx]] < a[idx]) next[idx]--;\n\n if(next[idx]>=k && prev[idx]<=k) ans = max(ans, a[idx]*(next[idx]-prev[idx] + 1));\n }\n \n s.push(i);\n }\n\n while(!s.empty()){\n idx = s.top();\n s.pop();\n l = s.empty() ? 0 : s.top();\n next[idx] = n-1;\n prev[idx] = l;\n if(a[prev[idx]] < a[idx]) prev[idx]++;\n if(a[next[idx]] < a[idx])next[idx]--;\n if(next[idx]>=k && prev[idx]<=k)ans = max(ans, a[idx]*(next[idx]-prev[idx] + 1));\n }\n \n // for(int i=0; i<n; i++) cout << i << \" : \" << a[i] << \" \" << next[i] << \" \" << prev[i] << endl;\n \n return ans;\n }\n};\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n return '$';\n}();", "memory": "94000" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n stack<int> firstHalf, secondHalf;\n for(int a=0; a<=k; a++){\n while(!firstHalf.empty() && nums[firstHalf.top()] > nums[a])\n firstHalf.pop();\n firstHalf.push(a);\n }\n for(int a=nums.size()-1; a>=k; a--){\n while(!secondHalf.empty() && nums[secondHalf.top()] > nums[a])\n secondHalf.pop();\n secondHalf.push(a);\n }\n\n int ans = nums[k];\n while(!firstHalf.empty() || !secondHalf.empty()){\n int lvalue = !firstHalf.empty() ? nums[firstHalf.top()] : -1, rvalue = !secondHalf.empty() ? nums[secondHalf.top()] : -1 ;\n int value;\n if(lvalue >= rvalue){\n firstHalf.pop(); \n value = lvalue;\n } else{\n secondHalf.pop();\n value = rvalue;\n }\n int leftPoint = !firstHalf.empty() ? firstHalf.top() : -1;\n int rightPoint = !secondHalf.empty() ? secondHalf.top() : nums.size();\n ans = max(ans, (rightPoint - leftPoint -1)*value);\n }\n return ans;\n }\n};\n", "memory": "94100" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n\n int minV, score = 0, s;\n vector<pair<int, int>> left;\n \n minV = nums[k];\n for (int i = k - 1; i >= 0; i--) {\n if (nums[i] < minV) {\n left.push_back({minV, i + 1});\n minV = nums[i];\n }\n }\n if (left.empty() || minV < left.back().first) left.push_back({minV, 0});\n // for (auto [v, i] : left) cout << i << \"(\" << v << \")\" << endl;\n // cout << endl;\n \n minV = nums[k];\n for (int j = k + 1; j < n; j++) {\n if (nums[j] < minV) {\n for (auto [v1, i] : left) {\n int s = min(minV, v1) * (j - i);\n score = max(score, s);\n }\n minV = nums[j];\n }\n }\n int j = n;\n for (auto [v1, i] : left) {\n int s = min(minV, v1) * (j - i);\n score = max(score, s);\n }\n return score;\n }\n};", "memory": "94200" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& a, int k) {\n int n = a.size();\n int next[n];\n int prev[n];\n stack<int> s;\n int l, idx, ans = -1 ;\n for(int i=0; i<n; i++) {\n while(!s.empty() && a[s.top()] > a[i]){\n idx = s.top();\n s.pop();\n l = s.empty() ? 0 : s.top();\n next[idx] = i;\n prev[idx] = l;\n // cout << prev[idx] << \" \" << next[idx] << \" \" << a[idx] << endl;\n if(a[prev[idx]] < a[idx]) prev[idx]++;\n if(a[next[idx]] < a[idx])next[idx]--;\n if(next[idx]>=k && prev[idx]<=k)ans = max(ans, a[idx]*(next[idx]-prev[idx] + 1));\n }\n \n s.push(i);\n }\n\n while(!s.empty()){\n idx = s.top();\n s.pop();\n l = s.empty() ? 0 : s.top();\n next[idx] = n-1;\n prev[idx] = l;\n if(a[prev[idx]] < a[idx]) prev[idx]++;\n if(a[next[idx]] < a[idx])next[idx]--;\n if(next[idx]>=k && prev[idx]<=k)ans = max(ans, a[idx]*(next[idx]-prev[idx] + 1));\n }\n \n // for(int i=0; i<n; i++) cout << i << \" : \" << a[i] << \" \" << next[i] << \" \" << prev[i] << endl;\n \n return ans;\n }\n};", "memory": "94500" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int ans = 0;\n stack<pair<int, int>> st;\n for(int i=0;i<nums.size();i++){\n int idx = i;\n while(!st.empty() && st.top().first > nums[i]){\n pair<int, int> val = st.top();\n st.pop();\n if(val.second <= k && i - 1 >= k){\n int aux = val.first * (((i - 1) - val.second) + 1);\n ans = max(ans, aux);\n }\n idx = val.second;\n }\n st.push({nums[i], idx});\n }\n while(!st.empty()){\n pair<int, int> val = st.top();\n st.pop();\n if(val.second <= k){\n int aux = val.first * (((nums.size() - 1) - val.second) + 1);\n ans = max(ans, aux); \n }\n }\n return ans;\n }\n};", "memory": "94700" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int ans = 0;\n stack<pair<int, int>> st;\n for(int i=0;i<nums.size();i++){\n int idx = i;\n while(!st.empty() && st.top().first > nums[i]){\n pair<int, int> val = st.top();\n st.pop();\n if(val.second <= k && i - 1 >= k){\n int aux = val.first * (((i - 1) - val.second) + 1);\n ans = max(ans, aux);\n }\n idx = val.second;\n }\n st.push({nums[i], idx});\n }\n while(!st.empty()){\n pair<int, int> val = st.top();\n st.pop();\n if(val.second <= k){\n int aux = val.first * (((nums.size() - 1) - val.second) + 1);\n ans = max(ans, aux); \n }\n }\n return ans;\n }\n};", "memory": "94800" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n constexpr int padding = 0;\n \n nums.emplace(nums.begin(), padding);\n nums.push_back(padding);\n \n int res = 0;\n stack<size_t> mins;\n mins.push(padding);\n\n ++k; // adjust due to padding\n for (size_t pos = 1; pos < nums.size(); ++pos) {\n while (nums[mins.top()] > nums[pos]) {\n const size_t less = mins.top();\n mins.pop();\n\n const size_t i = mins.top() + 1;\n const size_t j = pos - 1;\n\n if (i <= k && k <= j) {\n res = max(res, nums[less] * static_cast<int>(j - i + 1));\n }\n }\n mins.push(pos);\n }\n return res;\n }\n};", "memory": "95100" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int ans = 0;\n stack<pair<int, int>> st;\n for(int i=0;i<nums.size();i++){\n int idx = i;\n while(!st.empty() && st.top().first > nums[i]){\n pair<int, int> val = st.top();\n st.pop();\n if(val.second <= k && i - 1 >= k){\n int aux = val.first * (((i - 1) - val.second) + 1);\n ans = max(ans, aux);\n }\n idx = val.second;\n }\n st.push({nums[i], idx});\n }\n while(!st.empty()){\n pair<int, int> val = st.top();\n st.pop();\n if(val.second <= k){\n int aux = val.first * (((nums.size() - 1) - val.second) + 1);\n ans = max(ans, aux); \n }\n }\n return ans;\n }\n};", "memory": "95200" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int>ans(n,1e9);\n ans[k]=nums[k];\n for(int i=k-1;i>=0;i--)\n {\n ans[i]=min(ans[i+1],nums[i]);\n }\n for(int i=k+1;i<n;i++)\n {\n ans[i]=min(ans[i-1],nums[i]);\n }\n int i=0,j=n-1,count=0;\n while(i<=k&&j>=k)\n {\n count=max(count,(j-i+1)*min(ans[i],ans[j]));\n if(ans[i]==min(ans[i],ans[j]))i++;\n else j--;\n }\n // for(auto it:ans)cout<<it<<\" \";\n return count;\n }\n};", "memory": "96400" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> v(n);\n v[k] = nums[k];\n for(int i = k-1;i>=0;i--){\n v[i] = min(v[i+1],nums[i]);\n }\n for(int i = k+1;i<n;i++){\n v[i] = min(v[i-1],nums[i]);\n }\n int i = 0;\n int j = n-1;\n int maxi = -1;\n while(i<=j){\n if(v[i] < v[j]){\n maxi = max(maxi, v[i] * (j-i+1));\n i++;\n }\n else{\n maxi = max(maxi, v[j] * (j-i+1));\n j--;\n }\n }\n return maxi;\n }\n};", "memory": "96400" }
1,918
<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p> <p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p> <p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3 <strong>Output:</strong> 15 <strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0 <strong>Output:</strong> 20 <strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt; nums.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n vector<int> pref(nums.size());\n\n pref[k] = nums[k];\n\n int minEl = nums[k];\n\n for (int i = k - 1; i >= 0; --i) {\n if (nums[i] < minEl)\n minEl = nums[i];\n\n pref[i] = minEl;\n }\n\n minEl = nums[k];\n\n for (int i = k + 1; i < nums.size(); ++i) {\n if (nums[i] < minEl)\n minEl = nums[i];\n pref[i] = minEl;\n }\n\n int left = 0, right = nums.size() - 1;\n\n int res = nums[k];\n\n while (left < k && right > k) {\n res = max(res, (right - left + 1) * min(pref[left], pref[right]));\n res = max(res, (right - k + 1) * min(pref[right], pref[k]));\n\n res = max(res, (k - left + 1) * min(pref[left], pref[k]));\n\n if (pref[left] <= pref[right]) {\n ++left;\n } else\n --right;\n }\n\n while (left < k) {\n res = max(res, (right - left + 1) * min(pref[left], pref[right]));\n res = max(res, (k - left + 1) * min(pref[left], pref[k]));\n\n ++left;\n }\n\n while (right > k) {\n res = max(res, (right - left + 1) * min(pref[left], pref[right]));\n res = max(res, (right - k + 1) * min(pref[right], pref[k]));\n --right;\n }\n\n return res;\n }\n};", "memory": "96500" }