id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n int steps = 0;\n queue<int> q;\n q.push(n);\n unordered_set<int> vis;\n \n while(! q.empty()){\n int size = q.size();\n while(size--){\n int front = q.front();\n ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n\n std::queue<int> q; \n q.push(n);\n\n int count = 0;\n while (!q.empty())\n {\n std::unordered_set<int> seen;\n int level_size = q.size();\n for (int i=0; i<level_size; ++i)\n ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n\n std::queue<int> q; \n q.push(n);\n\n int count = 0;\n while (!q.empty())\n {\n std::unordered_set<int> seen;\n int level_size = q.size();\n for (int i=0; i<level_size; ++i)\n ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int getNums(vector<int>& sq_list, set<int>rst, int n, int cnt)\n {\n set<int> tem_rst;\n for(auto it=rst.begin();it!=rst.end();it++)\n {\n if(*it==n)\n {\n return cnt+1;\n }\n }\n\n for(auto...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n std::unordered_set<int> visited;\n std::queue<int> red;\n \n int duzina = std::sqrt(n);\n std::vector<int> neighbors(duzina);\n for(int i=1; i<=duzina; i++) {\n neighbors[i - 1] = i * i; \n ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n queue<pair<int, int>> q; \n q.push({n, 0}); \n while (!q.empty()) {\n auto[x, lvl] = q.front(); q.pop(); \n for (int i=1; i<=sqrt(x); ++i) {\n int y = x-i*i; \n if (!y) return lvl+1; \n if (y>0) q.push({y, lvl+1}...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n vector<int> squares;\n \n for (int i = 1; i*i <= n; ++i) {\n int square = i*i;\n \n if (square == n) {\n return 1;\n }\n \n squares.push_back(square...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n vector<int> squares;\n for(int i = 1; i <= n; i++)\n {\n if(isPerfectSquare(i)) squares.push_back(i);\n }\n \n queue<pair<int, int>> sums;\n sums.push({0, 0});\n \n while(!sums...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n for (int i = 1; i * i <= n; ++i) {\n squares.insert(i * i);\n }\n\n dp.push({n, 0});\n\n return bfs();\n }\nprivate:\n set<int> squares;\n queue<pair<int, int>> dp;\n\n int bfs() {\n while (!d...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int bfs(vector<int>p,int t){\n queue<pair<int,int>>q;\n q.push(make_pair(0,0));\n unordered_set<int>s;\n while(!q.empty()){\n pair<int,int> k=q.front();\n q.pop();\n for(auto i:p){\n int sum=k.first+i...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\nint funct(int ind,vector<int>& a,int count,int sum,int n){\n if(sum==n){\n return count;\n }\n \n if(ind==a.size()||sum>n){\n return INT_MAX;\n }\n \n \n return min(funct(ind,a,count+1,sum+a[ind],n),funct(ind+1,a,count,sum,n));\n}\n\n\n in...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n vector<int> vis(n+1,0);\n queue<pair<int,int>> q;\n q.push({n,0});\n while(!q.empty()){\n int num = q.front().first;\n int steps = q.front().second;\n q.pop();\n for(int i=1; i*i...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int solve(int num , vector<int>& dp , vector<int>& nums){\n if(num==0){\n return 0;\n }\n if(dp[num]!=-1){\n return dp[num];\n }\n int ans = INT_MAX;\n for(int i=0 ; i<nums.size() ;i++){\n if (nums[i]<...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int solve(int n){\n queue<pair<int,int>> bfs;\n vector<bool> vist(10000, false);\n vector<int> squares;\n for(int i=1; i<=100; i++){\n squares.push_back(i*i);\n }\n \n bfs.push({0,0});\n while(!bfs.empty()){\...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n queue<pair<int, int>> q;\n q.push({n, 0});\n int node = n;\n int dist = 0;\n vector<int> visited(n+1, 0);\n while(!q.empty()){\n node = q.front().first;\n dist = q.front().second;\n ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n vector<int> squares;\n int i = 1;\n while (i <= n / i) {\n squares.push_back(i * i);\n ++i;\n }\n\n queue<tuple<int, int, int>> q;\n q.push(make_tuple(0, n, squares.size() - 1));\n ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n vector<int> squares;\n int i = 1;\n while (i * i <= n) {\n squares.push_back(i * i);\n ++i;\n }\n\n queue<tuple<int, int, int>> q;\n q.push(make_tuple(0, n, squares.size() - 1));\n ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int solve(int n){\n vector<bool> visit(100005, false);\n //vector<pair<int, int>> visit(100005);\n vector<int> squares;\n for(int i=1; i<=100; i++) squares.push_back(i*i);\n\n queue<pair<int, int>> bfs;\n for(int i=0; i<100; i++){\n ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n vector<int> lessThanN;\n for(int i = 0; i <= n; i++) {\n auto sq = i * i;\n if(sq < n) {\n lessThanN.push_back(sq);\n } else if (sq == n) {\n return 1;\n }\n ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int minNumber(int remain, int val, int nWays, vector<int>& dp)\n {\n if (remain < 0 || val == 0) {\n return INT_MAX;\n }\n\n if (remain == 0) {\n return nWays;\n }\n\n if (dp[remain] != INT_MAX) {\n return...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int minNumber(int remain, int val, int nWays, vector<int>& dp)\n {\n if (remain < 0 | val == 0) {\n return INT_MAX;\n }\n\n if (remain == 0) {\n return nWays;\n }\n\n if (dp[remain] != INT_MAX) {\n return ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int minNumber(int remain, int val, int nWays, vector<int>& dp)\n {\n if (remain < 0 | val == 0) {\n return INT_MAX;\n }\n\n if (remain == 0) {\n return nWays;\n }\n\n if (dp[remain] != INT_MAX) {\n return ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int minNumber(int remain, int val, int nWays, vector<int>& dp)\n {\n if (remain < 0 | val == 0) {\n return INT_MAX;\n }\n\n if (remain == 0) {\n return nWays;\n }\n\n if (dp[remain] != INT_MAX) {\n return ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int minNumber(int remain, int val, int nWays, vector<int>& dp)\n {\n if (remain < 0 | val == 0) {\n return INT_MAX;\n }\n\n if (remain == 0) {\n return nWays;\n }\n\n if (dp[remain] != INT_MAX) {\n return ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int f(int n,vector<vector<int>>&dp,vector<int>v,int i){\n if(n==0||i==0)\n return n;\n if(dp[i][n]!=-1) return dp[i][n];\n int notPick=f(n,dp,v,i-1);\n int pick=1e6;\n if(n>=v[i])\n pick= n/v[i] + f(n - (n/v[i])*v[i],dp,v,i); \...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int f(int ind,int total,vector<int>&v,vector<vector<int>>&dp)\n {\n if(ind==0)\n {\n if(total%v[ind]==0)\n return total/v[ind];\n return INT_MAX-1;\n }\n if(dp[ind][total]!=-1)\n return...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int solve(int i, int n, vector<int>& sq, vector<vector<int>>& dp) {\n if (n == 0)\n return 0; \n if (i >= sq.size() || n < 0)\n return INT_MAX; \n if (dp[i][n] != -1) return dp[i][n];\n\n int take = solve(i, n - sq[i], sq, d...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int helper(int root, int target, vector<vector<int>>& dp){\n if(target == 0) return 0;\n if(root < 1) return 1e5;\n\n if(dp[root][target] != -1) return dp[root][target];\n\n int take = INT_MAX, not_take = INT_MAX;\n \n if((root * root...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int helper(int root, int target, vector<vector<int>>& dp){\n if(target == 0) return 0;\n if(root < 1) return 1e5;\n\n if(dp[root][target] != -1) return dp[root][target];\n\n int take = INT_MAX, not_take = INT_MAX;\n \n if((root * root...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n\n long long count(int n, int a, vector <vector <int>> &dp)\n { \n if(n == 0)\n return 1;\n\n if(a < 1 && n != 0)\n return INT_MAX;\n\n if(dp[a][n] != -1)\n return dp[a][n];\n \n if(n >= a*a)\n return dp[a][n] ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n if(n==1)\n return 1;\n vector<int>a;\n int i=1,mini=20000,j;\n while(i*i<n){\n a.push_back(i*i);\n i++;\n }\n if(i*i==n)\n return 1;\n vector<vector<int>>dp(...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "\n\nclass Solution {\npublic:\n\n int f(int i, int sum, int tar, vector<int>&a, vector<vector<int>>&dp){\n if(sum == tar) return 0;\n if(sum > tar) return 1e9;\n if(i == a.size()) return 1e9;\n\n if(dp[i][sum] != -1) return dp[i][sum];\n int tk = 1 + f(i, sum+a[i], tar...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n// int dp[10000+1][10000+1];\nbool f(int n){\n int b=sqrt(n);\n if(b*b==n)return true;\n return false;\n}\nint func(int n,int sum,int m, vector<vector<int>>&dp){\n if(sum>m)return 1e9;\n if(sum==m)return 0;\n if(n==0)return 1e9;\n if(dp[n][sum]!=-1)return dp[...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int f(int n,int t,vector<vector<int>>& dp){\n \n if(t==0){\n return 0;\n }\n \n if(n==0){\n return INT_MAX;\n }\n \n if(dp[n][t]!=-1){\n return dp[n][t];\n }\n \n int...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n vector<int>v;\n bool isPerfectSquare(int n){\n int s = sqrt(n);\n return (n==(s*s));\n }\n int solve(int i,int n,vector<vector<int>>&dp){\n if(i>=v.size() or n<0){\n return 1e9;\n }\n if(n==v[i]){\n return 1;\n...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n vector<int>v;\n bool isPerfectSquare(int n){\n int s = sqrt(n);\n return (n==(s*s));\n }\n int solve(int i,int n,vector<vector<int>>&dp){\n if(i>=v.size() or n<0){\n return 1e9;\n }\n if(n==v[i]){\n return 1;\n...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int solve(int ind,int tar,vector<long>& arr,vector<vector<int>>&dp){\n int n=arr.size();\n if(tar==0) return 1;\n if(ind==n-1){\n if(tar%arr[ind]==0) return tar/arr[ind];\n else return 1e9;\n }\n if(dp[ind][tar]!=-1) re...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n \n int helper(vector<int>& vec, int sum, int i, int** dp) {\n\n if (sum == 0) return 0;\n if (sum < 0) return INT_MAX - 1;\n if (i < 0) return INT_MAX - 1;\n if (dp[i][sum]!=-1)\n return dp[i][sum];\n\n int k = 1 + helper(vec, ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n \n int helper(vector<int>& vec, int sum, int i, int** dp) {\n\n if (sum == 0) return 0;\n if (sum < 0) return INT_MAX - 1;\n if (i < 0) return INT_MAX - 1;\n if (dp[i][sum]!=-1)\n return dp[i][sum];\n\n int k = 1 + helper(vec, ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "#define LARGE 9999\nclass Solution {\npublic:\n int numSquares(int n) {\n vector<int> mem (n+1, LARGE);\n vector<int> nextMem (n+1, LARGE);\n for (int i = 0; i < mem.size(); i++){\n mem[i] = i;\n }\n for (int level = 2; level * level <= n; level++){\n ...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n vector<int> squares;\n int current(1);\n while(pow(current, 2)<=n) {\n squares.push_back(current*current);\n current++;\n }\n vector<int> prev(n+1, INT_MAX/2);\n prev[0] = 0;\n fo...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n \n int numSquares(int n) {\n int t = sqrt(n);\n vector<int> coins(t,0);\n for(int i=1;i<=t;i++){\n coins[i-1]=i*i;\n }\n vector<vector<int>> dp(t,vector<int>(n+1,0));\n for(int i=1;i<=n;i++){\n if(i%coins[0]==...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n\nint fun(int i, vector<int>& nums, int n, vector<vector<int>>& dp) {\n if (n == 0) return 0;\n if (n < 0) return 1e8;\n if (i < 0) return 1e8;\n if (dp[i][n] != -1) return dp[i][n];\n\n int take = 1e8;\n if (nums[i] <= n) take = 1 + fun(i, nums, n - nums[i], dp...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int helper(vector<int> &choices, int target) {\n int n = choices.size();\n vector<vector<int>> dp(n, vector<int>(target + 1, INT_MAX)); \n \n for (int j = 0; j <= target; j++) {\n dp[0][j] = j;\n }\n\n for (int i = 1; i < n...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\n private:\n bool isPerfect(int i) {\n int root = sqrt(i);\n return root * root == i;\n}\n\npublic:\n int numSquares(int n) {\n // if(n==1 ) return 1;\n vector<int>perfect;\n for(int i=1;i<=n;i++){\n if(isPerfect(i)){\n perfect....
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int f(int ind, int cnt, vector<int> &v, int rem,\n vector<vector<int>> &dp)\n {\n if(ind==0)\n {\n if(rem%v[0]==0)\n return rem/v[0];\n return INT_MAX;\n }\n if(rem==0)\n return cnt;\n if...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int f(int ind, int cnt, vector<int> &v, int rem,\n vector<vector<int>> &dp)\n {\n if(ind==0)\n {\n if(rem%v[0]==0)\n return rem/v[0];\n return INT_MAX;\n }\n if(rem==0)\n return cnt;\n if...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n int func(int ind,int target,vector<int>& arr,vector<vector<int>>& dp){\n if(ind==0){\n if(target%arr[0]==0) return target/arr[0];\n else return 1e9;\n }\n if(dp[ind][target]!=-1) return dp[ind][target];\n int notTake=func(ind-...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "class Solution {\npublic:\n\n int func(int ind, vector<int> &pfsq, int n,vector<vector<int>> &dp){\n if(ind < 0) return 1e9;\n\n\n if(dp[ind][n]!=-1) return dp[ind][n];\n\n if(n == 0) return 0;\n int t=1e9,nt=1e9;\n\n if(pfsq[ind]<= n){\n t = 1+func(ind, pfs...
279
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
3
{ "code": "\nclass Solution {\npublic:\n int solve(int i, vector<int>& arr, int sum, int target, vector<vector<int>>& dp) {\n if (sum == target) return 0;\n if (i >= arr.size() || sum > target) return 1e9;\n if (dp[i][sum] != -1) return dp[i][sum];\n int take = 1 + solve(i, arr, sum + a...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "class Solution {\npublic:\n static vector<vector<int>> construct2DArray(vector<int>& original, const uint16_t m, const uint16_t n) {\n vector<vector<int>>vtr;\n const uint16_t l = original.size();\n if(l == m*n){\n vtr.reserve(m);\n vtr.emplace_back(move(origin...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "#define NO_SAN __attribute__((no_sanitize(\"undefined\", \"address\", \"coverage\", \"thread\")))\n#define INL __attribute__((always_inline))\n\nclass Solution {\npublic:\n static vector<vector<int>> construct2DArray(vector<int> &original, const uint16_t m, const uint16_t n) INL NO_SAN {\n const ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n auto size = original.size();\n if (m * n != size) return {};\n auto answer = vector<vector<int>>{};\n answer.reserve(m);\n answer.emplace_back(move(original));\n ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "\n\nclass Solution {\npublic:\n static vector<vector<int>> construct2DArray(vector<int> &original, const uint16_t m, const uint16_t n) {\n const uint16_t l = original.size();\n vector<vector<int>> r;\n if (l == m * n) {\n r.reserve(m);\n r.emplace_back(move(or...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n vector<vector<int>> out;\n if (original.size() != m * n)\n return out;\n out.resize(m);\n for (int r = 0; r < m; ++r)\n {\n out[r].resize(n);...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "#include <vector>\n#include <algorithm>\n#include <iterator>\n\nclass Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n if (original.size() != m * n) {\n return {};\n }\n std::vector<std::vector<int>> output(m);\n f...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "class Solution {\npublic:\n static vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n const int sz=original.size();\n if (sz!=m*n) return {};\n vector<vector<int>> ans(m);\n for(int i=0; i<m; i++){\n ans[i].assign(original.begin()+i*n, or...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "class Solution {\npublic:\n static vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n const int sz=original.size();\n if (sz!=m*n) return {};\n vector<vector<int>> ans(m);\n for(int i=0; i<m; i++){\n ans[i].assign(original.begin()+i*n, or...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "class Solution {\npublic:\n static vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n const int sz=original.size();\n if (sz!=m*n) return {};\n vector<vector<int>> ans(m);\n for(int i=0; i<m; i++){\n ans[i].assign(original.begin()+i*n, or...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "class Solution {\npublic:\n static vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n const int sz=original.size();\n if (sz!=m*n) return {};\n vector<vector<int>> ans(m);\n for(int i=0; i<m; i++){\n ans[i].assign(original.begin()+i*n, or...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n if(m*n!=original.size()){\n return {};\n }\n vector<vector<int>> ans(m,vector<int> (n,0));\n for(int i=0;i<original.size();i++){\n int row=i/n;\n ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& org, int m, int n) {\n if(m*n!=org.size()) return {};\n int c=0;\n vector<vector<int>> ans(m,vector<int>(n));\n for(int x=0;x<m;x++)\n for(int y=0;y<n;y++)\n ans[x][y]=org[c++];\n ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n if (original.size() != m * n) {\n return {};\n }\n \n vector<vector<int>> result(m, vector<int>(n));\n \n for (int i = 0; i < original.size(); ++...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n if (original.size() != m * n) return {};\n vector<vector<int>> result(m, vector<int>(n));\n for (int i = 0; i < original.size(); ++i)\n result[i / n][i % n] = original[i]...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "class Solution {\npublic:\n Solution() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n }\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n if(m*n!=original.size()){\n return {};\n }\n else{\n ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n \n if(original.size() != (m*n)) return {};\n\n vector<vector<int>> ans(m, vector<int>(n));\n\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n a...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n int len = original.size();\n if (len != m * n) return {}; // return an empty 2D array\n vector<vector<int>> ans(m, vector<int>(n));\n int x = 0;\n for (int i = 0; ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
1
{ "code": "static const auto _ = []() -> int {\n std::ios::sync_with_stdio(false);\n std::cin.sync_with_stdio(false);\n std::cout.sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nconstexpr auto get_iota = [](){\n int const N = 4 * 10'000 + 1;\n st...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n int k {static_cast<int>(original.size())};\n if (n * m != k) return {};\n \n std::vector<int> offset(m, 0);\n std::iota(offset.begin(), offset.end(), 0);\nfor (int...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n int k {static_cast<int>(original.size())};\n if (n * m != k) return {};\n \n std::vector<int> offset(m, 0);\n std::iota(offset.begin(), offset.end(), 0);\n\n ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n if(original.size() != m*n){\n return {};\n }\n vector<vector<int>> ans(m, vector<int>(n));\n vector<int> initial(n, 0);\n for(int i = 0; i < original.si...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) \n {\n vector<vector<int>> result;\n \n if (original.size() != m * n)\n {\n return result;\n }\n \n result.reserve(m);\n\n for (int ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
1
{ "code": "static const auto _ = []() -> int {\n std::ios::sync_with_stdio(false);\n std::cin.sync_with_stdio(false);\n std::cout.sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nconstexpr auto get_iota = [](){\n int const N = 4 * 10'000 + 1;\n st...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
1
{ "code": "static const auto _ = []() -> int {\n std::ios::sync_with_stdio(false);\n std::cin.sync_with_stdio(false);\n std::cout.sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nconstexpr auto get_iota = [](){\n int const N = 4 * 10'000 + 1;\n st...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n vector<vector<int>>ans(m);\n if(n*m != original.size()) {\n vector<vector<int>>x;\n return x;\n }\n \n int k = 0;\n for(int i = 0;i < ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n vector<vector<int>> result(m);\n vector<vector<int>> arr;\n // arr[0] = vector<int>(1);\n // if(m==1 && n==1)\n // return arr[0][0] = original[0];\n \n ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n vector<vector<int>> v(m);\n if(original.size() != m * n)return {};\n for(int i = 0;i<m;i++){\n v[i].assign(original.begin() + i * n, original.begin() + (i + 1) * n);\...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n\n vector<vector<int>> ans(m);\n int k=0;\n if( m*n !=original.size())\n {\n return {};\n }\n\n for(int i=0;i<m;i++)\n { \n ans[...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n vector<vector<int>> ans(m);\n // vector<vector<int>> ans;\n int num_of_elements = original.size();\n // process only if size is equal to m*n\n if (num_of_elements ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n if (m * n != original.size()) {\n return {};\n }\n \n vector<vector<int>> res;\n for (int i = 0; i < m; i++) {\n res.push_back(vector<int>(or...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n \n vector<vector<int>> ans = {}; \n\n if(original.size() == m * n) {\n int j = -1;\n for(int i = 0; i < original.size(); i++) {\n if(i % n =...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n if ( original.size() != m * n) return {};\n vector<vector<int>> res;\n //res.reserve(m);\n auto it = original.begin();\n while (it != original.end()) {\n ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n int a=original.size();\n vector<vector<int>>ans;\n int j=0;\n if(a!=(m*n)){\n return ans;\n }\n vector<int>v1(n);\n for(int i=0;i<a;i++)...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n if(m * n != original.size())\n return {};\n vector<vector<int>> v;\n vector v1(n,1);\n for(int i = 0; i < m; i++){\n for(int j = 0; j < n; j++){\n ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n vector<vector<int>> ans;\n if( m*n != original.size()) return ans;\n vector<vector<int>> vec;\n vector<int> temp(n);\n int i=0,j=0;\n while( i<original.size...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& org, int m, int n) {\n if(org.size() != m*n) return {} ;\n if(m==1) return {org} ;\n\n int i=0 , tn;\n vector<int> temp ;\n vector<vector<int>> ans; \n while(m--) {\n tn = n...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n if (original.size() != m * n) return {};\n if (m == 1) return {original};\n\n vector<int> temp;\n vector<vector<int>> ans;\n temp.reserve(m);\n ans.reserve(...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\nvector<vector<int>> construct2DArray(vector<int> original, int m, int n)\n{\n if (m * n != original.size())\n return {};\n vector<vector<int>> res(m,vector<int>(n));\n int row = 0, col = 0;\n for (int i = 0; i < original.size(); i++)\n {\n\n res[row][...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\nvector<vector<int>> construct2DArray(vector<int> original, int m, int n)\n{\n if (m * n != original.size())\n return {};\n vector<vector<int>> res(m,vector<int>(n));\n int row = 0, col = 0;\n for (int i = 0; i < original.size(); i++)\n {\n\n res[row][...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n \n vector<vector<int>> ans;\n\n int total = original.size();\n\n if(total != m*n){\n return ans;\n }\n\n vector<int> v;\n\n for(int i=0; i...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n int len = original.size();\n vector<vector<int>> ans;\n if (m * n != len) {\n return ans;\n }\n\n vector<int> row;\n for (int i = 0; i < len; i++...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n){\n int sz=original.size();\n vector<vector<int>> ans;\n int cnt=0;\n vector<int> temp;\n if(m*n!=sz){\n return ans; \n }\n for(int val:origina...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n if (m * n != original.size())\n return {};\n vector<int> res;\n vector<vector<int>>ans;\n int c = 0;\n for (int i = 0; i < original.size(); i++) {\n ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n size_t size = original.size();\n\n if(size != (m * n))\n return {};\n \n vector<vector<int>> answer{};\n\n vector<int> temp{};\n\n for(int i{}; i...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n vector<vector<int>> ans;\n if(n*m!=original.size()){\n return ans;\n }\n vector<int> temp;\n int count =0;\n for(int val: original){\n ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n int k=0;\n vector<vector<int>> f;\n vector<int> v;\n if(m*n!=original.size()) return {};\n for(int i=0;i<original.size();i++){\n v.push_back(original[i]...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n int arr[m][n];\n vector<vector<int>> ans;\n if(m*n != original.size()) {return ans;}\n\n\n for(int i = 0; i < m; i++){\n for(int j = 0; j < n; j++){\n ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "#include <span>\nclass Solution \n{\npublic: \n auto construct2DArray(std::span<int32_t> original, int32_t rows, int32_t cols) \n {\n std::vector<std::vector<int32_t>> result(rows);\n for (auto& row : result)\n {\n row.reserve(cols);\n }\n\n if (rows * cols != original.size())\n ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n \n if(original.size()< m*n || original.size()> m*n){\n return {};\n }\n if(m==1){\n vector<vector<int>> v(m);\n v[0] = original;\n ...
2,132
<p>You are given a <strong>0-indexed</strong> 1-dimensional (1D) integer array <code>original</code>, and two integers, <code>m</code> and <code>n</code>. You are tasked with creating a 2-dimensional (2D) array with <code> m</code> rows and <code>n</code> columns using <strong>all</strong> the elements from <code>origi...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {\n int required_elements = m * n;\n if(required_elements != original.size()) {\n std::vector<std::vector<int>> empty;\n return empty;\n }\n\n int ptr =...