id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n int m = mat.size();\n int n = mat[0].size();\n vector<int> diagonal;\n for (int i = 0; i < m; ++i)\n {\n diagonal.clear();\n for (int j = 0; i + j < m && j < n; ++j)\n diagonal.push_back(mat[i + j][j]);\n sort(diagonal.begin(), diagonal.end());\n for (int j = 0; i + j < m && j < n; ++j)\n mat[i + j][j] = diagonal[j];\n }\n for (int j = 1; j < n; ++j)\n {\n diagonal.clear();\n for (int i = 0; i + j < n && i < m; ++i)\n diagonal.push_back(mat[i][i + j]);\n sort(diagonal.begin(), diagonal.end());\n for (int i = 0; i + j < n && i < m; ++i)\n mat[i][i + j] = diagonal[i];\n }\n return mat;\n }\n};", "memory": "11400" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n vector<int> c_diag;\n int m = mat.size() , n = mat[0].size();\n\n for (int r = 0; r < m; r++) {\n c_diag.clear();\n\n for (int i = r, j = 0; i < m && j < n; i++, j++) {\n c_diag.push_back(mat[i][j]);\n }\n\n sort(c_diag.begin(), c_diag.end());\n\n for (int i = r, j = 0; i < m && j < n; i++, j++) {\n mat[i][j] = c_diag[j];\n }\n }\n\n for (int c = 1; c < n; c++) {\n c_diag.clear();\n\n for (int i = 0, j = c; j < n && i < m; i++, j++) {\n c_diag.push_back(mat[i][j]);\n }\n\n sort(c_diag.begin(), c_diag.end());\n\n for (int i = 0, j = c; j < n && i < m; i++, j++) {\n mat[i][j] = c_diag[i];\n }\n }\n\n return mat;\n }\n};", "memory": "11500" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n int rows=mat.size(),cols=mat[0].size(),d=rows+cols-1;\n vector<vector<int>> res(rows,vector<int>(cols,0));\n vector<int>sorting;\n for(int r=0;r<rows;r++)\n { int i=r,j=0;\n while(i>=0 && i<rows && j>=0 && j<cols)\n sorting.push_back(mat[i++][j++]);\n\n sort(sorting.begin(),sorting.end());\n\n i=r,j=0;\n int counter=0;\n while(i>=0 && i<rows && j>=0 && j<cols)\n { \n res[i][j]=sorting[counter++];\n i++; \n j++;\n }\n sorting.clear();\n }\n\n for(int c=0;c<cols;c++)\n { int i=0,j=c;\n while(i>=0 && i<rows && j>=0 && j<cols)\n sorting.push_back(mat[i++][j++]);\n\n sort(sorting.begin(),sorting.end());\n\n i=0,j=c;\n int counter=0;\n while(i>=0 && i<rows && j>=0 && j<cols)\n { \n res[i][j]=sorting[counter++];\n i++; \n j++;\n }\n sorting.clear();\n }\n\n return res;\n }\n};", "memory": "11600" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n int m=mat.size(),n=mat[0].size();\n auto sortDiagonal=[&](int row,int col) \n {\n vector<int> diagonal;\n int r=row,c=col;\n while(r<m&&c<n) \n {\n diagonal.push_back(mat[r][c]);\n r++;\n c++;\n }\n sort(diagonal.begin(),diagonal.end());\n r=row;\n c=col;\n int idx=0;\n while(r<m&&c<n) \n {\n mat[r][c]=diagonal[idx++];\n r++;\n c++;\n }\n };\n for(int col=0;col<n;col++) \n sortDiagonal(0,col);\n for(int row=1;row<m;row++) \n sortDiagonal(row,0);\n return mat;\n }\n};", "memory": "11900" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n int m=mat.size();\n int n=mat[0].size();\n \n \n for(int i=0;i<n;i++) {\n vector<int>vec;\n int r = 0;\n int c = i;\n while(r < m && c < n) {\n vec.push_back(mat[r][c]);\n c++;\n r++;\n }\n sort(vec.begin(), vec.end());\n r = 0, c = i;\n int d = 0;\n while(r < m && c < n) {\n mat[r][c] = vec[d];\n r++;c++;d++;\n }\n }\n for(int i=1;i<m;i++) {\n vector<int>vec;\n int r = i;\n int c = 0;\n while(r < m && c < n) {\n vec.push_back(mat[r][c]);\n c++;\n r++;\n }\n sort(vec.begin(), vec.end());\n r = i, c = 0;\n int d = 0;\n while(r < m && c < n) {\n mat[r][c] = vec[d];\n r++;c++;d++;\n }\n }\n return mat;\n }\n};\n/*\n[0 0] [1 1] [2 2]\n[0 1] [1 2] [2 3]\n*/", "memory": "12000" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n \n for(int i=0;i<mat.size();i++){\n vector<int>v;\n int x=i;\n int y=0;\n while(x<mat.size() && y<mat[0].size()){\n v.push_back(mat[x][y]);\n x++;\n y++;\n }\n sort(v.begin(),v.end());\n x=i;\n y=0;\n for(int k=0;k<v.size();k++){\n mat[x][y]=v[k];\n x++;\n y++;\n }\n\n }\n for(int i=0;i<mat[0].size();i++){\n vector<int>v;\n int x=0;\n int y=i;\n while(x<mat.size() && y<mat[0].size()){\n v.push_back(mat[x][y]);\n x++;\n y++;\n }\n sort(v.begin(),v.end());\n x=0;\n y=i;\n for(int k=0;k<v.size();k++){\n mat[x][y]=v[k];\n x++;\n y++;\n }\n\n }\n return mat;\n \n }\n};", "memory": "12100" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n void solve(int row, int col, vector<int> &temp, vector<vector<int>> &ans, vector<vector<int>> &mat, int n, int m) {\n int i = row, j = col;\n \n while(i >= 0 && i < n && j >= 0 && j < m) {\n temp.push_back(mat[i][j]);\n i++;\n j++;\n }\n\n sort(temp.begin(), temp.end());\n i = row, j = col;\n\n int k = 0;\n\n while(i >= 0 && i < n && j >= 0 && j < m && k < temp.size()) {\n ans[i][j] = temp[k];\n k++;\n i++;\n j++;\n }\n }\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n int n = mat.size(), m = mat[0].size();\n vector<vector<int>> ans(n, vector<int>(m));\n\n for(int i=0; i<n; i++) {\n for(int j=0; j<m; j++) {\n if(i == 0 || j == 0) {\n vector<int> temp;\n solve(i, j, temp, ans, mat, n, m);\n }\n }\n }\n\n return ans;\n }\n};", "memory": "12200" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n const int n = mat.size();\n const int m = mat[0].size();\n int row = n-1;\n int col = 0;\n \n while(row>=0){\n vector<int> temp;\n int x = row, y = col;\n while(x<n && y<m){\n temp.push_back(mat[x][y]);\n x++;\n y++;\n }\n sort(temp.begin(),temp.end());\n int i = 0;\n for(x=row,y=col; x<n&&y<m; x++,y++){\n mat[x][y] = temp[i++];\n }\n row--;\n }\n row++;\n col++;\n while(col<m){\n vector<int> temp;\n int x = row, y = col;\n while(x<n && y<m){\n temp.push_back(mat[x][y]);\n x++;\n y++;\n }\n sort(temp.begin(),temp.end());\n int i = 0;\n for(x=row,y=col; x<n&&y<m; x++,y++){\n mat[x][y] = temp[i++];\n }\n col++;\n }\n return mat;\n }\n};\n\nauto init = [](){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();", "memory": "12300" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n int m = mat.size();\n int n = mat[0].size();\n map<int , vector<int>>mp;\n\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n mp[i-j].push_back(mat[i][j]);\n }\n }\n for(auto &it : mp){\n sort(it.second.begin() , it.second.end());\n }\n for(int i=m-1;i>=0;i--){\n for(int j=n-1;j>=0;j--){\n mat[i][j] = mp[i-j].back();\n mp[i-j].pop_back();\n }\n }\n return mat;\n }\n};", "memory": "12400" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n map<int,vector<int>>mp;\n for(int i=0;i<mat.size();i++){\n for(int j=0;j<mat[0].size();j++){\n mp[i-j].push_back(mat[i][j]);\n }\n }\n for(auto &it:mp){\n sort(it.second.begin(),it.second.end());\n }\n for(int i=mat.size()-1;i>=0;i--){\n for(int j=mat[0].size()-1;j>=0;j--){\n mat[i][j]=mp[i-j].back();\n mp[i-j].pop_back();\n }\n }\n return mat;\n }\n};", "memory": "12500" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n map<int, vector<int>>mp;\n int n = mat.size();\n int m = mat[0].size();\n\n for(int i = 0; i < n; i++){\n for(int j = 0; j < m; j++){\n mp[i-j].push_back(mat[i][j]);\n }\n }\n\n for(auto &temp : mp){\n sort(temp.second.begin(), temp.second.end());\n }\n\n for(int i = n - 1; i >= 0; i--){\n for(int j = m - 1; j >= 0; j--){\n mat[i][j] = mp[i-j].back();\n mp[i-j].pop_back();\n }\n }\n \n return mat;\n }\n};", "memory": "12500" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool inMatrix(int x, int y, int m, int n){\n if(x < 0 || x >= m) return false;\n if(y < 0 || y >= n) return false;\n return true;\n }\n\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n int m = mat.size(), n = mat[0].size(), i, j, offset;\n vector<vector<int>> lists;\n\n for(offset = 0; offset < m+n-1; offset++){\n vector<int> l;\n for(i=(m-1)-offset, j=0; i<m && j<n; i++, j++){\n if(inMatrix(i,j,m,n)){\n l.push_back(mat[i][j]);\n }\n }\n sort(l.begin(), l.end());\n lists.push_back(l);\n }\n\n for(offset = 0; offset < m+n-1; offset++){\n int cnt = 0;\n for(i=(m-1)-offset, j=0; i<m && j<n; i++, j++){\n if(inMatrix(i,j,m,n)){\n mat[i][j] = lists[offset][cnt];\n cnt++;\n }\n }\n }\n\n return mat;\n }\n};", "memory": "12600" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n unordered_map<int, vector<int>> mp;\n\n // Collect diagonals starting from each row of the first column\n for (int i = mat.size() - 1; i >= 0; i--) {\n vector<int> diag;\n int row = i;\n int col = 0;\n while (row < mat.size() && col < mat[0].size()) {\n diag.push_back(mat[row][col]);\n row++;\n col++;\n }\n\n sort(diag.begin(), diag.end());\n mp[i] = diag;\n }\n\n // Collect diagonals starting from each column of the first row (except\n // the first element)\n for (int i = 1; i < mat[0].size(); i++) {\n vector<int> diag;\n int row = 0;\n int col = i;\n while (row < mat.size() && col < mat[0].size()) {\n diag.push_back(mat[row][col]);\n row++;\n col++;\n }\n\n sort(diag.begin(), diag.end());\n mp[-1 * i] = diag;\n }\n\n // Place the sorted diagonals back into the matrix\n for (auto& [f, arr] : mp) {\n int row, col;\n if (f >= 0) {\n row = f;\n col = 0;\n } else {\n row = 0;\n col = -1 * f;\n }\n\n int k = 0;\n while (k < arr.size()) {\n mat[row][col] = arr[k];\n k++;\n row++;\n col++;\n }\n }\n\n return mat;\n }\n};\n", "memory": "12700" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n int rows=mat.size();\n int cols=mat[0].size();\n vector<vector<int>> diags;\n for(int i=rows-1; i>=0; i--){\n int j=0;\n int row=i;\n vector<int> diag;\n while(row<rows && j<cols){\n diag.push_back(mat[row][j]);\n row++, j++;\n }\n diags.push_back(diag);\n }\n for(int i=1; i<cols; i++){\n int j=0;\n int col=i;\n vector<int> diag;\n while(col<cols && j<rows){\n diag.push_back(mat[j][col]);\n col++, j++;\n }\n diags.push_back(diag);\n }\n int size=diags.size();\n for(int i=0; i<size; i++){\n sort(diags[i].begin(), diags[i].end());\n }\n int diagsInd=0;\n for(int i=rows-1; i>=0; i--){\n int j=0;\n int row=i;\n vector<int> diag=diags[diagsInd];\n diagsInd++;\n int ind=0;\n while(row<rows && j<cols){\n mat[row][j]=diag[ind];\n ind++;\n row++, j++;\n }\n }\n for(int i=1; i<cols; i++){\n int j=0;\n int col=i;\n vector<int> diag=diags[diagsInd];\n diagsInd++;\n int ind=0;\n while(col<cols && j<rows){\n mat[j][col]=diag[ind];\n ind++;\n col++, j++;\n }\n }\n return mat;\n }\n};", "memory": "12800" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n // Store the matrix dimensions.\n size_t m = mat.size();\n size_t n = mat[0].size();\n\n // Sort each diagonal that starts on a row.\n for (size_t row = 0; row < m; row++) {\n sortDiagonal(row, 0, mat);\n }\n\n // Sort each diagonal that starts on a col.\n // Note that we've already sorted the one starting\n // at col = 0; this is the same as the one starting\n // at row = 0.\n for (size_t col = 0; col < n; col++) {\n sortDiagonal(0, col, mat);\n }\n\n return mat;\n }\n\n // Helper function to sort the current diagonal at row, col\n void sortDiagonal(size_t row, size_t col, vector<vector<int>>& mat) {\n size_t m = mat.size();\n size_t n = mat[0].size();\n\n // create a min-heap to store the diagonal\n priority_queue<int, vector<int>, greater<int>> diagonal;\n\n size_t diagonal_length = min(m - row, n - col);\n // Put values on this diagonal into the heap.\n for (size_t i = 0; i < diagonal_length; i++) {\n diagonal.push(mat[row + i][col + i]);\n }\n\n // Put values on heap back into this diagonal\n for (size_t i = 0; i < diagonal_length; i++) {\n mat[row + i][col + i] = diagonal.top();\n diagonal.pop();\n }\n }\n};", "memory": "12900" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n int m = mat.size();\n int n = mat[0].size();\n unordered_map<int,vector<int>> mp;\n for(int i = 0;i<m ;i++){\n for(int j = 0;j<n;j++){\n mp[i-j].push_back(mat[i][j]);\n }\n }\n\n for(auto &it : mp){\n sort(it.second.begin(),it.second.end());\n }\n\n for(int i = m-1;i>=0;i--){\n for(int j = n-1 ;j>=0;j--){\n mat[i][j] = mp[i-j].back();\n mp[i-j].pop_back();\n }\n }\n return mat;\n }\n};", "memory": "13000" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n int m = mat.size();\n int n = mat[0].size();\n \n unordered_map<int, vector<int>> mp;\n \n for(int i = 0; i<m; i++) {\n for(int j = 0; j<n; j++) {\n mp[i-j].push_back(mat[i][j]);\n }\n }\n \n \n for(auto &it : mp) {\n sort(it.second.begin(), it.second.end());\n }\n \n \n \n for(int i = m-1; i>=0; i--) {\n for(int j = n-1; j>=0; j--) {\n mat[i][j] = mp[i-j].back();\n \n mp[i-j].pop_back();\n }\n }\n \n return mat;\n }\n};", "memory": "13100" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n unordered_map<int,vector<int>>mp;\n\n int rows = mat.size();\n int cols = mat[0].size();\n\n for(int i=0;i<rows;i++){\n for(int j=0;j<cols;j++){\n mp[i-j].push_back(mat[i][j]);\n }\n }\n\n for(auto& ele : mp){\n sort(ele.second.begin(),ele.second.end());\n }\n\n for(int i = rows-1;i>=0;i--){\n for(int j = cols-1;j>=0;j--){\n mat[i][j] = mp[i-j].back();\n mp[i-j].pop_back();\n }\n }\n\n return mat;\n\n }\n};", "memory": "13200" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n int m=mat.size();\n int n=mat[0].size();\n\n unordered_map<int,vector<int>> myMap;\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n myMap[i-j].push_back(mat[i][j]);\n }\n }\n for(auto& i : myMap){\n sort(i.second.begin(),i.second.end());\n }\n\n for(int i=m-1;i>=0;i--){\n for(int j=n-1;j>=0;j--){\n mat[i][j]=myMap[i-j].back();\n myMap[i-j].pop_back();\n }\n }\n return mat;\n\n }\n};", "memory": "13200" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n int n = mat.size();\n int m = mat[0].size();\n unordered_map<int, vector<int>>mp;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n mp[i-j].push_back(mat[i][j]);\n }\n }\n for(auto &it : mp){\n sort(it.second.begin() , it.second.end());\n }\n vector<vector<int>>ans(n,vector<int>(m));\n for(int i=n-1;i>=0;i--){\n for(int j=m-1;j>=0;j--){\n ans[i][j]=mp[i-j].back();\n mp[i-j].pop_back();\n }\n }\n return ans;\n }\n}; ", "memory": "13300" }
1,253
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p> <p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" /> <pre> <strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]] <strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]] <strong>Output:</strong> [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 100</code></li> <li><code>1 &lt;= mat[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {\n \n int n=mat.size();\n int m=mat[0].size();\n unordered_map<int,vector<int>>mp;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n mp[i-j].push_back(mat[i][j]);\n }\n }\n for(auto &vec:mp){\n sort(begin(vec.second),end(vec.second));\n }\n for(int i=n-1;i>=0;i--){\n for(int j=m-1;j>=0;j--){\n mat[i][j]=mp[i-j].back();\n mp[i-j].pop_back();\n }\n }\n return mat;\n }\n};", "memory": "13300" }
1,454
<p>You are given a string <code>s</code> consisting <strong>only</strong> of letters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>. In a single step you can remove one <strong>palindromic subsequence</strong> from <code>s</code>.</p> <p>Return <em>the <strong>minimum</strong> number of steps to make the given string empty</em>.</p> <p>A string is a <strong>subsequence</strong> of a given string if it is generated by deleting some characters of a given string without changing its order. Note that a subsequence does <strong>not</strong> necessarily need to be contiguous.</p> <p>A string is called <strong>palindrome</strong> if is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ababa&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> s is already a palindrome, so its entirety can be removed in a single step. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> &quot;<u>a</u>bb&quot; -&gt; &quot;<u>bb</u>&quot; -&gt; &quot;&quot;. Remove palindromic subsequence &quot;a&quot; then &quot;bb&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;baabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> &quot;<u>baa</u>b<u>b</u>&quot; -&gt; &quot;<u>b</u>&quot; -&gt; &quot;&quot;. Remove palindromic subsequence &quot;baab&quot; then &quot;b&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s[i]</code> is either <code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int removePalindromeSub(string s) {\n //only 2 letters so if palindrom then we can remove in 1 go if not then remove all occurence of a or b in one go then remove all the remaing in sencond go\n // so ans is 1 or 2\n int n=s.length();\n int l=0,r=n-1;\n while(l<=r){\n if(s[l]!=s[r]) return 2;\n l++;r--;\n }\n return 1;\n }\n};", "memory": "7400" }
1,454
<p>You are given a string <code>s</code> consisting <strong>only</strong> of letters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>. In a single step you can remove one <strong>palindromic subsequence</strong> from <code>s</code>.</p> <p>Return <em>the <strong>minimum</strong> number of steps to make the given string empty</em>.</p> <p>A string is a <strong>subsequence</strong> of a given string if it is generated by deleting some characters of a given string without changing its order. Note that a subsequence does <strong>not</strong> necessarily need to be contiguous.</p> <p>A string is called <strong>palindrome</strong> if is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ababa&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> s is already a palindrome, so its entirety can be removed in a single step. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> &quot;<u>a</u>bb&quot; -&gt; &quot;<u>bb</u>&quot; -&gt; &quot;&quot;. Remove palindromic subsequence &quot;a&quot; then &quot;bb&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;baabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> &quot;<u>baa</u>b<u>b</u>&quot; -&gt; &quot;<u>b</u>&quot; -&gt; &quot;&quot;. Remove palindromic subsequence &quot;baab&quot; then &quot;b&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s[i]</code> is either <code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int removePalindromeSub(string s) {\n int n = s.size();\n for(int i = 0; i < n / 2; ++i)\n if(s[i] != s[n - i - 1]) return 2;\n return 1;\n }\n};", "memory": "7500" }
1,454
<p>You are given a string <code>s</code> consisting <strong>only</strong> of letters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>. In a single step you can remove one <strong>palindromic subsequence</strong> from <code>s</code>.</p> <p>Return <em>the <strong>minimum</strong> number of steps to make the given string empty</em>.</p> <p>A string is a <strong>subsequence</strong> of a given string if it is generated by deleting some characters of a given string without changing its order. Note that a subsequence does <strong>not</strong> necessarily need to be contiguous.</p> <p>A string is called <strong>palindrome</strong> if is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ababa&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> s is already a palindrome, so its entirety can be removed in a single step. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> &quot;<u>a</u>bb&quot; -&gt; &quot;<u>bb</u>&quot; -&gt; &quot;&quot;. Remove palindromic subsequence &quot;a&quot; then &quot;bb&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;baabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> &quot;<u>baa</u>b<u>b</u>&quot; -&gt; &quot;<u>b</u>&quot; -&gt; &quot;&quot;. Remove palindromic subsequence &quot;baab&quot; then &quot;b&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s[i]</code> is either <code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int removePalindromeSub(string s) {\n bool isPalindrome = true;\n int left = 0;\n int right = s.size() - 1;\n while (left < right && isPalindrome) {\n if (s[left] != s[right]) {\n isPalindrome = false;\n } else {\n left++;\n right--;\n }\n }\n return (isPalindrome)? 1: 2;\n }\n};", "memory": "7500" }
1,454
<p>You are given a string <code>s</code> consisting <strong>only</strong> of letters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>. In a single step you can remove one <strong>palindromic subsequence</strong> from <code>s</code>.</p> <p>Return <em>the <strong>minimum</strong> number of steps to make the given string empty</em>.</p> <p>A string is a <strong>subsequence</strong> of a given string if it is generated by deleting some characters of a given string without changing its order. Note that a subsequence does <strong>not</strong> necessarily need to be contiguous.</p> <p>A string is called <strong>palindrome</strong> if is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ababa&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> s is already a palindrome, so its entirety can be removed in a single step. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> &quot;<u>a</u>bb&quot; -&gt; &quot;<u>bb</u>&quot; -&gt; &quot;&quot;. Remove palindromic subsequence &quot;a&quot; then &quot;bb&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;baabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> &quot;<u>baa</u>b<u>b</u>&quot; -&gt; &quot;<u>b</u>&quot; -&gt; &quot;&quot;. Remove palindromic subsequence &quot;baab&quot; then &quot;b&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s[i]</code> is either <code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int removePalindromeSub(string s) {\n int l = 0;\n int r = s.length()-1;\n\n while(l<=r){\n if(s[l] != s[r]){\n return 2;\n }\n l++;\n r--;\n }\n\n return 1;\n }\n};", "memory": "7600" }
1,454
<p>You are given a string <code>s</code> consisting <strong>only</strong> of letters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>. In a single step you can remove one <strong>palindromic subsequence</strong> from <code>s</code>.</p> <p>Return <em>the <strong>minimum</strong> number of steps to make the given string empty</em>.</p> <p>A string is a <strong>subsequence</strong> of a given string if it is generated by deleting some characters of a given string without changing its order. Note that a subsequence does <strong>not</strong> necessarily need to be contiguous.</p> <p>A string is called <strong>palindrome</strong> if is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ababa&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> s is already a palindrome, so its entirety can be removed in a single step. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> &quot;<u>a</u>bb&quot; -&gt; &quot;<u>bb</u>&quot; -&gt; &quot;&quot;. Remove palindromic subsequence &quot;a&quot; then &quot;bb&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;baabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> &quot;<u>baa</u>b<u>b</u>&quot; -&gt; &quot;<u>b</u>&quot; -&gt; &quot;&quot;. Remove palindromic subsequence &quot;baab&quot; then &quot;b&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s[i]</code> is either <code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int removePalindromeSub(string s) {\n if (s==string(s.rbegin(), s.rend())) return 1;\n else return 2;\n }\n};", "memory": "7700" }
1,454
<p>You are given a string <code>s</code> consisting <strong>only</strong> of letters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>. In a single step you can remove one <strong>palindromic subsequence</strong> from <code>s</code>.</p> <p>Return <em>the <strong>minimum</strong> number of steps to make the given string empty</em>.</p> <p>A string is a <strong>subsequence</strong> of a given string if it is generated by deleting some characters of a given string without changing its order. Note that a subsequence does <strong>not</strong> necessarily need to be contiguous.</p> <p>A string is called <strong>palindrome</strong> if is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ababa&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> s is already a palindrome, so its entirety can be removed in a single step. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> &quot;<u>a</u>bb&quot; -&gt; &quot;<u>bb</u>&quot; -&gt; &quot;&quot;. Remove palindromic subsequence &quot;a&quot; then &quot;bb&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;baabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> &quot;<u>baa</u>b<u>b</u>&quot; -&gt; &quot;<u>b</u>&quot; -&gt; &quot;&quot;. Remove palindromic subsequence &quot;baab&quot; then &quot;b&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s[i]</code> is either <code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool isPalindrome(string &s)\n {\n int n=s.size();\n for(int i=0;i<s.size();i++)\n {\n if(s[i]!=s[n-1-i]) return false;\n }\n return true;\n }\n int removePalindromeSub(string s) {\n if(s.size()==0) return 0;\n if(isPalindrome(s)) return 1;\n else return 2;\n }\n};", "memory": "7700" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
0
{ "code": "#include<bits/stdc++.h>\n#define M 10001\n#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\nvoid parse_input_and_solve(std::ofstream& out, std::string& a, std::string& s, std::string& b) {\n s[0] = ',';\n s.pop_back();\n int n = stoi(a);\n int thresh = stoi(b);\n\n vector<vector<int>> graph(n, vector<int>(n, M));\n for(int i=0; i<n; i++) {\n graph[i][i] = 0;\n }\n\n istringstream iss(s);\n string row;\n vector<int> it;\n while(getline(iss, row, ']')) {\n row.erase(row.begin());\n row.erase(row.begin());\n istringstream ss(row);\n string w;\n while(getline(ss, w, ',')) {\n it.push_back(stoi(w));\n }\n graph[it[0]][it[1]] = it[2];\n graph[it[1]][it[0]] = it[2];\n it.clear();\n }\n \n for(int k=0; k<n; k++) {\n for(int i=0; i<n; i++) {\n for(int j=0; j<n; j++) {\n graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);\n }\n }\n }\n\n int minReachCities = n;\n int res = -1;\n\n for(int i=0; i<n; i++) {\n int reachCities = 0;\n for(int j=0; j<n; j++) {\n if(graph[i][j] <= thresh) reachCities++;\n }\n if(reachCities <= minReachCities) {\n minReachCities = reachCities;\n res = i;\n }\n }\n\n out << res << endl;\n}\n\nbool Solve = []() {\n std::ofstream out(\"user.out\");\n std::string s, a, b;\n while (std::getline(std::cin, a) && std::getline(std::cin, s) && std::getline(std::cin, b)) {\n parse_input_and_solve(out, a, s, b);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\n\n\nclass Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int thresh) {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n\n vector<vector<int>> graph(n, vector<int>(n, M));\n for(int i=0; i<n; i++) {\n graph[i][i] = 0;\n }\n for(auto& it:edges) {\n graph[it[0]][it[1]] = it[2];\n graph[it[1]][it[0]] = it[2];\n }\n \n for(int k=0; k<n; k++) {\n for(int i=0; i<n; i++) {\n for(int j=0; j<n; j++) {\n graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);\n }\n }\n }\n\n int minReachCities = n;\n int res = -1;\n\n for(int i=0; i<n; i++) {\n int reachCities = 0;\n for(int j=0; j<n; j++) {\n if(graph[i][j] <= thresh) reachCities++;\n }\n if(reachCities <= minReachCities) {\n minReachCities = reachCities;\n res = i;\n }\n }\n\n // for(int i=0; i<n; i++) {\n // for(int j=0; j<n; j++) {\n // if(graph[i][j] == M) cout << \"inf\";\n // else cout << graph[i][j];\n // cout << \" \";\n // }\n // cout << endl;\n // }\n return res;\n }\n};", "memory": "9949" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
0
{ "code": "class Solution {\npublic:\n static int findTheCity(const uint8_t n, vector<vector<int>>& edges, const uint16_t dt) {\n constexpr uint8_t MAXN = 100;\n constexpr uint INF = 1'000'000'000;\n uint dist[n][n];\n // Floyd-Warshall's\n for (uint8_t i = 0; i < n; i++) {\n uint *const di = dist[i];\n for (uint8_t j = 0; j < n; j++)\n di[j] = -(i != j) & INF;\n }\n for (auto &e : edges) {\n const uint16_t w = e[2];\n const uint8_t i = e[0], j = e[1];\n dist[i][j] = dist[j][i] = w <= dt ? w : INF;\n }\n for (uint8_t k = 0; k < n; k++) {\n const uint *const dk = dist[k];\n for (uint8_t i = 0; i < n; i++) {\n uint *const di = dist[i];\n if (const uint dik = di[k]; dik < INF)\n for (uint8_t j = 0; j < n; j++)\n di[j] = min(di[j], dik + dk[j]);\n }\n }\n // Find the city with min neighbourds\n uint8_t m = n, z = 0;\n for (uint8_t i = 0; i < n; i++) {\n const uint8_t c = count_if(dist[i], dist[i+1u], [dt](const uint d) __attribute__((always_inline)) { return d <= dt; });\n if (c <= m) {\n m = c;\n z = i;\n }\n }\n return z;\n }\n};\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();", "memory": "9949" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int n, distanceThreshold;\n int dist[100][100];\n \n void FW(vector<vector<int>>& edges){\n fill(&dist[0][0], &dist[0][0]+100*100, 1e9);\n for (int i = 0; i < n; i++) \n dist[i][i] = 0;\n for (auto& e : edges){\n int u=e[0], v = e[1], w = e[2];\n if (w <= distanceThreshold) \n dist[u][v]=dist[v][u]=w;\n }\n \n for (int k = 0; k < n; k++)\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++) {\n dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j]);\n }\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n this->n = n;\n this->distanceThreshold = distanceThreshold;\n FW(edges);\n \n int min_cnt = n, city = -1;\n for (int i = 0; i <n ; i++){\n int cnt = -1; \n for (int j = 0; j < n; j++){\n if (dist[i][j] <= distanceThreshold) \n cnt++;\n }\n if (cnt <=min_cnt ){\n min_cnt=cnt;\n city=i;\n }\n }\n return city;\n }\n};\n\n\n\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}(); ", "memory": "10248" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int n, distanceThreshold;\n int dist[100][100];\n \n void FW(vector<vector<int>>& edges){\n fill(&dist[0][0], &dist[0][0]+100*100, 1e9);\n for (int i = 0; i < n; i++) \n dist[i][i] = 0;\n for (auto& e : edges){\n int u=e[0], v = e[1], w = e[2];\n if (w <= distanceThreshold) //drop large weights\n dist[u][v]=dist[v][u]=w;\n }\n // Main loop\n for (int k = 0; k < n; k++)\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++) {\n dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j]);\n }\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n this->n = n;\n this->distanceThreshold = distanceThreshold;\n FW(edges);\n \n int min_cnt = n, city = -1;\n for (int i = 0; i <n ; i++){\n int cnt = -1; // i itself doesn't count\n for (int j = 0; j < n; j++){\n if (dist[i][j] <= distanceThreshold) \n cnt++;\n }\n if (cnt <=min_cnt ){\n min_cnt=cnt;\n city=i;\n }\n }\n return city;\n }\n};\n\n\n\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}(); ", "memory": "10546" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int n, distanceThreshold;\n int dist[100][100];\n \n void FW(vector<vector<int>>& edges){\n fill(&dist[0][0], &dist[0][0]+100*100, 1e9);\n for (int i = 0; i < n; i++) \n dist[i][i] = 0;\n for (auto& e : edges){\n int u=e[0], v = e[1], w = e[2];\n if (w <= distanceThreshold) //drop large weights\n dist[u][v]=dist[v][u]=w;\n }\n // Main loop\n for (int k = 0; k < n; k++)\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++) {\n dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j]);\n }\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n this->n = n;\n this->distanceThreshold = distanceThreshold;\n FW(edges);\n \n int min_cnt = n, city = -1;\n for (int i = 0; i <n ; i++){\n int cnt = -1; // i itself doesn't count\n for (int j = 0; j < n; j++){\n if (dist[i][j] <= distanceThreshold) \n cnt++;\n }\n if (cnt <=min_cnt ){\n min_cnt=cnt;\n city=i;\n }\n }\n return city;\n }\n};\n\n\n\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}(); ", "memory": "10845" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int n, distanceThreshold;\n int dist[100][100];\n \n void FW(vector<vector<int>>& edges){\n fill(&dist[0][0], &dist[0][0]+100*100, 1e9);\n for (int i = 0; i < n; i++) \n dist[i][i] = 0;\n for (auto& e : edges){\n int u=e[0], v = e[1], w = e[2];\n if (w <= distanceThreshold) //drop large weights\n dist[u][v]=dist[v][u]=w;\n }\n // Main loop\n for (int k = 0; k < n; k++)\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++) {\n dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j]);\n }\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n this->n = n;\n this->distanceThreshold = distanceThreshold;\n FW(edges);\n \n int min_cnt = n, city = -1;\n for (int i = 0; i <n ; i++){\n int cnt = -1; // i itself doesn't count\n for (int j = 0; j < n; j++){\n if (dist[i][j] <= distanceThreshold) \n cnt++;\n }\n if (cnt <=min_cnt ){\n min_cnt=cnt;\n city=i;\n }\n }\n return city;\n }\n};\n\n\n\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}(); ", "memory": "10845" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int n, distanceThreshold;\n int dist[100][100];\n \n void FW(vector<vector<int>>& edges){\n fill(&dist[0][0], &dist[0][0]+100*100, 1e9);\n for (int i = 0; i < n; i++) \n dist[i][i] = 0;\n for (auto& e : edges){\n int u=e[0], v = e[1], w = e[2];\n if (w <= distanceThreshold) //drop large weights\n dist[u][v]=dist[v][u]=w;\n }\n // Main loop\n for (int k = 0; k < n; k++)\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++) {\n dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j]);\n }\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n this->n = n;\n this->distanceThreshold = distanceThreshold;\n FW(edges);\n \n int min_cnt = n, city = -1;\n for (int i = 0; i <n ; i++){\n int cnt = -1; // i itself doesn't count\n for (int j = 0; j < n; j++){\n if (dist[i][j] <= distanceThreshold) \n cnt++;\n }\n if (cnt <=min_cnt ){\n min_cnt=cnt;\n city=i;\n }\n }\n return city;\n }\n};\n\n\n\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}(); ", "memory": "11144" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n int dist[n][n];\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n dist[i][j] = i == j ? 0 : 10001;\n }\n }\n for (auto &e : edges) {\n dist[e[0]][e[1]] = dist[e[1]][e[0]] = e[2];\n }\n for (int k = 0; k < n; k++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);\n }\n }\n }\n int res = -1, resCount = INT_MAX;\n for (int i = 0; i < n; i++) {\n int count = 0;\n for (int j = 0; j < n; j++) {\n if (dist[i][j] <= distanceThreshold) {\n count++;\n }\n }\n if (count <= resCount) {\n resCount = count;\n res = i;\n }\n }\n return res;\n }\n};", "memory": "11144" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int n, distanceThreshold;\n int dist[100][100];\n \n void FW(vector<vector<int>>& edges){\n fill(&dist[0][0], &dist[0][0]+100*100, 1e9);\n for (int i = 0; i < n; i++) \n dist[i][i] = 0;\n for (auto& e : edges){\n int u=e[0], v = e[1], w = e[2];\n if (w <= distanceThreshold) //drop large weights\n dist[u][v]=dist[v][u]=w;\n }\n // Main loop\n for (int k = 0; k < n; k++)\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++) {\n dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j]);\n }\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n this->n = n;\n this->distanceThreshold = distanceThreshold;\n FW(edges);\n \n int min_cnt = n, city = -1;\n for (int i = 0; i <n ; i++){\n int cnt = -1; // i itself doesn't count\n for (int j = 0; j < n; j++){\n if (dist[i][j] <= distanceThreshold) \n cnt++;\n }\n if (cnt <=min_cnt ){\n min_cnt=cnt;\n city=i;\n }\n }\n return city;\n }\n};\n\n\n\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}(); ", "memory": "11443" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<int>> dp(n, vector<int>(n, 1e8));\n\n for(auto &edge : edges){\n int a = edge[0];\n int b = edge[1];\n int wt = edge[2];\n\n dp[a][b] = wt;\n dp[b][a] = wt;\n }\n\n for(int i = 0; i < n; i++) dp[i][i] = 0;\n\n for(int k = 0; k < n; k++){\n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);\n }\n }\n }\n\n int Max = INT_MAX, ans = 0;\n for(int i = 0; i < n; i++){\n int count = 0;\n for(int j = 0; j < n; j++){\n if(dp[i][j] <= distanceThreshold){\n count++;\n }\n }\n \n if(count <= Max){\n Max = count;\n ans = i;\n }\n }\n return ans;\n }\n};", "memory": "11741" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int dt) \n {\n int inf = 1e9;\n vector<vector<int>> g(n, vector<int>(n,inf)); \n for(int i=0; i<edges.size(); i++)\n {\n g[edges[i][0]][edges[i][1]] = edges[i][2];\n g[edges[i][1]][edges[i][0]] = edges[i][2];\n }\n for(int i=0; i<n; i++)\n {\n g[i][i] = 0;\n }\n for(int k=0; k<n; k++)\n {\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n if(g[i][k]+g[k][j] < g[i][j])\n {\n g[i][j] = g[i][k] + g[k][j];\n }\n }\n }\n }\n int minm = 0;\n for(int j=0; j<n; j++)\n {\n if(g[n-1][j] <= dt)\n {\n minm++;\n }\n }\n int mini = n-1;\n for(int i=n-2; i>=0; i--)\n {\n int c=0;\n for(int j=0; j<n; j++)\n {\n if(g[i][j] <= dt)\n {\n c++;\n }\n }\n if(c < minm)\n {\n minm = c;\n mini = i;\n }\n }\n return mini;\n }\n};", "memory": "12040" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<int>res(n,0);\n vector<vector<int>>mat(n,vector<int>(n,10001));\n\n for(auto& edge:edges)\n {\n int u = edge[0];\n int v = edge[1];\n int wt = edge[2];\n\n mat[u][v] = wt;\n mat[v][u] = wt;\n }\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<n;j++)\n {\n if(i==j) mat[i][j] = 0;\n }\n }\n\n for(int via=0;via<n;via++)\n {\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<n;j++)\n {\n mat[i][j] = min(mat[i][j],mat[i][via]+mat[via][j]);\n }\n }\n }\n\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<n;j++)\n {\n if( i!=j && mat[i][j]<=distanceThreshold )\n {\n res[i]++;\n }\n }\n }\n\n int city=INT_MAX,k=-1;\n for(int i=0;i<n;i++)\n {\n if(res[i]<=city) \n {\n city = res[i];\n k=i;\n }\n }\n\n return k;\n }\n};", "memory": "12339" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<int>> dis(n, vector<int>(n, 1e7));\n for(int i = 0; i < n; i++)\n dis[i][i] = 0;\n for(vector<int> &e: edges)\n {\n dis[e[0]][e[1]] = e[2];\n dis[e[1]][e[0]] = e[2];\n }\n for(int k = 0; k < n; k++)\n {\n for(int i = 0; i < n; i++)\n {\n for(int j = 0; j < n; j++)\n {\n dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);\n }\n }\n }\n int mn = n;\n int ans = n;\n for(int i = 0; i < n; i++)\n {\n int reach = 0;\n for(int j = 0; j < n; j++)\n {\n if(i == j)\n continue;\n if(dis[i][j] <= distanceThreshold)\n reach++;\n }\n if(reach == mn)\n ans = i;\n if(reach < mn)\n {\n mn = reach;\n ans = i;\n }\n }\n return ans;\n }\n};", "memory": "12638" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int k) {\n vector<vector<int>>arr(n,vector<int>(n,INT_MAX));\n for(int i=0;i<edges.size();i++)\n {\n int node1=edges[i][0];\n int node2=edges[i][1];\n int wt=edges[i][2];\n arr[node1][node2]=wt;\n arr[node2][node1]=wt;\n }\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<n;j++)\n {\n if(i==j)\n {\n arr[i][j]=0;\n }\n }\n }\n for(int k=0;k<n;k++)\n {\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<n;j++)\n {\n if(arr[i][k]!=INT_MAX && arr[k][j]!=INT_MAX)\n arr[i][j]=min(arr[i][j],arr[i][k]+arr[k][j]);\n }\n }\n }\n int ans=INT_MAX;\n int index;\n for(int i=n-1;i>=0;i--)\n { int c=0;\n for(int j=0;j<n;j++)\n {\n if(arr[i][j]<=k && arr[i][j]!=INT_MAX)\n {\n c++;\n }\n }\n if(c<ans)\n {\n ans=c;\n index=i;\n }\n }\n return index;\n\n }\n};", "memory": "12638" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int dt) {\n vector<vector<int>> mat(n,vector<int>(n,1e9));\n for(int i=0;i<edges.size();i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n int wt = edges[i][2];\n\n mat[u][v]=wt;\n mat[v][u]=wt;\n }\n for(int i=0;i<n;i++)\n mat[i][i]=0;\n\n for(int k=0;k<n;k++)\n {\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<n;j++)\n {\n mat[i][j]=min(mat[i][j],mat[i][k]+mat[k][j]);\n }\n }\n }\n int city=0;\n int maxi=1e9;\n\n for(int i=0;i<n;i++)\n {\n int c=0;\n for(int j=0;j<n;j++)\n {\n if(mat[i][j]<=dt)\n c++;\n }\n if(c<=maxi)\n {\n city=i;\n maxi=c;\n }\n }\n return city;\n }\n};", "memory": "12936" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<int>> matrix( n , vector<int> ( n , INT_MAX));\n for(int i = 0 ; i < edges.size() ; i++){\n int u = edges[i][0];\n int v = edges[i][1];\n int w = edges[i][2];\n\n matrix[u][v] = w;\n matrix[v][u] = w;\n }\n for(int i = 0 ; i < n ; i++){\n matrix[i][i] = 0;\n }\n\n for(int k = 0 ; k < n ; k++){\n for(int i = 0 ; i < n ; i++){\n for(int j = 0 ; j < n ; j++){\n if(matrix[i][k] != INT_MAX && matrix[k][j] != INT_MAX){\n matrix[i][j] = min(matrix[i][j] , matrix[i][k] + matrix[k][j]);\n }\n }\n }\n }\n int minCount = INT_MAX; \n int currCity = -1;\n for(int i = 0 ; i < n ; i++){\n int currCount = 0 ;\n for(int j = 0 ; j < n ; j++){\n if( i != j && matrix[i][j] <= distanceThreshold ){\n currCount++;\n }\n }\n if( currCount <= minCount ){\n currCity = i ;\n minCount = currCount ;\n }\n \n }\n return currCity ;\n }\n};", "memory": "12936" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\tint findTheCity(int n, vector<vector<int>>& edges,\n\t int distanceThreshold) {\n\t\tvector<vector<int>> dist(n, vector<int> (n, INT_MAX));\n\t\tfor (auto it : edges) {\n\t\t\tdist[it[0]][it[1]] = it[2];\n\t\t\tdist[it[1]][it[0]] = it[2];\n\t\t}\n\t\tfor (int i = 0; i < n; i++) dist[i][i] = 0;\n\t\tfor (int k = 0; k < n; k++) {\n\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\t\tif (dist[i][k] == INT_MAX || dist[k][j] == INT_MAX)\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\tdist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tint cntCity = n;\n\t\tint cityNo = -1;\n\t\tfor (int city = 0; city < n; city++) {\n\t\t\tint cnt = 0;\n\t\t\tfor (int adjCity = 0; adjCity < n; adjCity++) {\n\t\t\t\tif (dist[city][adjCity] <= distanceThreshold)\n\t\t\t\t\tcnt++;\n\t\t\t}\n\n\t\t\tif (cnt <= cntCity) {\n\t\t\t\tcntCity = cnt;\n\t\t\t\tcityNo = city;\n\t\t\t}\n\t\t}\n\t\treturn cityNo;\n\n\t}\n};", "memory": "14430" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<int>> matrix(n,vector<int>(n,1e9));\n for(auto it : edges){\n matrix[it[0]][it[1]] = it[2];\n matrix[it[1]][it[0]] = it[2];\n }\n for(int i = 0;i<n;i++){\n matrix[i][i] = 0;\n }\n for(int k = 0;k<n;k++){\n for(int i = 0;i<n;i++){\n for(int j = 0;j<n;j++){\n matrix[i][j] = min(matrix[i][j],matrix[i][k]+matrix[k][j]);\n }\n }\n }\n int mn = n+1;\n int ans = -1;\n for(int i = 0;i<n;i++){\n int cnt = 0;\n for(int j = 0;j<n;j++){\n if(matrix[i][j]<=distanceThreshold){\n cnt++;\n }\n }\n if(cnt<mn){\n mn = cnt;\n ans = i;\n }\n else if(cnt==mn){\n mn = min(mn,cnt);\n ans = max(ans,i);\n }\n }\n return ans;\n }\n};", "memory": "14430" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<int>>mat(n,vector<int>(n,1e9));\n\n for(auto it:edges){\n int u = it[0];\n int v = it[1];\n int wt = it[2];\n\n mat[u][v] = wt;\n mat[v][u] = wt;\n }\n\n\n for(int k=0;k<n;k++){\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n\n mat[i][j] = min(mat[i][j],mat[i][k] + mat[k][j]);\n }\n }\n }\n\n int node = -1;\n int mnumber = INT_MAX;\n\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n cout<<mat[i][j]<<\" \";\n }\n cout<<endl;\n }\n for(int i=0;i<n;i++){\n int number = 0;\n for(int j=0;j<n;j++){\n if(i==j)continue;\n if(mat[i][j]<=distanceThreshold){\n number++;\n }\n }\n // cout<<number<<\" \"<<i<<endl;\n if(number <= mnumber){\n mnumber = number;\n node = i;\n }\n }\n\n return node;\n }\n};", "memory": "14729" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "static auto _ = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return nullptr;\n}();\nclass Solution {\npublic:\n vector<vector<int>> adjMatrix(vector<vector<int>>& edges, int n) {\n vector<vector<int>> adj(n, vector<int>(n, 1e9));\n\n for (auto node : edges) {\n int u = node[0];\n int v = node[1];\n int w = node[2];\n\n adj[u][v] = w;\n adj[v][u] = w;\n }\n\n return adj;\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<int>> adj = adjMatrix(edges, n);\n\n for(int i = 0 ; i < n; i++){\n for(int j = 0 ; j < n ; j++) {\n if(i==j) adj[i][j] = 0;\n }\n }\n\n for (int k = 0; k < n; k++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n adj[i][j] = min(adj[i][j], (adj[i][k] + adj[k][j]));\n }\n }\n }\n\n vector<int> sol(n ,0);\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if(adj[i][j] <= distanceThreshold && i!= j) {\n sol[i]++;\n }\n cout << adj[i][j] << \" \";\n }\n cout << endl;\n }\n\n int min = INT_MAX;\n int ind = INT_MIN;\n \n for(int i = 0 ; i < n ; i++){\n cout << sol[i] << \" \";\n if(sol[i] <= min){\n min = sol[i];\n ind = i;\n }\n }\n\n return ind;\n\n }\n};", "memory": "14729" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n\n vector<vector<int>>mat(n,vector<int>(n,1e9));\n\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(i == j){\n mat[i][j]=0;\n }\n }\n }\n\n for(auto it:edges){\n int u=it[0];\n int v=it[1];\n int wt=it[2];\n mat[u][v]=wt;\n mat[v][u]=wt;\n }\n\n for(int k=0;k<n;k++){\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n mat[i][j]=min(mat[i][j],mat[i][k]+mat[k][j]);\n }\n }\n }\n for(auto it:mat){\n for(auto i:it){\n cout<<i<<\" \";\n }\n cout<<endl;\n }\n int ans=-1;\n int cnt=INT_MAX;\n for(int i=0;i<n;i++){\n int c=0;\n for(int j=0;j<n;j++){\n if(mat[i][j] <= distanceThreshold){\n c++;\n }\n }\n if(c<=cnt){\n cnt=c;\n ans=i;\n }\n }\n return ans;\n\n }\n};", "memory": "15028" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n // floyd warshall\n\n vector<vector<int>> dp(n, vector<int>(n, INT_MAX));\n vector<vector<int>> next(n, vector<int>(n, -1));\n\n for(vector<int> vc:edges) {\n int n1 = vc[0], n2 = vc[1], w = vc[2];\n dp[n1][n2] = w;\n dp[n2][n1] = w;\n next[n1][n2] = n2;\n next[n2][n1] = n1;\n }\n\n // base case\n for(int i=0; i<n; i++) {\n dp[i][i] = 0;\n next[i][i] = i;\n }\n for(int k=0; k<n; k++) {\n for(int i=0; i<n; i++) {\n for(int j=0; j<n; j++) {\n\n if(dp[i][k] != INT_MAX and dp[k][j] != INT_MAX and dp[i][j] > dp[i][k] + dp[k][j]) {\n dp[i][j] = dp[i][k] + dp[k][j];\n next[i][j] = next[i][k];\n }\n }\n }\n }\n\n int mn = INT_MAX;\n int ans;\n\n for(int i=0; i<n; i++) {\n int cnt = 0;\n\n for(int j=0; j<n; j++) {\n if(dp[i][j] <= distanceThreshold) {\n cnt++;\n }\n }\n if(cnt <= mn) {\n mn = cnt;\n ans = i;\n }\n }\n\n return ans;\n }\n};", "memory": "15326" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> floydWarshell(vector<vector<int>>& matrix, int n) {\n vector<vector<int>> cost(n, vector<int>(n));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (i == j) cost[i][j] = 0;\n else if (matrix[i][j] == 0) cost[i][j] = 1e9;\n else cost[i][j] = matrix[i][j];\n }\n }\n for (int k = 0; k < n; k++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);\n }\n }\n }\n return cost;\n }\n\n int solution(vector<vector<int>>& matrix, int n, int distanceThreshold) {\n vector<vector<int>> cost = floydWarshell(matrix, n);\n int ans = 0;\n int mn_cities = n - 1;\n for (int i = 0; i < n; i++) {\n int temp = 0;\n for (int j = 0; j < n; j++) {\n if (i == j) continue;\n if (cost[i][j] <= distanceThreshold) temp++;\n }\n if (temp <= mn_cities) {\n mn_cities = temp;\n ans = i;\n }\n }\n return ans;\n }\n \n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<int>> matrix(n, vector<int>(n));\n for (auto it : edges) {\n matrix[it[0]][it[1]] = it[2];\n matrix[it[1]][it[0]] = it[2];\n }\n return solution(matrix, n, distanceThreshold);\n }\n};", "memory": "15326" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n // undirected edges\n // floyd washall\n vector<vector<int>> matrix(n, vector<int>(n, INT_MAX));\n for (int i = 0; i < n; i++) {\n matrix[i][i] = 0;\n }\n for (auto edge : edges) {\n matrix[edge[0]][edge[1]] = edge[2];\n matrix[edge[1]][edge[0]] = edge[2];\n }\n\n vector<vector<int>> dis = matrix;\n for (int k = 0; k < n; k++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (dis[i][k] != INT_MAX && dis[k][j] != INT_MAX)\n dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);\n }\n }\n }\n\n int minCity = 0;\n int minCities = INT_MAX;\n // if the can reach to any city i to city j from the shortest path the\n // we will have some path to reach within the threshold Otherwiaw there\n // is no way to reach from a city i to city j within the threshold\n for (int i = 0; i < n; i++) {\n int cnt = 0;\n for (int j = 0; j < n; j++) {\n if (dis[i][j] <= distanceThreshold)\n cnt++;\n }\n if (cnt <= minCities) {\n minCities = cnt;\n minCity = i;\n }\n }\n\n return minCity;\n }\n};", "memory": "15625" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int n, distanceThreshold;\n int dist[100][100];\n\n void FW(vector<vector<int>> edges) {\n fill(&dist[0][0], &dist[0][0] + 100 * 100, 1e9);\n for (int i = 0; i < n; i++) dist[i][i] = 0;\n for (auto e : edges) {\n int u = e[0], v = e[1], w = e[2];\n if (w <= distanceThreshold) dist[u][v] = dist[v][u] = w;\n }\n\n for (int k = 0; k < n; k++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);\n }\n }\n }\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n this -> n = n;\n this -> distanceThreshold = distanceThreshold;\n FW(edges);\n\n int min_cnt = n, city = -1;\n for (int i = 0; i < n; i++) {\n int cnt = -1;\n for (int j = 0; j < n; j++) {\n if (dist[i][j] <= distanceThreshold) cnt++;\n }\n if (cnt <= min_cnt) {\n min_cnt = cnt;\n city = i;\n }\n }\n return city;\n }\n};", "memory": "15924" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<int>> cost(n,vector<int>(n,1e9));\n for(auto it: edges){\n int from = it[0];\n int to = it[1];\n int wt = it[2];\n cost[from][to]=wt;\n cost[to][from]=wt;\n }\n for(int i=0;i<n;i++){\n cost[i][i]=0;\n }\n vector<vector<int>> ans(n);\n\n for(int k=0;k<n;k++){\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n cost[i][j] = min(cost[i][j], (cost[i][k]+cost[k][j]));\n }\n }\n }\n\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if( i!=j && cost[i][j]<=distanceThreshold){\n ans[i].push_back(j);\n }\n }\n }\n\n pair<int,int> p = {1e9,-1};\n for(int i=0;i<n;i++){\n if(p.first>=ans[i].size()){\n p.first = ans[i].size();\n p.second = i;\n }\n }\n\n return p.second;\n }\n};", "memory": "16223" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<int>> distVec(n, vector<int>(n, 1e9));\n for(int i=0; i<n; ++i){\n for(int j=0; j< n; ++j){\n if(i==j)distVec[i][j]=0;\n }\n }\n\n vector<vector<pair<int, int>>> adj(n, vector<pair<int, int>>());\n\n for(int i=0; i<edges.size(); ++i ){\n int u = edges[i][0];\n int v = edges[i][1];\n int wt = edges[i][2];\n adj[u].push_back({v, wt});\n adj[v].push_back({u, wt});\n }\n\n\n priority_queue< pair<int, int>, vector< pair<int, int>>, greater< pair<int, int>>> pq;\n for(int i=0; i<n; ++i){\n pq.push({0,i});\n while(!pq.empty()){\n int node = pq.top().second;\n int dis = pq.top().first;\n pq.pop();\n for(auto nodeVec: adj[node]){\n if(dis + nodeVec.second < distVec[i][nodeVec.first]){\n distVec[i][nodeVec.first] = dis + nodeVec.second;\n pq.push({distVec[i][nodeVec.first], nodeVec.first});\n }\n } \n }\n }\n\n vector<int> cities(n, 0);\n for(int i=0; i< n; ++i){\n for(int j=0; j< n; ++j){\n if(i!=j){\n if(distVec[i][j]<=distanceThreshold){\n cities[i]++;\n }\n }\n }\n }\n\n int min = INT_MAX;\n int city = -1;\n for(int i=0; i<n; ++i){\n if(cities[i]<=min){\n city = i;\n min = cities[i];\n\n }\n }\n return city;\n }\n};", "memory": "16521" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int,int>>>adjLs(n);\n for(int i=0;i<edges.size();i++)\n {\n adjLs[edges[i][0]].push_back({edges[i][1],edges[i][2]});\n adjLs[edges[i][1]].push_back({edges[i][0],edges[i][2]});\n }\n int cityno=-1,mini=INT_MAX;\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n\n for(int i=0;i<n;i++)\n {\n vector<int>ans(n,INT_MAX);\n pq.push({0,i});\n ans[i]=0;\n while(!pq.empty())\n {\n int node=pq.top().second;\n int wt=pq.top().first;\n pq.pop();\n for(auto it:adjLs[node])\n {\n int adjNode=it.first;\n int adjWeight=it.second;\n if(adjWeight+wt<ans[adjNode])\n {\n ans[adjNode]=adjWeight+wt;\n pq.push({ans[adjNode],adjNode});\n }\n }\n }\n int count=0;\n for(int j=0;j<n;j++)\n {\n if(ans[j]<=distanceThreshold)\n {\n count++;\n }\n }\n if(count<=mini)\n {\n mini=count;\n cityno=i;\n }\n }\n return cityno;\n }\n};", "memory": "16820" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int d) {\n vector<vector<pair<int,int>>> adj(n);\n vector<vector<int>> dist(n, vector<int> (n, 1e9));\n for(int i=0;i<n;i++) dist[i][i] = 0;\n for(auto e:edges){\n adj[e[0]].push_back({e[0], e[2]});\n adj[e[1]].push_back({e[0], e[2]});\n if(dist[e[0]][e[1]]>e[2]) dist[e[0]][e[1]] = e[2];\n if(dist[e[1]][e[0]]>e[2]) dist[e[1]][e[0]] = e[2];\n }\n for(int k=0;k<n;k++){\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]);\n }\n }\n }\n int mincity = -1, mincount = n;\n for(int i=0;i<n;i++){\n int curcount = 0;\n for(int j=0;j<n;j++){\n if(i==j) continue;\n if(dist[i][j]<=d) curcount++;\n }\n if(mincount>=curcount){\n mincount = curcount;\n mincity = i;\n }\n }\n return mincity;\n }\n};", "memory": "16820" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int,int>>>adj(n);\n vector<vector<int>>dist(n,vector<int>(n,1e9));\n for(auto it:edges){\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n dist[it[0]][it[1]]=it[2];\n dist[it[1]][it[0]]=it[2];\n }\n for(int i=0;i<n;i++){\n dist[i][i]=0;\n }\n for(int i=0;i<n;i++){\n for(int a=0;a<n;a++){\n for(int b=0;b<n;b++){\n if(dist[a][b]>dist[a][i]+dist[i][b]){\n dist[a][b]=dist[a][i]+dist[i][b];\n }\n }\n }\n }\n vector<int>citycnt(n,0);\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(i==j) continue;\n if(dist[i][j]<=distanceThreshold) citycnt[i]++;\n }\n }\n int mincitycnt=1e9,mincity=0;\n for(int i=0;i<n;i++){\n if(mincitycnt>=citycnt[i]){\n mincitycnt=citycnt[i];\n mincity=i;\n }\n }\n return mincity;\n }\n};", "memory": "17119" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n vector<pair<int,int>>adj[n];\n int cntcity=n;\n int cityno=-1;\n for(auto it:edges)\n {\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n }\n for(int i=0;i<n;i++)\n {\n pq.push({0,i});\n vector<int>dist(n,INT_MAX);\n dist[i]=0;\n while(!pq.empty())\n {\n int dis=pq.top().first;\n int node=pq.top().second;\n pq.pop();\n for(auto it:adj[node])\n {\n if(it.second+dis<dist[it.first])\n {\n dist[it.first]=it.second+dis;\n pq.push({it.second+dis,it.first});\n }\n }\n }\n int count=0;\n for(int j=0;j<n;j++)\n {\n if(dist[j]<=distanceThreshold)\n {\n count++;\n }\n }\n \n if(cntcity>=count)\n {\n cntcity=count;\n cityno=i;\n }\n }\n return cityno;\n }\n};", "memory": "17119" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n\n unordered_map<int,vector<pair<int,int>>> adj;\n\n for(auto it : edges) {\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n } \n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n\n int mini=n,city=-1;\n\n for(int i=0; i<n; i++) {\n\n vector<int> har_city_ka_dis(n,1e9);\n har_city_ka_dis[i] = 0;\n pq.push({0,i});\n\n while(!pq.empty()) {\n int distance = pq.top().first;\n int node = pq.top().second;\n pq.pop();\n\n for(auto it : adj[node]) {\n int adjNode = it.first;\n int edgedistance = it.second;\n\n if(distance + edgedistance < har_city_ka_dis[adjNode]) {\n har_city_ka_dis[adjNode] = distance+edgedistance;\n pq.push({har_city_ka_dis[adjNode],adjNode});\n }\n }\n }\n\n int count = 0;\n\n int cnt=0;\n for(int j=0; j<n; j++){\n if(har_city_ka_dis[j]<=distanceThreshold){\n cnt++;\n }\n }\n if(cnt<=mini){\n mini=cnt;\n city=i;\n }\n }\n\n return city;\n }\n};", "memory": "17418" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<pair<int, int>> adj[n];\n\n for(auto it : edges) {\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n \n int cityNo = -1;\n int minCityCnt = n;\n\n for(int i = 0; i < n; i++) {\n vector<int> dist(n, 1e9);\n pq.push({0, i});\n dist[i] = 0;\n\n while(!pq.empty()) {\n auto temp = pq.top();\n pq.pop();\n\n int dis = temp.first;\n int node = temp.second;\n\n for(auto it : adj[node]) {\n int adjNode = it.first;\n int adjW = it.second;\n\n if(dis + adjW < dist[adjNode]) {\n dist[adjNode] = dis + adjW;\n pq.push({dis + adjW, adjNode});\n }\n }\n }\n\n int cnt = 0;\n for(int j = 0; j < n; j++) {\n if(dist[j] <= distanceThreshold) {\n cnt++;\n }\n }\n if(cnt <= minCityCnt) {\n minCityCnt = cnt;\n cityNo = i;\n }\n }\n return cityNo;\n }\n};", "memory": "17418" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<pair<int,int>> adj[n];\n for(auto ele:edges){\n adj[ele[0]].push_back({ele[1],ele[2]});\n adj[ele[1]].push_back({ele[0],ele[2]});\n }\n vector<vector<int>> dis;\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pt;\n for(int i=0;i<n;i++){\n \n vector<int> vis(n,INT_MAX);\n vis[i]=0;\n pt.push({0,i});\n while(!pt.empty()){\n int d=pt.top().first;\n int node=pt.top().second;\n pt.pop();\n for(auto ele:adj[node]){\n int u=ele.first;\n int wt=ele.second;\n if(d+wt<vis[u]){\n vis[u]=d+wt;\n pt.push({d+wt,u});\n }\n }\n }\n dis.push_back(vis);\n\n\n }\n int count=n;\n int city=-1;\n for(int i=0;i<n;i++){\n int c=0;\n for(int j=0;j<n;j++){\n if(dis[i][j]<=distanceThreshold){\n c++;\n }\n }\n if(c<=count){\n count=c;\n city=i;\n \n\n }\n }\n return city;\n }\n};", "memory": "17716" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n struct Edge {\n int to;\n int weight;\n };\n \n int findNumReachableCities(const std::vector<std::vector<Edge>>& vertices, int distanceThreshold, int src) {\n // initialize an array with the distances to each city\n const size_t V = vertices.size();\n std::vector<int> distances(V, std::numeric_limits<int>::max());\n distances[src] = 0;\n\n // use a priority queue for the order that we should visit nodes\n // <distance_to_node, index>\n std::priority_queue<std::pair<int, int>> priq;\n priq.push({ distances[src], src });\n\n std::vector<bool> visited(V, false);\n\n // run Djikstra's algorithm\n int num_cities = 0;\n while (!priq.empty()) {\n int distance_to_node, index;\n std::tie(distance_to_node, index) = priq.top();\n priq.pop();\n \n if (distances[index] < distance_to_node) {\n // stale value from the priority queue\n continue;\n }\n\n // update neighboring edges\n for (const Edge& e : vertices[index]) {\n int dist = distances[index] + e.weight;\n if (dist < distances[e.to] && dist <= distanceThreshold) {\n if (distances[e.to] == std::numeric_limits<int>::max()) {\n ++num_cities;\n }\n\n distances[e.to] = dist;\n priq.push({ distances[e.to], e.to });\n }\n }\n }\n\n return num_cities;\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n // create adjacency list\n std::vector<std::vector<Edge>> vertices(n);\n for (const std::vector<int>& e : edges) {\n vertices[e[0]].push_back(Edge { .to = e[1], .weight = e[2] });\n vertices[e[1]].push_back(Edge { .to = e[0], .weight = e[2] });\n }\n\n struct Result {\n int num_cities;\n int index;\n };\n\n // (1) For any given city i, what is the number of cities that are reachable\n // through some path with a distance of distanceThreshold?\n // O(V * (V log V + E log V))\n // std::pair<num_cities, index>\n std::vector<Result> results(n);\n for (int i = 0; i < n; ++i) {\n results[i] = { findNumReachableCities(vertices, distanceThreshold, i), i };\n }\n\n // (2) Find the city with the smallest number of reachable cities\n // O(V log V)\n std::sort(results.begin(), results.end(), [](const Result& r1, const Result& r2) {\n if (r1.num_cities < r2.num_cities) {\n return true;\n }\n if (r1.num_cities > r2.num_cities) {\n return false;\n }\n return r1.index > r2.index;\n });\n\n return results.front().index;\n }\n};", "memory": "17716" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n struct Edge {\n int to;\n int weight;\n };\n \n int findNumReachableCities(const std::vector<std::vector<Edge>>& vertices, int distanceThreshold, int src) {\n // initialize an array with the distances to each city\n const size_t V = vertices.size();\n std::vector<int> distances(V, std::numeric_limits<int>::max());\n distances[src] = 0;\n\n // use a priority queue for the order that we should visit nodes\n // <distance_to_node, index>\n std::priority_queue<std::pair<int, int>> priq;\n priq.push({ distances[src], src });\n\n // run Djikstra's algorithm\n int num_cities = 0;\n while (!priq.empty()) {\n int distance_to_node, index;\n std::tie(distance_to_node, index) = priq.top();\n priq.pop();\n\n if (distances[index] < distance_to_node) {\n // stale value from the priority queue\n continue;\n }\n\n // update neighboring edges\n for (const Edge& e : vertices[index]) {\n int dist = distances[index] + e.weight;\n if (dist < distances[e.to] && dist <= distanceThreshold) {\n if (distances[e.to] == std::numeric_limits<int>::max()) {\n ++num_cities;\n }\n\n distances[e.to] = dist;\n priq.push({ distances[e.to], e.to });\n }\n }\n }\n\n return num_cities;\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n // create adjacency list\n std::vector<std::vector<Edge>> vertices(n);\n for (const std::vector<int>& e : edges) {\n vertices[e[0]].push_back(Edge { .to = e[1], .weight = e[2] });\n vertices[e[1]].push_back(Edge { .to = e[0], .weight = e[2] });\n }\n\n struct Result {\n int num_cities;\n int index;\n };\n\n // (1) For any given city i, what is the number of cities that are reachable\n // through some path with a distance of distanceThreshold?\n // O(V * (V log V + E log V))\n // std::pair<num_cities, index>\n std::vector<Result> results(n);\n for (int i = 0; i < n; ++i) {\n results[i] = { findNumReachableCities(vertices, distanceThreshold, i), i };\n }\n\n // (2) Find the city with the smallest number of reachable cities\n // O(V log V)\n std::sort(results.begin(), results.end(), [](const Result& r1, const Result& r2) {\n if (r1.num_cities < r2.num_cities) {\n return true;\n }\n if (r1.num_cities > r2.num_cities) {\n return false;\n }\n return r1.index > r2.index;\n });\n\n return results.front().index;\n }\n};", "memory": "18015" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n struct Edge {\n int to;\n int weight;\n };\n \n int findNumReachableCities(const std::vector<std::vector<Edge>>& vertices, int distanceThreshold, int src) {\n // initialize an array with the distances to each city\n const size_t V = vertices.size();\n std::vector<int> distances(V, std::numeric_limits<int>::max());\n distances[src] = 0;\n\n // use a priority queue for the order that we should visit nodes\n // <distance_to_node, index>\n std::priority_queue<std::pair<int, int>> priq;\n priq.push({ distances[src], src });\n\n // run Djikstra's algorithm\n int num_cities = 0;\n while (!priq.empty()) {\n int distance_to_node, index;\n std::tie(distance_to_node, index) = priq.top();\n priq.pop();\n\n if (distances[index] < distance_to_node) {\n // stale value from the priority queue\n continue;\n }\n\n // update neighboring edges\n for (const Edge& e : vertices[index]) {\n int dist = distances[index] + e.weight;\n if (dist < distances[e.to] && dist <= distanceThreshold) {\n if (distances[e.to] == std::numeric_limits<int>::max()) {\n ++num_cities;\n }\n\n distances[e.to] = dist;\n priq.push({ distances[e.to], e.to });\n }\n }\n }\n\n return num_cities;\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n // create adjacency list\n std::vector<std::vector<Edge>> vertices(n);\n for (const std::vector<int>& e : edges) {\n vertices[e[0]].push_back(Edge { .to = e[1], .weight = e[2] });\n vertices[e[1]].push_back(Edge { .to = e[0], .weight = e[2] });\n }\n\n struct Result {\n int num_cities;\n int index;\n };\n\n // (1) For any given city i, what is the number of cities that are reachable\n // through some path with a distance of distanceThreshold?\n // O(V * (V log V + E log V))\n // std::pair<num_cities, index>\n std::vector<Result> results(n);\n for (int i = 0; i < n; ++i) {\n results[i] = { findNumReachableCities(vertices, distanceThreshold, i), i };\n }\n\n // (2) Find the city with the smallest number of reachable cities\n // O(V log V)\n std::sort(results.begin(), results.end(), [](const Result& r1, const Result& r2) {\n if (r1.num_cities < r2.num_cities) {\n return true;\n }\n if (r1.num_cities > r2.num_cities) {\n return false;\n }\n return r1.index > r2.index;\n });\n\n return results.front().index;\n }\n};", "memory": "18015" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int, int>>> adj(n);\n for (auto& it : edges) {\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n pair<int, int> mini = {INT_MAX, -1};\n for (int i = 0; i < n; i++) {\n vector<int> d(n, INT_MAX);\n d[i] = 0;\n priority_queue<pair<int, int>> pq;\n pq.push({0, i});\n\n while (!pq.empty()) {\n int u = pq.top().second;\n pq.pop();\n\n for (auto& [v, w] : adj[u]) {\n if (d[v] > d[u] + w) {\n d[v] = d[u] + w;\n pq.push({d[v], v});\n }\n }\n }\n int cnt = 0 ; \n\n for(int i = 0 ; i< n ; i++){\n if(d[i]<=distanceThreshold)cnt++;\n // cout << d[i] << \" \";\n }\n // cout << endl;\n // cout << mini.first <<\" \"<<mini.second << endl;\n // cout << i <<\" \"<<cnt << endl <<endl;\n\n if(mini.first>=cnt)\n mini ={cnt, i};\n }\n return mini.second;\n }\n};", "memory": "18314" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n int findTheCity(int n, vector<vector<int>>& edges, int dt) \n {\n int minC = INT_MAX, ans;\n vector<vector<int>> adj(n, vector<int>(n, 0));\n\n for(auto edge: edges)\n {\n adj[edge[0]][edge[1]] = edge[2];\n adj[edge[1]][edge[0]] = edge[2];\n }\n\n for(int i=0; i<n; i++)\n {\n int cnt=0;\n queue<pair<int, int>> q;\n q.push({i, 0});\n vector<int> dist(n, INT_MAX);\n dist[i] = 0;\n\n while(!q.empty())\n {\n int node = q.front().first, c = q.front().second;\n q.pop();\n\n for(int j=0; j<adj.size(); j++)\n {\n if(adj[node][j] > 0 and dist[j] >= c+adj[node][j] and c+adj[node][j] <= dt)\n {\n dist[j] = c + adj[node][j];\n q.push({j, dist[j]});\n }\n }\n }\n\n for(int j=0; j<n; j++)\n if(dist[j] <= dt and i != j) ++cnt;\n \n if(cnt <= minC)\n {\n minC = cnt;\n ans = i;\n }\n }\n \n return ans;\n }\n};", "memory": "18314" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n \n vector<pair<int,int>> adj[n];\n int ans = 0;\n int mincount = INT_MAX;\n \n for(const auto& edge : edges){\n int from = edge[0];\n int to = edge[1];\n int w = edge[2];\n adj[from].push_back({w,to});\n adj[to].push_back({w,from});\n }\n\n for(int src=0 ; src<n ; src++){\n\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n vector<int> distance(n,INT_MAX);\n distance[src] = 0;\n pq.push({distance[src],src});\n\n while(pq.empty() == false){\n const auto [d,u] = pq.top();\n pq.pop();\n for(const auto& [w,v] : adj[u])\n if( w+d < distance[v] && w+d <= distanceThreshold)\n distance[v] = w+d,\n pq.push({distance[v],v});\n }\n\n int count=-1;\n for(auto& d : distance)\n if(d<=distanceThreshold)\n count++;\n if(count<=mincount)\n ans = src,\n mincount = count;\n }\n\n return ans;\n }\n};", "memory": "18613" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int dt) \n {\n vector<pair<int,int>> adj[n]; // adjacency list\n for(auto it:edges)\n {\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n } \n map<int,int> mp;\n for(int i=0;i<n;i++)\n {\n vector<int> dis(n, 1e8); // distance array\n dis[i] = 0; // starting node distance is 0\n queue<int>q;\n q.push(i);\n \n while(!q.empty())\n {\n int node = q.front();\n q.pop();\n for(auto it: adj[node])\n {\n int adjnode = it.first;\n int wt = it.second;\n if(dis[node] + wt < dis[adjnode])\n {\n dis[adjnode] = dis[node] + wt;\n q.push(adjnode);\n }\n }\n }\n\n // Count the cities reachable within the distance threshold\n for(int j=0; j<n; j++)\n {\n if(dis[j] <= dt && i != j) // ensure not counting the city itself\n {\n mp[i]++;\n }\n }\n }\n\n // Find the city with the smallest number of reachable cities\n int smallnode = -1;\n int smallval = INT_MAX;\n for(int i=0; i<n; i++)\n {\n if(mp[i] < smallval || (mp[i] == smallval && i > smallnode))\n {\n smallval = mp[i];\n smallnode = i;\n }\n }\n \n return smallnode;\n }\n};\n", "memory": "18613" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\nprivate:\n void floydWarshall(int& n, vector<vector<int>>& edges, vector<vector<int>>& dist, int& distanceThreshold) {\n for(int i=0; i<n; i++) {\n dist[i][i] = 0;\n }\n\n for(auto edge: edges) {\n int u=edge[0], v=edge[1], w=edge[2];\n if(w <= distanceThreshold) {\n dist[u][v] = dist[v][u] = w;\n }\n }\n\n for(int k=0; k<n; k++) {\n for(int i=0; i<n; i++) {\n for(int j=0; j<n; j++) {\n dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);\n }\n }\n }\n\n }\n\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<int>> dist(100, vector<int>(100, 1e9));\n floydWarshall(n, edges, dist, distanceThreshold);\n\n int min_cnt = n, city = -1;\n \n for(int i=0; i<n; i++) {\n int cnt = -1;\n\n for(int j=0; j<n; j++) {\n if(dist[i][j] <= distanceThreshold) {\n cnt++;\n }\n }\n\n if(cnt <= min_cnt) {\n min_cnt = cnt;\n city = i;\n }\n }\n\n return city;\n }\n};", "memory": "18911" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n std::vector<std::vector<std::pair<int, int>>> adjGraph(n);\n for (const auto &edge : edges) {\n adjGraph[edge[0]].push_back({edge[1], edge[2]});\n adjGraph[edge[1]].push_back({edge[0], edge[2]});\n } \n\n int resultCity = -1;\n int minReachable = INT_MAX;\n\n for (int i = 0; i < n; ++i) {\n std::vector<int> dists(n, INT_MAX);\n dists[i] = 0;\n\n std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> pq;\n pq.push({0, i});\n\n while (!pq.empty()) {\n auto [currDist, currCity] = pq.top();\n pq.pop();\n\n if (currDist > dists[currCity]) {\n continue;\n }\n\n for (const auto &[nextCity, nextDist] : adjGraph[currCity]) {\n int newDist = currDist + nextDist;\n if (newDist <= distanceThreshold && newDist < dists[nextCity]) {\n dists[nextCity] = newDist;\n pq.push({newDist, nextCity});\n }\n }\n }\n\n int currReachable = 0;\n for (int j = 0; j < n; ++j) {\n if (i != j && dists[j] <= distanceThreshold) {\n ++currReachable;\n }\n }\n\n if (currReachable < minReachable || (currReachable == minReachable && i > resultCity)) {\n resultCity = i;\n minReachable = currReachable;\n }\n }\n\n return resultCity;\n }\n};", "memory": "18911" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n void djikstra(unordered_map<int, vector<pair<int, int>>> &adj, vector<int> &vis, int start, int distanceThreshold, vector< vector<int>> &ans){\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n pq.push({start, 0});\n vis[start] = 0;\n\n while(!pq.empty()){\n int node = pq.top().first;\n int weight = pq.top().second;\n pq.pop();\n\n\n for(auto &k: adj[node]){\n int nextNode = k.first;\n int extraW = k.second;\n\n if(vis[nextNode] > weight + extraW && weight + extraW <= distanceThreshold){\n vis[nextNode] = weight + extraW;\n pq.push({nextNode, vis[nextNode]});\n ans[start][nextNode] = 1;\n }\n }\n }\n return;\n\n }\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n unordered_map<int, vector<pair<int, int>>> adj;\n vector< vector<int>> ans(n, vector<int>(n, 0));\n\n for(auto &i: edges){\n int node1 = i[0];\n int node2 = i[1];\n int weight = i[2];\n\n adj[node1].push_back({node2, weight});\n adj[node2].push_back({node1, weight});\n } \n\n for(int i=0; i< n; i++) {\n vector<int> vis(n, INT_MAX);\n int start = i;\n djikstra(adj, vis, start,distanceThreshold, ans);\n }\n \n int out, count =INT_MAX;;\n for(int i=0; i< n; i++){\n int c =0;\n for(int j=0; j<n; j++){\n if(ans[i][j] == 1){\n c++;\n }\n }\n if(c <= count){\n count = c;\n out = i;\n }\n }\n\n return out; \n }\n};", "memory": "19210" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int,int>>>g(n);\n for(int i=0;i<edges.size();i++)\n {\n g[edges[i][0]].push_back({edges[i][1],edges[i][2]});\n g[edges[i][1]].push_back({edges[i][0],edges[i][2]});\n }\n int inx=-1,ans=INT_MAX;\n for(int i=0;i<n;i++)\n {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n pq.push({0,i});\n vector<int>wt(n,INT_MAX),vis(n,0);\n wt[i]=0;\n \n while(!pq.empty())\n {\n auto node=pq.top();\n pq.pop();\n if(node.first>distanceThreshold)\n break;\n for(int i=0;i<g[node.second].size();i++)\n {\n int v=g[node.second][i].first;\n int d=g[node.second][i].second;\n if(wt[v]>node.first+d && node.first+d<=distanceThreshold)\n {\n wt[v]=node.first+d;\n pq.push({wt[v],v});\n vis[v]=1;\n }\n }\n\n }\n int temp=accumulate(vis.begin(),vis.end(),0);\n if(temp<=ans)\n {\n inx=i;\n ans=temp;\n }\n\n }\n return inx;\n }\n};", "memory": "19210" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int,int>>> adj(n);\n \tfor(auto& edge: edges){\n adj[edge[0]].push_back({edge[1], edge[2]});\n adj[edge[1]].push_back({edge[0], edge[2]});\n }\n int minNeighbours =n+1, ansCity= -1;\n\n for(int node = 0;node<n;node++){\n vector<int> dist(n, INT_MAX);\n dist[node] = 0;\n using pii = pair<int,int>;\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n pq.push({0, node});\n int numNeighbours =0;\n while(!pq.empty() && numNeighbours < n){\n auto [currDist, city] = pq.top();\n pq.pop();\n if(currDist > distanceThreshold)break;\n if(currDist == dist[city]) numNeighbours++;\n for(auto [neighbour, cost]: adj[city]){\n if(dist[neighbour] > cost+currDist){\n dist[neighbour] = cost+currDist;\n pq.push({dist[neighbour], neighbour});\n }\n }\n }\n if(minNeighbours >= numNeighbours ){\n minNeighbours = numNeighbours ;\n ansCity = node;\n }\n }\n return ansCity;\n }\n\n};", "memory": "19509" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n void djikstra(int node,vector<int> &dist,vector<pair<int,int>> adj[]){\n priority_queue<pair<int,int>,vector<pair<int,int>>, greater<pair<int,int>> > pq;\n pq.push({0,node});\n dist[node]=0;\n while(!pq.empty()){\n auto [d,n]=pq.top();\n pq.pop();\n for(auto ne:adj[n]){\n if(dist[ne.first]>dist[n]+ne.second){\n dist[ne.first]=dist[n]+ne.second;\n pq.push({dist[ne.first],ne.first});\n }\n }\n }\n }\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<pair<int,int>> adj[n];\n for(auto edge:edges){\n adj[edge[0]].push_back({edge[1],edge[2]});\n adj[edge[1]].push_back({edge[0],edge[2]});\n }\n int mini=n;\n int res=0;\n for(int i=0;i<n;i++){\n vector<int> dist(n,distanceThreshold+1);\n djikstra(i,dist,adj);\n int count=0;\n for(int j=0;j<n;j++){\n if(dist[j]<=distanceThreshold){\n count++;\n }\n }\n cout<<i<<\" \"<<count<<endl;\n if(count-1<=mini){\n mini=count-1;\n res=i;\n }\n }\n return res;\n }\n};", "memory": "19509" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int dt) {\n vector<pair<int,int>> adj[n];\n for(auto it:edges)\n {\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n }\n vector<vector<int>> dis;\n for(int i=0;i<n;i++)\n {\n vector<int> dist(n,1e9);\n priority_queue<pair<int,int>,\n vector<pair<int,int>>,greater<pair<int,int>>> pq;\n\n pq.push({0,i});\n dist[i]=0;\n while(pq.size())\n {\n int node=pq.top().second;\n int w=pq.top().first;\n pq.pop();\n for(auto it:adj[node])\n {\n int nbr=it.first;\n int nbrW=it.second;\n if(nbrW+w<dist[nbr] && nbrW+w<=dt)\n {\n dist[nbr]=nbrW+w;\n pq.push({dist[nbr],nbr});\n }\n }\n }\n dis.push_back(dist);\n }\n int maxs=INT_MAX;\n int ans=0;\n for(int i=0;i<dis.size();i++)\n {\n int c=0;\n for(int j=0;j<n;j++)\n {\n if(dis[i][j]<=dt)\n c++;\n }\n if(c<=maxs)\n {\n maxs=c;\n ans=i;\n }\n }\n return ans;\n }\n};", "memory": "19808" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int dt) {\n vector<pair<int,int>> adj[n];\n for(auto it:edges)\n {\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n }\n vector<vector<int>> dis;\n for(int i=0;i<n;i++)\n {\n vector<int> dist(n,INT_MAX);\n priority_queue<pair<int,int>,\n vector<pair<int,int>>,greater<pair<int,int>>> pq;\n\n pq.push({0,i});\n dist[i]=0;\n while(pq.size())\n {\n int node=pq.top().second;\n int w=pq.top().first;\n pq.pop();\n for(auto it:adj[node])\n {\n int nbr=it.first;\n int nbrW=it.second;\n if(nbrW+w<dist[nbr] && nbrW+w<=dt)\n {\n dist[nbr]=nbrW+w;\n pq.push({dist[nbr],nbr});\n }\n }\n }\n dis.push_back(dist);\n }\n int maxs=INT_MAX;\n int ans=0;\n for(int i=0;i<dis.size();i++)\n {\n int c=0;\n for(int j=0;j<n;j++)\n {\n if(dis[i][j]<=dt)\n c++;\n }\n if(c<=maxs)\n {\n maxs=c;\n ans=i;\n }\n }\n return ans;\n }\n};", "memory": "19808" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int dijkstra(vector<pair<int, int>> adj[], int distanceThreshold, int S,\n int V) {\n priority_queue<pair<int, int>, vector<pair<int, int>>,\n greater<pair<int, int>>>\n pq;\n vector<int> dist(V, INT_MAX);\n\n pq.push(make_pair(0, S));\n dist[S] = 0;\n int reach = 0;\n\n while (!pq.empty()) {\n int u = pq.top().second;\n pq.pop();\n\n for (auto& neighbor : adj[u]) {\n int v = neighbor.first;\n int weight = neighbor.second;\n if (dist[v] > dist[u] + weight) {\n dist[v] = dist[u] + weight;\n pq.push(make_pair(dist[v], v));\n }\n }\n }\n\n for (int i = 0; i < V; i++) {\n if (dist[i] <= distanceThreshold) {\n reach++;\n }\n }\n\n return reach;\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<pair<int, int>> adj[n];\n for (auto edge : edges) {\n int u = edge[0];\n int v = edge[1];\n int wt = edge[2];\n adj[u].push_back({v, wt});\n adj[v].push_back({u, wt});\n }\n int city = 0;\n int mini = INT_MAX;\n for (int i = 0; i < n; i++) {\n int reach = dijkstra(adj, distanceThreshold, i, n);\n if (reach <= mini) {\n mini = reach;\n city = i;\n }\n }\n return city;\n }\n};\n", "memory": "21003" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n int useDij(int n,int city, vector<vector<pair<int,int>>>&adj, int thres){\n vector<int>dist(n,INT_MAX);\n dist[city] = 0;\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>>pq;\n\n pq.push({0,city}); // {dist,node}\n while(!pq.empty()){\n int dis = pq.top().first;\n int node = pq.top().second;\n pq.pop();\n\n for(auto it: adj[node]){\n int adjNode = it.first;\n int edW = it.second;\n if(dis + edW < dist[adjNode]){\n dist[adjNode] = dis+edW;\n pq.push({dis+edW,adjNode});\n }\n }\n\n }\n\n int cnt=0;\n for(int i=0;i<n;i++){\n if(i!=city && dist[i]<=thres)\n cnt++;\n }\n\n return cnt;\n\n }\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n // Using Dijktra's Algorithm\n\n vector<vector<pair<int,int>>>adj(n);\n\n for(auto it: edges){\n int u = it[0];\n int v = it[1];\n int wt = it[2];\n adj[u].push_back({v,wt});\n adj[v].push_back({u,wt});\n }\n\n int cntcity = n; \n int cityNo = -1;\n\n for(int i=0;i<n;i++){\n int city = useDij(n,i,adj,distanceThreshold); // reacheable cities\n if(city<=cntcity){\n cntcity = city;\n cityNo = i;\n }\n }\n return cityNo;\n\n }\n};", "memory": "21003" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n typedef pair<int,int> pii;\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<int>> dist(n, vector<int>(n, INT_MAX));\n vector<vector<int>> shortestDistance(n, vector<int>(n, INT_MAX));\n vector<vector<int>> finalDistance(n, vector<int>(n, INT_MAX));\n vector<vector<int>> adj(n);\n vector<int> result(n, 0);\n initialize(dist, adj, edges);\n dijkstra(0, dist, adj, shortestDistance);\n int m1 = INT_MAX;\n int ans;\n for (int i=0;i<n;i++) {\n for (int i = 0; i < n; ++i) {\n fill(shortestDistance[i].begin(), shortestDistance[i].end(), INT_MAX);\n }\n dijkstra(i, dist, adj, shortestDistance);\n for (int j=0;j<n;j++) {\n if (i!=j && shortestDistance[i][j]<=distanceThreshold) {\n result[i]++;\n }\n //cout<<\"Distance between \"<<i<<\" and \"<<j<<\" is \"<<shortestDistance[i][j]<<endl;\n }\n //cout<<\"Result for:\"<<i<<\" is\"<<result[i]<<endl;\n if (result[i] <= m1) {\n ans = i;\n m1 = result[i];\n }\n }\n\n\n\n /*for (int i=0;i<n;i++) {\n for (int j=i+1;j<n;j++) {\n shortestPath(i,j,dist,adj,shortestDistance);\n }\n }\n\n for (int i=0;i<n;i++) {\n for (int j=i+1;j<n;j++) {\n cout<<shortestDistance[i][j]<<\" \";\n }\n cout<<endl;\n }*/\n return ans;\n }\n\n /*int shortestPath(int start, int end, vector<vector<int>>& dist, vector<vector<int>>& adj, vector<vector<int>>& shortestDistance) {\n cout<<\"Calculating for:\"<<start<<\" \"<<end<<endl;\n if (shortestDistance[start][end]!=INT_MAX) {\n return dist[start][end];\n }\n int size = adj[start].size();\n cout<<\"Size is:\"<<size<<endl;\n for (int i=0;i<size;i++) {\n int mid = adj[start][i];\n cout<<\"Mid is:\"<<mid<<endl;\n if (mid == end) {\n shortestDistance[start][end] = min(shortestDistance[start][end], dist[start][end]);\n shortestDistance[end][start] = shortestDistance[start][end];\n }\n else {\n int firstHalf = shortestPath(start,mid,dist,adj,shortestDistance);\n int secondHalf = shortestPath(mid,end,dist,adj,shortestDistance);\n shortestDistance[start][end] = min(shortestDistance[start][end], firstHalf + secondHalf);\n shortestDistance[end][start] = shortestDistance[start][end];\n }\n }\n return shortestDistance[start][end];\n }*/\n\n void dijkstra(int start, vector<vector<int>>& dist, vector<vector<int>>& adj, vector<vector<int>>& shortestDistance) {\n vector<bool> vis(dist.size(), false);\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n pq.push({0, start});\n while(!pq.empty()) {\n pii p = pq.top();\n int node = p.second;\n int distance = p.first;\n int size = adj[node].size();\n vis[node] = true;\n pq.pop();\n for (int i=0;i<size;i++) {\n int nextNode = adj[node][i];\n if (!vis[nextNode]) {\n vis[nextNode] = true;\n shortestDistance[start][nextNode] = distance + dist[node][nextNode];\n pq.push({shortestDistance[start][nextNode], nextNode});\n } else if (shortestDistance[start][nextNode] > distance + dist[node][nextNode]) {\n shortestDistance[start][nextNode] = distance + dist[node][nextNode];\n pq.push({shortestDistance[start][nextNode], nextNode});\n }\n }\n }\n }\n\n void initialize(vector<vector<int>>& dist, vector<vector<int>>& adj, vector<vector<int>>& edges) {\n int edgeSize = edges.size();\n for (int i=0;i<edgeSize;i++){\n int start = edges[i][0];\n int end = edges[i][1];\n int distance = edges[i][2];\n dist[start][end] = distance;\n dist[end][start] = distance;\n\n adj[start].push_back(end);\n adj[end].push_back(start);\n }\n }\n};", "memory": "21301" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> fun(vector<vector<pair<int,int>>> &g, int s) {\n int n=g.size();\n vector<int> dist(n,1e5);\n vector<int> vis(n,0);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;\n dist[s]=0;\n q.emplace(0,s); \n while(!q.empty()) {\n auto u = q.top().second;\n q.pop();\n vis[u]=true;\n for(auto [w,v]:g[u]) {\n if(dist[v]>dist[u]+w) {\n dist[v] = dist[u]+w;\n if(!vis[v]) q.emplace(dist[v],v);\n }\n }\n }\n return dist;\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n //g[u] = [[w1,v1] [w1,v2] ... ]\n vector<vector<pair<int,int>>> g(n);\n for(auto i:edges) {\n g[i[0]].emplace_back(i[2],i[1]);\n if(i[0]!=i[1]) g[i[1]].emplace_back(i[2],i[0]);\n }\n int mnCnt=1e5, city=-1;\n for(int i=0;i<n;i++) {\n auto dist = fun(g,i);\n int cnt=0;\n for(auto j:dist) cnt+=(j<=distanceThreshold);\n if(cnt<=mnCnt) {mnCnt=cnt;city=i;}\n }\n return city;\n }\n};", "memory": "21301" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\n private:\n vector<int> dijkstra(int& src,vector<vector<pair<int,int>>>& adjList, int threshold,int n){\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n pq.push({0,src});\n vector<int> dist(n,INT_MAX);\n dist[src]=0;\n vector<int> ans;\n while(!pq.empty()){\n int node = pq.top().second;\n int weight = pq.top().first;\n pq.pop();\n for(auto& i:adjList[node]){\n int adjNode = i.first;\n int cost = i.second;\n if(cost+weight < dist[adjNode]){\n dist[adjNode]=cost+weight;\n pq.push({dist[adjNode],adjNode});\n }\n }\n }\n for(auto it:dist){\n if(it<=threshold && it!=0){\n ans.push_back(it);\n }\n }\n return ans;\n }\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int,int>>> adjList(n);\n for (auto it : edges) {\n adjList[it[0]].push_back({it[1], it[2]});\n adjList[it[1]].push_back({it[0], it[2]});\n }\n \n vector<vector<int>> arr(n);\n for (int src = 0; src < n; src++) {\n arr[src] = dijkstra(src, adjList, distanceThreshold, n);\n }\n \n int minReachableCities = INT_MAX;\n int cityWithMinReachableCities = -1;\n \n for (int i = 0; i < n; ++i) {\n int reachableCities = arr[i].size();\n if (reachableCities < minReachableCities || (reachableCities == minReachableCities && i > cityWithMinReachableCities)) {\n minReachableCities = reachableCities;\n cityWithMinReachableCities = i;\n }\n }\n\n return cityWithMinReachableCities;\n }\n};", "memory": "21600" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "#define PII pair<int, pair<int,int>>\n#define PI pair<int,int>\nclass Comparator{\n public:\n bool operator()(const PI &a, const PI &b){\n return a.first < b.first;\n\n }\n};\nclass Solution {\npublic:\n map<int, set<int>> ans;\n vector<PI>G[105];\n void dij(int node, int thresh, int n) {\n int vis[102] = {0};\n int dis[102] = {0};\n vis[node] = 1;\n for(int i =0; i<= n; i++) dis[i] = INT_MAX;\n dis[node] = 0;\n priority_queue<PI, vector<PI>, Comparator> q;\n q.push(make_pair(0, node));\n while(!q.empty()) {\n PI p = q.top();\n q.pop();\n int w = p.first;\n int u = p.second;\n int sz = G[u].size();\n for (int i = 0; i < sz; i++) {\n int u1, v1, w1;\n w1 = G[u][i].second + w;\n v1 = G[u][i].first;\n if ( w1 <= thresh && w1 < dis[v1]) {\n dis[v1] = w1;\n q.push(make_pair(w1, v1));\n this->ans[node].insert(v1);\n vis[v1] = 1;\n }\n\n }\n }\n // cout << node << \" node : \" << endl;\n // for(int i =0; i < n; i++) cout << dis[i] << \" \";\n // cout << endl;\n // int sz = this->G[node].size();\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n int ln = edges.size();\n for(int i = 0; i < ln; i++) {\n int u = edges[i][0];\n int v = edges[i][1];\n int w = edges[i][2];\n G[u].push_back(make_pair(v,w));\n G[v].push_back(make_pair(u,w));\n }\n for(int i = 0 ; i < n; i++) {\n set<int> jj;\n if(this->ans.find(i) == this->ans.end()) {\n this->ans[i] = jj;\n }\n dij(i, distanceThreshold, n);\n }\n\n map<int, set<int>> ::iterator it;\n int node_ans = INT_MAX, idx = -1;\n for(it = this->ans.begin(); it != ans.end(); ++it) {\n // cout << it->first << \" \" << it->second.size() << endl;\n if(it->second.size() == node_ans) {\n node_ans = it->second.size();\n idx = max(it->first, idx);\n }\n if(it->second.size() < node_ans) {\n node_ans = it->second.size();\n idx = it->first;\n } \n }\n\n \n return idx;\n \n }\n};", "memory": "21600" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\n\n void dijkstra( int i, int n, vector<int>& city, vector<pair<int,int>> adj[], int& distance){\n\n vector<int> dist(n,INT_MAX); \n priority_queue<pair<int,int>,vector<pair<int,int>>, greater<pair<int,int>>> pq;\n pq.push({0,i});\n dist[i]=0;\n\n unordered_set<int> st;\n\n while( !pq.empty() ) {\n int node = pq.top().second;\n int wt = pq.top().first;\n pq.pop();\n\n for( auto it: adj[node]){\n int adjNode = it.first;\n int adjWt = it.second;\n\n if( wt+adjWt < dist[adjNode] && wt+adjWt<=distance ){\n st.insert(adjNode);\n dist[adjNode]=wt+adjWt ;\n pq.push({wt+adjWt ,adjNode});\n }\n }\n }\n city[i]=st.size();\n }\n\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int dist) {\n \n // make adj\n vector<pair<int,int>> adj[n];\n for( auto it : edges ){\n adj[it[0]].push_back( {it[1],it[2]});\n adj[it[1]].push_back( {it[0],it[2]});\n }\n\n // vector for store no of city;\n vector<int> city(n); \n\n for ( int i=0; i<n ; i++ ){\n dijkstra(i,n,city,adj,dist);\n }\n int mini=INT_MAX;\n int cityIndex =-1;\n for(int i=n-1;i>=0;i--){\n if(city[i]<mini){\n mini = city[i];\n cityIndex=i;\n }\n }\n return cityIndex; \n }\n};", "memory": "21899" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "struct Node{\n int val;\n int distance;\n \n bool operator>(const Node& other) const {\n return distance > other.distance;\n }\n};\n\nclass Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n \n vector<unordered_set<int>> canReach;\n unordered_map<int,vector<Node>> graph;\n\n for(int i=0;i<edges.size();++i) {\n graph[edges[i][0]].push_back({edges[i][1], edges[i][2]});\n graph[edges[i][1]].push_back({edges[i][0], edges[i][2]});\n }\n\n priority_queue<Node> pq;\n \n for(int i=0;i<n;++i) {\n canReach.push_back(dijkstra(graph, distanceThreshold, n, i));\n }\n\n int minSize = INT_MAX;\n int bestIdx = 0;\n \n \n\n for(int i=0;i<canReach.size();++i) {\n\n if(canReach[i].size()<=minSize) {\n minSize = canReach[i].size();\n bestIdx = max(i, bestIdx);\n }\n }\n\n return bestIdx;\n }\n\n unordered_set<int> dijkstra(unordered_map<int,vector<Node>>& graph, int distanceThreshold, int n, int start) {\n \n priority_queue<Node, vector<Node>, greater<Node>> pq;\n pq.push({start,0});\n vector<int> distance(n,INT_MAX);\n distance[start] = 0;\n unordered_set<int> res;\n\n while(!pq.empty()) {\n int curVal = pq.top().val;\n int curDist = pq.top().distance;\n pq.pop();\n\n for(auto & nei : graph[curVal]) {\n int newDist = curDist + nei.distance;\n if(distance[nei.val] > newDist) {\n distance[nei.val] = newDist;\n if(distance[nei.val] <= distanceThreshold) {\n res.insert(nei.val);\n pq.push({nei.val, distance[nei.val]});\n }\n }\n }\n }\n // cout << start << \" distance vector\" << \"\\n\";\n // for(int i=0;i<distance.size();++i) {\n // cout << distance[i]<< \"\\n\";\n // }\n return res;\n }\n\n};", "memory": "21899" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int bfs(int n, vector<vector<pair<int, int>>>& graph, int distanceThreshold, int cur) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;\n q.push({0, cur}); // {distance, city}\n set<int> visited;\n\n vector<int> distances(n, INT_MAX);\n distances[cur] = 0;\n\n while (!q.empty()) {\n auto [currentDist, currentCity] = q.top();\n q.pop();\n\n if (currentDist > distanceThreshold) {\n break;\n }\n\n visited.insert(currentCity);\n\n for (auto& [neighbor, weight] : graph[currentCity]) {\n int newDist = currentDist + weight;\n if (newDist < distances[neighbor]) {\n distances[neighbor] = newDist;\n q.push({newDist, neighbor});\n }\n }\n }\n\n return visited.size();\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n // Build the graph\n vector<vector<pair<int, int>>> graph(n);\n for (auto& edge : edges) {\n graph[edge[0]].push_back({edge[1], edge[2]});\n graph[edge[1]].push_back({edge[0], edge[2]});\n }\n\n int minReachableCities = n;\n int cityWithMinReach = -1;\n\n for (int i = 0; i < n; ++i) {\n int reachableCities = bfs(n, graph, distanceThreshold, i);\n if (reachableCities <= minReachableCities) {\n minReachableCities = reachableCities;\n cityWithMinReach = i;\n }\n }\n\n return cityWithMinReach;\n }\n};\n", "memory": "22198" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\n // city to city | weight\n unordered_map<int,unordered_map<int,int>> adjList;\n unordered_map<int,set<int>> reach;\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n int answer = 0;\n buildAdjList ( edges);\n for ( int i = 0; i < n; ++i) {\n int dist = 0;\n unordered_map<int,int> visited;\n dfs ( i, distanceThreshold, dist, i, visited);\n }\n int minCities = INT_MAX;\n for ( int i = 0; i < n; ++i) {\n minCities = min ( (size_t)minCities, reach[i].size());\n if ( minCities == reach[i].size() )\n answer = i;\n }\n return answer;\n }\n void buildAdjList ( vector<vector<int>>& edges) {\n for ( vector<int> & conn : edges) {\n int from = conn[0];\n int to = conn[1];\n int w = conn[2];\n adjList[from][to] = w;\n adjList[to][from] = w;\n }\n }\n void dfs ( int city, int distanceThreshold, int dist, int rootCity, unordered_map<int,int> & visited) {\n unordered_map<int,int> & citiesAround = adjList[city];\n visited[city] = dist;\n for ( auto & entry : citiesAround) {\n int currentW = dist + entry.second;\n if ( visited.count(entry.first) == 0 || visited[entry.first] > currentW) { \n if ( currentW <= distanceThreshold)\n {\n reach[rootCity].insert(entry.first);\n dfs( entry.first, distanceThreshold, dist + entry.second, rootCity, visited );\n }\n } \n }\n }\n};", "memory": "22198" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int check(int src, int n, unordered_map<int, list<vector<int>>>& adj, int distanceThreshold) {\n int ans = 0;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n vector<int> dist(n, INT_MAX); // Initialize distances to all nodes as INT_MAX\n dist[src] = 0;\n pq.push({0, src}); // Push the source node with distance 0\n\n while (!pq.empty()) {\n auto fr = pq.top();\n pq.pop();\n int distill = fr.first;\n int node = fr.second;\n\n // If we find a longer path, skip it\n if (distill > dist[node]) continue;\n\n for (auto& i : adj[node]) {\n int neighbor = i[0];\n int weight = i[1];\n\n // Relaxation step\n if (dist[node] + weight < dist[neighbor]) {\n dist[neighbor] = dist[node] + weight;\n pq.push({dist[neighbor], neighbor});\n }\n }\n }\n\n for (int i = 0; i < n; ++i) {\n if (dist[i] <= distanceThreshold) ans++;\n }\n\n return ans - 1; // Subtract 1 to exclude the src itself\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n unordered_map<int, list<vector<int>>> adj;\n for (auto& i : edges) {\n int u = i[0];\n int v = i[1];\n int w = i[2];\n adj[u].push_back({v, w});\n adj[v].push_back({u, w});\n }\n\n int minReachableCities = INT_MAX;\n int cityWithMinReachable = -1;\n\n for (int i = 0; i < n; i++) {\n int reachableCities = check(i, n, adj, distanceThreshold);\n if (reachableCities <= minReachableCities) {\n minReachableCities = reachableCities;\n cityWithMinReachable = i;\n }\n }\n\n return cityWithMinReachable;\n }\n};\n", "memory": "22496" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n #define pii pair<long long,int>\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<pii> adj[n];\n for( int i = 0 ; i < edges.size() ; i++ )\n {\n adj[edges[i][0]].push_back({edges[i][1],edges[i][2]});\n adj[edges[i][1]].push_back({edges[i][0],edges[i][2]});\n }\n\n vector<vector<int>> dist(n,vector<int> (n,INT_MAX));\n\n for( int i = 0 ; i < n ; i++ )\n {\n priority_queue<pii,vector<pii>,greater<pii>> pq;\n pq.push({0,i});\n dist[i][i] = 0;\n while( !pq.empty() )\n {\n int dst = pq.top().first;\n int node = pq.top().second;\n pq.pop();\n\n if( dst > distanceThreshold ) continue;\n for( auto& it : adj[node] )\n {\n if( dist[i][it.first] > it.second + dst )\n {\n dist[i][it.first] = it.second + dst;\n pq.push({dist[i][it.first],it.first});\n }\n }\n }\n }\n\n int ans = INT_MAX;\n int city = -1;\n for( int i = 0 ; i < dist.size() ; i++ )\n {\n int temp = 0;\n for( int j = 0 ; j < dist[i].size() ; j++ )\n {\n if( dist[i][j] > 0 && dist[i][j] <= distanceThreshold ) temp++;\n }\n if( ans >= temp )\n {\n ans = temp;\n city = i;\n }\n }\n\n return city;\n }\n};", "memory": "22496" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "// class Solution {\n// int bfs( vector<vector<pair<int,int>>>& adj,int startNode,int n, int distanceThreshold){\n// set<pair<int,int>> s;\n// s.insert({0,startNode});\n// vector<int> dist(n,INT_MAX);\n// dist[startNode] = 0;\n// vector<int> path(n,0);\n// path[startNode] = 1;\n// while(!s.empty()){\n// auto it = *s.begin();\n// s.erase(it);\n// int d = it.first;\n// int node = it.second;\n\n// for(auto i:adj[node]){\n// int neighbour = i.first;\n// int weight = i.second;\n\n// if(dist[neighbour]>d+weight){\n// if(dist[neighbour]!=INT_MAX){\n// s.erase({dist[neighbour],neighbour});\n// }\n// dist[neighbour] = d+weight;\n// s.insert({dist[neighbour] ,neighbour});\n// path[neighbour] = path[node];\n// }\n// else if( dist[neighbour]<=distanceThreshold){\n// path[neighbour] += path[node];\n// }\n// }\n// }\n// return path[startNode]; \n// }\n// public:\n// int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n// vector<vector<pair<int,int>>> adj;\n// int m = edges.size();\n// for(int i=0;i<m;i++){\n// adj[edges[i][0]].push_back({edges[i][1],edges[i][0]});\n// adj[edges[i][1]].push_back({edges[i][0],edges[i][0]});\n// }\n// return bfs(adj,0,n,distanceThreshold);\n \n// }\n// };\n\nclass Solution {\npublic:\n int bfs(vector<vector<pair<int,int>>>& adj, int startNode, int n, int distanceThreshold) {\n set<pair<int,int>> s;\n s.insert({0, startNode});\n vector<int> dist(n, INT_MAX);\n dist[startNode] = 0;\n\n while (!s.empty()) {\n auto it = *s.begin();\n s.erase(it);\n int d = it.first;\n int node = it.second;\n \n if(d==distanceThreshold){\n continue;\n }\n for (auto i : adj[node]) {\n int neighbour = i.first;\n int weight = i.second;\n\n if (dist[neighbour] > d + weight && d+weight<=distanceThreshold) {\n if (dist[neighbour] != INT_MAX) {\n s.erase({dist[neighbour], neighbour});\n }\n dist[neighbour] = d + weight;\n s.insert({dist[neighbour], neighbour});\n }\n \n }\n }\n\n int cnt =0;\n for(int i=0;i<n;i++){\n if(dist[i]!=INT_MAX && i!=startNode){\n cnt++;\n }\n }\n \n return cnt;\n }\n public:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int,int>>> adj(n);\n int m = edges.size();\n for(int i=0;i<m;i++){\n adj[edges[i][0]].push_back({edges[i][1],edges[i][2]});\n adj[edges[i][1]].push_back({edges[i][0],edges[i][2]});\n }\n\n int ans = INT_MAX;\n int minVal = INT_MAX;\n\n for(int i=0;i<n;i++){\n int val = bfs(adj,i,n,distanceThreshold);\n cout<<val<<\" \";\n if(val<=minVal){\n minVal = val;\n ans = i;\n }\n }\n return ans;\n \n }\n};\n", "memory": "22795" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n // for each city, using dijkstra algorithm to find the number cities that can reach;\n\n if (n <= 1) {\n return 0;\n } \n\n int maxCities = INT_MAX;\n int city = -1;\n \n // Construct the graph;\n vector<vector<pair<int, int>>> graph(n, vector<pair<int, int>>());\n\n for (vector<int> e : edges) {\n graph[e[0]].push_back({e[1], e[2]});\n graph[e[1]].push_back({e[0], e[2]});\n }\n for (int i = 0; i < n; i++) {\n int cities = findCities(graph, distanceThreshold, n, i);\n\n if (cities < maxCities) {\n maxCities = cities;\n city = i;\n }\n else if (cities == maxCities) {\n city = max(city, i);\n }\n }\n\n return city;\n }\n\nprivate:\n struct cmp{\n bool operator()(pair<int, int>& a, pair<int, int> & b) {\n return a.second > b.second;\n }\n };\n // dijkstra algorithm;\n int findCities(vector<vector<pair<int, int>>>& graph, int distanceThreshold, int n, int start) {\n vector<int> dist(n, INT_MAX);\n dist[start] = 0;\n priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq;\n unordered_set<int> visited;\n int cities = 0;\n\n pq.push({start, 0});\n while (!pq.empty()) {\n int cur = pq.top().first;\n int curDist = pq.top().second;\n pq.pop();\n\n if (visited.count(cur)) continue;\n visited.insert(cur);\n\n if (curDist > distanceThreshold) {\n return cities;\n }\n cities++;\n\n for (auto next : graph[cur]) {\n int nextNode = next.first;\n if (visited.count(nextNode)) {\n continue;\n }\n\n if (dist[nextNode] > curDist + next.second) {\n dist[nextNode] = curDist + next.second;\n pq.push({nextNode, dist[nextNode]});\n }\n }\n }\n\n return cities;\n\n }\n};", "memory": "22795" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int dt) {\n vector<pair<int,int>> adj[n];\n for(auto it:edges)\n {\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n }\n vector<vector<int>> dis;\n for(int i=0;i<n;i++)\n {\n vector<int> dist(n,INT_MAX);\n set<pair<int,int>> pq;\n\n pq.insert({0,i});\n dist[i]=0;\n while(pq.size())\n {\n auto itr=*(pq.begin());\n int node=itr.second;\n int w=itr.first;\n pq.erase(itr);\n for(auto it:adj[node])\n {\n int nbr=it.first;\n int nbrW=it.second;\n if(nbrW+w<dist[nbr] && nbrW+w<=dt)\n {\n \n if(dist[nbr]!=INT_MAX)\n {\n pq.erase({dist[nbr],nbr});\n }\n dist[nbr]=nbrW+w;\n pq.insert({dist[nbr],nbr});\n }\n }\n }\n dis.push_back(dist);\n }\n int maxs=n+1;\n int ans=0;\n for(int i=0;i<dis.size();i++)\n {\n int c=0;\n for(int j=0;j<n;j++)\n {\n if(dis[i][j]<=dt)\n c++;\n }\n if(c<=maxs)\n {\n maxs=c;\n ans=i;\n }\n }\n return ans;\n }\n};", "memory": "23094" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int dt) {\n vector<pair<int,int>> adj[n];\n for(auto it:edges)\n {\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n }\n vector<vector<int>> dis;\n for(int i=0;i<n;i++)\n {\n vector<int> dist(n,INT_MAX);\n set<pair<int,int>> pq;\n\n pq.insert({0,i});\n dist[i]=0;\n while(pq.size())\n {\n auto itr=*(pq.begin());\n int node=itr.second;\n int w=itr.first;\n pq.erase(itr);\n for(auto it:adj[node])\n {\n int nbr=it.first;\n int nbrW=it.second;\n if(nbrW+w<dist[nbr] && nbrW+w<=dt)\n {\n dist[nbr]=nbrW+w;\n pq.insert({dist[nbr],nbr});\n }\n }\n }\n dis.push_back(dist);\n }\n int maxs=n+1;\n int ans=0;\n for(int i=0;i<dis.size();i++)\n {\n int c=0;\n for(int j=0;j<n;j++)\n {\n if(dis[i][j]<=dt)\n c++;\n }\n if(c<=maxs)\n {\n maxs=c;\n ans=i;\n }\n }\n return ans;\n }\n};", "memory": "23094" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n#define append push_back\n void dij(vector<pair<int, int>> g[], int src, int n, int &dist, vector<int> &v){\n vector<int> dis(n, INT_MAX); \n dis[src]=0;\n multiset<pair<int, int>> ms; // dist, node\n ms.insert({0, src});\n while(ms.size()>0){\n int node = (*ms.begin()).second;\n int current_dist = ms.begin()->first;\n // cout<<node<<endl;\n for(auto pr: g[node]){\n int wt = pr.second;\n int child = pr.first;\n if (current_dist + wt <= dis[child] and current_dist + wt <= dist){ \n ms.insert({current_dist + wt, child});\n if (dis[child]==INT_MAX)v[src]++;\n dis[child] = current_dist + wt;\n \n // if (src==3) cout<<child<< \" \";\n }\n }\n \n ms.erase(ms.begin());\n }\n // if (src==3) cout<<endl;\n }\n int findTheCity(int n, vector<vector<int>>& e, int dist) {\n vector<pair<int, int>> g[n];\n for(auto vec: e){\n g[vec[0]].append({vec[1], vec[2]});\n g[vec[1]].append({vec[0], vec[2]});\n }\n vector<int> v(n, 0);\n for(int i=0; i<n; i++){\n dij(g, i, n, dist, v);\n }\n int ans=0, mn=INT_MAX;\n // for(int i=0; i<n; i++) cout<<i<<\" \"<<v[i]<<endl;\n for(int i=n-1; i>=0; i--) mn = min(mn, v[i]);\n for(int i=n-1; i>=0; i--){\n if (mn == v[i]) {ans=i; break;}\n }\n return ans;\n }\n};", "memory": "23393" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int dijkstra(vector<int>&dist,vector<bool>&vis,int node,vector<vector<pair<int,int>>>&adj,int thres){\n dist[node]=0;\n set<pair<int,int>> st;\n st.insert({0,node});\n int visits=0;\n while(!st.empty()){\n auto it = *st.begin();\n st.erase(it);\n int nod=it.second;\n int dis=it.first;\n if(vis[nod])continue;\n vis[nod]=1;\n visits++;\n for(auto child : adj[nod]){\n int ch=child.first;\n int dist_child=child.second;\n if(!vis[ch] && dis + dist_child <= thres && dis + dist_child < dist[ch]){\n dist[ch]=dis + dist_child;\n st.insert({dist[ch],ch});\n }\n }\n }\n return visits;\n }\n int findTheCity(int n, vector<vector<int>>& edges, int dist_thresh) {\n vector<vector<pair<int,int>>> adj(n);\n for(auto it : edges){\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n }\n int city_visited=n+1;\n int res=-1;\n for(int i=0;i<n;i++){\n vector<int> dist(n,1e7);\n vector<bool> vis(n,0);\n int visits=dijkstra(dist,vis,i,adj,dist_thresh);\n if(visits <= city_visited){\n res=i;\n city_visited=visits;\n }\n }\n return res;\n }\n};", "memory": "23393" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "#include <vector>\n#include <queue>\n#include <climits>\n#include <algorithm>\n\nusing namespace std;\n//this is like no of cities within certain distance, the count of ways wala was no of ways \n//to go to the same city with the same distance\nclass Solution {\nprivate:\n int countways(int n, vector<vector<pair<int, int>>>& graph, int src, int k) {\n vector<long long> distance(n, LLONG_MAX);\n vector<int> path(n, 0);\n \n priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;\n pq.push({0, src}); // Push the source with distance 0\n distance[src] = 0;\n \n while(!pq.empty()) {\n long long currDist = pq.top().first;\n int node = pq.top().second;\n pq.pop();\n \n // if (currDist > distance[node]) continue; // Skip if the distance is not optimal\n \n for(auto &nbr: graph[node]) {\n long long vert = nbr.first;\n long long edge = nbr.second;\n \n if(distance[vert] > distance[node] + edge) {\n distance[vert] = distance[node] + edge;\n pq.push({distance[vert], vert});\n }\n }\n }\n \n int count = 0;\n for(int i = 0; i < n; ++i) {\n if (distance[i] <= k) {\n count++;\n }\n }\n \n return count;\n }\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int, int>>> graph(n);\n for(auto &road: edges) {\n graph[road[0]].push_back({road[1], road[2]});\n graph[road[1]].push_back({road[0], road[2]});\n }\n \n int cityWithMinReachableCities = -1;\n int minCount = INT_MAX;\n \n for(int i = 0; i < n; ++i) {\n int reachableCities = countways(n, graph, i, distanceThreshold);\n if (reachableCities <= minCount) {\n minCount = reachableCities;\n cityWithMinReachableCities = i;\n }\n }\n \n return cityWithMinReachableCities;\n }\n};\n", "memory": "23691" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Node{\n public:\n int node;\n int distance;\n Node(int node, int distance){\n this->node = node;\n this->distance = distance;\n }\n Node(){\n this->node = -1;\n this->distance = -1;\n }\n};\n\nclass Compare{\n public:\n bool operator()(Node &a, Node &b){\n return a.distance > b.distance;\n }\n};\n\nvoid dijkstra(unordered_map<int, vector<Node>> &graph, vector<vector<int>> &distances, int sourceNode){\n priority_queue<Node, vector<Node>, Compare> pq;\n distances[sourceNode][sourceNode] = 0;\n pq.push(Node(sourceNode, 0));\n\n while(!pq.empty()){\n Node top = pq.top();\n pq.pop();\n int currentNode = top.node;\n int currentDistance = top.distance;\n for(auto el : graph[currentNode]){\n int nbr = el.node;\n int totalDistance = currentDistance + el.distance;\n if(totalDistance < distances[sourceNode][nbr]){\n distances[sourceNode][nbr] = totalDistance;\n pq.push(Node(nbr, totalDistance));\n }\n }\n\n // for(Node el: graph[currentNode]){\n // cout<<el.node<< \" \";\n // }\n // cout<<endl; \n }\n}\n\nclass Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<int>> distances(n, vector<int>(n, INT_MAX));\n \n unordered_map<int, vector<Node>> graph;\n for(auto edge : edges){\n int u = edge[0];\n int v = edge[1];\n int dist = edge[2];\n graph[u].push_back(Node(v, dist));\n graph[v].push_back(Node(u, dist));\n }\n\n for(int i = 0 ; i < distances.size(); i++){\n for(int j = 0; j < distances.size(); j++){\n int sourceNode = i;\n dijkstra(graph, distances, sourceNode);\n }\n }\n // for(int i = 0 ; i < distances.size(); i++){\n // for(int j = 0; j < distances.size(); j++){\n // cout<<distances[i][j]<<\" \";\n // }\n // cout<<endl;\n // }\n int city = -1;\n int leastCities = INT_MAX;\n for(int i = 0; i < distances.size(); i++){\n int count = 0;\n for(int j = 0; j < distances.size(); j++){\n if(i != j and distances[i][j] <= distanceThreshold) count++;\n }\n if(count <= leastCities){\n city = i;\n leastCities = count;\n }\n }\n return city;\n }\n};", "memory": "23691" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n // Create graph\n vector<vector<int>> gra(n, vector<int>(n, INT_MAX));\n for(auto e : edges) {\n gra[e[0]][e[1]] = e[2];\n gra[e[1]][e[0]] = e[2];\n }\n\n int minCnt = INT_MAX;\n int minNode = 0;\n // Go through all nodes with dijkstra\n for(int i = 0; i < n; i++) {\n vector<int> dist = dijkstra(i, gra, n);\n int cnt = 0;\n for(auto d : dist) {\n if(d <= distanceThreshold) cnt++;\n }\n if(cnt <= minCnt) {\n minCnt = cnt;\n minNode = i;\n }\n }\n return minNode;\n }\nprivate:\n vector<int> dijkstra(int start, vector<vector<int>>& graph, int n) {\n unordered_set<int> visit;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n vector<int> dist(n, INT_MAX);\n dist[start] = 0;\n \n pq.push(make_pair(0, start));\n while(!pq.empty()) {\n int cur = pq.top().second;\n pq.pop();\n if(visit.find(cur) != visit.end()) continue;\n visit.insert(cur);\n\n for(int i = 0; i < n; i++) {\n int weight = graph[cur][i];\n if(weight == INT_MAX) continue;\n if(weight + dist[cur] < dist[i]) {\n dist[i] = weight + dist[cur];\n pq.push(make_pair(dist[i], i));\n }\n }\n }\n return dist;\n }\n};", "memory": "23990" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n int mi = INT_MAX;\n int city = -1;\n\n vector<vector<int>> adj[n];\n\n for(const auto& edge: edges) {\n adj[edge[0]].push_back({edge[1], edge[2]});\n adj[edge[1]].push_back({edge[0], edge[2]});\n }\n\n for(int i=n-1; i>=0; i--) {\n int newd = dijkastra(n, adj, i, distanceThreshold, mi);\n cout<<newd<<\" \";\n if(mi > newd) {\n mi = newd;\n city = i;\n }\n }\n\n return city;\n }\n\nprivate:\n int dijkastra(int n, vector<vector<int>> adj[], int s, int dt, int mi) {\n set<pair<int, int>> spt;\n vector<int> dist(n, INT_MAX);\n int count = 0;\n\n spt.insert({0, s});\n dist[s] = 0;\n\n while(!spt.empty()) {\n const auto& near = *(spt.begin());\n int distance = near.first;\n int node = near.second;\n spt.erase(spt.begin());\n\n if(distance > dt) {\n return count;\n }\n\n for(auto& p: adj[node]) {\n int neighbour = p[0];\n int edge = p[1];\n if(distance + edge < dist[neighbour]) {\n if(dist[neighbour] != INT_MAX) {\n if(dist[neighbour] <= dt)\n count--;\n spt.erase({dist[neighbour], neighbour});\n }\n \n if(distance+edge <= dt) {\n count++;\n if(count >= mi) {\n return count;\n }\n }\n\n dist[neighbour] = distance + edge;\n spt.insert({distance+edge, neighbour});\n }\n }\n }\n\n return count;\n }\n};", "memory": "23990" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int dijkstra(vector<vector<pair<int, int>>>& adj, int s, int n, int d) {\n vector<int> dist(n, INT_MAX);\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n \n dist[s] = 0;\n pq.push({0, s});\n \n while (!pq.empty()) {\n int dis = pq.top().first;\n int node = pq.top().second;\n pq.pop();\n \n for (auto it : adj[node]) {\n int neighbor = it.first;\n int weight = it.second;\n \n if (dist[neighbor] > dis + weight) {\n dist[neighbor] = dis + weight;\n pq.push({dist[neighbor], neighbor});\n }\n }\n }\n \n int count = 0;\n for (int i = 0; i < n; ++i) {\n if (dist[i] <= d) count++;\n }\n return count - 1; // Assuming you want to exclude the source node\n }\n \n int findTheCity(int n, vector<vector<int>>& edges, int d) {\n vector<vector<pair<int, int>>> adj(n);\n for (auto it : edges) {\n adj[it[0]].emplace_back(it[1], it[2]);\n adj[it[1]].emplace_back(it[0], it[2]);\n }\n \n int mini = INT_MAX;\n int ans = 0;\n \n for(int i=0;i<n;i++)\n {\n if(dijkstra(adj, i, n, d) <= mini) \n {\n mini = dijkstra(adj, i, n, d);\n ans = i;\n }\n }\n \n return ans;\n }\n};", "memory": "24289" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int dijkstra(vector<vector<pair<int, int>>>& adj, int s, int n, int d) {\n vector<int> dist(n, INT_MAX);\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n \n dist[s] = 0;\n pq.push({0, s});\n \n while (!pq.empty()) {\n int dis = pq.top().first;\n int node = pq.top().second;\n pq.pop();\n \n for (auto it : adj[node]) {\n int neighbor = it.first;\n int weight = it.second;\n \n if (dist[neighbor] > dis + weight) {\n dist[neighbor] = dis + weight;\n pq.push({dist[neighbor], neighbor});\n }\n }\n }\n \n int count = 0;\n for (int i = 0; i < n; ++i) {\n if (dist[i] <= d) count++;\n }\n return count;} \n \n int findTheCity(int n, vector<vector<int>>& edges, int d) {\n vector<vector<pair<int, int>>> adj(n);\n for (auto it : edges) {\n adj[it[0]].emplace_back(it[1], it[2]);\n adj[it[1]].emplace_back(it[0], it[2]);\n }\n \n int mini = INT_MAX;\n int ans = 0;\n \n for(int i=0;i<n;i++)\n {\n if(dijkstra(adj, i, n, d) <= mini) \n {\n mini = dijkstra(adj, i, n, d);\n ans = i;\n }\n }\n \n return ans;\n }\n};", "memory": "24289" }