id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
0
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n int res = 0;\n sort(citations.begin(), citations.end(), greater<int>());\n for (int i = 0; i < citations.size(); i++) {\n if (i + 1 > citations[i]) {\n break;\n }\n res...
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
0
{ "code": "class Solution {\npublic:\n \n void sort(vector<int>& arr, int n){\n\n for(int i = 0; i < n - 1; i++){\n int maxIndex = i;\n\n for(int j = i + 1; j < n; j++){\n if(arr[j] > arr[maxIndex]){\n maxIndex = j;\n }\n ...
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
1
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n int ans=0;\n for(int i=1;i<=1000;i++) {\n int cnt=0;\n for(int j:citations)\n cnt+=(i<=j);\n if(i<=cnt)\n ans=i;\n }\n return ans;\n }\n};", ...
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
1
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n sort(citations.begin(),citations.end());\n reverse(citations.begin(),citations.end());\n int i;\n for(i=0;i<citations.size() && citations[i]>=(i+1);i++);\n return i;\n }\n};", "memory": "11100" }
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n map<int,int> ans;\n int n = citations.size();\n \n int totalval = 0;\n for(int i=0;i<n;i++){\n ans[citations[i]]++; \n totalval = totalval + 1;\n }\n int hind = n;\...
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
3
{ "code": "class Solution {\npublic:\n bool solve(int mid, vector<int> citations)\n {\n int count = 0;\n for(auto c:citations)\n {\n if(c>=mid)\n count++;\n }\n return count>=mid;\n }\n int hIndex(vector<int>& citations) {\n \n int...
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n map<int,int> ans;\n int n = citations.size();\n \n int totalval = 0;\n for(int i=0;i<n;i++){\n ans[citations[i]]++; \n totalval = totalval + 1;\n }\n int hind = n;\...
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n int count[1001];\n std::fill(count,count+1001,0);\n for (auto &it:citations) count[it]++;\n for (int i = 999 ; i >= 0 ; i--)\n {\n count[i] = count[i] + count[i+1];\n if (count[i] ...
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n int sz = citations.size();\n unordered_map<int, int> poses;\n for(auto i = 0; i < sz; ++i)\n if (!poses.contains(citations[i]))\n poses[citations[i]] = i;\n sort(citations);\n ...
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n priority_queue<int, vector<int>, greater<int>> pq;\n for(auto it:citations) pq.push(it);\n while(pq.size() && pq.top()<pq.size()) pq.pop();\n return pq.size();\n }\n};", "memory": "11900" }
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n //good approach\n int cnt[5005] = {0};\n\n for (int i = 0 ; i < citations.size(); i++) cnt[citations[i]]++;\n //cnt(i) = cnt(i) + cnt(i+1)\n\n for (int i = 5000 ; i >= 0; i--) {\n cnt[i] += c...
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n int n = citations.size();\n vector<int> freq(1001, 0);\n vector<int> suffix(1001, 0);\n \n for(auto c : citations){\n freq[c]++;\n }\n\n suffix[1000] = freq[1000];\n\n fo...
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n int n = citations.size();\n vector<int> prefix(1001);\n vector<int> prefixsum(1001);\n prefixsum[0] = n;\n sort(citations.begin(), citations.end());\n for (int i=0; i<n; i++) {\n prefi...
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n int n = citations.size();\n unordered_map<int, int> map;\n for(int i = 0; i < n; i++){\n if(!map.count(citations[i])){\n for(int j = 0; j < n; j++){\n if(citations[j] >= c...
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) \n {\n sort(citations.rbegin(), citations.rend());\n short res = 0;\n for(int ind = 0; ind < citations.size(); ++ind)\n {\n if(citations[ind] >= ind + 1)\n res++;\n else\n ...
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n sort(citations.rbegin(), citations.rend());\n int h = 0;\n while(h < citations.size() && citations[h] > h) {\n h++;\n }\n return h;\n }\n};", "memory": "12300" }
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n sort(citations.rbegin(), citations.rend());\n int size = citations.size();\n int ans = 0;\n while(ans < size && citations[ans] > ans) {\n ans++;\n }\n return ans;\n \n }\n};", ...
274
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper, return <em>the researcher&#39;s h-index</em>.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">defini...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n sort(citations.rbegin(), citations.rend());\n for (int i = 0; i < citations.size(); ++i) {\n if (citations[i] < (i+1)) {\n return i;\n }\n }\n return citations.size();\n ...
275
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper and <code>citations</code> is sorted in <strong>ascending order</strong>, return <em>the researcher&#39;s h-index</em>.</p> <p>According to t...
0
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& cit) {\n int n=cit.size();\n int lo=0,hi=n-1;\n int ans=0;\n while(lo<=hi){\n int mid = lo+(hi-lo)/2;\n if(cit[mid]>=(n-mid)){\n ans=(n-mid);\n hi=mid-1;\n }\n ...
275
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper and <code>citations</code> is sorted in <strong>ascending order</strong>, return <em>the researcher&#39;s h-index</em>.</p> <p>According to t...
0
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n\n int n=citations.size();\n int fans=0;\n\n int si=1;\n int ei=citations.size();\n\n while(si<=ei){\n int mid=(si+ei)/2;\n\n if(citations[n-mid]>=mid){\n fans=max(fa...
275
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper and <code>citations</code> is sorted in <strong>ascending order</strong>, return <em>the researcher&#39;s h-index</em>.</p> <p>According to t...
0
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n int n = citations.size();\n int res = 0;\n int lo=0, hi=n-1;\n int mid = lo + (hi-lo)/2;\n while(lo<=hi)\n {\n mid = lo + (hi-lo)/2;\n \n if(citations[mid] == n-m...
275
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper and <code>citations</code> is sorted in <strong>ascending order</strong>, return <em>the researcher&#39;s h-index</em>.</p> <p>According to t...
0
{ "code": "class Solution {\n public:\n int hIndex(vector<int>& citations) {\n const int n = citations.size();\n int l = 0;\n int r = n;\n\n while (l < r) {\n const int m = (l + r) / 2;\n if (citations[m] >= n - m)\n r = m;\n else\n l = m + 1;\n }\n\n return n - l;\n }...
275
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper and <code>citations</code> is sorted in <strong>ascending order</strong>, return <em>the researcher&#39;s h-index</em>.</p> <p>According to t...
0
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n if(citations.size()==0)return 0;\n if(citations.size()==1){\n if(citations[0]==0)return 0;\n else return 1;\n }\n int n=citations.size();\n int l=0,h=n-1;\n int ans=0;\n ...
275
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper and <code>citations</code> is sorted in <strong>ascending order</strong>, return <em>the researcher&#39;s h-index</em>.</p> <p>According to t...
0
{ "code": "class Solution {\npublic:\n int count(vector<int>& citations,int mid){\n int cnt=0;\n for(auto c:citations){\n if(c>=mid)\n ++cnt;\n }\n return cnt;\n }\n\n int hIndex(vector<int>& citations) {\n int st=0;\n int end=*max_element(cit...
275
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper and <code>citations</code> is sorted in <strong>ascending order</strong>, return <em>the researcher&#39;s h-index</em>.</p> <p>According to t...
2
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n int n=citations.size();\n int s=0;\n int e=n-1; \n int ans=0;\n while(s<=e){\n int mid=s+(e-s)/2;\n if(n-mid==citations[mid]){\n return citations[mid];\n }...
275
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper and <code>citations</code> is sorted in <strong>ascending order</strong>, return <em>the researcher&#39;s h-index</em>.</p> <p>According to t...
2
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n int n = citations.size(), ans = 0;\n int low = 0, high = n-1;\n while(low <= high) {\n int mid = low + (high - low)/2;\n if(citations[mid] >= n - mid) {\n ans = max(ans, n - mid);...
275
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper and <code>citations</code> is sorted in <strong>ascending order</strong>, return <em>the researcher&#39;s h-index</em>.</p> <p>According to t...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n int n = citations.size();\n vector<int> count(citations.back() + 1,0);\n for(auto &x : citations) {\n count[x]++;\n }\n for(int i = citations.back()-1;i >= 0;i--) {\n count[i] += c...
275
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper and <code>citations</code> is sorted in <strong>ascending order</strong>, return <em>the researcher&#39;s h-index</em>.</p> <p>According to t...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n vector<int> sum(1002);\n for(int i =0;i < citations.size();i++){\n ++sum[1];\n --sum[citations[i]+1];\n }\n for(int i=1;i<=1000;i++)\n sum[i]+=sum[i-1];\n int ans = 0;\n...
275
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper and <code>citations</code> is sorted in <strong>ascending order</strong>, return <em>the researcher&#39;s h-index</em>.</p> <p>According to t...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n sort(citations.begin(),citations.end());\n int n = citations.size();\n int ans = 0;\n for(int i = 0;i<n;i++){\n if(citations[i]>=(n-i)){\n return n-i;\n }\n }\n ...
275
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper and <code>citations</code> is sorted in <strong>ascending order</strong>, return <em>the researcher&#39;s h-index</em>.</p> <p>According to t...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n int n=citations.size();\n vector<int> ans(n+1,0);\n for(int c:citations){\n if(c>n) ans[n]++;\n else ans[c]++;\n }\n int count=0;\n for(int i=n;i>=0;i--){\n coun...
275
<p>Given an array of integers <code>citations</code> where <code>citations[i]</code> is the number of citations a researcher received for their <code>i<sup>th</sup></code> paper and <code>citations</code> is sorted in <strong>ascending order</strong>, return <em>the researcher&#39;s h-index</em>.</p> <p>According to t...
3
{ "code": "class Solution {\npublic:\n int hIndex(vector<int>& citations) {\n sort(citations.begin(),citations.end());\n int maxi=0,n=citations.size();\n for(int i=0;i<citations.size();i++){\n if(citations[i]>=n-i){\n maxi=max(maxi,n-i);\n }\n }\n ...
278
<p>You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.</p> <p>Suppose you have <code>n</code> version...
0
{ "code": "// The API isBadVersion is defined for you.\n// bool isBadVersion(int version);\n\nclass Solution {\npublic:\n int firstBadVersion(int n) {\n \n int left=1;\n int right=n;\n int ans;\n while(left<=right){\n int mid=left+(right-left)/2;\n \n ...
278
<p>You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.</p> <p>Suppose you have <code>n</code> version...
0
{ "code": "// The API isBadVersion is defined for you.\n// bool isBadVersion(int version);\n\nclass Solution {\npublic:\n long long firstBadVersion(int n) {\n long long low=1;\n long long high=n;\n long long ans=0;\n if(low<=high){\n while(low<=high){\n long long m...
278
<p>You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.</p> <p>Suppose you have <code>n</code> version...
0
{ "code": "// The API isBadVersion is defined for you.\n// bool isBadVersion(int version);\n\nclass Solution {\npublic:\n int firstBadVersion(int n) {\n int low = 1;\n int high = n;\n int firstBad = -1;\n while(low <= high){\n int mid = low + ((high - low)/2);\n\n ...
278
<p>You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.</p> <p>Suppose you have <code>n</code> version...
0
{ "code": "// The API isBadVersion is defined for you.\n// bool isBadVersion(int version);\n\nclass Solution {\npublic:\n int firstBadVersion(int n) {\n if(isBadVersion(1)) return 1;\n long good=1, bad=n;\n while(good < bad-1){\n long mid = (good+bad) /2;\n if(isBadVersio...
278
<p>You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.</p> <p>Suppose you have <code>n</code> version...
0
{ "code": "// The API isBadVersion is defined for you.\n// bool isBadVersion(int version);\n\nclass Solution {\npublic:\n int firstBadVersion(int n) {\n int low = 1, high = n, middle = (high - low) / 2 + low;\n while(low<high){\n if(!isBadVersion(middle)){\n low = middle+1;\...
278
<p>You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.</p> <p>Suppose you have <code>n</code> version...
0
{ "code": "// The API isBadVersion is defined for you.\n// bool isBadVersion(int version);\n\nclass Solution {\npublic:\n int firstBadVersion(int n) {\n int left=1,right=n;\n int ans=-1;\n while(left<=right){\n int mid=left+(right-left)/2;\n if(isBadVersion(mid)){\n ...
278
<p>You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.</p> <p>Suppose you have <code>n</code> version...
0
{ "code": "// The API isBadVersion is defined for you.\n// bool isBadVersion(int version);\n\nclass Solution {\npublic:\n int firstBadVersion(int n) {\n int left=1,right=n;\n int ans=-1;\n while(left<=right){\n int mid=left+(right-left)/2;\n if(isBadVersion(mid)){\n ...
278
<p>You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.</p> <p>Suppose you have <code>n</code> version...
1
{ "code": "class Solution {\npublic:\n int firstBadVersion(int n) {\n int left = 1, right = n;\n while (left < right) {\n int mid = left + (right - left) / 2;\n if (isBadVersion(mid)) {\n right = mid;\n } else {\n left = mid + 1;\n ...
278
<p>You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.</p> <p>Suppose you have <code>n</code> version...
1
{ "code": "// The API isBadVersion is defined for you.\n// bool isBadVersion(int version);\n\nclass Solution {\npublic:\n int firstBadVersion(int n) {\n long long l = 0, r = n;\n while(l <= r){\n int m = (l+r)/2;\n if(isBadVersion(m)){\n r = m-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>,...
0
{ "code": "class Solution {\npublic:\n bool issquare(int num) {\n if (num == 1) return true;\n int low = 1, high = num / 2;\n while (low <= high) {\n int mid = low + (high - low) / 2;\n long long square = static_cast<long long>(mid) * mid; // Avoid overflow\n 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>,...
0
{ "code": "class Solution {\npublic:\n int dp[10001];\n int numSquares(int n) {\n dp[0] = 0;\n for (int i = 1; i <= n; i++) {\n int res = INT_MAX;\n int k = sqrt(i);\n for (int j = 1 ; j <= k; j++) {\n res = min (res, 1 + dp[i - (j * j)]);\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>,...
0
{ "code": "class Solution {\nprivate:\n vector<int> sq;\n int dp[10001];\n static constexpr int INF = 10000000;\n\npublic:\n Solution() {\n for (int i = 1; i <= 100; ++i)\n sq.push_back(i * i);\n }\n\n int numSquares(int n) {\n dp[0] = 0;\n for (int i = 1; i <= n; ++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>,...
0
{ "code": "class Solution {\nprivate:\n vector<int> sq;\n int dp[10001];\n static constexpr int INF = 10000000;\n\npublic:\n Solution() {\n for (int i = 100; i > 0; --i)\n sq.push_back(i * i);\n }\n\n int numSquares(int n) {\n dp[0] = 0;\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>,...
2
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n vector<int> nums;\n for(int i = 1; i*i<=n;i++)\n {\n nums.push_back(i*i);\n }\n vector<int> dp(n+1,INT_MAX);\n dp[0] = 0;\n for(int i = 0; i < nums.size(); i++)\n {\n for(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>,...
2
{ "code": "class Solution {\npublic:\n int numSquares(int n) {\n vector<int>squares;\n vector<int>dp(n+1,n+1);\n dp[0]=0;\n if(n==1)return 1;\n for(int i=1;i<n;i++)\n {\n if(i*i>n)\n break;\n\n squares.push_back(i*i);\n }\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 int* table;\n int* ceilingSqr;\n int* squares;\n void recur(int n){\n table[n]=n-1;\n for(int i=ceilingSqr[n];i>-1;--i){\n table[n]=min(table[n], table[n-squares[i]]);\n if(table[n]==1) break;\n }\n table[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* table;\n int* ceilingSqr;\n int* squares;\n void recur(int n){\n table[n]=n;\n for(int i=ceilingSqr[n];i>-1;--i){\n table[n]=min(table[n], table[n-squares[i]]+1);\n }\n }\n int numSquares(int n) {\n int sz=(n+1)<<2;\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 std::queue<std::pair<int, int>> q;\n q.push({n, 0});\n vector<bool> visited(n + 1, false);\n\n while (!q.empty()) {\n auto [num, dist] = q.front();\n q.pop();\n\n if (num == 0)\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 std::queue<std::pair<int, int>> q;\n q.push({n, 0});\n vector<bool> visited(n + 1, false);\n\n while (!q.empty()) {\n auto [num, dist] = q.front();\n q.pop();\n\n if (num == 0)\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 long long dp[101][10001];\n long long func(int n,long long i,long long sum)\n { \n if(i*i>n){\n return 1e9;\n }\n if(sum==n)\n {\n return 0;\n }\n \n if(dp[i][sum]!=-1)return dp[i][sum];\n l...
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[201][10001];\n int rec(int ind,int target,vector<int>&a,int nn){\n if(ind==nn-1){\n if(target%a[nn-1]==0)return target/a[nn-1];\n else return 1e9;\n }\n if(dp[ind][target]!=-1)return dp[ind][target];\n int ntake...
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, vector<int> &arr,vector<int> &dp){\n if(n < 0){\n return INT_MAX;\n }\n if(n == 0){\n return 1;\n }\n\n //already existing case\n if(dp[n] != -1){\n return dp[n];\n }\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 solve(int n, vector<int> &arr,vector<int> &dp){ //is function se jo answer return ho rha hai...wo mera ek jyada ho rha hai..isliye jb mai is solution ka answer return krunga toh -1 krke krunga\n if(n < 0){\n return INT_MAX;\n }\n if(n == 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 struct Node\n {\n int value;\n int numPerfectSquares; \n int * compare; \n std::map<int,int> * cache;\n\n Node():value(0),numPerfectSquares(0),compare(nullptr),cache(nullptr)\n {\n\n }\n\n Node(int v, int n, int * cmp...
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 struct Node\n {\n int value;\n int numPerfectSquares; \n int * compare; \n std::map<int,int> * cache;\n\n Node():value(0),numPerfectSquares(0),compare(nullptr),cache(nullptr)\n {\n\n }\n\n Node(int v, int n, int * cmp...
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 {\nprivate:\n map<int,int> dp;\npublic:\n int numSquares(int n) {\n if(n<=3) return n;\n if(dp.count(n) > 0) return dp[n];\n int &ans = dp[n];\n ans = n;\n int low = max(1, (int) sqrt(n)-15);\n for(int i=sqrt(n); i>=low;i--) {\n ans ...
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 = sqrt(n); i > 0; i--) {\n squares.push_back(i * i);\n }\n queue<int> q;\n q.push(n);\n int size = 1;\n int count = 1;\n while (!q.empty()) {\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> results (n + 1, -1);\n int num = 1;\n vector<int> squares;\n set<int> seen;\n queue<pair<int, int>> visit;\n\n while (num * num <= n) {\n results[num * num] = 1;\n squares.pu...
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 // BFS\n vector<int> perfect_squares;\n for(int i = 0; i * i <= n; i++)\n {\n perfect_squares.push_back(i * i);\n }\n queue<int> q;\n q.push(n);\n int level = 0;\n while(!q.emp...
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 ios::sync_with_stdio(false);\n cin.tie(0);\n\n queue<int> q;\n unordered_set<int> visited;\n\n q.push(n);\n visited.insert(n);\n int step = 0;\n\n while(!q.empty()) {\n int qSize = q....
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 <= 0) return 0;\n std::queue<int> q;\n q.push(n);\n std::unordered_set<int> visited;\n visited.insert(n);\n int level = 0;\n while (!q.empty()) {\n ++level;\n int size = q.s...
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 ios::sync_with_stdio(false);\n cin.tie(0);\n \n queue<int> q;\n unordered_set<int> visited;\n\n q.push(n);\n visited.insert(n);\n int step = 0;\n\n while(!q.empty()) {\n int qS...
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 // Consider a tree with root value 0 and children whose values are\n // perfect squares up to greatest_square. Each children also has\n // children with the same perfect squares as values. We perform a\n // BFS on this tre...
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 // Consider a tree with root value 0 and children whose values are\n // perfect squares up to greatest_square. Each children also has\n // children with the same perfect squares as values. We perform a\n // BFS on this tre...
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 ans = 0;\n vector<int> dp(n+1,INT_MAX);\n vector<int> squares;\n for(int i = 1; i*i<= n; i++){\n squares.push_back(i*i);\n }\n unordered_set<int> vis;\n int m = squares.size();\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 std::set<int> sq;\n int lim = sqrt(n);\n for(int i = 1; i <= lim; ++i)\n sq.insert(i*i);\n\n int result = 0;\n std::unordered_set<int> qu;\n qu.insert(n);\n while( !qu.empty()){\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\n for(int i = 1;i*i<=n;i++){\n squares.push_back(i*i);\n }\n \n set<int> q;\n q.insert(n);\n\n int level = 0;\n\n while(!q.empty()){\n level++;\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 numSquares(int n) {\n vector<int> squareNums;\n for (int i = 1; i * i <= n; ++i) {\n squareNums.push_back(i * i);\n }\n int level = 1; \n set<int> myQueue; \n myQueue.insert(n);\n\n while (!myQueue....
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> dp;\n int numSquares(int n) {\n dp.resize(10001,0);\n for(int i = 1; i <= n; ++i)\n {\n if(dp[i] != 0){\n continue;\n }\n int mini = 10000;\n\n for(int j=1; j * j <= i; ++j)\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> dp(10001);\n dp[0] = 0;\n for (int i = 1 ; i <= n; i++) {\n dp[i] = 10000;\n for (int j = 0 ; j*j <= i; ++j) {\n dp[i] = min(dp[i], dp[i - (j*j)] + 1);\n }\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 {\n vector<int> memo;\npublic:\nSolution(){\n memo=vector<int>(1e4+1,-1);\n}\n int numSquares(int n) {\n if(n==0) return 0;\n int ans=1e9;\n if(memo[n]!=-1) return memo[n];\n for(int i=1;i*i<=n;i++){\n ans=min(ans,1+numSquares(n-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 vector<int> allSquares;\n vector<int> dp;\n int sz = 0;\n\n Solution() {\n int i = 1;\n // while(i*i <= 1e4) {\n // allSquares.push_back(i*i);\n // i++;\n // }\n // sz = allSquares.size();\n dp.resize(1e4+1, -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\n\n int solve(int n,vector<int>& dp) {\n if(n<=3)\n return n;\n int ans=INT_MAX;\n if(dp[n]!=-1)\n return dp[n];\n for(int i=1;i<=sqrt(n);i++)\n {\n int x=n;\n int sum=0;\n x-=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": "#include <array>\n#include <cstddef> // For std::size_t\n\n// constexpr function to calculate the square of a number\nconstexpr int square(int n) {\n return n * n;\n}\n\n// Generate an array of squares from 1 to 10000 at compile-time\nconstexpr std::array<int, 10000> generateSquareArray() {\n std::ar...
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<std::pair<int, int>> q;\n unordered_set<int> visited;\n\n q.push({n, 0});\n\n while (!q.empty()) {\n auto [num, steps] = q.front();\n q.pop();\n\n for (size_t i = 1; (i * i) <= n; ++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 numSquares(int n) {\n if(n == 10000)\n return 1;\n vector<int>dp;\n vector<int>sd(10001,INT_MAX);\n queue<int>q;\n for(int i=1;i<101;i++)\n {\n dp.push_back(i*i);\n sd[i*i] = 1;\n q.push...
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 std::optional<int> explore(int target, const set<int>& squares, unordered_map<int, int>& dp) {\n if(target < 0) return -1;\n if(squares.count(target))\n return 1;\n auto it = dp.find(target);\n if(it != dp.end()) return it->second == -1 ? std::op...
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 unordered_map<int,int>memo;\n int compute(int n){\n if(n == 0){\n return 0;\n }\n int p = sqrt(n);\n if(p*p==n)return 1;\n if(memo.find(n)!=memo.end())return memo[n];\n int ans = INT_MAX;\n for(int i = 1;i*i<=n;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 {\n std::unordered_map<int, int> leastNum;\n \n int helper(int n, const std::vector<int>& nums, int maxIdx)\n {\n if(auto itr = leastNum.find(n); itr != leastNum.end())\n return itr->second;\n \n int ans = 100000;\n for (auto i = maxIdx; 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 numSquares(int n)\n {\n cache[0] = INT_MAX;\n check(n, sqrt(n), 0);\n return cache[0];\n }\n\nprivate:\n unordered_map<int, int> cache;\n\n bool check(int num, int square, int depth)\n {\n if (!num)\n {\n cache[...
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 unordered_map<int, int> hmap;\n // for a given state of a number what all reductions that you can do on him and pass it forward\n int solve(int n)\n {\n if(n == 0)return 0;\n\n if(hmap.find(n) != hmap.end())return hmap[n];\n\n int result = INT_MA...
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<int> q;\n set<int> vis;\n q.push(n);\n int ans = 0;\n while (!q.empty()) {\n int s = q.size();\n for (int i = 0; i < s; i++) {\n int node = q.front(); q.pop();\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> listS;\n int i = 1;\n while(i * i <= n) {\n listS.push_back(i * i);\n i++;\n }\n int res = INT_MAX;\n stack<pair<int, int>> q;\n q.push({n, 0});\n while(!q.empt...
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> num = {};\n map<int, int> m;\n m[0] = 0;\n int l=1, k=1;\n for(int i=1; i<=n; i++){\n if(k == i){ num.push_back(k); l++; k=l*l; }\n int min = 9999;\n for(int j=0; j<num.s...
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": "#include <cmath>\nclass Solution {\npublic:\n int numSquares(int n) {\n \n\n\n map<int, bool> m;\n vector<int> vec(n+1, INT_MAX);\n\n for (int i = 1; i <= n; i++) {\n int root = static_cast<int>(sqrt(i)); \n if (root * root == i) {\n m.ins...
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[10001][1000];\n int f(int t,int i)\n {\n if(t == 0) return 0;\n if(i*i > t) return INT_MAX;\n if(dp[t][i] != -1) return dp[t][i];\n int ans = INT_MAX;\n\n if(t - i*i >=0) ans = f(t- i*i,i);\n if(ans != INT_MAX) ans += 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 set<int> squares;\n int numSquares(int n) {\n queue<int> q;\n for (int i = 1; i*i <= n; i++) {\n squares.insert(i*i);\n }\n q.push(n);\n int level = 0;\n while (!q.empty()) {\n ++level;\n int sz = q...
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<int> q;\n int d = 1;\n q.push(n);\n while (!q.empty()) {\n int s = q.size();\n while (s--) {\n int curr = q.front();\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 numSquares(int n) {\n // int ans = 0;\n vector<int> dp(n+1,INT_MAX);\n vector<int> squares;\n for(int i = 1; i*i<= n; i++){\n squares.push_back(i*i);\n }\n int m = squares.size();\n int ans = 1;\n queue<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 {\n int sum=0;\n set<int>ans;\n int level=0;\n ans.insert(0);\n while(!ans.empty())\n {\n set<int>s;\n level++;\n for(auto it:ans)\n {\n for(int i=1;i*i<=n;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 int sum=0;\n set<int>ans;\n int level=0;\n ans.insert(0);\n while(1)\n {\n set<int>s;\n level++;\n for(auto it:ans)\n {\n for(int i=1;i*i<=n;i++)\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 numSquares(int n) {\n int step = 0;\n queue<int> q;\n for (int i=0; i<=n; ++i) {\n if (i*i > n) break;\n if (i*i == n) return 1;\n q.push(i*i);\n }\n \n while (!q.empty()) {\n step++;\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<int> q;\n int step = 1;\n q.push(n);\n \n while(!q.empty()){\n \n int size = q.size();\n for(int i = 0; i < size; i++){\n int node = q.front();\n q.po...
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<int> q;\n int step = 1;\n q.push(n);\n \n while(!q.empty()){\n \n int size = q.size();\n for(int i = 0; i < size; i++){\n int node = q.front();\n q.po...
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": "#include <queue>\nclass Solution {\npublic:\n int numSquares(int n) {\n vector<int> square_nums;\n\n for (int i = 0; i * i <= n; ++i) {\n square_nums.push_back(i * i);\n }\n\n queue<int> queue;\n queue.push(n);\n\n int level = 0;\n while (!queu...
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": "#include <queue>\nclass Solution {\npublic:\n int numSquares(int n) {\n vector<int> square_nums;\n\n for (int i = 0; i * i <= n; ++i) {\n square_nums.push_back(i * i);\n }\n\n queue<int> queue;\n queue.push(n);\n\n int level = 0;\n while (!queu...
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> vec(n+1,INT_MAX);\n // vec[0] = 0;\n // int count = 1;\n // while( count*count <= n ) {\n // int sq = count*count;\n // for(int i = sq; i < n+1; i++) {\n // vec[i] = min(...
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 map<int,bool>mp;\n void initially()\n {\n for(int i=1;i*i<=10000;i++)\n mp[i*i]=true;\n }\n int check(int n,vector<int>& dp)\n {\n if(mp[n])\n return 1;\n if(dp[n]!=-1)\n return dp[n];\n int ans=INT_MA...
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/*\nCouple of ideas:\n\n1. First, generate the possible search options (set of perfect squares up to n)\n2. Run a search where we expand the biggest possible next option\n\neither:\nia) we can do it exhaustively\nib) or we can stop the search once we know we've already exceeded t...