id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\npublic:\n int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {\n int n = grid1.size(), m = grid1[0].size(), answ = 0 ;\n bool visited[501][501]; memset(visited, false, sizeof visited);\n pair<int, int> moves[] = { {1,0}, {-1,0}, {0,1} ,{0,-... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\npublic:\n // vector<vector<int>> visited; //needed for 1st and 2nd\n int w, h;\n\n // //first try\n // vector<vector<int>> getIsland(vector<vector<int>>& grid2, int m, int n){\n // queue<pair<int, int>> q;\n // q.push(make_pair(m, n));\n \n // vecto... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\npublic:\n int flag = 1;\n vector<int> dir = {0, 1, 0, -1, 0};\n void dfs(vector<vector<int>>& grid1, vector<vector<int>>& grid2, int i, int j){\n int m = grid2.size();\n int n = grid2[0].size();\n stack<pair<int, int>> st;\n st.push({i, j});\n w... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\n private:\n bool isvalid(int row,int col,int m , int n){\n return row>=0 && col>=0 && row<m && col<n;\n }\n void dfs(vector<vector<int>>&grid1,int row, int col,unordered_map<int,int>&mpp,int island,vector<vector<int>>&visited,int m,int n){\n visited[row][col]=1;\... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\npublic:\n void sink(vector<vector<int>>&land,vector<vector<bool>>&visted,stack<pair<int,int>>&qu,int m,int n){\n while(!qu.empty()){\n int g=qu.top().first;\n int h=qu.top().second;\n //cout<<\"(\"<<g<<\",\"<<h<<\")\"<<endl;\n qu.pop()... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\n int n, m;\n bool bfs1(int row, int col, vector<vector<int>> &grid1, vector<vector<int>> &grid2){\n queue<pair<int,int>> q;\n q.push({row, col});\n vector<int> moves = {0,1,0,-1,0};\n bool issub = 1;\n while(!q.empty()){\n pair<int,int> ... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\npublic:\n int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {\n int res = 0, m = grid1.size(), n = grid1[0].size();\n\n for(int i = 0; i < m; i++){\n for(int j = 0; j < n; j++){\n if(grid1[i][j] == 1 && grid2[i][j] == 1)... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\npublic:\n\n bool isValid(vector<vector<int>> &grid2, int r, int c, int rows, int cols) {\n return r >= 0 && c >= 0 && r < rows && c < cols && grid2[r][c];\n }\n\n int bfs(vector<vector<int>> &grid1, vector<vector<int>> &grid2, int startRow, int startCol, int rows, int cols... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\npublic:\n int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {\n int m = grid1.size(), n = grid1[0].size();\n vector<pair<int, int>> lands2;\n for(int i = 0; i < m; i++) {\n for(int j = 0; j < n ; j++) {\n if(grid1... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\npublic:\n int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {\n m = grid1.size();\n n = grid1[0].size();\n int res = 0;\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (bfs(grid1, grid2... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\npublic:\n int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {\n m = grid1.size();\n n = grid1[0].size();\n int res = 0;\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (bfs(grid1, grid2... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class DSU {\n public:\n vector<int> parent,size;\n\n DSU(int n){\n parent.resize(n+1);\n size.resize(n+1);\n for(int i=0;i<=n;i++){\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n int prt(int x){\n if(x==parent[x]) return x;\n\n retu... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class DSU {\npublic:\n vector<int> parent, size;\n\n DSU(int n) {\n parent.resize(n + 1);\n size.resize(n + 1);\n for (int i = 0; i <= n; i++) {\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n int prt(int x) {\n if (x == parent[x]) return x;\... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\nprivate:\n int n;\n int m;\n vector<pair<int, int>> neighbors = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};\n\n void dfs(int i, int j, vector<vector<int>>& grid1, vector<vector<int>>& grid2, vector<vector<bool>>& isVisit, bool& flag){\n isVisit[i][j] = true;\n if (grid1[... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\npublic:\n int dx[4] = {1, 0, -1, 0};\n int dy[4] = {0, 1, 0, -1};\n\n bool helper(int x, int y, vector<vector<bool>> &vis, vector<vector<int>>& grid1, vector<vector<int>>& grid2) {\n queue<pair<int,int>> q;\n q.push({x, y});\n\n int ans = 1;\n while (!... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\npublic:\nbool f(int i,int j,vector<vector<int>>& grid1, vector<vector<int>>& grid2){\n int n=grid1.size();\n int m=grid1[0].size();\nqueue<pair<int,int>> q;\nq.push({i,j});\n\ngrid2[i][j]=-1;\n\nbool result=true;\nif(grid1[i][j]!=1){\n result=false;\n}\nint delrow[]={0,1,-1,... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "static const void __attribute__((constructor)) fast()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n}\n\n\n\nclass Solution {\npublic:\n\n bool bfs(int r, int c, vector<vector<int>>& vis, vector<vector<int>>& a, vector<vector<int>>& b) {\n queue<pair<int, int>> q;\n int... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\n int m, n, sz;\n vector<int> root;\n vector<int> rank;\n int find(int x){\n if(x == root[x])\n return x;\n return root[x] = find(root[x]);\n }\n void un(int x, int y){\n int rootx = find(x);\n int rooty = find(y);\n if(rootx ... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\n bool has = false;\n void dfs(vector<vector<int>>& grid1,vector<vector<int>>& grid2,vector<vector<int>>& visi,int i,int j,vector<vector<int>> &directions){\n visi[i][j]=0;\n if(grid1[i][j]==0 && grid2[i][j]==1){\n has = true;\n }\n for(auto it:... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\npublic:\n int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {\n int n=grid1.size(); \n int m=grid1[0].size(); \n vector<vector<int>> grid=grid1;\n for(int i=0;i<n;i++) {\n for(int j=0;j<m;j++) {\n if(grid2[... |
2,035 | <p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>'s (representing water) and <code>1</code>'s (representing land). An <strong>island</strong> is a group of <code>1</code>'s connected <strong>4-directionally</strong> (horizontal o... | 3 | {
"code": "class Solution {\npublic:\n const int dy[4] = {-1,0,1,0};\n const int dx[4] = {0,1,0,-1};\n int row, col;\n int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {\n row = grid1.size();\n col = grid1[0].size();\n int color = 2;\n for (int r = 0;... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n for (int i = 0; i + 1 < size(weights); ++i) {\n weights[i] += weights[i + 1];\n }\n weights.pop_back();\n int64_t result = 0;\n nth_element(begin(weights), begin(weights) + (k ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "// Time: O(n) on average\n// Space: O(1)\n\n// greedy, quick select\nclass Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n for (int i = 0; i + 1 < size(weights); ++i) {\n weights[i] += weights[i + 1];\n }\n weights.pop_back();\n int64_t... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n if (k <= 1) {\n return 0;\n }\n --k;\n for (int i = 1; i < weights.size(); ++i) {\n weights[i - 1] += weights[i];\n }\n weights.pop_back();\n\n if (k * 2 > weights.size()) {\n k = weights.... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "#include <ranges>\n\nclass Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n int n = weights.size();\n\n if (n <= 2 || n == k) return 0;\n\n for (int i = 1; i < n; i++) weights[i - 1] += weights[i];\n weights.pop_back();\n \n int count ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\n public:\n long long putMarbles(const vector<int> &weights, const int k) {\n const int n = static_cast<int>(weights.size());\n if (k == 1 || k == n) {\n return 0;\n }\n\n int pair_sums[n - 1];\n memset(pair_sums, 0, sizeof(pair_sums));\n for (int i = 0; i < n - 1; ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n if (k <= 1) {\n return 0;\n }\n --k;\n for (int i = 1; i < weights.size(); ++i) {\n weights[i - 1] += weights[i];\n }\n weights.pop_back();\n\n if (k *... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n if (k <= 1) {\n return 0;\n }\n --k;\n for (int i = 1; i < weights.size(); ++i) {\n weights[i - 1] += weights[i];\n }\n weights.pop_back();\n\n if (k *... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n if (k <= 1) {\n return 0;\n }\n --k;\n for (int i = 1; i < weights.size(); ++i) {\n weights[i - 1] += weights[i];\n }\n weights.pop_back();\n\n if (k *... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& nums, int k) {\n int n=nums.size();\n for(int i=0;i<n-1;i++)\n nums[i]+=nums[i+1];\n nums.pop_back();\n sort(nums.begin(), nums.end());\n long long ans=0;\n for(int i=0;i<k-1;i++)\n ans+... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\n // int n;\n // long long rec(int i,int parts,vector<vector<long long>> &dp,vector<int> &a){\n // // base case ;\n // if(parts==0){\n // return a[i]+a[n-1];\n // }\n // // prune;\n // if(i >= n-1 && parts > 0){\n // return 1e1... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n // Time: O(n)\n // Space: O(n)\n long long putMarbles(vector<int>& weights, int k) {\n int n = weights.size();\n if (n == k) {\n return 0;\n }\n // splitting the array into k subarrays. it needs k-1 splittig points\n // the ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int> A, int k) {\n long long res = 0, n = A.size() - 1;\n for (int i = 0; i < n; ++i)\n A[i] += A[i + 1];\n A.pop_back();\n for (int k : A)\n cout << k << ' ';\n sort(A.begin(), A.end());\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n\n int n=weights.size();\n vector<int> sum(n-1);\n for(int i=0;i<n-1;i++)\n sum[i] = weights[i]+weights[i+1]; \n sort(sum.begin(), sum.end());\n\n long max=0,min=0;\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n int n=weights.size();\n int m=n-1;\n vector<int>pair(m,0);\n for(int i=0;i<m;i++)\n {\n pair[i]=weights[i]+weights[i+1];\n }\n sort(pair.begin(),pair.end());\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n const int n = weights.size();\n vector<int> pairs;\n\n pairs.reserve(n-1);\n\n for (int i = 1;i < n;i++) {\n pairs.push_back(weights[i]+weights[i-1]);\n }\n\n sort(pairs... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n int n=weights.size();\n int m=n-1;\n vector<int>pair(m,0);\n for(int i=0;i<m;i++)\n {\n pair[i]=weights[i]+weights[i+1];\n }\n sort(begin(pair),end(pair));\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n int n = weights.size();\n if (k == 1 || k == n) {\n return 0; // All marbles in one bag or each marble in separate bags.\n }\n\n vector<int> pair_sums(n - 1);\n for (int i = 0;... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "auto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\nclass Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n const int n = weights.size();\n vector<int> pairs;\n\n pairs.reserve(n-1);\n\n for (... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "auto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\nclass Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n const int n = weights.size();\n vector<int> pairs;\n\n pairs.reserve(n-1);\n\n for (... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n int n = weights.size();\n if(n==1) return 0;\n vector<long long> adj(n-1);\n for(int i=0; i<n-1; i++){\n adj[i] = (long long)weights[i] + (long long)weights[i+1];\n }\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n int n = weights.size();\n if (k == 1 || k == n) {\n return 0;\n }\n vector<long long> pairSums(n - 1);\n for (int i = 0; i < n - 1; i++) {\n pairSums[i] = static_cas... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& wt, int k) {\n int n = wt.size();\n vector<long long> pr_sum(n - 1);\n for (int i = 0; i < n - 1; i++) {\n pr_sum[i] = (long long)wt[i] + (long long)wt[i + 1];\n }\n sort(pr_sum.begin(), pr_sum.en... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& w, int k) {\n int n=w.size()-1;\n vector<long long>v(n);\n for(int i=0;i<n;i++){\n v[i]=(w[i]+w[i+1]);\n }\n sort(v.begin(),v.end());\n long long maxi=0;\n long long mini=0;\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 0 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n int n = weights.size();\n vector<long long> ps(n-1);\n for(int i =0 ; i<n-1 ; i++){\n ps[i] = weights[i] + weights[i+1];\n }\n long long maxsum = 0 ;\n long long minsum ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 1 | {
"code": "class Solution {\n public:\n long long putMarbles(vector<int>& weights, int k) {\n // To distribute marbles into k bags, there will be k - 1 cuts. If there's a\n // cut after weights[i], then weights[i] and weights[i + 1] will be added to\n // the cost. Also, no matter how we cut, weights[0] and ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 1 | {
"code": "class Solution {\n public:\n long long putMarbles(vector<int>& weights, int k) {\n // To distribute marbles into k bags, there will be k - 1 cuts. If there's a\n // cut after weights[i], then weights[i] and weights[i + 1] will be added to\n // the cost. Also, no matter how we cut, weights[0] and ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 1 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& w, int k) {\n vector<int> ans;\n long a=0;\n long b=0;\n int n=w.size();\n for(int i=0;i<n-1;i++)\n {\n ans.push_back(w[i]+w[i+1]);\n }\n sort(ans.begin(),ans.end());\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 1 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n int n = weights.size();\n if (k == 1 || n == k) return 0;\n vector<int> candidates;\n for (int i = 0; i < n-1; i++)\n {\n candidates.push_back(weights[i] + weights[i+1]);\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 1 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n int n = weights.size();\n if (k == 1) return 0; // If k=1, there's no splitting, so the difference is 0.\n\n // Step 1: Calculate the pair costs (weights[i] + weights[i+1])\n vector<int> pairCost... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 1 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n int n = weights.size();\n if(k == 1 || n == k) return 0; //Edge case (Trivial ones)\n\n vector<int> candidates;\n for(int i = 0; i < n-1; i++){\n candidates.push_back(weights[i] + wei... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 1 | {
"code": "class Solution {\npublic:\nstatic const int N = 1e5 + 10;\n\n long long int A[N];\n\n long long putMarbles(vector<int>& weights, int k) {\n int n = weights.size(); vector<int> vec;\n for (int i = 0; i + 1 < n; i++) {\n vec.push_back(weights[i] + weights[i + 1]);\n }\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 1 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n vector<int>w(weights.size()-1,0);\n for(int i=0;i<weights.size()-1;i++){\n w[i]=weights[i]+weights[i+1];\n }\n long long ans=0;\n long long ans1=0;\n\n priority_queue<... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 2 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n int n = weights.size();\n priority_queue<int> minHeap;\n priority_queue<int, vector<int>, greater<int>> maxHeap;\n\n for(int i = 0; i < n - 1; ++i){\n minHeap.push(weights.at(i) + wei... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 2 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n int n = weights.size();\n if(n <= 2) return 0;\n\n priority_queue<int> maxhp;\n priority_queue<int, vector<int>, greater<int>> minhp;\n long long maxi = 0, mini = 0;\n for(int i=0;... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 2 | {
"code": "class Solution {\n long long putMarbles(vector<int>& weights, int k, bool isMaximum) {\n long long score = 0;\n priority_queue<int> PQ;\n for (int i = 0; i < weights.size() - 1; i++) {\n int cost = weights[i] + weights[i + 1];\n PQ.push(isMaximum ? -cost: cost)... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 2 | {
"code": "class Solution{\npublic://The universe is alive because we are alive. It becomes more like us by interacting with it\n long long putMarbles(vector<int>& weights, int k){\n int n=weights.size();\n priority_queue<int> pq1;\n priority_queue<int,vector<int>,greater<int>> pq2;\n f... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 2 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n priority_queue<int> maxHeap, minHeap;\n\n for(int i = 0; i < weights.size() - 1; i++) {\n maxHeap.push(weights[i] + weights[i+1]);\n minHeap.push(-(weights[i] + weights[i+1]));\n\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 2 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n if (k==1) return 0;\n int n=weights.size();\n long long maxs=weights[0]+weights[n-1],mins=maxs;\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 2 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n if (k==1) return 0;\n int n=weights.size();\n long long maxs=weights[0]+weights[n-1],mins=maxs;\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 2 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n vector<long long> v;\n for (int i = 0; i < (int)weights.size() - 1; i++) {\n v.push_back(weights[i] + weights[i + 1]);\n }\n long long front = 0,back = 0;\n sort(v.begin(), v.e... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 2 | {
"code": "#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\nclass Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n int n = weights.size();\n \n // Base case: if k == 1, all marbles are in one bag\n if (k == 1) return 0;\n\n // Vector t... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 2 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n vector<long long> v;\n int n = weights.size();\n for(int i = 0; i < n-1; i++) {\n v.push_back(weights[i] + weights[i+1]);\n }\n\n if(v.size()) sort(v.begin(), v.end());\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 3 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& nums, int k) \n {\n priority_queue<int> max_pq;\n priority_queue<int,vector<int>,greater<int>> min_pq;\n for(int i=1;i<nums.size();i++)\n {\n max_pq.push(nums[i-1]+nums[i]);\n min_pq.push(n... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 3 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& wei, int k) {\n vector<long long>pre;\n for(int i=1;i<wei.size();i++){\n pre.push_back(wei[i]+wei[i-1]);\n }\n sort(pre.begin(),pre.end());\n long long ans=0;\n for(int i=0;i<k-1;i++){\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 3 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n long long minans = weights[0] + weights[weights.size()-1];\n long long maxans = minans;\n priority_queue<int> pqmax;\n priority_queue<int, vector<int>, greater<int>> pqmin;\n for(int i=1;... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 3 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n priority_queue<int,vector<int> >pq1;\n priority_queue<int,vector<int> ,greater<int> >pq2;\n\n for(int i=1;i<weights.size();i++)\n {\n int sum = weights[i]+weights[i-1];\n p... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 3 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n long long minans = weights[0] + weights[weights.size()-1];\n long long maxans = minans;\n priority_queue<int> pqmax;\n priority_queue<int, vector<int>, greater<int>> pqmin;\n for(int i=1;... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 3 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n if (k == 1 || k == weights.size()) {\n return 0;\n }\n vector<long long> arr;\n for (int i... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 3 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n int n=weights.size();\n long long minScore=weights[0]+weights[n-1];\n long maxScore=minScore;\n if(k==1 || k==n) return 0;\n priority_queue<int,vector<int>,greater<int>> minHeap;\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 3 | {
"code": "class Solution {\npublic:\n\tlong long putMarbles(std::vector<int>& weights, int k) {\n\t\tstd::vector<long long> cuts;\n\t\tfor (int i = 1; i < weights.size(); i++)\n\t\t{\n\t\t\tcuts.push_back(weights[i - 1] + weights[i]);\n\t\t}\n\t\tstd::stable_sort(cuts.begin(), cuts.end());\n\t\tlong long maxs = 0, m... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 3 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& wt, int k) {\n int n = wt.size();\n long long mx = wt[0] + wt[n-1];\n long long mn = wt[0] + wt[n-1];\n vector<long long> adj;\n for(int i = 0;i < n-1;i++){\n adj.push_back(wt[i]+wt[i+1]);\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 3 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n int n = weights.size();\n k--;\n long long ans1 = 0,ans2 = 0;\n priority_queue<long long> pq1;\n priority_queue<long long,vector<long long>,greater<long long>> pq2;\n for(int i = 0... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 3 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n priority_queue<long long,vector<long long>,greater<long long>> min_pq;\n priority_queue<long long> max_pq;\n int n=weights.size();\n for(int i=0;i<n-1;i++)\n {\n max_pq.push(we... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 3 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) \n {\n #define ll long long\n priority_queue<ll>pq1;\n int n = weights.size();\n auto cmp=[&](ll &a,ll &b)\n {\n return b<a;\n };\n priority_queue<ll,vector<ll>,de... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 3 | {
"code": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) \n {\n #define ll long long\n priority_queue<ll>pq1;\n int n = weights.size();\n priority_queue<ll,vector<ll>,greater<ll>>pq2;\n k--;\n for(int i=0;i<n-1;i++)\n {\n ... |
2,681 | <p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p>
<p>Divide the marbles into the <code>k</code> bags according to the fo... | 3 | {
"code": "class Solution {\n typedef long long ll;\npublic:\n ll putMarbles(vector<int>& weights, int k) {\n auto pq = priority_queue<ll>();\n ll lo, hi; lo = hi = { weights.front() + weights.back() };\n for (int i = 0; i < weights.size() - 1; i++) pq.push(weights[i] + weights[i + 1]);\n ... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "class Solution {\npublic:\n\n int head[50010], to[50010], next[50010];\n int in[50010], rec[50010];\n int cnt, ans;\n queue<int> q;\n\n void init(){\n ans = 0;\n cnt = 1;\n memset(head, 0, sizeof(head));\n memset(in, 0, sizeof(in));\n memset(rec, 0, sizeof(... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "static const auto Initialize = []{\n ios::sync_with_stdio(false); cin.tie(nullptr);\n return nullptr;\n}();\n\nint indegrees[50000], maximumTime[50000];\nvector <int> gotos[50000];\n\nclass Solution {\npublic:\n int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {\n i... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "static const auto Initialize = []{\n ios::sync_with_stdio(false); cin.tie(nullptr);\n return nullptr;\n}();\n\nint indegrees[50000], maximumTime[50000];\narray<vector <int>,50000> gotos;\n\nclass Solution {\npublic:\n int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {\n ... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "static const auto Initialize = []{\n ios::sync_with_stdio(false); cin.tie(nullptr);\n return nullptr;\n}();\n\nint indegrees[50000]={0}, maximumTime[50000];\narray<vector <int>,50000> gotos;\n\nclass Solution {\npublic:\n int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {\... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "static const auto Initialize = [] {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n return nullptr;\n}();\n\nint indegrees[50000] = {0}, maximumTime[50000];\narray<vector<int>, 50000> gotos;\n\nclass Solution {\npublic:\n int minimumTime(int n, vector<vector<int>>& relations, vector<int>& ... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "#include <iostream>\n#include <queue>\n#include <vector>\n#include <cmath>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <algorithm>\n#include <climits>\n#include <stack>\n#include <set>\n#include <map>\n#include <list>\n#include <cassert>\n#include <unordered_map>\n#include <numeric... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "#include <iostream>\n#include <queue>\n#include <vector>\n#include <cmath>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <algorithm>\n#include <climits>\n#include <stack>\n#include <set>\n#include <map>\n#include <list>\n#include <cassert>\n#include <unordered_map>\n#include <numeric... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "#include <iostream>\n#include <queue>\n#include <vector>\n#include <cmath>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <algorithm>\n#include <climits>\n#include <stack>\n#include <set>\n#include <map>\n#include <list>\n#include <cassert>\n#include <unordered_map>\n#include <numeric... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "#include <iostream>\n#include <queue>\n#include <vector>\n#include <cmath>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <algorithm>\n#include <climits>\n#include <stack>\n#include <set>\n#include <map>\n#include <list>\n#include <cassert>\n#include <unordered_map>\n#include <numeric... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "#include <iostream>\n#include <queue>\n#include <vector>\n#include <cmath>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <algorithm>\n#include <climits>\n#include <stack>\n#include <set>\n#include <map>\n#include <list>\n#include <cassert>\n#include <unordered_map>\n#include <numeric... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "#include <iostream>\n#include <queue>\n#include <vector>\n#include <cmath>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <algorithm>\n#include <climits>\n#include <stack>\n#include <set>\n#include <map>\n#include <list>\n#include <cassert>\n#include <unordered_map>\n#include <numeric... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "// ^\n// | Course i\n// 6 |\n// 5 | >--------------* 0 dependent courses (=terminating course)\n// 4 | >-----------<\n// 3 | #--------< 0 predrecessors\n// 2 | #-----< 0 predrecessors\n// 1 | #--< 0 predrecessors\n// +---------------------------------------> Month\n// ... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "// ^\n// | Course i\n// 6 |\n// 5 | >--------------* 0 dependent courses (=terminating course)\n// 4 | >-----------<\n// 3 | #--------< 0 predrecessors\n// 2 | #-----< 0 predrecessors\n// 1 | #--< 0 predrecessors\n// +---------------------------------------> Month\n// ... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "class Solution {\npublic:\n int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {\n // prev -> next\n int indeg[n + 1];\n memset(indeg, 0, sizeof(indeg));\n vector<int>v[n+1];\n for (int i = 0; i < relations.size(); i++) {\n int from = ... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "class Solution {\npublic:\n int ans=0;\n queue<int>q;\n vector<int>adj[50005];\n int time2[50005];\n int in[50005];\n void topo(vector<int>& time){\n while (!q.empty()){\n int nod=q.front();\n time[nod-1]+=time2[nod-1];\n ans=max(ans,time[nod-1]);\n... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "class Solution {\npublic:\n int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {\n vector<int> edg(n+1,0);\n vector<int> adj[n+1];\n for(int i=0;i<relations.size();i++){\n adj[relations[i][0]].push_back(relations[i][1]);\n edg[relations[i... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "class Solution {\npublic:\n int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {\n vector<int>adj[n];\n int indegree[n];\n memset(indegree,0,sizeof(indegree));\n for(int i =0 ; i < relations.size(); i++){\n adj[relations[i][0]-1].push_back(re... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "class Solution {\npublic:\n int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {\n if(n==1 || relations.size()==0){\n int sum=0;\n for(int i=0;i<n;i++){\n sum=max(sum,time[i]);\n }\n return sum;\n }\n ... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "class Solution {\npublic:\n int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) \n {\n vector<int> adj[n];\n vector<int> InDeg(n,0);\n vector<int> timeTaken(n,0);\n queue<int> q;\n\n for(int i = 0 ; i < relations.size();i++)\n {\n ... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "class Solution {\npublic:\n int minimumTime(int n, vector<vector<int>>& A, vector<int>& time) {\n vector<int> adj[n+1];\n vector<int> ind(n+1,0);\n for(auto& v: A) {\n adj[v[0]].push_back(v[1]);\n ind[v[1]]++;\n }\n queue<int> q;\n int ans=... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "class Solution {\npublic:\n int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) \n {\n //create adj list\n vector<int>adj[n];\n\n for(int i=0; i<relations.size(); i++)\n {\n //convert to 0 based indexing\n adj[relations[i][0] - 1... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "class Solution {\npublic:\n int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {\n //adjacency list\n vector<int>adj[n];\n vector<int>indeg(n,0);\n //create 0 based adj list and calculate indegree\n for(int i=0;i<relations.size();i++)\n {\... |
2,176 | <p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevC... | 0 | {
"code": "class Solution {\npublic:\n int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {\n vector<int>adj[n];\n vector<int>InDeg(n,0);\n for(int i=0;i<relations.size();i++)\n {\n adj[relations[i][0]-1].push_back(relations[i][1]-1);\n InDeg... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.