id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int n=matrix.size();\n int m=matrix[0].size(); \n int ans=INT_MIN;\n for(int i=0;i<m;i++)\n {\n vector<int>sum(n,0);\n for(int r=i;r<m;r++)\n {\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& m, int k) {\n int res = INT_MIN, rows = m.size(), cols = m[0].size();\n for (int l = 0; l < cols; ++l) {\n vector<int> sums(rows);\n for (int r = l; r < cols; ++r) {\n int kadane = 0, ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>> const& matrix, int k) {\n int m = matrix.size(), n = matrix[0].size(), ans{INT_MIN};\n vector<int> sum(n);\n for (int l{}; l < m; ++l) {\n fill(sum.begin(), sum.end(), 0);\n for (int r{l}; r < ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int n = matrix.size();\n int m = matrix[0].size();\n std::vector<std::vector<int>> prefix_sums(n, std::vector<int>(m, 0));\n \n for(int i = 0; i < n; i++) {\n for(int j =...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int result = INT_MIN;\n void updateResult(vector<int>& nums, int k) {\n int sum = 0;\n\n // Container to store sorted prefix sums.\n set<int> sortedSum;\n set<int>::iterator it;\n\n // Add 0 as the prefix sum for an empty sub-array.\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int m = matrix.size(), n = matrix[0].size();\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n int top = (i > 0 ? matrix[i - 1][j] : 0);\n int le...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int n = matrix.size();\n int m = matrix[0].size();\n int best = std::numeric_limits<int>::min();\n\n for(int j_start = 0; j_start < m; j_start++) {\n vector<int> rowSum(n, 0);\n...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "\n\n\n\nint _ = [](){ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); return 0; }();\n\nclass Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& grid, int max_area) {\n int const r = grid.size(), c = grid[0].size();\n int ans = INT_MIN;\n \tstd::vector<int> col...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int kadane(int arr[], int n, int k) {\n set<int> prefixSums;\n prefixSums.insert(0); \n \n int sum = 0;\n int maxSum = INT_MIN;\n \n for (int i = 0; i < n; i++) {\n sum += arr[i];\n // Find the smallest pref...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int kadane(int arr[], int n, int k) {\n set<int> prefixSums;\n prefixSums.insert(0); \n\n int sum = 0;\n int maxSum = INT_MIN;\n \n for (int i = 0; i < n; i++) {\n sum += arr[i];\n // Find the smallest prefix sum w...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int result = INT_MIN;\n void updateResult(vector<int>& nums, int k) {\n int sum = 0;\n\n // Container to store sorted prefix sums.\n set<int> sortedSum;\n set<int>::iterator it;\n\n // Add 0 as the prefix sum for an empty sub-array.\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n\n std::vector<int> rowSum(matrix[0].size());\n for(int i = 0; i < matrix.size(); ++i) {\n\n fill(rowSum.begin(), rowSum.end(), 0);\n \n for(int row = i; row < matrix.siz...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int rows = matrix.size();\n int cols = matrix[0].size();\n int maxSum = INT_MIN;\n \n \n for (int top = 0; top < rows; top++) {\n vector<int> colSum(cols, 0);\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\n int diff_ = INT_MAX;\n\n void FindOptimal(const vector<int>& cols, int k) {\n int n = cols.size();\n // use prefix sum diff to find rectangle areas <= k, we don't need\n // the prefix sum array here\n int sum = 0;\n set<int> se{0};\n for (i...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "// Using kadane's algorithm in n square times i.e. time complexity of O(n cube)\nclass Solution {\n void print(vector<int> &vec){\n for(auto &i: vec)\n cout<<i<<\" \";\n cout<<endl;\n }\n void kadanesAlgo(vector<int> &vec, int &n, int &ans, int &k){\n int currentSum...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int best_cumulative_sum(vector<int>ar,int K) \n{ \n int N= ar.size();\n set<int> cumset; \n cumset.insert(0); \n \n int best=INT_MIN,cum=0; \n \n for(int i=0;i<N;i++) \n { \n cum+=ar[i]; \n set<int>::iterator sit=cumset.lower_bound(cum-K); \n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\n int diff_ = INT_MAX;\n\n void FindOptimal(const vector<int>& col_sum, int k) {\n int n = col_sum.size();\n vector<int> prefix(n + 1);\n set<int> se{0};\n for (int i = 0; i < n && diff_ != 0; ++i) {\n int num = prefix[i+1] = prefix[i] + col_sum...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\n int diff_ = INT_MAX;\n\n void FindOptimal(const vector<int>& col_sum, int k) {\n int n = col_sum.size();\n vector<int> prefix(n + 1);\n set<int> se{0};\n for (int i = 0; i < n && diff_ != 0; ++i) {\n int num = prefix[i+1] = prefix[i] + col_sum...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& grid, int target) {\n int n = grid.size(), m = grid[0].size();\n int res = INT_MIN;\n for (int j=0;j<m;j++) {\n vector<int> arr(n, 0);\n for (int k=j;k>=0;k--) {\n for (int i=0;...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int target) {\n int n = matrix.size(), m = matrix[0].size(), ans = INT_MIN;\n vector <vector <int>> pref(n, vector <int> (m, 0));\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& mat, int k) {\n int n = mat.size(), m = mat[0].size(), ans = INT_MIN;\n for(int i=0;i<n;i++){\n for(int j=1;j<m;j++) mat[i][j] += mat[i][j-1]; // prefix sum for row\n }\n for(int c1=0;c1<m;c1++){\...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int m = matrix.size(), n = m?matrix[0].size():0;\n if(!m || !n)\n return INT_MIN;\n int ret = INT_MIN;\n for(int i = 1;i<m;i++){\n for(int j = 0;j<n;j++){\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int n = matrix.size();\n int m = matrix[0].size();\n std::vector<std::vector<int>> prefix_sums(n, std::vector<int>(m, 0));\n \n for(int i = 0; i < n; i++) {\n for(int j =...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int kadane(int i,int j,vector<vector<int>>&colPrefix,int x){\n int n=colPrefix.size(),m=colPrefix[0].size();\n int maxi=-1e9;\n int curr=0;\n set<int>st;\n for(int k=0;k<m;k++){\n int p=0;\n if(j-1>=0) p=colPrefix[j-1][...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int max_sum(vector<int>&nums ,int k){\n int ans = INT_MIN;\n set<int>st;\n int sum = 0;\n for(int i = 0;i<nums.size();i+=1){\n sum += nums[i];\n if(sum<=k)ans = max(ans ,sum);\n auto it = st.lower_bound(sum-k);\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int target) {\n int n = matrix.size(), m = matrix[0].size();\n\n for(int i = 0; i < n; i++) {\n for(int j = 1; j < m; j++) {\n matrix[i][j] += matrix[i][j-1];\n }\n }\n\...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int n=matrix.size();\n int m=matrix[0].size();\n\n vector<int> sums(n, 0);\n int ans=-1e9;\n\n for (int l=0; l<m; l++) {\n for (int i=0; i<n; i++) {\n sums...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int maxSum = INT_MIN;\n int m = matrix.size();\n int n = matrix[0].size();\n set<int> sortedSum;\n if (m < n) {\n for (int i = 0; i < m; i++) {\n vector<in...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int n=matrix.size();\n int m=matrix[0].size();\n\n vector<int> sums(n, 0);\n int ans=-1e9;\n\n for (int l=0; l<m; l++) {\n for (int i=0; i<n; i++) {\n sums...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int m = matrix.size(), n = matrix.front().size();\n int result = INT_MIN;\n vector<int> sums(m);\n for (int l = 0; l < n; l++) {\n fill(sums.begin(), sums.end(), 0);\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int m = matrix.size(), n = matrix.front().size();\n int result = INT_MIN;\n vector<int> sums(m);\n for (int l = 0; l < n; l++) {\n for (int r = l; r < n; r++) {\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int m = matrix.size();\n int n = matrix[0].size();\n int maxSum = INT_MIN;\n\n // iterate through all possible pairs of columns (left and right boundaries)\n for (int left = 0; left...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
2
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& a, int k) {\n\n int n = a.size();\n int m = a[0].size();\n\n int ans = INT_MIN;\n\n vector<vector<int>> sums(n, vector<int> (m, 0));\n int sum = 0;\n int rowSum = 0;\n\n for (int j = 0; ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
2
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int n = matrix.size();\n int m = matrix[0].size();\n int maxSum = INT_MIN;\n\n for (int top = 0; top < n; ++top) {\n vector<int> rowSum(m, 0);\n for (int bottom = top...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
2
{ "code": "#include <set>\n#include <vector>\n#include <climits>\nusing namespace std;\n\nclass Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int n = matrix.size();\n int m = matrix[0].size();\n int maxSum = INT_MIN;\n\n // Loop over all pairs of rows...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
2
{ "code": "#include <iostream>\n#include <vector>\n#include <set>\n#include <climits>\nusing namespace std;\n\nclass Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int rows = matrix.size();\n int cols = matrix[0].size();\n int result = INT_MIN;\n\n // ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
2
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int target) {\n int n = matrix.size(), m = matrix[0].size();\n int ans = INT_MIN;\n for(int i = 0; i < n;i++){\n for(int j = 1; j < m;j++){\n matrix[i][j] += matrix[i][j-1];\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
2
{ "code": "class Solution {\npublic:\n\n int kadane(vector<int> &temp,int n,int k){\n int local_start=0,max_sum=INT_MIN,sum=0;\n set<int> s;s.insert(0);\n for(auto i:temp){\n sum+=i;\n auto it=s.lower_bound(sum-k);\n s.insert(sum);\n if(it!=s.end())...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
2
{ "code": "class Solution {\n public:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n const int m = matrix.size();\n const int n = matrix[0].size();\n int ans = INT_MIN;\n\n for (int baseCol = 0; baseCol < n; ++baseCol) {\n // sums[i] := sum(matrix[i][baseCol..j])\n vector<int> su...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
2
{ "code": "#include <set>\n#include <vector>\n#include <climits>\nusing namespace std;\n\nclass Solution {\npublic:\n // Kadane's Algorithm variation to find maximum subarray sum no more than k\n int kadaneMaxSumNoMoreThanK(vector<int> &arr, int k) {\n int maxSum = INT_MIN;\n int currentSum = 0;\n...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
2
{ "code": "class Solution {\npublic:\n \n int kadane(vector<int>&arr,int k){\n int curr_sum = 0;\n int max_sum = INT_MIN;\n set<int>s;\n s.insert(0);\n for(int i=0;i<arr.size();i++){\n curr_sum+=arr[i];\n auto it = s.lower_bound(curr_sum-k);\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
2
{ "code": "class Solution {\n public:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n const int m = matrix.size();\n const int n = matrix[0].size();\n int ans = INT_MIN;\n\n for (int baseCol = 0; baseCol < n; ++baseCol) {\n // sums[i] := sum(matrix[i][baseCol..j])\n vector<int> su...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int n=matrix.size();\n int m=matrix[0].size();\n vector<vector<int>> sum(n+1,vector<int>(m+1,0));\n for(int i=1;i<=n;i++){\n for(int j=1;j<=m;j++){\n sum[i][j]...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n \n\nint findmax(vector<int>& mat, int k) {\n int sum = 0, maxSum = INT_MIN;\n set<int> prefixSums;\n prefixSums.insert(0); // To handle the case where the subarray starts from the beginning\n\n for (int val : mat) {\n sum += val;\n \n // Find t...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n if(matrix.empty())\n {\n return 0;\n }\n int ans = INT_MIN;\n int m = matrix.size();\n int n = matrix[0].size();\n // Iterate over all pairs of rows\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int m = matrix.size();\n int n = matrix[0].size();\n int maxSum = INT_MIN;\n\n for (int top = 0; top < m; ++top) {\n vector<int> temp(n, 0);\n for (int bottom = top; ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n int kadanes(vector<int>& v,int k){\n int right=0,left,n=v.size(),ans=INT_MIN;\n set<int> st;\n st.insert(0);\n for(int i=0;i<n;i++){\n right+=v[i];\n auto it=lower_bound(st.begin(),st.end(),right-k);\n // cout<<righ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n int kadanes(vector<int>& v,int k){\n int right=0,left,n=v.size(),ans=INT_MIN;\n set<int> st;\n st.insert(0);\n for(int i=0;i<n;i++){\n right+=v[i];\n auto it=lower_bound(st.begin(),st.end(),right-k);\n if(it!=st.end...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\nint helper(int row1,int row2,int m,int k,vector<vector<int>>&dp){\n int ans=INT_MIN;\n int sum=0;\n set<int>st;\n st.insert(0);\n for(int i=0;i<m;i++){\n int val=dp[row2][i]-dp[row1-1][i];\n sum+=val;\n // cout<<sum<<endl;\n int x=sum-k;...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\n void findMax(vector<int> &temp, int k, int &ans){\n set<int> found={0};\n int i,n=temp.size(),x=0;\n for(i=0; i<n; i++){\n x+=temp[i];\n auto it=found.lower_bound(x-k);\n if(it!=found.end())\n ans=max(ans, x-*it);\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n int func(vector<int>& temp, int k) {\n map<int, int> mp;\n int sum = 0;\n int maxi = -1e9;\n mp[0] = 1; \n for (int i = 0; i < temp.size(); i++) {\n sum += temp[i];\n int target = sum - k;\n auto it = mp.lowe...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n const int m = matrix.size(), n = matrix[0].size(); \n int ans = INT_MIN;\n\n for (int i1 = 0; i1 < m; ++i1) {\n vector<int> prefix(n);\n for (int i2 = i1; i2 < m; ++i2) {...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n const int m = matrix.size(), n = matrix[0].size(); \n int ans = INT_MIN;\n\n for (int i1 = 0; i1 < m; ++i1) {\n vector<int> prefix(n);\n for (int i2 = i1; i2 < m; ++i2) {...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n const int m = matrix.size(), n = matrix[0].size();\n int ans = INT_MIN;\n\n for (int i1 = 0; i1 < m; ++i1) {\n vector<int> prefix(n);\n for (int i2 = i1; i2 < m; ++i2) {\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n\nusing namespace __gnu_pbds;\ntemplate <typename T>\nusing ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;\nclass Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int n = matrix.size();\n int m = matrix[0].size();\n int ans = -INT_MAX;\n vector<int> cur;\n \n for(int i=0;i<n;++i) {\n cur.assign(m,0);\n\n for(int j...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int m = matrix.size(), n = matrix[0].size();\n int res = INT_MIN;\n\n for (int i = 0; i < m; i++) {\n vector<int> sum(m, 0);\n for (int j = i; j < n; j++) {\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\n#define ll int\ntemplate<class T> using pbds=tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>;\nclass Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\n#define ll int\ntemplate<class T> using pbds=tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>;\nclass Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int m = matrix.size(); //row\n int n = matrix[0].size(); //columns\n // cout<<\"m & n: \"<<m<<\" \"<<n<<\"\\n\";\n for(int i = 0; i<m; i++) {\n for(int j = 0; j<n; j++) {\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {\n int m =matrix.size();\n int n = matrix[0].size();\n for(int i=1;i<m;++i){\n for(int j=0;j<n;++j){\n matrix[i][j] += matrix[i-1][j];\n // cout<<matrix[i][j...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n int maxSumSubmatrix(vector<vector<int>>& matrix, int t) {\n \n int maxN=INT_MIN;\n \n for(int i=0; i<matrix.size(); i++){\n \n vector<int> temp(matrix[0].size(), 0);\n for(int j=i; j<matrix.size(); j++){\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n\n int solve(vector<int>& temp, int k){\n set<int> s;\n int ans = INT_MIN;\n int acc_sum = 0;\n // a + b = acc_sum\n // a >= acc_sum - k\n for(auto num: temp){\n acc_sum += num;\n if (acc_sum <= k) {\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "#pragma GCC optimize(\"Ofast\",\"inline\",\"-ffast-math\")\n#pragma GCC target(\"avx,mmx,sse2,sse3,sse4\")\nstatic const int _ = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0; }();\n\n\nclass Solution {\npublic:\n int atmostk(vector<int>&v, int kk){\n ...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "class Solution {\n int kadane(vector<int> nums, int k){//KADANES ALGO FOR LARGEST SUM NOT GREATER THAN K\n int currsum=0,maxsum=INT_MIN;\n set<int> set1;\n set1.insert(0);\n\n for(int i=0;i<nums.size();i++){//eg:{-2, -3, 11}\n currsum+= nums[i];\n set<in...
363
<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p> <p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p> <p...
3
{ "code": "#pragma GCC optimize(\"Ofast\",\"inline\",\"-ffast-math\")\n#pragma GCC target(\"avx,mmx,sse2,sse3,sse4\")\nstatic const int _ = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0; }();\n\n\nclass Solution {\npublic:\n int atmostk(vector<int>&v, int kk){\n ...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "vector<int> group(100005);\nvector<int> g_score(100005);\nint find(int a) {\n if (group[a] == a) {\n return a;\n }\n return group[a] = find(group[a]);\n}\n\nbool is_same_union(int a, int b) { return find(a) == find(b); }\n\nvoid unite(int a, int b) {\n int ua = find(a);\n int ub = fin...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\n int find(int a,vector<int>& parent)\n {\n if(parent[a]<0)\n return a;\n return parent[a]=find(parent[a],parent);\n }\npublic:\n int minScore(int n, vector<vector<int>>& roads) {\n vector<int> parent(n+1,-1);\n for(auto& r:roads)\n {\n...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "struct Disjoint{\n vector<int> parent;\n vector<int> minval;\n Disjoint(int n){\n parent.resize(n);\n for(int i{};i<n;++i)parent[i]=i;\n minval.resize(n,2000000000);\n }\n int findParent(int x){\n if(parent[x]!=x) {\n parent[x]=findParent(parent[x]);\n }\n return parent[x...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class UnionFind\n{\nprivate:\n vector<int> par;\n vector<int> rank;\n\npublic:\n UnionFind(int n) : par(n), rank(n)\n {\n for (int i = 0; i < n; i++)\n {\n par[i] = i;\n rank[i] = 0;\n }\n }\n\n int root(int x)\n {\n return par[x] == x ...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic: \n vector<int>par, sz;\n void init(int n){\n par.assign(n+10, 0);\n sz.assign(n+10, 1);\n for(int i=0;i<n+10;i++) par[i]=i;\n\n }\n int find(int n){\n if(par[n]!=n) return par[n]=find(par[n]);\n return n;\n }\n void merge(int x,...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class UnionFind {\n std::vector<int> root;\n std::vector<int> rank;\npublic:\n explicit UnionFind(int size) {\n root.resize(size);\n rank.resize(size);\n for (int i = 0; i < size; ++i) {\n root[i] = rank[i] = i;\n }\n }\n\n int find(int x) {\n if...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class DisjointSet {\n\npublic:\n vector<int> rank, parent, size;\n DisjointSet(int n) {\n rank.resize(n + 1, 0);\n parent.resize(n + 1);\n size.resize(n + 1);\n for (int i = 0; i <= n; i++) {\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n in...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class UnionFind {\n vector<int> parent, size;\npublic:\n UnionFind(int n) : parent(n), size(n, 1) {\n iota(parent.begin(), parent.end(), 0);\n }\n \n int find(int a) {\n return parent[a] == a ? a : (parent[a] = find(parent[a]));\n }\n \n void connect(int a, int b) {\n ...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic:\n void make_set(int v, vector<int> &parent) {\n parent[v] = v;\n }\n\n int find_set(int v, vector<int> &parent) {\n if (v == parent[v])\n return v;\n return parent[v] = find_set(parent[v], parent); \n }\n\n void union_sets(int a, int ...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class UnionFind{\n vector<int>par,rank;\npublic:\n UnionFind(int n){\n for(int i=0;i<n;i++){\n par.push_back(i);\n rank.push_back(1);\n }\n }\n int find(int a){\n if(par[a]==a){\n return a;\n }\n return par[a]=find(par[a]);\n ...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "\nconst int SIZE = 1e5 + 5;\nconst int INF = 1e9 + 7;\n\npair<int,int> pn[SIZE];\nint sz[SIZE];\n\npair<int,int> find_set(int v, int d) {\n if (v == pn[v].first)\n return {v,min(d,pn[v].second)};\n\n pair<int,int> ret = find_set(pn[v].first, min(d,pn[v].second));\n return pn[v] = {ret.first...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic:\n int minScore(int n, vector<vector<int>>& roads) {\n vector< pair<int, int> > roots(n + 1);\n\n for (int i = 0; i <= n; ++i) {\n roots[i] = make_pair(i, INT_MAX);\n }\n\n for (const auto& road: roads) {\n auto rootP = find_root...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class DisjointSet{\n public:\n vector<int>size,parent;\n DisjointSet(int n){\n size.resize(n+1,1);\n parent.resize(n+1);\n for(int i=0;i<=n;i++)parent[i]=i;\n }\n int findUpar(int node){\n if(node==parent[node])return node;\n return parent[node]=findUpar(pa...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic:\n\n int findParent(int x,vector<int>&parent){\n if(x==parent[x]) return x;\n return parent[x] = findParent(parent[x],parent);\n }\n\n int minScore(int n, vector<vector<int>>& roads) {\n vector<int>parent(n+1,0);\n for(int i=0;i<=n;i++){\n ...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic:\n \n int Size[100005];\n int par[100005];\n int dist[100005];\n int INF = INT_MAX;\n void make( int i ){\n par[i] = i;\n Size[i] = 1;\n dist[i] = INF;\n }\n int find( int a ){\n if( par[a] == a ) return a;\n else return pa...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic:\n int find(int u,vector<int> &parent){\n if(parent[u]<0){\n return u;\n }\n return parent[u] = find(parent[u],parent);\n }\n void unite(vector<int> &parent , int p1, int p2){\n if(parent[p1]<parent[p2]){\n parent[p...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class DSU {\npublic:\n std::vector<int> parent;\n DSU(int n) : parent(n, -1) {} // Initialize all elements to -1\n\n int find(int x) {\n if (parent[x] < 0) return x;\n return parent[x] = find(parent[x]); // Path compression\n }\n\n void unite(int u, int v) {\n int pare...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic:\n int find(int u,vector<int> &parent){\n if(parent[u]<0){\n return u;\n }\n return parent[u] = find(parent[u],parent);\n }\n void unite(vector<int> &parent , int p1, int p2){\n if(parent[p1]<parent[p2]){\n parent[p...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic:\nint par[100005],sz[100005];\n\n int find(int i ){\n if(i!=par[i])par[i] = find(par[i]);\n return par[i];\n }\n\n void uni(int x,int y){\n int a= find(x);\n int b = find(y);\n if(a!=b){\n if(sz[a]<sz[b])swap(a,b);\n ...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class dsuf{\n public:\n int n;\n vector<int>par,rank;\n vector<long long>dist;\n dsuf(int num)\n {\n n=num;\n for(int i=0;i<n+1;i++){par.push_back(i);rank.push_back(1);dist.push_back(1e18);}\n }\n int findpar(int x)\n {\n if(x==par[x])return x;\n retur...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class DSU{\n public:\n vector<int>rank,parent;\n DSU(int n){\n rank.resize(n+1,0);\n parent.resize(n+1);\n for(int i=1;i<=n;i++)parent[i]=i;\n }\n int findpar(int u){\n if(parent[u]==u)return u;\n return parent[u]=findpar(parent[u]);\n }\n void ubran...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class DisjointSet{\n public:\n vector<int> parent;\n vector<int> size;\n DisjointSet(int n){\n parent.resize(n+1);\n size.resize(n+1,1);\n for(int i = 0; i<n+1; i++){\n parent[i] = i;\n }\n }\n int findUparent(int node){\n if(node == parent[no...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class DisjointSet {\n vector<int> size, parent, minDistance;\npublic:\n DisjointSet(int n) {\n size.resize(n+1, 1);\n parent.resize(n+1);\n minDistance.resize(n+1, INT_MAX);\n for(int i = 0; i <= n; i++) {\n parent[i] = i;\n }\n }\n\n int getMinDist...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class DisjointSet{\n public:\n vector<int> parent;\n vector<int> size;\n DisjointSet(int n){\n parent.resize(n+1);\n size.resize(n+1,1);\n for(int i = 0; i<n+1; i++){\n parent[i] = i;\n }\n }\n int findUparent(int node){\n if(node == parent[no...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class DSU {\n vector<int> par, rank;\npublic:\n DSU(int n){\n rank.resize(n, INT_MAX);\n for(int i=0;i<n;i++)par.push_back(i);\n }\n\n int findPar(int node){\n return par[node] == node ? par[node] : par[node] = findPar(par[node]);\n }\n\n void dounion(int i, int j, in...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic:\n int minScore(int n, vector<vector<int>>& roads) \n {\n // Storing graph in a adjacency list\n vector<int> adj[n+1];\n for(int i=0;i<roads.size();i++)\n {\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_ba...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic:\n int minScore(int n, vector<vector<int>>& roads) \n {\n // Storing graph in a adjacency list\n vector<int> adj[n+1];\n for(int i=0;i<roads.size();i++)\n {\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_ba...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic:\n int minScore(int n, vector<vector<int>>& roads) {\n vector<int> adj[n+1];\n int ans=INT_MAX;\n\n for(int i=0;i<roads.size();i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n\n\n ...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic:\n int minScore(int n, vector<vector<int>>& roads) {\n vector<int> adj[n+1];\n int ans=INT_MAX;\n\n for(int i=0;i<roads.size();i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n\n\n ...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic:\n int mini;\n\n void dfs(int vertex, vector<bool>& vis, vector<pair<int, int>> g[]){\n vis[vertex] = true;\n for(auto& [child, distance] : g[vertex]){\n mini = min(mini, distance);\n if(!vis[child]){\n dfs(child, vis, g);\n ...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic:\n int mini;\n\n void dfs(int vertex, vector<bool>& vis, vector<pair<int, int>> g[]){\n vis[vertex] = true;\n for(auto& [child, distance] : g[vertex]){\n mini = min(mini, distance);\n if(!vis[child]){\n dfs(child, vis, g);\n ...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic:\n int mini = INT_MAX;\n \n void dfs(int s, vector<bool>&vis, vector<pair<int, int>>adj[]) {\n vis[s] = true;\n \n for(auto &it : adj[s]) {\n int v = it.first, wt = it.second;\n mini = min(mini, wt);\n if(!vis[v])\n ...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "\nclass Solution {\npublic:\n int mini = INT_MAX;\n void dfs(int s, vector<bool>&vis, vector<pair<int, int>>adj[]) {\n vis[s] = true;\n \n for(auto &it : adj[s]) {\n int v = it.first, wt = it.second;\n mini = min(mini, wt);\n if(!vis[v])\n ...
2,582
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bid...
0
{ "code": "class Solution {\npublic:\n int ans = 1e9;\n int minScore(int n, vector<vector<int>>& roads) {\n vector<pair<int , int>> adj[n+1];\n for(int i = 0 ; i < roads.size() ; i++){\n adj[roads[i][0]].push_back({roads[i][1] , roads[i][2]});\n adj[roads[i][1]].push_back({...