id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,696 | <p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p>
<p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p>
<p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p>
<p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,6], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].
It can be proved that there are only 4 beautiful subsets in the array [2,4,6].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> The beautiful subset of the array nums is [1].
It can be proved that there is only 1 beautiful subset in the array [1].
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>1 <= nums[i], k <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // Function to generate all possible subsets of the array\nvoid generate(vector<vector<int>>& all, vector<int>& temp, vector<int>& nums, int x) {\n all.push_back(temp);\n for (int i = x; i < nums.size(); i++) {\n temp.push_back(nums[i]);\n generate(all, temp, nums, i + 1);\n temp.pop_back();\n }\n}\n\n// Function to determine the number of \"beautiful\" subsets based on the given condition\nint beautifulSubsets(vector<int>& nums, int k) {\n vector<vector<int>> all;\n vector<int> temp;\n generate(all, temp, nums, 0);\n\n int count = -1;\n\n // Check each subset to see if it is \"beautiful\"\n for (const auto& subset : all) {\n bool isBeautiful = true;\n for (int i = 0; i < subset.size(); ++i) {\n for (int j = i + 1; j < subset.size(); ++j) {\n if (abs(subset[i] - subset[j]) == k) {\n isBeautiful = false;\n break;\n }\n }\n if (!isBeautiful) {\n break;\n }\n }\n if (isBeautiful) {\n count++;\n }\n }\n\n return count;\n}\n};",
"memory": "246332"
} |
2,696 | <p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p>
<p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p>
<p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p>
<p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,6], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].
It can be proved that there are only 4 beautiful subsets in the array [2,4,6].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> The beautiful subset of the array nums is [1].
It can be proved that there is only 1 beautiful subset in the array [1].
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>1 <= nums[i], k <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int cnt = 0 ;\n vector<int> v;\n vector<vector<int>> res;\n void dfs(int idx,vector<int>& nums){\n if(idx == nums.size()){\n return;\n }\n for(int i = idx; i < nums.size(); i++){\n v.push_back(nums[i]);\n res.push_back(v); \n dfs(i+1,nums);\n v.pop_back();\n }\n }\n int beautifulSubsets(vector<int>& nums, int k) {\n dfs(0,nums);\n int n = res.size();\n for(int i = 0 ; i < n ; i++){\n bool check = true;\n for(int j = 0 ; j < res[i].size();j++){\n for(int l = j+1 ; l < res[i].size(); l++){\n int x = abs(res[i][l] - res[i][j]);\n if(x == k){\n check = false;\n break;\n }\n }\n if(!check){\n break;\n }\n }\n if(check) cnt++;\n }\n return cnt;\n }\n};\n",
"memory": "252417"
} |
2,696 | <p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p>
<p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p>
<p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p>
<p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,6], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].
It can be proved that there are only 4 beautiful subsets in the array [2,4,6].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> The beautiful subset of the array nums is [1].
It can be proved that there is only 1 beautiful subset in the array [1].
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>1 <= nums[i], k <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int res = 0;\n\n bool isBeautiful(const vector<int>& curr, int k) {\n int n = curr.size();\n for (int i = 0; i < n; ++i) {\n for (int j = i + 1; j < n; ++j) {\n if (abs(curr[j] - curr[i]) == k) {\n return false;\n }\n }\n }\n return true;\n }\n\n void backtrack(int idx,vector<int> &curr,vector<int> nums,int k){\n \n if(isBeautiful(curr,k)) res++;\n \n\n for(int i=idx;i<nums.size();i++){\n //take\n curr.push_back(nums[i]);\n backtrack(i+1,curr,nums,k);\n //not take\n curr.pop_back();\n }\n }\n\n int beautifulSubsets(vector<int>& nums, int k) {\n vector<int> curr;\n backtrack(0,curr,nums,k);\n return res-1;\n }\n};",
"memory": "258502"
} |
2,696 | <p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p>
<p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p>
<p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p>
<p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,6], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].
It can be proved that there are only 4 beautiful subsets in the array [2,4,6].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> The beautiful subset of the array nums is [1].
It can be proved that there is only 1 beautiful subset in the array [1].
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>1 <= nums[i], k <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n vector<vector<int>> ans;\n vector<int> temp;\n unordered_map<int, int> mp;\n f(0, nums, k, temp, ans, mp);\n return ans.size() - 1; \n }\n\n void f(int index, vector<int>& nums, int k, vector<int> &temp, vector<vector<int>>& ans, unordered_map<int, int> &mp) {\n ans.push_back(temp); \n \n for (int i = index; i < nums.size(); ++i) {\n if (mp.find(nums[i] + k) == mp.end() && mp.find(nums[i] - k) == mp.end()) {\n temp.push_back(nums[i]);\n mp[nums[i]]++;\n f(i + 1, nums, k, temp, ans, mp);\n temp.pop_back();\n mp[nums[i]]--;\n if (mp[nums[i]] == 0) {\n mp.erase(nums[i]);\n }\n }\n }\n }\n};\n",
"memory": "264587"
} |
2,696 | <p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p>
<p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p>
<p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p>
<p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,6], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].
It can be proved that there are only 4 beautiful subsets in the array [2,4,6].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> The beautiful subset of the array nums is [1].
It can be proved that there is only 1 beautiful subset in the array [1].
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>1 <= nums[i], k <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void helper(vector<int>& nums, vector<int> prev, int idx, int k, int& ans ){\n \n if(idx >= nums.size()){\n return;\n }\n // 1,2,4,5, 7, 10 \n \n // 2, 4, 7, \n // 4\n for(int st = idx; st < nums.size(); st++){\n if(prev.size() != 0 ){\n int i =0; \n for( i = 0; i< prev.size(); i++){\n if( abs(nums[st] - prev[i]) == k)\n break;\n }\n if(i != prev.size()){\n continue;\n }\n }\n prev.push_back(nums[st]);\n ans++;\n helper(nums, prev,st+1, k, ans);\n prev.pop_back();\n // prev = -1;\n }\n }\n int beautifulSubsets(vector<int>& nums, int k) {\n //sort(nums.begin(), nums.end());\n int ans =0;\n vector<int> temp ;\n helper(nums, temp, 0, k, ans);\n return ans;\n }\n};",
"memory": "270672"
} |
2,696 | <p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p>
<p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p>
<p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p>
<p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,6], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].
It can be proved that there are only 4 beautiful subsets in the array [2,4,6].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> The beautiful subset of the array nums is [1].
It can be proved that there is only 1 beautiful subset in the array [1].
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>1 <= nums[i], k <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int res = 0;\n int beautifulSubsets(vector<int>& nums, int k) {\n unordered_multiset<int> valueMap;\n recuSubset(nums, k, valueMap, 0);\n return res;\n }\n\n void recuSubset(vector<int> nums, int k, unordered_multiset<int> &valueMap, int index) {\n if(index == nums.size()) return;\n for(int i=index;i<nums.size();i++) {\n int val = nums[i];\n if(valueMap.find(val+k) == valueMap.end() && valueMap.find(val-k) == valueMap.end()) {\n res += 1;//(valueSet.size() == 0 ? 1 : valueSet.size());\n valueMap.insert(val);\n recuSubset(nums,k,valueMap,i+1);\n valueMap.erase(valueMap.find(val));\n }\n }\n }\n};",
"memory": "276757"
} |
2,696 | <p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p>
<p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p>
<p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p>
<p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,6], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].
It can be proved that there are only 4 beautiful subsets in the array [2,4,6].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> The beautiful subset of the array nums is [1].
It can be proved that there is only 1 beautiful subset in the array [1].
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>1 <= nums[i], k <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solve(int index,vector<int> &nums,int k,vector<int> curr)\n {\n if(index==nums.size())\n {\n if(curr.size()>0)\n return 1;\n else\n return 0;\n }\n int ans=0;\n if(curr.size()>0)\n ans+=1;\n for(int i=index;i<nums.size();i++)\n {\n if(curr.size()==0)\n {\n curr.push_back(nums[i]);\n ans+=solve(i+1,nums,k,curr);\n curr.pop_back();\n }\n else\n {\n int flag=0;\n for(auto j:curr)\n {\n if(abs(j-nums[i])==k)\n {\n flag=1;\n break;\n }\n }\n if(flag==0)\n {\n curr.push_back(nums[i]);\n ans+=solve(i+1,nums,k,curr);\n curr.pop_back();\n }\n }\n }\n return ans;\n \n }\n int beautifulSubsets(vector<int>& nums, int k) {\n return solve(0,nums,k,{});\n }\n};",
"memory": "282842"
} |
263 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <code>true</code> <em>if</em> <code>n</code> <em>is an <strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> 6 = 2 × 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 14
<strong>Output:</strong> false
<strong>Explanation:</strong> 14 is not ugly since it includes the prime factor 7.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isUgly(int n) {\n if(n==0)\n return false;\n while (n != 1) {\n if (n % 2 == 0)\n n = n / 2;\n else if (n % 3 == 0)\n n = n / 3;\n else if (n % 5 == 0)\n n = n / 5;\n else {\n break;\n }\n }\n if (n == 1)\n return true;\n else\n return false;\n }\n};",
"memory": "7300"
} |
263 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <code>true</code> <em>if</em> <code>n</code> <em>is an <strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> 6 = 2 × 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 14
<strong>Output:</strong> false
<strong>Explanation:</strong> 14 is not ugly since it includes the prime factor 7.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isUgly(int n) {\n if (n <= 0) return false;\n\n while (n % 2 == 0) n /= 2;\n while (n % 3 == 0) n /= 3;\n while (n % 5 == 0) n /= 5;\n\n return n == 1;\n }\n};",
"memory": "7300"
} |
263 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <code>true</code> <em>if</em> <code>n</code> <em>is an <strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> 6 = 2 × 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 14
<strong>Output:</strong> false
<strong>Explanation:</strong> 14 is not ugly since it includes the prime factor 7.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isUgly(int n) {\n if(n<=0){\n return false;\n }\n while(n%2==0){\n n/=2;\n } \n while(n%3==0){\n n/=3;\n }\n while(n%5==0){\n n/=5;\n }\n if(n>1){\n return false;\n }\n return true;\n }\n};",
"memory": "7400"
} |
263 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <code>true</code> <em>if</em> <code>n</code> <em>is an <strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> 6 = 2 × 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 14
<strong>Output:</strong> false
<strong>Explanation:</strong> 14 is not ugly since it includes the prime factor 7.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isUgly(int n) {\n if(n==0)\n return false;\n int a[3]={2,3,5};\n for(int i=0;i<3;i++){\n while(n % a[i]==0){\n n=n/a[i];\n }\n if(n==1){\n return true;\n }\n }\n return false;\n }\n};",
"memory": "7400"
} |
263 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <code>true</code> <em>if</em> <code>n</code> <em>is an <strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> 6 = 2 × 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 14
<strong>Output:</strong> false
<strong>Explanation:</strong> 14 is not ugly since it includes the prime factor 7.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool isUgly(int n) {\n if(n<=0) return false;\n while(n%2==0) n/=2;\n while(n%3==0) n/=3;\n while(n%5==0) n/=5;\n return n==1;\n }\n};",
"memory": "7500"
} |
263 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <code>true</code> <em>if</em> <code>n</code> <em>is an <strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> 6 = 2 × 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 14
<strong>Output:</strong> false
<strong>Explanation:</strong> 14 is not ugly since it includes the prime factor 7.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool isUgly(int n) {\n if (n <= 0) return false; // Ugly numbers are positive integers\n \n // Divide n by 2, 3, and 5 as much as possible\n while (n % 2 == 0) n /= 2;\n while (n % 3 == 0) n /= 3;\n while (n % 5 == 0) n /= 5;\n \n // After removing all 2s, 3s, and 5s, if n becomes 1, it's an ugly number\n return n == 1;\n }\n};\n",
"memory": "7500"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 0 | {
"code": "const int res[1691] = {0,1,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,36,40,45,48,50,54,60,64,72,75,80,81,90,96,100,108,120,125,128,135,144,150,160,162,180,192,200,216,225,240,243,250,256,270,288,300,320,324,360,375,384,400,405,432,450,480,486,500,512,540,576,600,625,640,648,675,720,729,750,768,800,810,864,900,960,972,1000,1024,1080,1125,1152,1200,1215,1250,1280,1296,1350,1440,1458,1500,1536,1600,\n1620,1728,1800,1875,1920,1944,2000,2025,2048,2160,2187,2250,2304,2400,2430,2500,2560,2592,2700,2880,2916,3000,3072,3125,3200,3240,3375,3456,3600,3645,3750,3840,3888,4000,4050,4096,4320,4374,4500,4608,4800,4860,5000,5120,5184,5400,5625,5760,5832,6000,6075,6144,6250,6400,6480,6561,6750,6912,7200,7290,7500,7680,7776,8000,8100,8192,8640,8748,9000,9216,9375,9600,9720,10000,10125,10240,10368,10800,10935,11250,11520,11664,12000,12150,12288,12500,12800,12960,13122,13500,13824,14400,14580,15000,15360,15552,15625,16000,16200,16384,\n16875,17280,17496,18000,18225,18432,18750,19200,19440,19683,20000,20250,20480,20736,21600,21870,22500,23040,23328,24000,24300,24576,25000,25600,25920,26244,27000,27648,28125,28800,29160,30000,30375,30720,31104,31250,32000,32400,32768,32805,33750,34560,34992,36000,36450,36864,37500,38400,38880,39366,40000,40500,40960,41472,43200,43740,45000,46080,46656,46875,48000,48600,49152,50000,50625,51200,51840,52488,54000,54675,55296,56250,57600,58320,59049,60000,60750,61440,62208,62500,64000,64800,65536,65610,67500,69120,69984,72000,72900,73728,75000,76800,77760,78125,78732,80000,81000,81920,82944,84375,\n86400,87480,90000,91125,92160,93312,93750,96000,97200,98304,98415,100000,101250,102400,103680,104976,108000,109350,110592,112500,115200,116640,118098,120000,121500,122880,124416,125000,128000,129600,131072,131220,135000,138240,139968,140625,144000,145800,147456,150000,151875,153600,155520,156250,157464,160000,162000,163840,164025,165888,168750,172800,174960,177147,180000,182250,184320,186624,187500,192000,194400,196608,196830,200000,202500,204800,207360,209952,216000,218700,221184,225000,230400,233280,234375,236196,240000,243000,245760,248832,250000,253125,256000,259200,262144,262440,270000,273375,276480,279936,281250,288000,291600,294912,295245,300000,303750,307200,311040,312500,\n314928,320000,324000,327680,328050,331776,337500,345600,349920,354294,360000,364500,368640,373248,375000,384000,388800,390625,393216,393660,400000,405000,409600,414720,419904,421875,432000,437400,442368,450000,455625,460800,466560,468750,472392,480000,486000,491520,492075,497664,500000,506250,512000,518400,524288,524880,531441,540000,546750,552960,559872,562500,576000,583200,589824,590490,600000,607500,614400,622080,625000,629856,640000,648000,655360,656100,663552,675000,691200,699840,703125,708588,720000,729000,737280,746496,750000,759375,768000,777600,781250,786432,787320,800000,810000,819200,820125,829440,839808,843750,864000,874800,884736,885735,900000,911250,921600,933120,937500,944784,\n960000,972000,983040,984150,995328,1000000,1012500,1024000,1036800,1048576,1049760,1062882,1080000,1093500,1105920,1119744,1125000,1152000,1166400,1171875,1179648,1180980,1200000,1215000,1228800,1244160,1250000,1259712,1265625,1280000,1296000,1310720,1312200,1327104,1350000,1366875,1382400,1399680,1406250,1417176,1440000,1458000,1474560,1476225,1492992,1500000,1518750,1536000,1555200,1562500,1572864,1574640,1594323,1600000,1620000,1638400,1640250,1658880,1679616,1687500,1728000,1749600,1769472,1771470,1800000,1822500,1843200,1866240,1875000,1889568,1920000,1944000,1953125,1966080,1968300,1990656,2000000,2025000,2048000,2073600,2097152,2099520,2109375,2125764,2160000,2187000,2211840,2239488,2250000,2278125,2304000,2332800,2343750,2359296,2361960,2400000,2430000,2457600,2460375,2488320,\n2500000,2519424,2531250,2560000,2592000,2621440,2624400,2654208,2657205,2700000,2733750,2764800,2799360,2812500,2834352,2880000,2916000,2949120,2952450,2985984,3000000,3037500,3072000,3110400,3125000,3145728,3149280,3188646,3200000,3240000,3276800,3280500,3317760,3359232,3375000,3456000,3499200,3515625,3538944,3542940,3600000,3645000,3686400,3732480,3750000,3779136,3796875,3840000,3888000,3906250,3932160,3936600,3981312,4000000,4050000,4096000,4100625,4147200,4194304,4199040,4218750,4251528,4320000,4374000,4423680,4428675,4478976,4500000,4556250,4608000,4665600,4687500,4718592,4723920,4782969,4800000,4860000,4915200,4920750,4976640,5000000,5038848,5062500,5120000,5184000,5242880,5248800,5308416,5314410,5400000,5467500,5529600,5598720,5625000,5668704,5760000,5832000,5859375,5898240,5904900,\n5971968,6000000,6075000,6144000,6220800,6250000,6291456,6298560,6328125,6377292,6400000,6480000,6553600,6561000,6635520,6718464,6750000,6834375,6912000,6998400,7031250,7077888,7085880,7200000,7290000,7372800,7381125,7464960,7500000,7558272,7593750,7680000,7776000,7812500,7864320,7873200,7962624,7971615,8000000,8100000,8192000,8201250,8294400,8388608,8398080,8437500,8503056,8640000,8748000,8847360,8857350,8957952,9000000,9112500,9216000,9331200,9375000,9437184,9447840,9565938,9600000,9720000,9765625,9830400,9841500,9953280,10000000,10077696,10125000,10240000,10368000,10485760,10497600,10546875,10616832,10628820,10800000,10935000,11059200,11197440,11250000,11337408,11390625,11520000,11664000,11718750,11796480,11809800,11943936,12000000,12150000,12288000,12301875,12441600,12500000,12582912,12597120,12656250,12754584,12800000,\n12960000,13107200,13122000,13271040,13286025,13436928,13500000,13668750,13824000,13996800,14062500,14155776,14171760,14348907,14400000,14580000,14745600,14762250,14929920,15000000,15116544,15187500,15360000,15552000,15625000,15728640,15746400,15925248,15943230,16000000,16200000,16384000,16402500,16588800,16777216,16796160,16875000,17006112,17280000,17496000,17578125,17694720,17714700,17915904,18000000,18225000,18432000,18662400,18750000,18874368,18895680,18984375,19131876,19200000,19440000,19531250,19660800,19683000,19906560,20000000,20155392,20250000,20480000,20503125,20736000,20971520,20995200,21093750,21233664,21257640,21600000,21870000,22118400,22143375,22394880,22500000,22674816,22781250,23040000,23328000,23437500,23592960,23619600,23887872,23914845,24000000,24300000,24576000,24603750,24883200,25000000,25165824,25194240,25312500,25509168,25600000,25920000,26214400,26244000,26542080,\n26572050,26873856,27000000,27337500,27648000,27993600,28125000,28311552,28343520,28697814,28800000,29160000,29296875,29491200,29524500,29859840,30000000,30233088,30375000,30720000,31104000,31250000,31457280,31492800,31640625,31850496,31886460,32000000,32400000,32768000,32805000,33177600,33554432,33592320,33750000,34012224,34171875,34560000,34992000,35156250,35389440,35429400,35831808,36000000,36450000,36864000,36905625,37324800,37500000,37748736,37791360,37968750,38263752,38400000,38880000,39062500,39321600,39366000,39813120,39858075,40000000,40310784,40500000,40960000,41006250,41472000,41943040,41990400,42187500,42467328,42515280,43046721,43200000,43740000,44236800,44286750,44789760,45000000,45349632,45562500,46080000,46656000,46875000,47185920,47239200,47775744,47829690,48000000,48600000,48828125,49152000,49207500,49766400,50000000,50331648,50388480,50625000,51018336,51200000,51840000,\n52428800,52488000,52734375,53084160,53144100,53747712,54000000,54675000,55296000,55987200,56250000,56623104,56687040,56953125,57395628,57600000,58320000,58593750,58982400,59049000,59719680,60000000,60466176,60750000,61440000,61509375,62208000,62500000,62914560,62985600,63281250,63700992,63772920,64000000,64800000,65536000,65610000,66355200,66430125,67108864,67184640,67500000,68024448,68343750,69120000,69984000,70312500,70778880,70858800,71663616,71744535,72000000,72900000,73728000,73811250,74649600,75000000,75497472,75582720,75937500,76527504,76800000,77760000,78125000,78643200,78732000,79626240,79716150,80000000,80621568,81000000,81920000,82012500,82944000,83886080,83980800,84375000,84934656,85030560,86093442,86400000,87480000,87890625,88473600,88573500,89579520,90000000,90699264,91125000,92160000,93312000,93750000,94371840,94478400,94921875,95551488,95659380,96000000,97200000,97656250,\n98304000,98415000,99532800,100000000,100663296,100776960,101250000,102036672,102400000,102515625,103680000,104857600,104976000,105468750,106168320,106288200,107495424,108000000,109350000,110592000,110716875,111974400,112500000,113246208,113374080,113906250,114791256,115200000,116640000,117187500,117964800,118098000,119439360,119574225,120000000,120932352,121500000,122880000,123018750,124416000,125000000,125829120,125971200,126562500,127401984,127545840,128000000,129140163,129600000,131072000,131220000,132710400,132860250,134217728,134369280,135000000,136048896,136687500,138240000,139968000,140625000,141557760,141717600,143327232,143489070,144000000,145800000,146484375,147456000,147622500,149299200,150000000,150994944,151165440,151875000,153055008,153600000,155520000,156250000,157286400,157464000,158203125,159252480,159432300,160000000,161243136,162000000,163840000,164025000,165888000,167772160,167961600,168750000,169869312,170061120,170859375,172186884,172800000,174960000,175781250,\n176947200,177147000,179159040,180000000,181398528,182250000,184320000,184528125,186624000,187500000,188743680,188956800,189843750,191102976,191318760,192000000,194400000,195312500,196608000,196830000,199065600,199290375,200000000,201326592,201553920,202500000,204073344,204800000,205031250,207360000,209715200,209952000,210937500,212336640,212576400,214990848,215233605,216000000,218700000,221184000,221433750,223948800,225000000,226492416,226748160,227812500,229582512,230400000,233280000,234375000,235929600,236196000,238878720,239148450,240000000,241864704,243000000,244140625,245760000,246037500,248832000,250000000,251658240,251942400,253125000,254803968,255091680,256000000,258280326,259200000,262144000,262440000,263671875,265420800,265720500,268435456,268738560,270000000,272097792,273375000,276480000,279936000,281250000,283115520,283435200,284765625,286654464,286978140,288000000,291600000,292968750,294912000,295245000,298598400,300000000,301989888,302330880,303750000,306110016,307200000,\n307546875,311040000,312500000,314572800,314928000,316406250,318504960,318864600,320000000,322486272,324000000,327680000,328050000,331776000,332150625,335544320,335923200,337500000,339738624,340122240,341718750,344373768,345600000,349920000,351562500,353894400,354294000,358318080,358722675,360000000,362797056,364500000,368640000,369056250,373248000,375000000,377487360,377913600,379687500,382205952,382637520,384000000,387420489,388800000,390625000,393216000,393660000,398131200,398580750,400000000,402653184,403107840,405000000,408146688,409600000,410062500,414720000,419430400,419904000,421875000,424673280,425152800,429981696,430467210,432000000,437400000,439453125,442368000,442867500,447897600,450000000,452984832,453496320,455625000,459165024,460800000,466560000,468750000,471859200,472392000,474609375,477757440,478296900,480000000,483729408,486000000,488281250,491520000,492075000,497664000,500000000,503316480,503884800,506250000,509607936,510183360,512000000,512578125,516560652,518400000,\n524288000,524880000,527343750,530841600,531441000,536870912,537477120,540000000,544195584,546750000,552960000,553584375,559872000,562500000,566231040,566870400,569531250,573308928,573956280,576000000,583200000,585937500,589824000,590490000,597196800,597871125,600000000,603979776,604661760,607500000,612220032,614400000,615093750,622080000,625000000,629145600,629856000,632812500,637009920,637729200,640000000,644972544,645700815,648000000,655360000,656100000,663552000,664301250,671088640,671846400,675000000,679477248,680244480,683437500,688747536,691200000,699840000,703125000,707788800,708588000,716636160,717445350,720000000,725594112,729000000,732421875,737280000,738112500,746496000,750000000,754974720,755827200,759375000,764411904,765275040,768000000,774840978,777600000,781250000,786432000,787320000,791015625,796262400,797161500,800000000,805306368,806215680,810000000,816293376,819200000,820125000,829440000,838860800,839808000,843750000,849346560,850305600,854296875,859963392,860934420,\n864000000,874800000,878906250,884736000,885735000,895795200,900000000,905969664,906992640,911250000,918330048,921600000,922640625,933120000,937500000,943718400,944784000,949218750,955514880,956593800,960000000,967458816,972000000,976562500,983040000,984150000,995328000,996451875,1000000000,1006632960,1007769600,1012500000,1019215872,1020366720,1024000000,1025156250,1033121304,1036800000,1048576000,1049760000,1054687500,1061683200,1062882000,1073741824,1074954240,1076168025,1080000000,1088391168,1093500000,1105920000,1107168750,1119744000,1125000000,1132462080,1133740800,1139062500,1146617856,1147912560,1152000000,1162261467,1166400000,1171875000,1179648000,1180980000,1194393600,1195742250,1200000000,1207959552,1209323520,1215000000,1220703125,1224440064,1228800000,1230187500,1244160000,1250000000,1258291200,1259712000,1265625000,1274019840,1275458400,1280000000,1289945088,1291401630,1296000000,1310720000,1312200000,1318359375,1327104000,1328602500,1342177280,1343692800,1350000000,1358954496,1360488960,1366875000,1377495072,1382400000,1399680000,1406250000,\n1415577600,1417176000,1423828125,1433272320,1434890700,1440000000,1451188224,1458000000,1464843750,1474560000,1476225000,1492992000,1500000000,1509949440,1511654400,1518750000,1528823808,1530550080,1536000000,1537734375,1549681956,1555200000,1562500000,1572864000,1574640000,1582031250,1592524800,1594323000,1600000000,1610612736,1612431360,1620000000,1632586752,1638400000,1640250000,1658880000,1660753125,1677721600,1679616000,1687500000,1698693120,1700611200,1708593750,1719926784,1721868840,1728000000,1749600000,1757812500,1769472000,1771470000,1791590400,1793613375,1800000000,1811939328,1813985280,1822500000,1836660096,1843200000,1845281250,1866240000,1875000000,1887436800,1889568000,1898437500,1911029760,1913187600,1920000000,1934917632,1937102445,1944000000,1953125000,1966080000,1968300000,1990656000,1992903750,2000000000,2013265920,2015539200,2025000000,2038431744,2040733440,2048000000,2050312500,2066242608,2073600000,2097152000,2099520000,2109375000,2123366400};\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str; getline(cin, str);) {\n cout << res[stoi(str)] << '\\n';\n }\n exit(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n constexpr int nthUglyNumber(int n) {\n return 0;\n }\n};",
"memory": "7678"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n if (n <= 0) {\n return 0; // Invalid input\n }\n\n int uglyNumbers[n];\n uglyNumbers[0] = 1;\n\n int idx2 = 0, idx3 = 0, idx5 = 0; // Pointers for 2, 3, and 5\n int nextMultiple2 = 2, nextMultiple3 = 3, nextMultiple5 = 5;\n\n for (int i = 1; i < n; ++i) {\n int nextUglyNumber = std::min(nextMultiple2, std::min(nextMultiple3, nextMultiple5));\n uglyNumbers[i] = nextUglyNumber;\n\n if (nextUglyNumber == nextMultiple2) {\n ++idx2;\n nextMultiple2 = uglyNumbers[idx2] * 2;\n }\n\n if (nextUglyNumber == nextMultiple3) {\n ++idx3;\n nextMultiple3 = uglyNumbers[idx3] * 3;\n }\n\n if (nextUglyNumber == nextMultiple5) {\n ++idx5;\n nextMultiple5 = uglyNumbers[idx5] * 5;\n }\n }\n\n return uglyNumbers[n - 1];\n }\n};\n",
"memory": "7678"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n int arr[n+1];\n arr[1]=1;\n int i2=1,i3=1,i5=1;\n for(int i=2;i<=n;i++){\n int num1=arr[i2]*2;\n int num2=arr[i3]*3;\n int num3=arr[i5]*5;\n int mini=min({num1,num2,num3});\n arr[i]=mini;\n if(mini==num1) i2++;\n if(mini==num2) i3++;\n if(mini==num3) i5++;\n }\n return arr[n];\n }\n};",
"memory": "8034"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n int arr[n];\n arr[0]=1;\n int i1=0;int i2=0;int i3=0;\n int a,b,c,m;\n for(int j=1;j<n;j++){\n a=arr[i1]*2;\n b=arr[i2]*3;\n c=arr[i3]*5;\n m=min(a,b);\n m=min(m,c);\n arr[j]=m;\n if(m==a){\n i1++;\n }\n if(m==b){\n i2++;\n }\n if(m==c){\n i3++;\n }\n }\n return arr[n-1];\n }\n};",
"memory": "8034"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 0 | {
"code": "#include <bits/stdc++.h>\n#pragma GCC optimize (\"Ofast\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx2,tune=native\")\nusing namespace std;\n#define all(x) x.begin(), x.end()\n\n\nusing ull = unsigned long long;\nusing ll = long long;\n\ninline int gcd(int a,int b) { if (b==0) return a; return gcd(b, a%b); }\ninline long long gcd(long long a,long long b) { if (b==0) return a; return gcd(b, a%b); }\ninline int lcm(int a,int b) { return a/gcd(a,b)*b; } \ninline long long lcm(long long a,long long b) { return a/gcd(a,b)*b; }\ninline long long nth_prime(long long a) { a++;if(a <= 6) return (vector<long long>{2,3,5,7,11,13,17})[a]; long double lg = log((long double) a); return (long long) floor(a * (lg + log(lg))); }\ninline long long mod_exp(long long base, long long exp, long long modd) { unsigned long long ans = 1; base %= modd; while(exp > 0) { if(exp%2==1) ans = (base*ans)%modd; exp /= 2; base = (base*base)%modd; } return ans; }\ninline string to_upper(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A'; return a; }\ninline string to_lower(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A'; return a; }\n\nvector<int> uglyNumbers;\n\nbool init() {\n ios::sync_with_stdio(false);\n cout.tie(nullptr);\n cin.tie(nullptr);\n\n priority_queue<ll, vector<ll>, greater<ll>> minHeap;\n minHeap.push(1);\n uglyNumbers.reserve(1691);\n for(int n = 0; n <= 1690; n++) {\n ll num;\n do {\n num = minHeap.top();\n minHeap.pop();\n }while(n > 0 && uglyNumbers[n-1] == num);\n minHeap.push(2ll*num);\n minHeap.push(3ll*num);\n minHeap.push(5ll*num);\n uglyNumbers.push_back((int)num);\n }\n\n return true;\n}\nbool y4555123 = init();\n\nclass Solution {\npublic:\n int nthUglyNumber(int n) {\n return uglyNumbers[n-1];\n }\n};",
"memory": "8390"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 0 | {
"code": "#define ll long long\n\nclass Solution {\npublic:\n vector<ll> f = {5,3,2};\n bool is_exact(ll n,ll f){\n if(n==1) return false;\n while(n%f==0) n/=f;\n return n==1;\n }\n vector<map<ll,ll>> memo;\n\n ll gf(ll n,ll it){\n // if(memo[it].find(n)!=memo[it].end()) return memo[it][n];\n\n ll cf = f[it];\n if(n<1) return 0; \n\n //bool ie = is_exact(n,cf);\n ll mcit = log(n)/log(cf)+1;\n //if(!ie) mcit++;\n\n if(it==2) {\n //cout<<\"n: \"<<n<<\" cf: \"<<cf<<\" mcit: \"<<mcit<<\" ans: \"<<mcit<<\"\\n\";\n // memo[it][n] = mcit;\n return mcit;\n }\n ll ans = 0;\n for(ll i=0;i<=mcit;i++){\n ans += gf(n/pow(cf,i),it+1);\n }\n //cout<<\"n: \"<<n<<\" cf: \"<<cf<<\" mcit: \"<<mcit<<\" ans: \"<<ans<<\"\\n\";\n // memo[it][n] = ans;\n return ans;\n }\n \n int nthUglyNumber(int n) {\n memo = vector<map<ll,ll>>(3);\n ll ans = 0;\n ll l = 1;\n ll r = 2e10;\n //cout<<gf(r,0)<<\"\\n\";\n while(l<=r){\n ll m = (l+r)/2;\n\n ll cf = gf(m,0);\n ll pcf = gf(m-1,0);\n //cout<<\"m: \"<<m<<\" cf: \"<<cf<<\" pcf: \"<<pcf<<\"\\n\";\n if(cf==n && pcf==n-1){\n ans = m;\n break;\n }\n if(cf>n) r = m-1;\n else if(cf<n) l = m+1;\n else if(pcf==n) r = m-1;\n }\n return ans;\n }\n\n\n};",
"memory": "8390"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 0 | {
"code": "vector<long long int> func(){\n priority_queue<long long int, vector<long long int>, greater<long long int>> pq;\n pq.push(1);\n set<long long int> res;\n res.insert(1);\n while(res.size() < 2000){\n long long int top = pq.top();\n pq.pop();\n if(res.insert(top*2).second) pq.push(top*2);\n if(res.insert(top*3).second) pq.push(top*3);\n if(res.insert(top*5).second) pq.push(top*5);\n }\n vector<long long int> arr(res.begin(), res.end());\n return arr;\n}\n\nvector<long long int> arr = func();\n\n\n\nclass Solution {\npublic:\n int nthUglyNumber(int n) {\n return arr[n-1]; \n }\n};",
"memory": "8746"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n \n if(n <= 0)\n {\n return 0;\n }\n else if(n == 1)\n {\n return 1;\n }\n\n int l1 = 0, l2 = 0, l3 = 0;\n int dp[2000] = {0};\n dp[0] = 1;\n\n for(int i = 1; i < n; i++)\n {\n dp[i] = min(dp[l1]*2, min(dp[l2]*3, dp[l3]*5));\n\n if(dp[i] == dp[l1]*2)\n {\n l1++;\n }\n \n if(dp[i] == dp[l2]*3)\n {\n l2++;\n }\n \n if(dp[i] == dp[l3]*5)\n {\n l3++;\n }\n }\n\n return dp[n - 1];\n }\n};",
"memory": "8746"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 0 | {
"code": "bool f = 1;\nvector<int> ugly(2,-1);\nvector<int> ans;\ntypedef long long ll;\nbool check(ll n){\n ll i = 2;\n while(i*i<=n){\n if(n%i==0){\n if(i==2 || i==3 || i==5){\n while(n%i==0)n/=i;\n }\n else return 0;\n }\n i++;\n }\n if(n>1 && !(n==2 || n==3 || n==5))return 0;\n else return 1;\n}\nvoid temp(){\n f = 0;\n ugly[1] = ugly[2] = ugly[3] = ugly[4] = ugly[5] = 1;\n for(ll i = 2;i<2e5;i++){\n if(ugly[i] ==1 || check(i)){\n ugly[i] = 1;\n ll nxt = (i<<1);\n while(nxt<2e5){\n ugly[nxt] = 1;\n nxt<<=1;\n }\n nxt = i*3;\n while(nxt<2e5){\n ugly[nxt] = 1;\n nxt*=3;\n }\n nxt = i*5;\n while(nxt<2e5){\n ugly[nxt] = 1;\n nxt*=5;\n }\n }\n else{\n ll k = i,j=2;\n while(k<2e5){\n ugly[k] = 0;\n k*=j;\n j++;\n }\n\n }\n }\n for(int i=1;i<2e5;i++){\n if(ugly[i]){\n ans.push_back(i);\n }\n }\n}\nvoid generate(){\n f = 0;\n priority_queue<ll,vector<ll>,greater<ll>> pq;\n pq.push((ll)1);\n map<ll,bool> mp;\n mp[1] = 1;\n int cnt = 1;\n vector<ll> e = {2,3,5};\n while(cnt<1799){\n ll node = pq.top();\n pq.pop();\n for(auto &i:e){\n ll nxt = i*node;\n if(mp.count(nxt))continue;\n mp[nxt] = 1;\n pq.push(nxt);\n cnt++;\n }\n }\n for(auto &i:mp){\n ans.push_back(i.first);\n }\n}\nclass Solution {\nprivate:\n\npublic:\n int nthUglyNumber(int n) {\n // if(f){\n // // temp();\n // }\n // cout<<ans.size()<<endl;\n // for(auto &i:ans)cout<<i<<\" \";cout<<endl;\n // int k = ans.size();\n // for(k;k>0;k--)cout<<ans[k-1]<<\" \";\n // return ans[n-1];\n \n // Approach 02\n if(f){\n generate();\n }\n return ans[n-1];\n }\n};",
"memory": "9103"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[1691];\n int helper(int n, int i, int a, int b, int c){\n if(i==n) return dp[n-1];\n int x = dp[a]*2, y = dp[b]*3, z = dp[c]*5;\n\n dp[i] = min({x,y,z});\n\n if(dp[i]==x) a++;\n if(dp[i]==y) b++;\n if(dp[i]==z) c++;\n return helper(n,i+1,a,b,c);\n }\n int nthUglyNumber(int n) {\n dp[0]=1;\n return helper(n,1,0,0,0);\n }\n};",
"memory": "9103"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<int> dp(n+1,-1);\n dp[0]=1;\n int l1=0;\n int l2=0;\n int l3=0;\n for(int i=1;i<n;i++){\n int mini=min({dp[l1]*2,dp[l2]*3,dp[l3]*5});\n dp[i]=mini;\n if (mini == dp[l1] * 2)\n {\n l1++;\n }\n if (mini == dp[l2] * 3)\n {\n l2++;\n }\n if (mini == dp[l3] * 5)\n {\n l3++;\n }\n }\n return dp[n-1];\n }\n};",
"memory": "9459"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<int> dp(n+1,-1);\n dp[0]=1;\n int l1=0;\n int l2=0;\n int l3=0;\n for(int i=1;i<n;i++){\n int mini=min({dp[l1]*2,dp[l2]*3,dp[l3]*5});\n dp[i]=mini;\n if (mini == dp[l1] * 2)\n {\n l1++;\n }\n if (mini == dp[l2] * 3)\n {\n l2++;\n }\n if (mini == dp[l3] * 5)\n {\n l3++;\n }\n }\n return dp[n-1];\n }\n};",
"memory": "9459"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) \n {\n int a = 1, b = 1, c = 1;\n\n vector<int> arr(n + 1);\n arr[1] = 1;\n\n for(int i = 2;i <= n;i++)\n {\n int x = arr[a] * 2; \n int y = arr[b] * 3; \n int z = arr[c] * 5;\n\n arr[i] = min(x, min(y, z));\n\n if(x == arr[i])\n a++; \n if(y == arr[i])\n b++; \n if(z == arr[i])\n c++; \n }\n\n return arr[n];\n }\n};",
"memory": "9815"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<int> ugly(n);\n ugly[0] = 1;\n\n int a = 0, b = 0, c = 0;\n for(int i=1; i<n; i++) {\n int curr = min(ugly[a]*2, min(ugly[b]*3, ugly[c]*5));\n if(curr == ugly[a]*2) {\n ugly[i] = curr;\n a++;\n }\n if(curr == ugly[b]*3) {\n ugly[i] = curr;\n b++;\n }\n if(curr == ugly[c]*5) {\n ugly[i] = curr;\n c++;\n }\n cout << curr << \" \";\n }\n return ugly[n-1];\n }\n};",
"memory": "9815"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<pair<int, int>> index_of_multipliers = {{0, 2}, {0, 3}, {0, 5}};\n vector<long long> candidates = {2, 3, 5};\n vector<int> ugly_numbers = {1};\n ugly_numbers.reserve(n);\n\n for (int i = 1; i < n; ++i) {\n long long next_ugly_number = *ranges::min_element(candidates);\n ugly_numbers.push_back(next_ugly_number);\n\n for (int j = 0; j < 3; ++j) {\n if (candidates[j] == next_ugly_number) {\n auto& [index, multiplier] = index_of_multipliers[j];\n candidates[j] = ugly_numbers[++index] * multiplier;\n }\n }\n }\n\n return ugly_numbers.back();\n }\n};",
"memory": "10171"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n /**\n * Dynamic Programming (Tabulation)\n *\n * Track the next multiples of 2, 3, and 5.\n * Select the smallest multiple as the next ugly number.\n * Increment the index for the chosen multiple.\n *\n * Time Complexity: O(n)\n * Space Complexity: O(n) auxiliary\n */\n int nthUglyNumber(int n) {\n // Indices for each of the prime factors 2, 3, and 5\n vector<pair<int, int>> multipliers_indices = {{0, 2}, {0, 3}, {0, 5}};\n\n // Current candidates for the next ugly number, initialized with the first multiples\n vector<long long> candidates = {2, 3, 5};\n\n // List to store generated ugly numbers, starting with 1\n vector<int> ugly_numbers = {1};\n ugly_numbers.reserve(n);\n\n // Generate the first n ugly numbers\n for (int i = 1; i < n; ++i) {\n // Find the smallest candidate as the next ugly number\n long long next_ugly_number = *ranges::min_element(candidates);\n ugly_numbers.push_back(next_ugly_number);\n\n // Update candidates and indices based on the new ugly number\n for (int j = 0; j < 3; ++j) {\n if (candidates[j] == next_ugly_number) {\n auto& [index, multiplier] = multipliers_indices[j];\n candidates[j] = ugly_numbers[++index] * multiplier;\n }\n }\n }\n\n // Return the nth ugly number\n return ugly_numbers[n - 1];\n }\n};",
"memory": "10171"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 1 | {
"code": "// TC - O(n logn) time - By channa sir\nclass Solution1 {\npublic:\n int nthUglyNumber(int n) {\n set<int> fringeSet;\n fringeSet.insert(1);\n while(n > 1) {\n n--;\n int num = *fringeSet.begin(); // next ugly number\n fringeSet.erase(num);\n vector<int> primes = {2, 3, 5};\n for(int prime : primes) {\n // if(prime*num <= INT_MAX && fringeSet.find(prime*num) == fringeSet.end()) {\n if(num <= INT_MAX/prime && fringeSet.find(prime*num) == fringeSet.end()) {\n fringeSet.insert(prime*num);\n }\n }\n }\n return *fringeSet.begin();\n }\n};\n\n// O(n logn) time - by Channa Sir\nclass Solution {\npublic:\n int nthUglyNumber(int n) {\n priority_queue<int, vector<int>, greater<int>> pq;\n pq.push(1);\n while(n > 1) {\n n--;\n int num = pq.top();\n pq.pop();\n if(num % 3 != 0 && num % 5 != 0) {\n if(num <= INT_MAX / 2) pq.push(2 * num);\n }\n if(num % 5 != 0) {\n if(num <= INT_MAX / 3) pq.push(3 * num);\n }\n if(num <= INT_MAX / 5) pq.push(5 * num);\n }\n return pq.top();\n }\n};\n\n// TC-O(n) by Mohit Kuk\nclass Solution3 {\npublic:\n int nthUglyNumber(int n) {\n vector<long long> arr(n+1);\n arr[1]=1;\n int i=1,j=1,k=1;\n for(int x=2;x<=n;x++){\n long long fi=arr[i]*2;\n long long se=arr[j]*3;\n long long th=arr[k]*5;\n long long mn=min({fi,se,th});\n if(mn==fi)\n {\n arr[x]=fi;\n i++;\n }\n if(mn==se)\n {\n arr[x]=se;\n j++;\n }\n if(mn==th)\n {\n arr[x]=th;\n k++;\n }\n }\n return arr[n];\n }\n};",
"memory": "10528"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<long> arr(n + 1);\n \n arr[0] = 1;\n \n int t2 = 0, t3 = 0, t5 = 0;\n \n for (int i = 1; i <= n; i++) {\n arr[i] = min(arr[t2] * 2, min(arr[t5] * 5, arr[t3] * 3));\n if (arr[i] == arr[t2] * 2) t2++;\n if (arr[i] == arr[t3] * 3) t3++;\n if (arr[i] == arr[t5] * 5) t5++;;\n }\n \n return (int)arr[n - 1];\n }\n};",
"memory": "10528"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<long long> uglies(n);\n uglies[0] = 1;\n \n int i2 = 0, i3 = 0, i5 = 0;\n long long multi2 = 2;\n long long multi3 = 3;\n long long multi5 = 5;\n long long uglies1;\n \n for (int i = 1; i < n; ++i) {\n uglies1 = std::min({multi2,multi3,multi5});\n uglies[i] = uglies1;\n \n if (uglies1 == multi2) {\n ++i2;\n multi2 = uglies[i2] * 2;\n }\n if (uglies1 == multi3) {\n ++i3;\n multi3 = uglies[i3] * 3;\n }\n if (uglies1 == multi5) {\n ++i5;\n multi5 = uglies[i5] * 5;\n }\n }\n \n return uglies[n-1];\n }\n};",
"memory": "10884"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n#define ll long long\n int nthUglyNumber(int n) {\n vector<int>primes{2,3,5};\n int p=primes.size();\n ll inf=LLONG_MAX;\n vector<ll>vpidx(p,0);\n vector<ll>val(n);\n val[0]=1;\n for(int i=1;i<n;i++){\n ll v=inf;\n ll idx;\n for(int j=0;j<p;j++){\nif(v>(val[vpidx[j]]*primes[j]) && (val[vpidx[j]]*primes[j])>val[i-1] ){\n v=(val[vpidx[j]]*primes[j]); \n}}\n for(int j=0;j<p;j++){\nif(v==(val[vpidx[j]]*primes[j]) ){\n vpidx[j]++;\n}\n }\n // cout<<i<<\" \"<<v<<\" \"<<endl;\n val[i]=v;\n \n // for(int ti=0;ti<p;ti++){cout<<vpidx[ti]<<\" \";} cout<<endl;\n }\n \n return val[n-1];\n }\n};",
"memory": "10884"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "#define lli long long int \n#define ld long double\n#define vi vector<int>\n#define vlli vector<lli>\n#define vpii vector<pair<int, int>>\n#define pb push_back \n\ntemplate<typename T> void debug(T _a) {cout << _a << \" \";}\ntemplate<typename T1, typename T2> void debug(pair<T1, T2> _p) {cout<<\"{\";debug(_p.first);cout<<\": \";debug(_p.second);cout<<\"}\\n\";}\ntemplate<typename T> void debug(vector<T> _aa) {for (auto h: _aa) debug(h); cout << endl;}\ntemplate<typename T> void debug(deque<T> _aa) {for (auto h: _aa) debug(h); cout << endl;}\ntemplate<typename T> void debug(multiset<T> _aa) {for (auto h: _aa) debug(h); cout << endl;}\ntemplate<typename T> void debug(set<T> _aa) {for (auto h: _aa) debug(h); cout << endl;}\ntemplate<typename T1, typename T2> void debug(map<T1, T2> _mm) {for (auto h: _mm) debug(h);}\n\nclass Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<int> primes = {2,3,5};\n vector<int> dp(n+1);\n dp[1] = 1;\n priority_queue<pair<lli, pair<lli, lli>>> pq;\n for (int i: primes) pq.push({-i, {i, 1}});\n\n int i = 1;\n\n while (i < n) {\n auto cur = pq.top(); pq.pop();\n lli val = -cur.first, p = cur.second.first, idx = cur.second.second;\n if (val > dp[i]) {\n dp[++i] = val;\n }\n pq.push({-p * dp[idx+1], {p, idx+1}});\n }\n return dp[n];\n }\n};",
"memory": "11240"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector <long long> primes={2,3,5};\n vector <long long> mulIdx(3,1);\n vector <long long> dp(n+1,0);\n dp[1]=1;\n for (int i=2;i<=n;i++) {\n long long mini=INT_MAX;\n for (int j=0;j<3;j++) {\n long long temp = primes[j] * (dp[mulIdx[j]]);\n if (temp<mini) {\n mini=temp;\n }\n }\n dp[i]=mini;\n for (int j=0;j<3;j++) {\n long long temp = primes[j] * (dp[mulIdx[j]]);\n if (temp==mini) {\n mulIdx[j]++;\n }\n }\n }\n return dp[n];\n }\n};",
"memory": "11240"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<int> arr;\n arr.push_back(1);\n int i = 1;\n int tw = 0, thr = 0, fv = 0;\n while(i <= n){\n int ans = min({2*arr[tw], 3*arr[thr], 5*arr[fv]});\n if(2*arr[tw] == ans)\n tw++;\n \n if(3*arr[thr] == ans)\n thr++;\n \n if(5*arr[fv] == ans)\n fv++;\n \n arr.push_back(ans);\n i++;\n }\n return arr[n-1];\n }\n};",
"memory": "11596"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<int> nums = {1};\n int i2 = 0, i3 = 0, i5 = 0;\n for(int i = 0; i < n; i++) {\n int next_num = min(min(nums[i2] * 2, nums[i3] * 3), nums[i5] * 5);\n nums.push_back(next_num);\n if (next_num == nums[i2] * 2) {\n i2++;\n }\n if (next_num == nums[i3] * 3) {\n i3++;\n }\n if (next_num == nums[i5] * 5) {\n i5++;\n }\n }\n return nums[n - 1];\n }\n};",
"memory": "11596"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n priority_queue<long, vector<long>, greater<long>> pq;\n pq.push(1);\n\n long rval;\n for (int i=0; i<n; i++){\n rval=pq.top();\n pq.pop();\n if (rval%3!=0 && rval%5!=0) pq.push(rval*2);\n if (rval%5!=0) pq.push(rval*3);\n pq.push(rval*5);\n }\n return rval;\n }\n};",
"memory": "11953"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n priority_queue<int64_t, std::vector<int64_t>, std::greater<int64_t>> que;\n que.push(1);\n for (int i = 1; i < n; ++i) {\n int64_t x = que.top();\n que.pop();\n if (x % 3 && x % 5 && x * 2 < 1e12) {\n que.push(x * 2);\n }\n if (x % 5&& x * 3 < 1e12) {\n que.push(x * 3);\n }\n if (x * 5 < 1e12) {\n que.push(x * 5);\n }\n \n }\n return que.top();\n }\n};",
"memory": "11953"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nint nthUglyNumber(int n) {\n vector<int> store;\n store.push_back(1);\n\n unordered_map<int, int> mp;\n mp[2] = 1;\n mp[3] = 1;\n mp[5] = 1;\n\n while (store.size() != n) {\n int mini = INT_MAX;\n\n \n for (const auto &i : mp) {\n mini = min(mini, i.first * store[i.second - 1]);\n }\n\n // Increment the pointers for the factors that produced the minimum\n for (auto &i : mp) {\n if (mini == i.first * store[i.second - 1]) {\n i.second++;\n }\n }\n\n store.push_back(mini);\n }\n\n return store[n - 1];\n}\n};",
"memory": "12309"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n if (n==1) return 1;\n priority_queue<long long,vector<long long>,greater<long long> > l1,l2,l3;\n l1.push(2);\n l2.push(3);\n l3.push(5);\n \n for (int i = 1 ; i < n - 1; i++)\n {\n if (l1.top() < l2.top())\n {\n if (l1.top() < l3.top())\n {\n long long ele = l1.top();\n l1.pop();\n l1.push(2*ele);\n l2.push(3*ele);\n l3.push(5*ele);\n }\n else \n {\n long long ele = l3.top();\n l3.pop();\n l3.push(5*ele);\n }\n }\n else\n {\n if (l3.top() < l2.top())\n {\n long long ele = l3.top();\n l3.pop();\n l3.push(5*ele); \n }\n else\n {\n long long ele = l2.top();\n l2.pop();\n l2.push(3*ele);\n l3.push(5*ele);\n }\n }\n }\n return min(l1.top(),min(l2.top(),l3.top()));\n }\n};",
"memory": "12309"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void func(int pt1, int pt2, int pt3, vector<int>& ans, int n) {\n if (ans.size() == n)\n return;\n\n int op1 = ans[pt1] * 2;\n int op2 = ans[pt2] * 3;\n int op3 = ans[pt3] * 5;\n int mini = min({op1, op2, op3});\n\n ans.push_back(mini);\n\n if (mini == op1)\n pt1++;\n if (mini == op2)\n pt2++;\n if (mini == op3)\n pt3++;\n\n func(pt1, pt2, pt3, ans, n);\n }\n\n int nthUglyNumber(int n) {\n vector<int> ans;\n ans.push_back(1); // First ugly number is 1\n func(0, 0, 0, ans, n);\n return ans[n - 1];\n }\n};\n",
"memory": "12665"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<int> dp(1691);\n dp[0] = 1;\n int a=0,b=0,c=0;\n for(int i=1; i<n; i++){\n dp[i] = min(dp[a]*2, min(dp[b]*3, dp[c]*5));\n if(dp[i] == dp[a]*2) a++;\n if(dp[i] == dp[b]*3) b++;\n if(dp[i] == dp[c]*5) c++;\n }\n return dp[n-1];\n }\n};",
"memory": "12665"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n priority_queue<int>pq;\n for(int i = 0;i<33;i++){\n for(int j = 0;j<33;j++){\n for(int k = 0;k<33;k++){\n long long number = pow(2,i);\n if(number > INT_MAX){\n break;\n }\n if((double)pow(3,j) > INT_MAX){\n break;\n }\n number *= pow(3,j);\n if(number > INT_MAX){\n break;\n }\n if((double)pow(5,k) > INT_MAX){\n break;\n }\n number*= pow(5,k);\n if(number > INT_MAX){\n break;\n }\n pq.push(number);\n if(pq.size() > n){\n pq.pop();\n } \n }\n }\n } \n return pq.top();\n }\n};",
"memory": "13021"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n Solution(){\n ios_base :: sync_with_stdio(false);\n }\n int nthUglyNumber(int n) {\n priority_queue<int>pq;\n for(int i = 0;i<33;i++){\n for(int j = 0;j<33;j++){\n for(int k = 0;k<33;k++){\n long long number = pow(2,i);\n if(number > INT_MAX){\n break;\n }\n if((double)pow(3,j) > INT_MAX){\n break;\n }\n number *= pow(3,j);\n if(number > INT_MAX){\n break;\n }\n if((double)pow(5,k) > INT_MAX){\n break;\n }\n number*= pow(5,k);\n if(number > INT_MAX){\n break;\n }\n pq.push(number);\n if(pq.size() > n){\n pq.pop();\n } \n }\n }\n } \n return pq.top();\n }\n};",
"memory": "13021"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n map<int , int > m;\n m[2]=0;\n m[3]=0;\n m[5]=0;\n vector<int > sub;\n sub.push_back(1);\n int c=1;\n \n int ans=1;\n if (n==c) return c;\n while(n!=c){\n if (2*sub[m[2]] <= 3*sub[m[3]] && 2*sub[m[2]] <= 5*sub[m[5]]){\n if (sub[sub.size()-1]<2*sub[m[2]]){\n \n c+=1;\n ans=2*sub[m[2]];\n sub.push_back(2*sub[m[2]]);\n\n }\n \n m[2]++;\n }\n else if(3*sub[m[3]] <=2*sub[m[2]] && 3*sub[m[3]] <= 5*sub[m[5]]){\n if (sub[sub.size()-1]<3*sub[m[3]]){\n \n c+=1;\n ans=3*sub[m[3]];\n sub.push_back(3*sub[m[3]]);\n\n }\n m[3]++;\n }\n else if(5*sub[m[5]] <=2*sub[m[2]] && 5*sub[m[5]] <= 3*sub[m[3]]){\n if (sub[sub.size()-1]<5*sub[m[5]]){\n \n c+=1;\n ans=5*sub[m[5]];\n sub.push_back(5*sub[m[5]]);\n\n }\n m[5]++;\n }\n }\n return ans;\n }\n};",
"memory": "13378"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n #define ll long long\n int nthUglyNumber(int n) {\n vector<int> primes = {2,3,5};\n vector<ll> v(n, 0);\n priority_queue<pair<ll, pair<ll, ll>> ,vector<pair<ll, pair<ll, ll>>>, greater<pair<ll, pair<ll, ll>>>> pq;\n v[0] = 1;\n for(auto it:primes)pq.push({it, {it, 0}});\n for(ll i = 1; i<n; i++){\n auto t = pq.top(); pq.pop();\n v[i] = t.first;\n if(v[i] == v[i-1])i--;\n t.first = t.second.first * v[t.second.second+1];\n t.second.second++;\n if(t.second.second+1<n)pq.push(t);\n // cout<<t.first<<\" \"<<t.second<<endl;\n }\n // for(ll i = 0; i<n; i++)cout<<v[i]<<\" \";\n return v[n-1];\n }\n};",
"memory": "13378"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n queue<int> q2;\n queue<int> q3;\n queue<int> q5;\n\n q2.push(1);\n q3.push(1);\n q5.push(1);\n\n int number = 1;\n int prev = 0;\n while (n > 1) {\n number = min(q2.front(), min(q3.front(), q5.front()));\n \n if (number == q2.front()) {\n q2.pop();\n } else if (number == q3.front()) {\n q3.pop();\n } else {\n q5.pop();\n }\n if (number <= prev) {\n continue;\n }\n if (number <= INT_MAX / 2)\n q2.push(number * 2);\n if (number <= INT_MAX / 3)\n q3.push(number * 3);\n if (number <= INT_MAX / 5)\n q5.push(number * 5);\n\n prev = number;\n n--;\n }\n\n number = min(q2.front(), min(q3.front(), q5.front()));\n while (number <= prev) {\n number = min(q2.front(), min(q3.front(), q5.front()));\n if (number == q2.front()) {\n q2.pop();\n } else if (number == q3.front()) {\n q3.pop();\n } else {\n q5.pop();\n }\n if (number <= prev) {\n continue;\n }\n }\n\n return number;\n }\n};",
"memory": "13734"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "#define ll long long\n\nclass Solution {\npublic:\n int nthUglyNumber(int n) {\n priority_queue<ll,vector<ll>,greater<ll>>pq;\n // ll temp2 = 2,temp3 = 3,temp5 = 5;\n // ll idx2 = 2,idx3 = 2,idx5 = 2;\n ll ans;\n pq.push(1);\n ll cnt = 1;\n while( cnt <= n)\n {\n ll ele = pq.top();\n pq.pop();\n // cout<<\"while\"<<endl;\n while(pq.top() == ele && !pq.empty()) pq.pop();\n cnt++;\n ans = ele;\n ll u2 = 2*ele, u3 = 3*ele,u5 = 5*ele;\n pq.push(u2);\n pq.push(u3);\n pq.push(u5);\n }\n return (int)ans;\n }\n};",
"memory": "13734"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n priority_queue<long long int,vector<long long int>,greater<long long int>> pq;\n pq.push(1);\n for(int i=0;i<n-1;i++){\n long long int val = pq.top();\n // cout<<val<<endl;\n pq.pop();\n while(pq.size()>0&&pq.top()==val) pq.pop();\n pq.push(val*2);\n pq.push(val*3);\n pq.push(val*5);\n }\n\n return (int)pq.top();\n \n }\n};",
"memory": "14090"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n priority_queue<long long, vector<long long>, greater<long long>> minHeap;\n minHeap.push(1);\n int count =1;\n long long last_min = 0;\n\n while (count < n){\n \n long long min_num = minHeap.top();\n minHeap.pop();\n if (min_num== last_min){\n continue;\n }\n last_min = min_num;\n count++;\n minHeap.push(min_num*2);\n minHeap.push(min_num*3);\n minHeap.push(min_num*5);\n }\n while (minHeap.top() == last_min){\n minHeap.pop();\n }\n return minHeap.top() ;\n }\n};",
"memory": "14090"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n priority_queue<pair<long long ,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> pq;\n pq.push({1,1});\n while(n-1) {\n pair x = pq.top();\n pq.pop();\n if(x.second == 1) {\n pq.push({x.first*2, 1});\n pq.push({x.first*3, 2});\n pq.push({x.first*5, 3});\n } else if (x.second == 2) {\n pq.push({x.first*3, 2});\n pq.push({x.first*5, 3});\n } else {\n pq.push({x.first*5, 3});\n }\n n-=1;\n }\n\n return pq.top().first;\n }\n};",
"memory": "14446"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n if (n == 1) return 1;\n priority_queue<long long, vector<long long>, greater<long long>> two;\n priority_queue<long long, vector<long long>, greater<long long>> three;\n priority_queue<long long, vector<long long>, greater<long long>> five;\n two.push(2);\n three.push(3);\n five.push(5);\n for (int i = 2; i < n; i++){\n long long temp = min({two.top(), three.top(), five.top()});\n if (two.top() == temp) two.pop();\n if (three.top() == temp) three.pop();\n if (five.top() == temp) five.pop();\n two.push(temp * 2);\n three.push(temp * 3);\n five.push(temp * 5);\n }\n return min({two.top(), three.top(), five.top()});\n }\n};",
"memory": "14446"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nint nthUglyNumber(int n) \n{\n vector<long long int> alpha = {1};\n for(int i = 2;i<=n;i++)\n {\n long long int curUgly = INT_MAX;\n for(auto j : alpha)\n {\n if(j*2>alpha.back()) curUgly = min(curUgly,j*2);\n if(j*3>alpha.back()) curUgly = min(curUgly,j*3);\n if(j*5>alpha.back()) curUgly = min(curUgly,j*5);\n }\n alpha.push_back(curUgly);\n }\n return alpha.back();\n}\n};",
"memory": "14803"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nint nthUglyNumber(int n) \n{\n vector<long long int> alpha = {1};\n int start = 0;\n for(int i = 2;i<=n;i++)\n {\n long long int curUgly = INT_MAX;\n for(int j = start; j<alpha.size();j++)\n {\n if(alpha[j]*2>alpha.back()) curUgly = min(curUgly,alpha[j]*2);\n if(alpha[j]*3>alpha.back()) curUgly = min(curUgly,alpha[j]*3);\n if(alpha[j]*5>alpha.back()) curUgly = min(curUgly,alpha[j]*5);\n if(alpha[j]*5<=alpha.back()) start = j+1;\n }\n alpha.push_back(curUgly);\n }\n return alpha.back();\n}\n};",
"memory": "14803"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "typedef long long int ll;\n\nclass Solution {\npublic:\n int nthUglyNumber(int n) {\n const ll two = 2, three = 3, five = 5;\n \n vector<ll> A = {1,2,3,4,5};\n // 0 1 2 3 4\n // first case [1,5]\n if( n <= 5 ) return A[n-1];\n\n int x = 2, y = 2, z = 4;\n n -= 5;\n while( n > 0 ){\n n --;\n \n ll a = two * A[x];\n ll b = three * A[y];\n ll c = five * A[z];\n\n A.push_back( min(a, min(b,c) ) );\n\n if( A.back() == a ) x++;\n if( A.back() == b ) y ++;\n if( A.back() == c ) z ++;\n }\n\n return A.back();\n\n }\n};",
"memory": "15159"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<long> nums = {1};\n vector<int> factors = {2, 3, 5};\n vector<int> indexes(3, 0);\n vector<long> candidates(3, 0);\n\n for (int i = 1; i < n; ++i) {\n for (int j = 0; j < 3; ++j)\n candidates[j] = nums[indexes[j]] * factors[j];\n\n int m = *min_element(candidates.begin(), candidates.end());\n nums.push_back(m);\n\n for (int j = 0; j < 3; ++j)\n if (m == candidates[j])\n ++indexes[j];\n }\n\n return nums.back();\n }\n};",
"memory": "15159"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<int>ugly{0};\n priority_queue<int, vector<int>, greater<int>>pq;\n\n for(int i = 1; i <= n; i++){\n if(i < 6){\n ugly.emplace_back(i);\n pq.push(i * 2);\n pq.push(i * 3);\n pq.push(i * 5);\n continue;\n }\n\n long long ele = pq.top();\n pq.pop();\n\n if(ele <= ugly.back()){\n i--;\n continue;\n }\n else{\n ugly.emplace_back(ele);\n if(ele * 2 <= INT_MAX) pq.push(ele * 2);\n if(ele * 3 <= INT_MAX) pq.push(ele * 3);\n if(ele * 5 <= INT_MAX) pq.push(ele * 5);\n }\n \n }\n\n return ugly[n];\n }\n};",
"memory": "15515"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "\nclass Solution {\npublic:\n int nthUglyNumber(int n) {\n if (n <= 20) {\n // Faster path for smaller numbers.\n int index = 1;\n for (uint32_t i = 1;; ++i) {\n if (isUgly(i)) {\n if (index == n)\n return i;\n ++index;\n }\n }\n return -1;\n }\n std::vector<uint32_t> ugly_nums;\n ugly_nums.reserve(2048);\n for (uint32_t i = 0; i < 31; ++i) {\n uint32_t three_power = 1 << i;\n for (uint32_t j = 0; j < 21; ++j) {\n uint32_t five_power = three_power;\n for (uint32_t k = 0; k < 14; ++k) {\n ugly_nums.push_back(five_power);\n const uint32_t next_five_power= five_power * 5u;\n if (next_five_power / 5u != five_power)\n break;\n five_power = next_five_power;\n }\n const uint32_t next_three_power= three_power * 3u;\n if (next_three_power / 3u != three_power)\n break;\n three_power = next_three_power;\n }\n }\n std::sort(ugly_nums.begin(), ugly_nums.end());\n return ugly_nums[n - 1];\n }\n bool isUgly(uint32_t n) {\n while (true) {\n if (n == 1)\n return true;\n if (n % 2 == 0)\n n /= 2;\n else if (n % 3 == 0)\n n /= 3;\n else if (n % 5 == 0)\n n /= 5;\n else\n return false;\n }\n }\n};",
"memory": "15515"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n \n int count = 1,idx=1;\n vector<long long> ugly(n+1);\n ugly[0] = 1;\n vector<long long> m2,m3,m5;\n m2.push_back(ugly[0]*2);\n m3.push_back(ugly[0]*3);\n m5.push_back(ugly[0]*5);\n\n while(count < n){\n long long z = min(m2.front(),m3.front());\n ugly[idx] = min(z,m5.front());\n if(m2.front()== ugly[idx]) m2.erase(m2.begin());\n if(m3.front()== ugly[idx]) m3.erase(m3.begin());\n if(m5.front()== ugly[idx]) m5.erase(m5.begin());\n\n m2.push_back(ugly[idx]*2);\n m3.push_back(ugly[idx]*3);\n m5.push_back(ugly[idx]*5);\n\n idx++;\n count++;\n\n }\n \n return ugly[n-1];\n \n }\n};",
"memory": "15871"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int isUgly(priority_queue<long, vector<long>, greater<long> >& pq, int n, int &prev, int &cnt) {\n if(cnt == n) return prev;\n long top = pq.top();\n if(prev == top) pq.pop();\n else{\n cnt++;\n // cout<<top*2<<\" \"<<top*3<<\" \"<<top*5<<endl;\n pq.push(top*2);\n pq.push(top*3);\n pq.push(top*5);\n pq.pop();\n }\n prev = top;\n return isUgly(pq, n, prev, cnt);\n }\n\n int nthUglyNumber(int n) {\n priority_queue<long, vector<long>, greater<long> > pq;\n pq.push(1);\n int prev = 0, cnt = 0;\n return isUgly(pq, n, prev, cnt);\n }\n};",
"memory": "15871"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n if (n <= 6)\n return n;\n queue<pair<unsigned, unsigned>> q2, q3;\n queue<unsigned> q5;\n unsigned x = 6;\n int k = 6;\n q2.push({8, 4}); q2.push({10, 5});\n q3.push({9, 3});\n q5.push(10);\n while (k < n) {\n int newx;\n if (q2.front().first <= q3.front().first && q2.front().first <= q5.front()) {\n newx = q2.front().first;\n q3.push({3 * q2.front().second, q2.front().second});\n q2.pop();\n } else if (q3.front().first < q2.front().first && q3.front().first <= q5.front()) {\n newx = q3.front().first;\n q5.push(5 * q3.front().second);\n q3.pop();\n } else {\n newx = q5.front();\n q5.pop();\n }\n if (newx > x) {\n x = newx;\n q2.push({2*x, x});\n k++;\n }\n }\n return x;\n }\n};",
"memory": "16228"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "#define i64 long long\nclass Solution {\npublic:\n void rec(int n,priority_queue<i64>& q,vector<int>&chk,long long val,i64 ind){\n if(ind>3 || val>=INT_MAX){\n return;\n }\n if(q.size()<=n){\n q.push(val);\n }\n if(q.size()>n){\n q.pop();\n }\n for(int j=ind;j<3;j++){\n rec(n,q,chk,val*chk[j],j);\n }\n }\n int nthUglyNumber(int n) {\n priority_queue<i64> q;\n vector<int> chk(3);\n chk[0]=2,chk[1]=3,chk[2]=5;\n rec(n,q,chk,1,0);\n return (int)q.top();\n }\n};",
"memory": "16228"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<long long>dp(n,-1);\n priority_queue<long long,vector<long long>,greater<long long>>pq;\n pq.push(1);\n int i=0;\n while(!pq.empty() && dp[n-1]==-1)\n {\n long long top=pq.top();\n pq.pop();\n dp[i++]=top;\n pq.push(top*2);\n pq.push(top*3);\n pq.push(top*5);\n while(!pq.empty() && top == pq.top())\n pq.pop();\n }\n return dp[n-1];\n }\n};",
"memory": "16584"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n priority_queue<long long int ,vector<long long int>,greater<long long int>> pq;\n pq.push(1);\n vector<long long int> ans(n,0);\n int i=0;\n int prev=0;\n while(i<n){\n long long int a=pq.top();\n\n pq.pop();\n if(prev==a){\n continue;\n }\n ans[i++]=a;\n long long int fir=a*2;\n if(fir<INT_MAX){\n pq.push(fir); \n }\n long long int sec=a*3;\n if(sec<INT_MAX){\n pq.push(sec); \n }\n long long int thir=a*5;\n if(thir<INT_MAX){\n pq.push(thir); \n }\n prev=a;\n }\n return ans[n-1];\n\n \n }\n};",
"memory": "16584"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) \n {\n vector<int> uglyNumbers(1,1);\n\n std::queue<long> twoList, threeList, fiveList;\n twoList.push(1);\n threeList.push(1);\n fiveList.push(1);\n\n while ( uglyNumbers.size() < n )\n {\n auto nextNum2 = twoList.front() * 2;\n auto nextNum3 = threeList.front() * 3;\n auto nextNum5 = fiveList.front() * 5;\n if ( nextNum2 < nextNum3 && nextNum2 < nextNum5 )\n {\n uglyNumbers.push_back(nextNum2); \n twoList.pop();\n\n twoList.push(nextNum2);\n threeList.push(nextNum2);\n fiveList.push(nextNum2);\n }\n else if ( nextNum3 < nextNum5 )\n {\n uglyNumbers.push_back(nextNum3);\n threeList.pop();\n\n threeList.push(nextNum3);\n fiveList.push(nextNum3);\n }\n else\n {\n uglyNumbers.push_back(nextNum5);\n fiveList.pop();\n\n fiveList.push(nextNum5);\n }\n\n\n }\n\n return uglyNumbers[n-1];\n }\n};",
"memory": "16940"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n)\n {\n std::vector<std::uint64_t> ugly_numbers;\n ugly_numbers.reserve(n);\n std::priority_queue<std::pair<std::uint64_t,std::uint8_t>,std::vector<std::pair<std::uint64_t,std::uint8_t>>,std::greater<>> heap;\n heap.emplace(1,2);\n \n for(int found_numbers=1;found_numbers<=n;++found_numbers)\n {\n std::uint64_t product=heap.top().first;\n std::uint8_t prev_prime=heap.top().second;\n heap.pop();\n ugly_numbers.push_back(product);\n\n if(2==prev_prime)\n {\n heap.emplace(2*product,2);\n heap.emplace(3*product,3);\n heap.emplace(5*product,5);\n }\n \n else if(3==prev_prime)\n {\n heap.emplace(3*product,3);\n heap.emplace(5*product,5);\n }\n\n else\n {\n heap.emplace(5*product,5);\n }\n }\n\n return ugly_numbers[n-1];\n }\n};",
"memory": "16940"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<long> ugly;\n Solution(){\n ugly.resize(1691);\n long id2=0, id3=0, id5=0;\n \n ugly[0] = 1;\n for(int i=1; i<1691; i++){\n long t2 = ugly[id2]*2, t3 = ugly[id3]*3, t5 = ugly[id5]*5;\n \n if (t2 < t3){\n if (t2 <= t5) {\n ugly[i] = t2;\n }\n else {\n ugly[i] = t5;\n }\n }else{\n if (t3 <= t5){\n ugly[i] = t3;\n }\n else {\n ugly[i] = t5;\n }\n }\n \n if (ugly[i] == t2) ++id2;\n if (ugly[i] == t3) ++id3;\n if (ugly[i] == t5) ++id5;\n \n }\n }\n \n int nthUglyNumber(int n) {\n for(int i=0; i<10; i++) cout << ugly[i] << \" \";\n return ugly[n-1];\n }\n};",
"memory": "17296"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n \n vector<long long> ans(1700);\n ans[1] = 1;\n int p2 = 1; int p3 = 1; int p5 = 1;\n \n for(int i=2; i<1700; i++){\n long num_min = min({ ans[p2]*2, ans[p3]*3, ans[p5]*5 });\n ans[i] = num_min;\n if(ans[i]==ans[p2]*2){\n p2++;\n }\n if(ans[i]==ans[p3]*3){\n p3++;\n }\n if(ans[i]==ans[p5]*5){\n p5++;\n }\n }\n\n return ans[n];\n }\n};",
"memory": "17296"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n priority_queue<long, vector<long>, greater<long>>pq;\n pq.push(1);\n vector<int>ugly;\n\n while(ugly.size()<n){\n long curNum=pq.top();\n while(!pq.empty() && pq.top()==curNum)\n pq.pop();\n ugly.push_back((int)curNum);\n pq.push(curNum*2);\n pq.push(curNum*3);\n pq.push(curNum*5);\n }\n return ugly[n-1];\n }\n};",
"memory": "17653"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void helper(int twos, int threes, int fives, int n, long curr, vector<long>& nums) {\n switch (n) {\n case -1:\n return;\n break;\n case 2:\n for (int i = 0; i <= twos; ++i) {\n nums.push_back(curr);\n helper(i, threes, fives, 3, curr, nums);\n curr *= 2;\n }\n break;\n case 3:\n helper(twos, 0, fives, 5, curr, nums);\n for (int i = 1; i <= threes - (twos / 1.58); ++i) {\n curr *= 3;\n nums.push_back(curr);\n helper(twos, i, fives, 5, curr, nums);\n }\n break;\n case 5:\n helper(twos, threes, 0, -1, curr, nums);\n for (int i = 1; i <= fives - (twos / 2.32) - (threes / 1.465); ++i) {\n curr *= 5;\n nums.push_back(curr);\n helper(twos, threes, i, -1, curr, nums);\n }\n break;\n }\n }\n\n int nthUglyNumber(int n) {\n vector<long> nums;\n\n double num = 4.7 * pow(2.34, log(n)/log(10)-1);\n int num_threes = ceil(num / 1.58);\n int num_fives = ceil(num / 2.32);\n int num_twos = num;\n\n helper(num_twos, num_threes, num_fives, 2, 1, nums);\n sort(nums.begin(), nums.end());\n return nums[n-1];\n }\n};",
"memory": "17653"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n queue<long> a, b, c;\n a.push(1);\n b.push(1);\n c.push(1);\n int ans = 1;\n for(int i =1;i<n;i++){\n ans = min(min(a.front()*2, b.front()*3),c.front()*5);\n a.push(ans);\n b.push(ans);\n c.push(ans);\n if(a.front()*2 == ans)\n a.pop();\n if(b.front()*3 == ans)\n b.pop();\n if(c.front()*5 == ans)\n c.pop();\n }\n return ans;\n }\n};\n",
"memory": "18009"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n queue<long> q2;\n queue<long> q3;\n queue<long> q5;\n q2.push(2);\n q3.push(3);\n q5.push(5);\n\n long ugly = 1;\n long prev = 1;\n\n for (int count = 1; count < n; count++) {\n prev = ugly;\n if (q2.front() < q3.front()) {\n if (q2.front() < q5.front()) {\n ugly = q2.front();\n q2.pop();\n }\n else {\n ugly = q5.front();\n q5.pop();\n }\n }\n else {\n if (q3.front() < q5.front()) {\n ugly = q3.front();\n q3.pop();\n }\n else {\n ugly = q5.front();\n q5.pop();\n }\n }\n\n if (prev == ugly) {\n count--;\n continue;\n }\n\n q2.push(ugly * 2);\n q3.push(ugly * 3);\n q5.push(ugly * 5);\n }\n\n return (int)ugly;\n }\n};",
"memory": "18009"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<long long int>v={1};\n int i = 0;\n void solve(int n) {\n while(v.size()<=(n+200)) {\n long long int x = v[i];\n if(!count(v.begin(),v.end(), (x*2)))\n v.push_back(x*2);\n if(!count(v.begin(),v.end(), (x*3)))\n v.push_back(x*3);\n if(!count(v.begin(),v.end(), (x*5)))\n v.push_back(x*5);\n sort(v.begin(),v.end());\n i++;\n }\n }\n \n int nthUglyNumber(int n) {\n solve(n);\n // for(int i:v)\n // cout<<i<<\" \";\n sort(v.begin(),v.end());\n return v[n-1];\n }\n};",
"memory": "19790"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\n vector<int> primes = {2, 3, 5};\n int size = primes.size();\npublic:\n int nthUglyNumber(int n) {\n vector<int> ugs(n + 1, 0); \n vector<int> primes_idx(size, 1);\n\n ugs[1] = 1;\n for (int i = 2; i <= n; i++) {\n vector<int> temp(size, 0);\n int _min = INT_MAX;\n for (int j = 0; j < size; j++) {\n temp[j] = ugs[primes_idx[j]] * primes[j];\n _min = min(_min, temp[j]);\n }\n for (int j = 0; j < size; j++) {\n if (temp[j] == _min)\n primes_idx[j]++;\n }\n ugs[i] = _min;\n }\n return ugs[n];\n }\n};",
"memory": "19790"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "#include<bits/stdc++.h>\nusing namespace std;\nclass Solution {\npublic:\n long long int nthUglyNumber(long long int n) {\n priority_queue<long long int, vector<long long int>, greater<long long int>> q;\n q.push(1);\n vector<long long int> op;\n while(op.size()<=n)\n { \n long long int current = q.top();\n // cerr<<current;\n while(!q.empty() && current==q.top())\n {\n q.pop();\n }\n // cerr<<\" here\";\n op.push_back(current);\n q.push(2*current);\n q.push(3*current);\n q.push(5*current);\n }\n return op[n-1];\n }\n};\n",
"memory": "20146"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "// use priority_queue to get min and second min\nclass Solution {\npublic:\n int nthUglyNumber(int n) {\n if (n <= 3)\n return n;\n vector<long long> Uglys{1, 2, 3};\n vector<long long> Us{1, 2, 3};\n vector<long long> ts{5, 4, 6};\n vector<int> Vs{3, 1, 1};\n \n priority_queue<long long, vector<long long>, greater<long long>> Ts{};\n Ts.push(5);\n Ts.push(4);\n Ts.push(6);\n\n int next_Uglys = 3;\n while (true)\n {\n long long next = Ts.top();\n\n Uglys.push_back(next);\n if (Uglys.size() >= n)\n break;\n\n while (Ts.top() == next)\n Ts.pop();\n \n if (Ts.top() > Uglys[next_Uglys] * 2)\n {\n Us.push_back(Uglys[next_Uglys++]);\n Vs.push_back(0);\n int pos = Us.size() - 1;\n ts.push_back(getTs(Us[pos], Vs[pos]));\n if (next < ts[pos]) // if equal, ts[po] will be replaced later\n Ts.push(ts[pos]);\n }\n for (int pos = 0; pos < ts.size(); pos++)\n {\n bool get_ts = false;\n while (ts[pos] == next)\n {\n if (Vs[pos] == 3)\n {\n Us[pos] = Uglys[next_Uglys++];\n Vs[pos] = 0;\n }\n ts[pos] = getTs(Us[pos], Vs[pos]);\n get_ts = true;\n }\n if (get_ts)\n {\n Ts.push(ts[pos]);\n }\n }\n }\n return Uglys[n - 1];\n }\n \n long long getTs(long long U, int &V)\n {\n V++;\n switch (V)\n {\n case 1:\n return U * 2;\n case 2:\n return U * 3;\n case 3:\n return U * 5;\n }\n return 0;\n }\n};",
"memory": "20503"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n int a=1,b=1,c=1;\n // long long a=1,b=1,c=1;\n int ca=1,cb=1,cc=1;\n vector<int> dp(n+1,1);\n for(int i=2;i<=n;i++)\n {\n vector<int> v={a*2,b*3,c*5};\n sort(begin(v),end(v));\n dp[i]=v[0];\n // dp[i]=min( a*2, min(b*3,c*5) );\n if( dp[i]==a*2 ) a=dp[++ca];\n if( dp[i]== b*3 ) b=dp[++cb];\n if( dp[i]== c*5 ) c=dp[++cc];\n cout<<dp[i];\n cout<<\" a = \"<<a<<\" b = \"<<b<<\" c = \"<<c<<endl;\n }\n return dp[n];\n }\n};",
"memory": "20503"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n priority_queue<long, vector<long>, greater<long>> uglys;\n vector<long> used_vals = {0};\n uglys.push(1);\n long ans = 0;\n\n while (n != 0) {\n ans = uglys.top();\n uglys.pop();\n if (used_vals.back() == ans) {\n continue;\n }\n uglys.push(ans * 2);\n uglys.push(ans * 3);\n uglys.push(ans * 5);\n used_vals.push_back(ans);\n --n;\n }\n return ans;\n }\n};",
"memory": "20859"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n int nthUglyNumber(int n) {\n vector<int>v={2,3,5};\n priority_queue<long,vector<long>,greater<long>>q;\n vector<long>vv;\n long cur;\n q.push(1);\n vv.push_back(1);\n \n for(int i=0;i<n;i++){\n cur = q.top();\n q.pop();\n for(int i:v){\n long nn = cur*i;\n if(find(vv.begin(),vv.end(),nn) == vv.end()){\n q.push(nn);\n vv.push_back(nn);\n } \n }\n }\n return (int)cur;\n }\n};",
"memory": "20859"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n struct P {\n long long val;\n int k2;\n int k3;\n int k5;\n };\n\n struct PCompare {\n bool operator()(const P& y, const P& x) const { return x.val < y.val; }\n };\n\n int nthUglyNumber(int n) {\n std::priority_queue<P, std::vector<P>, PCompare> q;\n q.push({ 1, 0, 0, 0 });\n\n int prv_val{ 0 };\n\n for (;;) {\n P cur_p{ q.top() };\n q.pop();\n\n if (prv_val == cur_p.val) { continue; }\n\n prv_val = cur_p.val;\n\n if (--n == 0) { break; }\n\n q.push(P{ cur_p.val * 2, cur_p.k2 + 1, cur_p.k3, cur_p.k5 });\n q.push(P{ cur_p.val * 3, cur_p.k2, cur_p.k3 + 1, cur_p.k5 });\n q.push(P{ cur_p.val * 5, cur_p.k2, cur_p.k3, cur_p.k5 + 1 });\n }\n\n return prv_val;\n }\n};\n",
"memory": "21215"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<int> primes = {2, 3, 5}; // Initialize the primes array\n vector<int> indices = {0, 0, 0}; // Initialize indices for multiples of 2, 3, 5\n vector<int> uglyArr(1, 1); // Initialize the ugly number array with 1\n\n for (int i = 1; i < n; ++i) {\n // Calculate the next possible ugly numbers\n vector<int> next_uglies = {\n uglyArr[indices[0]] * primes[0],\n uglyArr[indices[1]] * primes[1],\n uglyArr[indices[2]] * primes[2]\n };\n int min_value = *min_element(next_uglies.begin(), next_uglies.end()); // Find the smallest value\n uglyArr.push_back(min_value); // Append the smallest value to uglyArr\n\n // Update indices for primes that generated the current min_value\n for (int j = 0; j < 3; ++j) {\n if (next_uglies[j] == min_value) {\n ++indices[j];\n }\n }\n }\n\n return uglyArr[n - 1];\n }\n};",
"memory": "21215"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\n public:\n static int nthUglyNumber(int n) {\n vector<int> primes = {2, 3, 5}; // Initialize the primes array\n vector<int> indices = {0, 0, 0}; // Initialize indices for multiples of 2, 3, 5\n vector<int> uglyArr{1}; // Initialize the ugly number array with 1\n\n for (int i = 1; i < n; ++i) {\n // Calculate the next possible ugly numbers\n vector<int> next_uglies = {uglyArr[indices[0]] * primes[0], uglyArr[indices[1]] * primes[1],\n uglyArr[indices[2]] * primes[2]};\n int min_value = *min_element(next_uglies.begin(), next_uglies.end()); // Find the smallest value\n uglyArr.push_back(min_value); // Append the smallest value to uglyArr\n\n // Update indices for primes that generated the current min_value\n for (int j = 0; j < 3; ++j) {\n if (next_uglies[j] == min_value) {\n ++indices[j];\n }\n }\n }\n\n return uglyArr[n - 1];\n }\n};",
"memory": "21571"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<int> primes = {2, 3, 5};\n vector<int> indices = {0, 0, 0};\n vector<int> uglyArr(1, 1);\n\n for (int i = 1; i < n; ++i) {\n vector<int> next_uglies = {\n uglyArr[indices[0]] * primes[0],\n uglyArr[indices[1]] * primes[1],\n uglyArr[indices[2]] * primes[2]\n };\n\n int min_value = *min_element(next_uglies.begin(), next_uglies.end());\n uglyArr.push_back(min_value);\n\n for (int j = 0; j < 3; ++j) {\n if (next_uglies[j] == min_value) ++indices[j];\n }\n }\n\n return uglyArr[n-1];\n }\n};",
"memory": "21571"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n Solution()\n {\n std::priority_queue<std::pair<std::uint64_t,std::uint8_t>,std::vector<std::pair<std::uint64_t,std::uint8_t>>,std::greater<>> heap;\n heap.emplace(1,2);\n int back=-1;\n \n for(int found_numbers=0;found_numbers<1690;++found_numbers)\n {\n std::uint64_t product=heap.top().first;\n std::uint8_t prev_prime=heap.top().second;\n heap.pop();\n ugly_numbers[++back]=product;\n\n if(2==prev_prime)\n {\n heap.emplace(2*product,2);\n heap.emplace(3*product,3);\n heap.emplace(5*product,5);\n }\n \n else if(3==prev_prime)\n {\n heap.emplace(3*product,3);\n heap.emplace(5*product,5);\n }\n\n else\n {\n heap.emplace(5*product,5);\n }\n }\n }\n\n int nthUglyNumber(int n)\n {\n return ugly_numbers[n-1];\n }\n\nprivate:\n static int ugly_numbers[1690];\n};\n\nint Solution::ugly_numbers[1690]={};",
"memory": "21928"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n if(n==1)\n return n;\n vector<queue<long long>> v;\n queue<long long> q;\n q.push(2);\n v.push_back(q);\n q.pop();\n q.push(3);\n v.push_back(q);\n q.pop();\n q.push(5);\n v.push_back(q);\n q.pop();\n long long ans = 1;\n for(int i=2; i<n+1; i++)\n {\n for(int i=0; i<3; i++)\n {\n while(v[i].front()==ans)\n v[i].pop();\n }\n ans = min(v[0].front(), min(v[1].front(), v[2].front()));\n v[0].push(ans*2);\n v[1].push(ans*3);\n v[2].push(ans*5);\n }\n return ans;\n }\n};",
"memory": "21928"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int>ans={1,2,3,4,5,6,8,9,10,12};\n int i=6,j=4,k=4;\n Solution(){\n while(ans.size()<1690){\n if((2*ans[i])<=(3*ans[j]) and (2*ans[i])<=(5*ans[k])){\n ans.push_back(2*ans[i]);\n if((2*ans[i])==(3*ans[j])){\n j++;\n }\n if((2*ans[i])==(5*ans[k])){\n k++;\n }\n i++;\n }\n else if((2*ans[i])>=(3*ans[j]) and (3*ans[j])<=(5*ans[k])){\n ans.push_back(3*ans[j]);\n if((2*ans[i])==(3*ans[j])){\n i++;\n }\n if((3*ans[j])==(5*ans[k])){\n k++;\n }\n j++;\n }\n else{\n ans.push_back(5*ans[k]);\n if((2*ans[i])==(5*ans[k])){\n i++;\n }\n if((3*ans[j])==(5*ans[k])){\n j++;\n }\n k++;\n }\n }\n }\n int nthUglyNumber(int n) {\n return ans[n-1]; \n }\n};",
"memory": "22284"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n // map<int, bool> uglyMap; \n // int count = 0;\n // int i = 1;\n // while ( count < n ) {\n // if ( i == 1 ) {\n // uglyMap[i] = true;\n // } else if ( i%2 == 0 ) {\n // uglyMap[i] = uglyMap[i/2];\n // } else if ( i%3 == 0 ) {\n // uglyMap[i] = uglyMap[i/3];\n // } else if ( i%5 == 0 ) {\n // uglyMap[i] = uglyMap[i/5];\n // } else {\n // uglyMap[i] = false;\n // }\n // if ( uglyMap[i] )\n // count++;\n // i++;\n // }\n // return (i-1);\n vector<int> uglyArray;\n vector<int> twoArray;\n vector<int> threeArray;\n vector<int> fiveArray;\n uglyArray.push_back(1);\n twoArray.push_back(1);\n threeArray.push_back(1);\n fiveArray.push_back(1);\n int uglyIdx = 0;\n int twoIdx = 0;\n int threeIdx = 0;\n int fiveIdx = 0;\n if (n == 1){\n return 1;\n }\n for ( int i = 1; i < n; i++ ) {\n int val = 0;\n int twoVal = twoArray[twoIdx] * 2;\n int threeVal = threeArray[threeIdx] * 3;\n int fiveVal = fiveArray[fiveIdx] * 5;\n if ( twoVal <= threeVal && twoVal <= fiveVal ) {\n val = twoVal;\n } else if ( threeVal <= twoVal && threeVal <= fiveVal ) {\n val = threeVal;\n } else {\n val = fiveVal;\n }\n cout << \"val : \" << val << endl;\n uglyArray.push_back(val);\n twoArray.push_back(val);\n threeArray.push_back(val);\n fiveArray.push_back(val);\n while ( twoVal == val ) {\n twoIdx++;\n twoVal = twoArray[twoIdx] * 2;\n }\n while ( threeVal == val ) {\n threeIdx++;\n threeVal = threeArray[threeIdx] * 3;\n }\n while ( fiveVal == val ) {\n fiveIdx++;\n fiveVal = fiveArray[fiveIdx] * 5;\n }\n }\n return uglyArray[n-1]; \n }\n};",
"memory": "22284"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "#define ll long long\nclass Solution {\n priority_queue<int, vector<int>, greater<int>> pq;\nprivate:\n void preComp(int N){\n int n = N;\n for(ll p=0; p<=n/2 + 1; p++){\n for(ll q=0; q<=n/3 + 1; q++){\n for(ll r=0; r<=n/5 + 1; r++){\n if(1LL*pow(2LL,p)*pow(3LL,q)*pow(5LL,r)>=INT_MAX){\n break;\n }\n long long val = 1LL*pow(2LL,p)*pow(3LL,q)*pow(5LL,r);\n pq.push(val);\n }\n }\n }\n }\npublic:\n int nthUglyNumber(int n) {\n // 2^p * 3^q * 5^r\n preComp(n);\n for(int i=0; i<n-1; i++){\n pq.pop();\n }\n return pq.top();\n }\n};",
"memory": "22640"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "#define ll long long\nclass Solution {\n priority_queue<int, vector<int>, greater<int>> pq;\n\nprivate:\n void preComp(int N) {\n ll n = 1LL * N;\n for (ll p = 0; p <= n / 2 + 1; p++) {\n for (ll q = 0; q <= n / 3 + 1; q++) {\n for (ll r = 0; r <= n / 5 + 1; r++) {\n if (1LL * pow(2LL, p) * pow(3LL, q) * pow(5LL, r) >\n INT_MAX) {\n break;\n }\n long long val =\n 1LL * pow(2LL, p) * pow(3LL, q) * pow(5LL, r);\n pq.push(val);\n }\n }\n }\n }\n\npublic:\n int nthUglyNumber(int n) {\n // 2^p * 3^q * 5^r\n preComp(n);\n for (int i = 0; i < n - 1; i++) {\n pq.pop();\n }\n return pq.top();\n }\n};",
"memory": "22640"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n int nthUglyNumber(int n) {\n \n vector<int> res;\n for(int i=0;i<=32;i++){\n if(i > (log2(INT_MAX)/log2(2))) continue;\n int a = pow(2,i); \n for(int j=0;j<=20;j++){\n if(j > (log2(INT_MAX)/log2(3))) continue;\n int b = pow(3,j);\n if(a > (INT_MAX/b)) continue;\n for(int k=0;k<=14;k++){\n if(k > (log2(INT_MAX)/log2(5))) continue;\n int c = pow(5,k);\n if(a*b > (INT_MAX/c)) continue;\n int subres = a * b * c; \n res.push_back(subres);\n }\n }\n }\n sort(res.begin(),res.end());\n \n return res[n-1];\n }\n};",
"memory": "22996"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "//#define int double\nclass Solution {\npublic:\n int nthUglyNumber(int n) {\n long long a,b,c;\n vector<int>v;\n for(int i=0;i<32;i++){\n a=pow(5,i);\n if(a>INT_MAX){\n break;\n }\n for(int j=0;j<32;j++){\n b=pow(3,j);\n if(b>INT_MAX){\n break;\n }\n if(a*b>INT_MAX){\n break;\n }\n for(int k=0;k<32;k++){\n c=pow(2,k);\n if(c>INT_MAX){\n break;\n }\n if(a*b*c>INT_MAX){\n break;\n }\n v.push_back(a*b*c);\n }\n }\n }\n sort(v.begin(),v.end());\n return v[n-1];\n \n \n \n \n\n \n } \n};",
"memory": "22996"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "typedef struct {\n uint val;\n uint cur;\n uint potential[3];\n} ugly;\n\nclass Solution {\n vector <ugly> sequence;\n uint current_l1;\n uint cur_ugly;\npublic:\n ugly create_ugly(uint n) {\n ugly a;\n a.val = n;\n a.cur = 0;\n a.potential[0] = n*2;\n a.potential[1] = n*3;\n a.potential[2] = n*5;\n return a;\n }\n uint find_min(uint l1) {\n /*\n if (sequence[l1].potential[sequence[l1].cur] <= sequence[l2].potential[sequence[l2].cur]) {\n if (sequence[l1].potential[sequence[l1].cur] <= sequence[l3].potential[sequence[l3].cur]) {\n return l1;\n } else {\n return l3;\n }\n } else {\n if (sequence[l2].potential[sequence[l2].cur] <= sequence[l3].potential[sequence[l3].cur]) {\n return l2;\n } else {\n return l3;\n }\n }*/\n uint i = l1;\n uint min = l1;\n bool flag = true;\n while (flag) {\n if (sequence[i].cur == 0) {\n flag = false;\n }\n if (sequence[i].potential[sequence[i].cur] <= cur_ugly){\n sequence[i].cur++;\n }\n if (sequence[min].potential[sequence[min].cur] > sequence[i].potential[sequence[i].cur]) {\n min = i;\n }\n i++;\n //cout << \"\\ni = \"<< i <<\", val = \"<<sequence[i].val << \", cur = \"<<sequence[i].cur << \"min\"<<sequence[min].val;\n }\n return min;\n }\n\n void recal_l1 () { \n /*for (uint i = current_l1; i <= current_l1 + 2; i++) {\n cout << \"\\n QweQwe : i \" << i << \"cur \" << sequence[i].cur << \" val \"<< sequence[i].val <<\" val at cur \" << sequence[i].potential[sequence[i].cur] <<\" cur_ugly\" << cur_ugly;\n if (sequence[i].potential[sequence[i].cur] == cur_ugly) {\n if (sequence[i].cur == 2) {\n current_l1++;\n continue;\n } else {\n sequence[i].cur++;\n }\n }\n }*/\n if (sequence[current_l1].cur == 2 && sequence[current_l1].potential[sequence[current_l1].cur] == cur_ugly) {\n current_l1++;\n }\n bool flag = true;\n uint i = current_l1;\n while (flag) {\n if (sequence[i].potential[sequence[i].cur] <= cur_ugly) {\n sequence[i].cur++;\n }\n if (sequence[i].cur == 0) {\n flag = false;\n }\n i++;\n }\n }\n uint nthUglyNumber(uint n) {\n switch (n){\n case 0 :\n return 0;\n case 1 :\n return 1;\n case 2 :\n return 2;\n case 3 :\n return 3;\n }\n ugly def1 = create_ugly(1);\n def1.cur = 2;\n ugly def2 = create_ugly(2);\n def2.cur = 1;\n ugly def3 = create_ugly(3);\n ugly def4 = create_ugly(4);\n\n sequence.push_back(def1);\n sequence.push_back(def2);\n sequence.push_back(def3);\n sequence.push_back(def4); \n\n uint prev = 0;\n current_l1 = 0;\n cur_ugly = 4;\n for (uint i = 3; i < n -1; i++) {\n uint choice = find_min(current_l1);\n cur_ugly = sequence[choice].potential[sequence[choice].cur];\n // cout<<\"\\nCur_ugly \"<<cur_ugly << \" choice \"<<choice <<\" cur \" << sequence[choice].cur;\n ugly temp = create_ugly(cur_ugly);\n sequence.push_back(temp);\n recal_l1(); \n }\n return cur_ugly; \n }\n};",
"memory": "23353"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "using ll = long long;\n\nclass Solution {\npublic:\n int nthUglyNumber(int n) {\n ll max_k = 2 * (n / 3ll + 1) * n * n;\n\n std::vector<ll> nums;\n for (ll k5 = 1; k5 <= max_k; k5 *= 5) {\n for (ll k3 = 1; k3 <= max_k / k5; k3 *= 3) {\n for (ll k2 = 1; k2 <= max_k / (k3 * k5); k2 *= 2) {\n nums.push_back(k2 * k3 * k5);\n }\n }\n }\n\n std::sort(nums.begin(), nums.end());\n return nums[n - 1];\n }\n};",
"memory": "23353"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n int nthUglyNumber(int n) {\n\n int maxval = std::numeric_limits<int>::max();\n \n std::queue<int> arr2;\n arr2.push(1);\n std::queue<int> arr3;\n arr3.push(1);\n std::queue<int> arr5;\n arr5.push(1);\n\n // std::vector<int> prevs = {1};\n\n int cur = 1;\n\n for (int i = 0; i < n; i++) {\n std::vector<int> possib_vals = {arr2.front(), arr3.front(), arr5.front()};\n\n cur = *std::min_element(possib_vals.begin(), possib_vals.end());\n\n if (maxval / cur >= 2)\n arr2.push(2 * cur);\n if (maxval / cur >= 3)\n arr3.push(3 * cur);\n if (maxval / cur >= 5)\n arr5.push(5 * cur);\n\n if (possib_vals[0] == cur) arr2.pop();\n if (possib_vals[1] == cur) arr3.pop();\n if (possib_vals[2] == cur) arr5.pop();\n\n // std::cout << cur << std::endl;\n }\n\n return cur;\n }\n};",
"memory": "23709"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n long long cache[50][50][50] = {0};\n \n int nthUglyNumber(int n) {\n vector<int> ugly_numbers = {1};\n \n int i=0, j=0, k=0;\n \n int x = 2123366400;\n \n while (x%2 == 0) {\n i++;\n x/=2;\n }\n x = 2123366400;\n while (x%3 == 0) {\n j++;\n x/=3;\n }\n x = 2123366400;\n while (x%5 == 0) {\n k++;\n x/=5;\n }\n \n cache[0][0][0] = 1;\n x = 0;\n \n for (int a=0; a<50; a++) {\n for (int b=0; b<50; b++) {\n for (int c=0; c<50; c++) {\n \n if (a+b+c == 0) {\n continue;\n }\n \n // cout<<\"a: \"<<a<<\" | b: \"<<b<<\" | c: \"<<c<<\"\\n\";\n \n if (a != 0 && cache[a-1][b][c] != 0 && cache[a-1][b][c] * 2 <= 2123366400) {\n cache[a][b][c] = cache[a-1][b][c] * 2;\n }\n else if (b != 0 && cache[a][b-1][c] != 0 && cache[a][b-1][c] * 3 <= 2123366400) {\n cache[a][b][c] = cache[a][b-1][c] * 3;\n }\n else if (c != 0 && cache[a][b][c-1] != 0 && cache[a][b][c-1] * 5 <= 2123366400) {\n cache[a][b][c] = cache[a][b][c-1] * 5;\n }\n else {\n continue;\n }\n \n if (cache[a][b][c] <= 2123366400) {\n x++;\n ugly_numbers.push_back(cache[a][b][c]);\n }\n }\n }\n }\n \n sort(ugly_numbers.begin(), ugly_numbers.end());\n \n // cout<<\"x: \"<<x<<\"\\n\";\n \n return ugly_numbers[n-1];\n }\n};",
"memory": "24065"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> getVec(int n){\n long long num=1;\n vector<int> vec;\n while(num<=INT_MAX){\n vec.push_back(num);\n num*=n;\n }\n return vec;\n }\n int nthUglyNumber(int n) {\n if(n==1){\n return 1;\n }\n vector<int> a=getVec(2);\n vector<int> b=getVec(3);\n vector<int> c=getVec(5);\n vector<int> ans;\n for(int i=0;i<a.size();i++){\n for(int j=0;j<b.size();j++){\n for(int k=0;k<c.size();k++){\n long long x=a[i];\n long long y=b[j];\n long long z=c[k];\n if(x*y<=INT_MAX && y*z<=INT_MAX && z*x<=INT_MAX && x*y*z<=INT_MAX) {\n ans.push_back(x*y*z);\n }\n }\n }\n }\n \n sort(ans.begin(),ans.end());\n // cout<<ans.back();\n cout<<ans.size();\n return ans[n-1];\n\n }\n};",
"memory": "24065"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n int nthUglyNumber(int n) {\n if (n <= 6)\n return n;\n\n int answer = 0;\n int count = 1;\n\n std::vector<int64_t> v2({2});\n std::vector<int64_t> v3({3});\n std::vector<int64_t> v5({5});\n v2.reserve(n);\n v2.reserve(n);\n v2.reserve(n);\n\n int i2 = 0;\n int i3 = 0;\n int i5 = 0;\n\n while (count < n) {\n int64_t m = std::min(std::min(v2[i2], v3[i3]), v5[i5]);\n answer = m;\n ++count;\n if (v2[i2] == m)\n ++i2;\n if (v3[i3] == m)\n ++i3;\n if (v5[i5] == m)\n ++i5;\n\n v2.push_back(2 * m);\n v3.push_back(3 * m);\n v5.push_back(5 * m);\n }\n\n return answer;\n }\n};",
"memory": "24421"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n int nthUglyNumber(int n) {\n if (n <= 6)\n return n;\n\n int answer = 0;\n int count = 1;\n\n std::vector<int64_t> v2({2});\n std::vector<int64_t> v3({3});\n std::vector<int64_t> v5({5});\n v2.reserve(n);\n v2.reserve(n);\n v2.reserve(n);\n\n int i2 = 0;\n int i3 = 0;\n int i5 = 0;\n\n while (count < n) {\n int64_t m = std::min(std::min(v2[i2], v3[i3]), v5[i5]);\n answer = m;\n ++count;\n if (v2[i2] == m)\n ++i2;\n if (v3[i3] == m)\n ++i3;\n if (v5[i5] == m)\n ++i5;\n\n v2.push_back(2 * m);\n v3.push_back(3 * m);\n v5.push_back(5 * m);\n }\n\n return answer;\n }\n};",
"memory": "24421"
} |
264 | <p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1690</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<long long> nums;\n deque<long long> test2;\n deque<long long> test3;\n deque<long long> test5;\n test2.push_back(1);\n while (nums.size()!=n){\n long long num = min({\n (!test2.empty())?test2.front():INT_MAX,\n (!test3.empty())?test3.front():INT_MAX, \n (!test5.empty())?test5.front():INT_MAX});\n if (test2.front() == num) test2.pop_front();\n if (test3.front() == num) test3.pop_front();\n if (test5.front() == num) test5.pop_front();\n test2.push_back(num*2);\n test3.push_back(num*3);\n test5.push_back(num*5);\n nums.push_back(num);\n }\n return (int)nums[n-1];\n \n }\n};",
"memory": "24778"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.