id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) { \n vector <long> ans;\n ans={1,2,3,4,5};\n if(n<=5) return (int)ans[n-1];\n queue<long>a1;\n queue<long>a3;\n queue<long>a5;\n a1.push(4);a1.push(6);a1.push(8);a1.push(10);\n a3.push(6);a3.push(9);a3.push(12);a3.push(15);\n a5.push(10);a5.push(15);a5.push(20);a5.push(25);\n while(ans.size()<n){\n while(a1.front()<=ans.back()){\n a1.pop();\n }\n while(a3.front()<=ans.back()){\n a3.pop();\n }\n while(a5.front()<=ans.back()){\n a5.pop();\n }\n int m=min((int)a1.front(),(int)a3.front());\n m=min(m,(int)a5.front());\n ans.push_back(m);\n a1.push(ans.back()*2);\n a3.push(ans.back()*3);\n a5.push(ans.back()*5);\n }\n return (int)ans.back();\n }\n};", "memory": "24778" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\n using Int = std::int64_t;\npublic:\n int nthUglyNumber(int const n) {\n std::priority_queue<Int> ugly_numbers;\n std::queue<std::pair<Int, char>> q;\n q.emplace(1, '2');\n while (!q.empty()) {\n auto const [u, tag] = q.front(); q.pop();\n ugly_numbers.push(u);\n if (ugly_numbers.size() > n) ugly_numbers.pop();\n if (ugly_numbers.size() < n || u < ugly_numbers.top()) {\n if (tag == '2') q.emplace(2*u, '2');\n if (tag <= '3') q.emplace(3*u, '3');\n q.emplace(5*u, '5');\n }\n }\n assert( ugly_numbers.size() == n );\n return static_cast<int>(ugly_numbers.top());\n }\n};", "memory": "25134" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n int a = 2, b = 3, c = 5;\n int l1 = 1, l2 = 1, l3 = 1;\n int tr = 1;\n set<int> si;\n si.insert(tr);\n while (n-- > 1) {\n tr = min(min(l1 * a, l2 * b), l3 * c);\n si.insert(tr);\n if (tr == l1 * a)\n l1 = *si.upper_bound(l1);\n if (tr == l2 * b)\n l2 = *si.upper_bound(l2);\n if (tr == l3 * c)\n l3 = *si.upper_bound(l3);\n }\n return tr;\n }\n};", "memory": "25490" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "\nconst vector<int> limited_prime_factors{ 2,3,5, };\nclass Solution {\npublic:\n\n\n int nthUglyNumber(int n) {\n set<long long> befores{ 1,};\n long long current_value = 1;\n for (int i = 1; i < n; i++) {\n long long current_min = INT_MAX;\n for (auto it = begin(befores); it != end(befores);) {\n const auto v = *it;\n bool exist = false;\n for (auto&& p: limited_prime_factors) {\n const auto candidate = p * v;\n if (current_value < candidate) {\n current_min = min(current_min, candidate);\n exist = true;\n }\n }\n if (exist) {\n ++it;\n }\n else {\n it = befores.erase(it);\n }\n }\n current_value = current_min;\n befores.insert(current_min);\n }\n\n return static_cast<int>(current_value);\n }\n};\n\nvoid test(int n) {\n cout << Solution().nthUglyNumber(n) << endl;\n}", "memory": "25846" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n int a = 2, b = 3, c = 5;\n int l1 = 1, l2 = 1, l3 = 1;\n int tr = 1;\n set<int> si;\n si.insert(tr);\n while (n-- > 1) {\n tr = min(min(l1 * a, l2 * b), l3 * c);\n si.insert(tr);\n cout << l1 << \"\\t\" << l2 << \"\\t\" << l3 << \"\\t\" << tr << \"\\n\";\n if (tr == l1 * a)\n l1 = *si.upper_bound(l1);\n if (tr == l2 * b)\n l2 = *si.upper_bound(l2);\n if (tr == l3 * c)\n l3 = *si.upper_bound(l3);\n // cout << l1 << \"\\t\" << l2 << \"\\t\" << l3 << \"\\t\" << tr << \"\\n\\n\\n\";\n }\n return tr;\n }\n};", "memory": "25846" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n std::set<int> nums{1};\n\n for (int i = 0; i < n - 1; ++ i) {\n const int a = 2 * *nums.upper_bound(*--nums.end() / 2);\n const int b = 3 * *nums.upper_bound(*--nums.end() / 3);\n const int c = 5 * *nums.upper_bound(*--nums.end() / 5);\n nums.insert(std::min(a, std::min(b, c)));\n }\n\n return *--nums.end();\n }\n};", "memory": "26203" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n set<int> s;\n s.insert(1);\n vector<int> primes = {2, 3, 5};\n int k = 1, maxi = INT32_MAX;\n for(auto it = s.begin(); it != s.end(); it++){\n int i = *it;\n if(k == n) return i;\n k++;\n for(int j : primes){\n if(i > maxi / j) break;\n s.insert(i * j);\n if(s.size() == n) maxi = (*s.rbegin());\n }\n }\n return 1;\n }\n};", "memory": "26203" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> m_ugly;\n Solution()\n {\n m_ugly = getUgly();\n }\n\n\n int nthUglyNumber(int n)\n {\n ios_base::sync_with_stdio(false);\n return m_ugly[n - 1]; \n }\n\n vector<int> getUgly()\n {\n vector<int> ugly;\n\n for (int i{}; i < 31; ++i)\n ugly.push_back(1 << i);\n\n const int nums[2] = {3, 5};\n\n for (int k{}; k < 2; ++k)\n {\n auto tmp = ugly;\n while (!tmp.empty())\n {\n int i{}, size = tmp.size();\n for (; i < size; ++i)\n {\n // if (tmp[i] * nums[k] <= INT_MAX)\n if (INT_MAX / nums[k] > tmp[i] )\n tmp[i] *= nums[k];\n else\n break;\n }\n\n for (; i < size; ++i)\n tmp.pop_back();\n\n ugly.insert(ugly.end(), tmp.begin(), tmp.end());\n }\n sort(ugly.begin(), ugly.end());\n }\n\n return ugly;\n }\n};", "memory": "26559" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n int a[2000];\n a[0] = 1;\n int q = 0, w = 0, e = 0;\n unordered_map<int ,int> ma;\n ma[1] = 1;\n for (int i=1; i<n; i++) {\n if (a[q]*2 <= a[w]*3 && a[q]*2 <= a[e]*5) {\n a[i] = a[q]*2;\n q++;\n } else if (a[w]*3 <= a[q]*2 && a[w]*3 <= a[e]*5) {\n a[i] = a[w]*3;\n w++;\n } else {\n a[i] = a[e]*5;\n e++;\n }\n if (ma[a[i]] == 0)\n ma[a[i]] = 1;\n else i--;\n }\n return a[n-1];\n }\n};", "memory": "26559" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> m_ugly;\n Solution()\n {\n m_ugly = getUgly();\n }\n\n\n int nthUglyNumber(int n)\n {\n ios_base::sync_with_stdio(false);\n return m_ugly[n - 1]; \n }\n\n vector<int> getUgly()\n {\n vector<int> ugly;\n\n for (int i{}; i < 31; ++i)\n ugly.push_back(1 << i);\n\n const int nums[2] = {3, 5};\n\n for (int k{}; k < 2; ++k)\n {\n auto tmp = ugly;\n while (!tmp.empty())\n {\n int i{}, size = tmp.size();\n for (; i < size; ++i)\n {\n if (INT_MAX / nums[k] > tmp[i] )\n tmp[i] *= nums[k];\n else\n break;\n }\n\n for (; i < size; ++i)\n tmp.pop_back();\n\n ugly.insert(ugly.end(), tmp.begin(), tmp.end());\n }\n sort(ugly.begin(), ugly.end());\n }\n\n return ugly;\n }\n};", "memory": "26915" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> m_ugly;\n Solution()\n {\n m_ugly = getUgly();\n }\n\n\n int nthUglyNumber(int n)\n {\n ios_base::sync_with_stdio(false);\n return m_ugly[n - 1]; \n }\n\n vector<int> getUgly()\n {\n vector<int> ugly;\n\n for (int i{}; i < 31; ++i)\n ugly.push_back(1 << i);\n\n const vector<int> primes = {3, 5};\n\n for (int prime : primes)\n {\n auto tmp = ugly;\n while (!tmp.empty())\n {\n int i{}, size = tmp.size();\n for (; i < size && INT_MAX / prime > tmp[i]; ++i)\n tmp[i] *= prime;\n\n for (; i < size; ++i)\n tmp.pop_back();\n\n ugly.insert(ugly.end(), tmp.begin(), tmp.end());\n }\n sort(ugly.begin(), ugly.end());\n }\n\n return ugly;\n }\n};", "memory": "26915" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n set<int> st;\n st.insert(1);\n // 1\n // 1 2\n // 1 2 4 3\n // 1 2 3 4 5 6 8 \n // 1 2 3 4 5 6 8 9 10 12 15 16 \n // 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32\n // 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\n \n while(st.size()<n){\n int t=st.size();\n auto it = st.begin();\n for(int i = 0;i<t;i++){\n long x = long(*it)*2;\n if(x>INT_MAX) break;\n st.insert((*it)*2);\n \n // if(st.size()>n) break;\n // cout<<(*it)<<\" \"<<(*it)*2<<\" \";\n it++;\n }\n \n // auto it_end=st.end();\n it=st.begin();\n for(int i = 0;i<t;i++){\n int x = (*it)*3;\n if(x<*prev(st.end())) st.insert(x);\n else break;\n it++;\n }\n it=st.begin();\n for(int i = 0;i<t;i++){\n int x = (*it)*5;\n if(x<*prev(st.end())) st.insert(x);\n else break;\n it++;\n }\n // for(auto i:st) cout<<i<<\" \";\n // cout<<st.size()<<\" \"<<*st.end()<<\"\\n\";\n }\n int count=0;\n for(auto i:st){\n count++;\n if(count==n) return i;\n }\n return n;\n \n \n }\n};", "memory": "27271" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n template <class T> using minHeap = priority_queue<T, vector<T>, greater<T>>;\n int nthUglyNumber(int n) {\n minHeap<int> pq;\n unordered_set<int> count;\n long long cur_max = 0;\n long long tmp_max = 0;\n\n pq.push(1);\n\n for (int i = 0; i < n;) {\n long long cur = pq.top();\n pq.pop();\n\n if (cur > cur_max) {\n cur_max = cur;\n if (count.count(cur * 2) == 0 && (tmp_max > cur * 2 || pq.size() < n - i)) {\n tmp_max = max(tmp_max, cur * 2);\n count.insert(cur * 2);\n pq.push(cur * 2);\n }\n \n if (count.count(cur * 3) == 0 && (tmp_max > cur * 3 || pq.size() < n - i)) {\n tmp_max = max(tmp_max, cur * 3);\n count.insert(cur * 3);\n pq.push(cur * 3);\n }\n\n if (count.count(cur * 5) == 0 && (tmp_max > cur * 5 || pq.size() < n - i)) {\n tmp_max = max(tmp_max, cur * 5);\n count.insert(cur * 5);\n pq.push(cur * 5);\n }\n\n ++i;\n }\n }\n\n return cur_max;\n }\n};", "memory": "27271" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 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 unordered_set<long>st;\n st.insert(1);\n vector<int>primes= {2,3,5};\n long ans;\n while(n--){\n long long node= pq.top();\n ans= node;\n pq.pop();\n\n st.erase(node);\n \n for(int i=0;i<primes.size(); i++){\n long num= node*primes[i];\n if(st.find(num)==st.end()){\n st.insert(num);\n pq.push(num);\n }\n }\n\n }\n return ans;\n }\n};", "memory": "27628" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n std::unordered_map<unsigned, bool> exists;\n std::vector<unsigned> dp(n);\n dp[0] = 1;\n\n int index_2 = 0;\n int index_3 = 0;\n int index_5 = 0;\n\n auto update_index = [&exists, &dp](int& index, int i, int multiplier) {\n while (index < i && exists[dp[index] * multiplier]) {\n index++;\n }\n };\n\n auto update_indexes = [&](int i) {\n update_index(index_2, i, 2);\n update_index(index_3, i, 3);\n update_index(index_5, i, 5);\n };\n\n for (int i = 1; i < n; i++) {\n update_indexes(i);\n unsigned ugly_2 = dp[index_2] * 2;\n unsigned ugly_3 = dp[index_3] * 3;\n unsigned ugly_5 = dp[index_5] * 5;\n\n if (ugly_2 < ugly_3 && ugly_2 < ugly_5) {\n dp[i] = ugly_2;\n index_2++;\n } else if (ugly_3 < ugly_5) {\n dp[i] = ugly_3;\n index_3++;\n } else {\n dp[i] = ugly_5;\n index_5++;\n }\n exists[dp[i]] = true;\n }\n\n return dp[n - 1];\n }\n};", "memory": "27628" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n)\n {\n unordered_set<long> s;\n priority_queue<long, vector<long>, greater<long>> q;\n q.push(1);\n s.insert(1);\n\n for (int i = 1; i < n; ++i)\n {\n long curr = q.top();\n q.pop();\n s.erase(curr);\n if (s.find(curr * 2) == s.end())\n {\n s.insert(curr * 2);\n q.push(curr * 2);\n }\n\n if (s.find(curr * 3) == s.end())\n {\n s.insert(curr * 3);\n q.push(curr * 3);\n }\n\n if (s.find(curr * 5) == s.end())\n {\n s.insert(curr * 5);\n q.push(curr * 5);\n }\n }\n\n return q.top();\n }\n};", "memory": "27984" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 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>> pq;\n pq.push(1);\n unordered_set<long long> nums;\n nums.insert(1);\n for (int i = 0; i < n; i++) {\n if (i + 1 == n)\n return pq.top();\n long long top = pq.top();\n pq.pop();\n nums.erase(top);\n if (nums.find(2 * top) == nums.end())\n pq.push(2 * top);\n if (nums.find(3 * top) == nums.end())\n pq.push(3 * top);\n if (nums.find(5 * top) == nums.end())\n pq.push(5 * top);\n nums.insert(2 * top);\n nums.insert(3 * top);\n nums.insert(5 * top);\n }\n return 0;\n }\n};", "memory": "27984" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n if (n == 1) {\n return 1;\n }\n\n int result;\n\n priority_queue<int, vector<int>, greater<int>> minHeap;\n minHeap.push(2);\n minHeap.push(3);\n minHeap.push(5);\n\n unordered_set<int> seen;\n\n for (int i = 1; i < n; i++) {\n while (seen.count(minHeap.top())) {\n minHeap.pop();\n }\n\n result = minHeap.top();\n minHeap.pop();\n\n if (result < INT_MAX / 2) {\n minHeap.push(result * 2);\n }\n\n if (result < INT_MAX / 3) {\n minHeap.push(result * 3);\n }\n\n if (result < INT_MAX / 5) {\n minHeap.push(result * 5);\n }\n seen.insert(result);\n }\n\n return result;\n }\n};", "memory": "28340" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) \n {\n std::priority_queue<int, std::vector<int>, std::greater<int>> factors;\n factors.push(1);\n int count = 0;\n std::unordered_set<int> poppedFactors;\n while(true)\n {\n if(poppedFactors.find(factors.top()) == poppedFactors.end())\n {\n count++;\n if(count == n)\n return factors.top();\n if(factors.top() < std::numeric_limits<int>::max()/2)\n factors.push(factors.top() * 2);\n if(factors.top() < std::numeric_limits<int>::max()/3)\n factors.push(factors.top() * 3);\n if(factors.top() < std::numeric_limits<int>::max()/5)\n factors.push(factors.top() * 5);\n std::cout << factors.top() << std::endl;\n poppedFactors.insert(factors.top());\n }\n factors.pop();\n }\n return -1;\n }\n};", "memory": "28340" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long nthUglyNumber(int n) {\n vector<long> vecs(1, 1);\n vector<int> primes = {2, 3, 5};\n vector<int> indices = {0, 0, 0};\n for (int i = 0; i < n; ++i) {\n vector<long> ugly_cands = {\n vecs[indices[0]] * primes[0], \n vecs[indices[1]] * primes[1], \n vecs[indices[2]] * primes[2]};\n auto minval = *min_element(ugly_cands.begin(), ugly_cands.end());\n vecs.push_back(minval);\n for (int j = 0; j < 3; ++j) {\n if (minval == ugly_cands[j]) {\n ++indices[j];\n }\n }\n }\n return vecs[n - 1];\n }\n};", "memory": "28696" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n //i know the first ugly number so let dp[n] denote the nth ugly number\n vector<long long>dp(n+1,0);\n unordered_set<long long>s;\n s.insert(1);\n dp[1]=1;\n for(int i=2;i<=n;i++){\n long long a=LLONG_MAX;\n for(int j=1;j<i;j++){\n long long mu1=dp[j]*2;\n long long mu2=dp[j]*3;\n long long mu3=dp[j]*5;\n if(s.find(mu1)==s.end()){\n a=min(a,mu1);\n }\n else if(s.find(mu2)==s.end()){\n a=min(a,mu2);\n \n }\n else if(s.find(mu3)==s.end()){\n a=min(a,mu3);\n \n }\n }\n dp[i]=a;\n s.insert(a);\n }\n return dp[n];\n }\n};", "memory": "28696" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n){\n vector<int64_t> bag(n,0);\n int p=1;\n int two=0,three=0,five=0;\n bag[0] = 1;\n set<int> used;\n for(;p<n;p++){\n int64_t ans = min({2*bag[two],3*bag[three],5*bag[five]});\n if(used.find(ans)==used.end()){\n bag[p] = ans;\n used.insert(ans);\n }\n else{\n p--;\n }\n if(ans==2*bag[two]){\n two++;\n }\n else if(ans==3*bag[three]){\n three++;\n }\n else{\n five++;\n }\n cout<<bag[p]<<\" \";\n }\n return bag[n-1];\n }\n};", "memory": "29053" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n // int count = 1;\n // priority_queue<int, vector<int>, greater<int>> pq;\n // unordered_set<int> nums_set;\n // pq.push(1);\n // int result = 1;\n // while (nums_set.size() < n) {\n // result = pq.top();\n // pq.pop();\n // nums_set.insert(result);\n // if (!nums_set.count(result * 2)) {\n // pq.push(result * 2);\n // }\n // if (!nums_set.count(result * 3)) {\n // pq.push(result * 3);\n // }\n // if (!nums_set.count(result * 5)) {\n // pq.push(result * 5);\n // }\n // }\n // return result;\n \n // int count = 1;\n int result = 1;\n priority_queue<int, vector<int>, greater<int>> pq_multiply_2;\n priority_queue<int, vector<int>, greater<int>> pq_multiply_3;\n priority_queue<int, vector<int>, greater<int>> pq_multiply_5;\n unordered_set<int> count_set;\n pq_multiply_2.push(1);\n pq_multiply_3.push(1);\n pq_multiply_5.push(1);\n count_set.insert(1);\n while (count_set.size() < n) {\n cout << \"count: \" << count_set.size() << \", result: \" << result << endl;\n int num_multiplied_2 = pq_multiply_2.top() * 2;\n while (count_set.count(num_multiplied_2)) {\n pq_multiply_2.pop();\n num_multiplied_2 = pq_multiply_2.top() * 2;\n }\n int num_multiplied_3 = pq_multiply_3.top() * 3;\n while (count_set.count(num_multiplied_3)) {\n pq_multiply_3.pop();\n num_multiplied_3 = pq_multiply_3.top() * 3;\n }\n int num_multiplied_5 = pq_multiply_5.top() * 5;\n while (count_set.count(num_multiplied_5)) {\n pq_multiply_5.pop();\n num_multiplied_5 = pq_multiply_5.top() * 5;\n }\n if (num_multiplied_5 < num_multiplied_3 && num_multiplied_5 < num_multiplied_2) {\n result = num_multiplied_5;\n pq_multiply_5.pop();\n pq_multiply_2.push(num_multiplied_5);\n pq_multiply_3.push(num_multiplied_5);\n pq_multiply_5.push(num_multiplied_5);\n } else if (num_multiplied_3 < num_multiplied_2) {\n result = num_multiplied_3;\n pq_multiply_3.pop();\n pq_multiply_2.push(num_multiplied_3);\n pq_multiply_3.push(num_multiplied_3);\n pq_multiply_5.push(num_multiplied_3);\n } else {\n result = num_multiplied_2;\n pq_multiply_2.pop();\n pq_multiply_2.push(num_multiplied_2);\n pq_multiply_3.push(num_multiplied_2);\n pq_multiply_5.push(num_multiplied_2);\n }\n count_set.insert(result);\n // count++;\n }\n return result;\n }\n};", "memory": "29053" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n int one = 0, two = 0, three = 0;\n unordered_map<int, int> used;\n used[0] = 1;\n\n vector<int> ans;\n ans.push_back(1);\n for (int i = 2; i <= n; i++) {\n int v1 = 1;\n int v2 = 1;\n int v3 = 1;\n while (used[ans[one] * 2] > 0) {\n one++;\n }\n while (used[ans[two] * 3] > 0) {\n two += 1;\n }\n while (used[ans[three] * 5] > 0) {\n three += 1;\n }\n int val = min({ans[one] * 2, ans[two] * 3, ans[three] * 5});\n\n used[val] = 1;\n ans.push_back(val);\n\n \n }\n return ans[n - 1];\n \n }\n};", "memory": "29409" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n std::vector<int> ugly = {1};\n std::unordered_set<int> ugly_set = {1};\n std::vector<int> primes = {2, 3, 5};\n std::vector<int> new_ugly;\n std::vector<int> pointers = {0, 0, 0};\n while (ugly.size() != n) {\n new_ugly.clear();\n for (int i = 0; i < 3; i++) {\n new_ugly.push_back(primes[i] * ugly[pointers[i]]);\n }\n int p = 0;\n int curr_min = new_ugly[0];\n for (int i = 0; i < 3; i++) {\n if (new_ugly[i] < curr_min) {\n curr_min = new_ugly[i];\n p = i;\n }\n }\n if (!ugly_set.contains(curr_min)) {\n ugly_set.insert(curr_min);\n ugly.push_back(curr_min);\n }\n pointers[p]++;\n }\n return ugly[n-1];\n }\n};", "memory": "29409" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n int one = 0, two = 0, three = 0;\n unordered_map<int, int> used;\n used[0] = 1;\n\n vector<int> ans;\n ans.push_back(1);\n for (int i = 2; i <= n; i++) {\n int v1 = 1;\n int v2 = 1;\n int v3 = 1;\n while (used[ans[one] * 2] > 0) {\n one++;\n }\n while (used[ans[two] * 3] > 0) {\n two += 1;\n }\n while (used[ans[three] * 5] > 0) {\n three += 1;\n }\n int val = min({ans[one] * 2, ans[two] * 3, ans[three] * 5});\n\n used[val] = 1;\n ans.push_back(val);\n\n \n }\n return ans[n - 1];\n \n }\n};", "memory": "29765" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n \n vector<int> vals(1, 1);\n set<int> singleset;\n singleset.insert(1);\n \n int last2 = 0;\n int last3 = 0;\n int last5 = 0;\n \n while(true){\n \n int a = 2 * vals[last2];\n int b = 3 * vals[last3];\n int c = 5 * vals[last5];\n \n if (a <= b && a <= c){\n if (singleset.find(a) == singleset.end()){ \n singleset.insert(a);\n vals.push_back(a);\n }\n last2 += 1;\n }\n else if (b <= a && b <= c){\n if (singleset.find(b) == singleset.end()){\n singleset.insert(b);\n vals.push_back(b);\n }\n last3 += 1;\n }\n else{\n if (singleset.find(c) == singleset.end()){\n singleset.insert(c);\n vals.push_back(c);\n }\n last5 += 1; \n }\n \n if (vals.size() >= n){\n return vals[n - 1];\n } \n } \n }\n};", "memory": "29765" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<int>primes = {2,3,5};\n priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>pq;\n pq.push({1,0});\n while(n>1)\n {\n n--;\n int uglynum = pq.top()[0];\n int i = pq.top()[1];\n pq.pop();\n\n for(int j = i;j < primes.size();j++)\n {\n if(uglynum <= INT_MAX / primes[j])\n pq.push({uglynum * primes[j] , j});\n }\n }\n return pq.top()[0];\n }\n};", "memory": "30121" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "// O(n logn) time using hb-BST\nclass Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<int> primes = {2, 3, 5};\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> seenNumbers;\n seenNumbers.push({1, 0});\n while(n > 1) {\n int num = seenNumbers.top()[0];\n int i = seenNumbers.top()[1];\n seenNumbers.pop();\n\n for(int j = i; j < primes.size(); j++) {\n if(num <= INT_MAX / primes[j]) {\n seenNumbers.push({primes[j] * num, j});\n }\n }\n n--;\n }\n return seenNumbers.top()[0];\n }\n};", "memory": "30121" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 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 int i = 0;\n long target = 0;\n vector<int> factors{2, 3, 5};\n unordered_set<long> visited;\n while (i<n){\n target = pq.top();\n pq.pop();\n if (visited.find(target) != visited.end()) continue;\n visited.insert(target);\n for (const int& factor: factors){\n pq.push(target * (long)factor);\n }\n ++i;\n }\n return (int)target;\n }\n};", "memory": "30478" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n\n \n\n priority_queue<long,vector<long>,greater<long>>pq;\n unordered_set<long>st ;\n int cnt=0;\n int i=1;\n pq.push(1);\n\n while(!pq.empty()){\n long val=pq.top();\n pq.pop();\n if(st.find(val)!=st.end()) {\n continue;\n }\n st.insert(val);\n \n pq.push(val*2);\n pq.push(val*3);\n pq.push(val*5);\n \n cnt++;\n // cout<<val<<\" \"<<cnt<<endl;\n\n if(cnt==n) return val;\n\n }\n \n return -1;\n }\n};", "memory": "30478" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n if (n <= 6)\n return n;\n\n std::unordered_set<int64_t> numbers;\n std::priority_queue<int64_t, std::vector<int64_t>, std::greater<int64_t>>\n candidates;\n candidates.push(1);\n const std::vector<int64_t> mult({2, 3, 5});\n\n int64_t answer = 0;\n int64_t pops = 0;\n\n while (numbers.size() < n) {\n int64_t c = candidates.top();\n candidates.pop();\n ++pops;\n auto iter = numbers.insert(c);\n if (!iter.second) {\n continue;\n }\n answer = c;\n for (int64_t v : mult) {\n if (v * c < std::numeric_limits<int>::max())\n candidates.push(v * c);\n }\n }\n\n return answer;\n }\n};", "memory": "30834" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
2
{ "code": "#define ll long long int\nclass Solution {\npublic:\n int nthUglyNumber(int n) {\n if(n==1)\n return 1;\n\n int rem = n-2;\n \n priority_queue<ll, vector<ll>, greater<ll>> pq;\n \n pq.push(2);\n pq.push(3);\n pq.push(5);\n \n unordered_set<ll> uset;\n \n while(rem--){\n ll temp = pq.top();\n pq.pop();\n if(uset.count(temp)){\n rem++;\n continue; \n }\n \n uset.insert(temp);\n \n pq.push(temp*2);\n pq.push(temp*3);\n pq.push(temp*5);\n \n }\n \n while(uset.count(pq.top()))\n pq.pop();\n \n return pq.top();\n \n }\n};", "memory": "30834" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<int> p = {2, 3, 5};\n priority_queue<int, vector<int>, greater<int>>pq;\n pq.push(1);\n unordered_set<int> seen;\n int curr;\n for(int i = 0;i < n;i++) {\n curr = pq.top();\n pq.pop();\n for(int j : p) {\n \n if(curr >= (INT_MAX / j)) continue;\n int nxt = curr * j;\n if(!seen.count(nxt)) {\n seen.insert(nxt);\n pq.push(nxt);\n }\n }\n }\n return curr;\n }\n};", "memory": "32615" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint nthUglyNumber(int n) \n{\n priority_queue<int> pq;\n unordered_set<int> s;\n pq.push(-1);\n int num;\n for(int i = 1;i<=n;i++)\n {\n num = pq.top();\n //cout<<num<<endl;\n pq.pop();\n if(num>INT_MIN/2 && s.find(num*2) == s.end()) \n {\n s.insert(num*2);\n pq.push(num*2);\n }\n if(num>INT_MIN/3 && s.find(num*3) == s.end()) \n {\n s.insert(num*3);\n pq.push(num*3);\n }\n if(num>INT_MIN/5 && s.find(num*5) == s.end()) \n {\n s.insert(num*5);\n pq.push(num*5);\n }\n }\n return -1*num;\n}\n};", "memory": "32615" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n\n vector<int> prime = {2, 3, 5};\n priority_queue<int, vector<int>, greater<int>> uglyHeap;\n unordered_set<int> visited;\n\n uglyHeap.push(1);\n visited.insert(1);\n\n int currUgly;\n for (int i = 0; i < n; i++) {\n currUgly = uglyHeap.top();\n uglyHeap.pop();\n for (int x : prime) {\n if((long long)x * currUgly > INT_MAX) break;\n int newUgly = x * currUgly;\n if (visited.find(newUgly) == visited.end()) {\n uglyHeap.push(newUgly);\n visited.insert(newUgly);\n }\n }\n }\n\n return currUgly;\n }\n};", "memory": "32971" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "#include <queue>\n#include <vector>\n#include <iostream>\n#include <unordered_set>\n#include <climits>\n\nclass Solution {\npublic:\n\n // 2 3 5 -> generate\n // remove smallest in heap -> multiply by either 2 3 5\n struct Compare {\n // const is promise to not modify the object\n bool operator() (int a, int b) const {\n // expects a boolean\n return a > b;\n }\n };\n\n int nthUglyNumber(int n) {\n std::priority_queue<int, std::vector<int>, Compare> pq;\n std::unordered_set<int> hashSet;\n\n pq.push(1);\n hashSet.insert(1);\n if (n == 1) {\n return n;\n }\n // ugly number generator\n for (int i = 1; i < n ; ++i) {\n int smallest = pq.top();\n pq.pop();\n if (smallest <= INT_MAX / 2 && hashSet.count(smallest * 2) == 0) {\n pq.push(smallest * 2);\n hashSet.insert(smallest * 2);\n }\n if (smallest <= INT_MAX / 3 && hashSet.count(smallest * 3) == 0) {\n pq.push(smallest * 3);\n hashSet.insert(smallest * 3);\n }\n if (smallest <= INT_MAX / 5 && hashSet.count(smallest * 5) == 0) {\n pq.push(smallest * 5);\n hashSet.insert(smallest * 5);\n }\n } \n\n // while (!pq.empty()) {\n // std::cout << pq.top() << std::endl;\n // pq.pop();\n // }\n return pq.top();\n }\n};", "memory": "32971" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isUgly(int n) {\n if(n==0) return false;\n \n while(n%2==0 || n%3==0 || n%5==0 ){\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\n \n }\n return n==1;\n \n }\n int nthUglyNumber(int n) {\n set<long long>s;\n vector<int>ans;\n s.insert(1);\n long long it=1;\n for(int i=0;i<n;i++){\n it = *s.begin();\n s.insert(it*2);\n s.insert(it*3);\n s.insert(it*5);\n s.erase(it);\n }\n for(auto it=s.begin();it!=s.end();it++){\n if(isUgly(*it)) {\n ans.push_back(*it);\n }\n }\n return it;\n }\n};", "memory": "33328" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<int> dp(n,1);\n set<long long> s;\n for(int i=1;i<n;i++){\n s.insert((long long)dp[i-1]*2);\n s.insert((long long)dp[i-1]*3);\n s.insert((long long)dp[i-1]*5);\n dp[i] = *s.begin();\n s.erase(s.begin());\n }\n return dp[n-1];\n }\n};", "memory": "33328" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n map<long long,int> mp;\n mp[1]++;\n for(auto it:mp){\n n--;\n long long num=it.first;\n if(n==0) return num;\n mp[num*2]++;\n mp[num*3]++;\n mp[num*5]++;\n }\n return 0;\n }\n};", "memory": "33684" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n map<long long,int> m;\n m[1] = 1;\n auto itr = m.begin();\n n--;\n for(;itr!=m.end() && n;itr++)\n {\n n--;\n m[itr->first*2] = 1;\n m[itr->first*3] = 1;\n m[itr->first*5] = 1;\n\n \n }\n return itr->first;\n \n }\n};", "memory": "33684" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "// naive apply ugly number 1 for each number\nclass Solution {\npublic:\n int nthUglyNumber(int n) {\n priority_queue<long long, vector<long long>, greater<long long>> pq;\n unordered_set<long long> set;\n pq.push(1);\n int count = 0;\n long long ans = 1;\n while (count < n) {\n long long val = pq.top();\n pq.pop();\n ans = val;\n long long first=ans*2;\n long long second=ans*3;\n long long third=ans*5;\n if(set.find(first)==set.end()){\n set.insert(first);\n pq.push(first);\n }\n if(set.find(second)==set.end()){\n set.insert(second);\n pq.push(second);\n }\n if(set.find(third)==set.end()){\n set.insert(third);\n pq.push(third);\n }\n\n count++;\n }\n return ans;\n }\n};", "memory": "34040" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n unordered_set<long long> seen; // Use long long to handle large numbers\n priority_queue<long long, vector<long long>, greater<long long>> pq;\n \n seen.insert(1);\n pq.push(1);\n \n int count = 0;\n \n while (count < n) {\n long long cur = pq.top();\n pq.pop();\n count++;\n \n if (count == n) // Check if we found the nth ugly number\n return cur;\n \n long long x1 = cur * 2;\n long long x2 = cur * 3;\n long long x3 = cur * 5;\n \n if (seen.find(x1) == seen.end()) {\n pq.push(x1);\n seen.insert(x1);\n }\n if (seen.find(x2) == seen.end()) {\n pq.push(x2);\n seen.insert(x2);\n }\n if (seen.find(x3) == seen.end()) {\n pq.push(x3);\n seen.insert(x3);\n }\n }\n \n return 0; // Should never reach here\n }\n};\n", "memory": "34040" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<long> multipliers;\n multipliers.push_back(2);\n multipliers.push_back(3);\n multipliers.push_back(5);\n priority_queue<long, vector<long>, greater<long>> pq;\n unordered_set<long> distinct;\n for (int i = 1; i <= 5; i++) {\n pq.push(i);\n distinct.insert(i);\n }\n for (int i = 0; i < n-1; i++) {\n long num = pq.top();\n pq.pop();\n for (int multiplier : multipliers)\n if (distinct.find(multiplier * num) == distinct.end()) {\n distinct.insert(multiplier * num);\n pq.push(multiplier * num);\n }\n }\n return pq.top();\n }\n};", "memory": "34396" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n int nthUglyNumber(int n) {\n int ans = 0;\n priority_queue<long long, vector<long long>, greater<long long>> q;\n unordered_set<long long> seen; // To avoid duplicates\n q.push(1);\n seen.insert(1);\n\n while (!q.empty()) {\n ans++;\n long long x = q.top();\n q.pop();\n\n if (ans == n) return x;\n if (seen.find(x * 2) == seen.end()) {\n q.push(x * 2);\n seen.insert(x * 2);\n }\n if (seen.find(x * 3) == seen.end()) {\n q.push(x * 3);\n seen.insert(x * 3);\n }\n if (seen.find(x * 5) == seen.end()) {\n q.push(x * 5);\n seen.insert(x * 5);\n }\n }\n\n return -1;\n }\n};\n", "memory": "34396" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n // priority_queue<int,vector<int>,greater<int>>pq;\n // pq.push(1);\n set<long long>s;\n s.insert(1ll);\n vector<long long>vec(n+1);\n vec[1] = 1;\n int idx = 1;\n while(idx<=n){\n\n // int t = pq.top();\n // pq.pop();\n auto ptr = s.begin();\n \n long long t = (long long)(*ptr);\n s.erase(ptr);\n\n vec[idx] = t;\n idx++;\n if(idx==n+1) break;\n s.insert(t*2ll);\n s.insert(t*3ll);\n s.insert(t*5ll);\n\n }\n // for(auto val:vec){\n // cout<<val<<\" \";\n // }\n return vec[n];\n }\n};", "memory": "34753" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n std::vector<int64_t> ugly(n);\n std::set<int64_t> next;\n ugly[0] = 1;\n next.insert(ugly[0]*2);\n next.insert(ugly[0]*3);\n next.insert(ugly[0]*5);\n int cnt = 1;\n while( cnt < n ) {\n ugly[cnt] = *(next.begin());\n next.erase( next.begin() );\n next.insert( (int64_t)ugly[cnt] * 2 );\n next.insert( (int64_t)ugly[cnt] * 3 );\n next.insert( (int64_t)ugly[cnt] * 5 );\n cnt++; \n }\n return *(ugly.rbegin());\n\n }\n};", "memory": "34753" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n vector<long long> arr2;\n vector<long long> arr3;\n vector<long long> arr5;\n vector<long long> result;\n if(n==1){\n return 1; \n }\n else{\n result.push_back(1);\n arr2.push_back(2);\n arr3.push_back(3);\n arr5.push_back(5);\n\n int i=0;\n int j=0;\n int k=0;\n \n while (result.size() < n) {\n int mini = min({arr2[i], arr3[j], arr5[k]});\n result.push_back(mini);\n\n if (mini == arr2[i]) i++;\n if (mini == arr3[j]) j++;\n if (mini == arr5[k]) k++;\n\n arr2.push_back(result.back() * 2);\n arr3.push_back(result.back() * 3);\n arr5.push_back(result.back() * 5);\n }\n return result.back();\n }\n }\n};", "memory": "35109" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "\nlong long my_pow(long long a, int n) {\n if (n == 0) return 1;\n if (n == 1 && a == 1) return a;\n if (n % 2 == 0) return my_pow(a * a, n / 2);\n return a * my_pow(a, n - 1);\n}\n\nclass Solution {\n\n struct number {\n int pow2;\n int pow3;\n int pow5;\n\n bool operator>(const number& other) const {\n return my_pow(2, pow2) * my_pow(3, pow3) * my_pow(5, pow5) >\n my_pow(2, other.pow2) * my_pow(3, other.pow3) * my_pow(5, other.pow5);\n }\n };\npublic:\n int nthUglyNumber(int n) {\n unordered_set<int> s;\n priority_queue<number, vector<number>, greater<number>> q;\n number t = {0, 0, 0};\n q.push(t);\n for (int i = 1; i < n; ++i) {\n auto curr = q.top();\n q.pop();\n int num2 = my_pow(2, curr.pow2 + 1) * my_pow(3, curr.pow3) * my_pow(5, curr.pow5);\n if (s.count(num2) == 0) {\n q.push({curr.pow2 + 1, curr.pow3, curr.pow5});\n s.insert(num2);\n }\n int num3 = my_pow(2, curr.pow2) * my_pow(3, curr.pow3 + 1) * my_pow(5, curr.pow5);\n if (s.count(num3) == 0) {\n q.push({curr.pow2, curr.pow3 + 1, curr.pow5});\n s.insert(num3);\n }\n int num5 = my_pow(2, curr.pow2) * my_pow(3, curr.pow3) * my_pow(5, curr.pow5 + 1);\n if (s.count(num5) == 0) {\n q.push({curr.pow2, curr.pow3, curr.pow5 + 1});\n s.insert(num5);\n }\n }\n return my_pow(2, q.top().pow2) * my_pow(3, q.top().pow3) * my_pow(5, q.top().pow5);\n }\n};", "memory": "35109" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n set<int>st;\n st.insert(1);\n vector<int>ds={2,3,5};\n priority_queue<long long int,vector<long long int>,greater<long long int>>pq;\n pq.push(1);\n int cnt=1;\n while(cnt<n){\n long long int num=pq.top();\n pq.pop();\n for(int i=0;i<ds.size();i++){\n long long int prod=num*ds[i];\n if(st.find(prod)==st.end()){\n st.insert(prod);\n pq.push(prod);\n }\n }\n cnt++;\n }\n return (int)pq.top();\n }\n};", "memory": "35465" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n std::priority_queue<long long, std::vector<long long>, std::greater<long long>> pq;\n pq.push(1);\n\n set<long int> search;\n search.insert(1);\n \n int arr[]={1,2,3,5};\n int pop_count=0;\n long int answer=1;\n while(pop_count<n){\n\n long int pop=pq.top();\n pq.pop();\n pop_count++;\n\n\n if(pop_count==n){\n answer=pop;\n break;\n }\n\n for(int i=0;i<4;i++){\n if(search.find(pop*(arr[i]))==search.end()){\n pq.push(pop*(arr[i]));\n search.insert(pop*(arr[i]));\n }\n }\n \n }\n return answer;\n }\n};", "memory": "35465" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n std::priority_queue<long long, std::vector<long long>, std::greater<long long>> pq{};\n pq.push(1LL);\n std::vector<long long> primes = {2LL, 3LL, 5LL};\n std::set<long long> visited{};\n int a{};\n for (int i=0; i<n; i++) {\n a = pq.top();\n pq.pop();\n for (auto p : primes) {\n long long b = a * p;\n if (!visited.contains(b)) {\n pq.push(b);\n visited.insert(b);\n }\n }\n }\n return a;\n }\n};", "memory": "35821" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1690</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int nthUglyNumber(int n) {\n if(n==0) return 1 ;\n\n set<long long int> seen ;\n\n priority_queue<long long int,vector<long long int>,greater<long long int>> min_heap ;\n\n min_heap.push(1) ;\n seen.insert(1) ;\n for(int i = 1 ; i < n ; i++){\n long long int e = min_heap.top() ;\n min_heap.pop() ;\n if(!seen.count(2*e)) min_heap.push(2*e) ;\n if(!seen.count(3*e)) min_heap.push(3*e) ;\n if(!seen.count(5*e)) min_heap.push(5*e) ;\n seen.insert(2*e) ;\n seen.insert(3*e) ;\n seen.insert(5*e) ;\n } \n return min_heap.top() ;\n }\n};", "memory": "35821" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
0
{ "code": "const static auto io_speed_up = []() {\n std::ios::sync_with_stdio(0);\n cin.tie(0);\n FILE *fptr = fopen(\"user.out\", \"w\");\n int x, out = 0, i;\n while (!cin.eof()) {\n if (cin.peek() == '[') cin.ignore();\n else break;\n out = 0;\n i = 0;\n while (cin.peek() != ']') {\n cin >> x;\n if (cin.peek() == ',') cin.ignore();\n out ^= x;\n out ^= i++;\n }\n out ^= i;\n fprintf(fptr, \"%d\\n\", out);\n cin.ignore(1024,'\\n');\n }\n fclose(fptr);\n exit(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int n=nums.size();\n int sum = (n*(n+1))/2;\n for(int i=0; i<n ; i++){\n sum-=nums[i];\n }\n return sum;\n }\n};", "memory": "7400" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
0
{ "code": "const static auto io_speed_up = []() {\n std::ios::sync_with_stdio(0);\n cin.tie(0);\n FILE *fptr = fopen(\"user.out\", \"w\");\n int x, out = 0, i;\n while (!cin.eof()) {\n if (cin.peek() == '[') cin.ignore();\n else break;\n out = 0;\n i = 0;\n while (cin.peek() != ']') {\n cin >> x;\n if (cin.peek() == ',') cin.ignore();\n out ^= x;\n out ^= i++;\n }\n out ^= i;\n fprintf(fptr, \"%d\\n\", out);\n cin.ignore(1024,'\\n');\n }\n fclose(fptr);\n exit(0);\n return 0;\n}();\n\n\nclass Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int n=nums.size();\n int sum=n*(n+1)/2;\n for(int i=0;i<n;i++){\n sum-=nums[i];\n }\n return sum;\n }\n};", "memory": "7500" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
0
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n \n int n = nums.size();\n\n for(int i=0; i<n; i++){\n int index = (nums[i] % (n+2));\n\n if(index <n){\n nums[index] += (n+2); \n }\n }\n\n for(int i =0; i<n; i++){\n if(nums[i]/(n+2) == 0)return i;\n }\n return n;\n }\n};", "memory": "20200" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
0
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n \n int startSum = (nums.size()*(nums.size() + 1))/2;\n\n for(int i = 0; i < nums.size() ; i++)\n {\n startSum = startSum - nums[i];\n }\n\n return startSum;\n }\n};", "memory": "20300" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
0
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int res = nums.size();\n \n for (int i = 0; i < nums.size(); i++) {\n res += i - nums[i];\n }\n \n return res; \n }\n};", "memory": "20400" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
0
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int n=nums.size();\n int i;\n for( i=1;i<=n;i++){\n int f=0;\n for(int j=0;j<n;j++){\n if(nums[j]==i){\n f=1;\n break;\n }\n \n }\n if(f==0){\n return i;\n }\n }\n return 0;\n \n }\n};", "memory": "20400" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
0
{ "code": "class Solution {\n public:\n int missingNumber(vector<int>& nums) {\n int ans = nums.size();\n\n for (int i = 0; i < nums.size(); ++i)\n ans ^= i ^ nums[i];\n\n return ans;\n }\n};", "memory": "20500" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
0
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int n = nums.size();\n int Xor = 0;\n for(int i=0; i<=n; i++){\n Xor = Xor ^ i;\n }\n\n for(int num: nums){\n Xor = Xor ^ num;\n }\n\n return Xor;\n }\n};", "memory": "20500" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
0
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int n=nums.size();\n int n1=n;\n int sum=0;\n int s=0;\n while(n1>0)\n {\n s=s+n1;\n n1--;\n }\n for(int i=0;i<n;i++)\n {\n sum=sum+(n-nums[i]);\n // s=s+n1;\n // n1--;\n\n }\n int a=s-sum;\n int ans=n-a;\n return ans;\n\n \n\n \n }\n};", "memory": "20600" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
0
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& arr) {\n int ans=0;\n for(int i=1;i<arr.size()+1;i++){\n ans^=i;\n }\n for(int i=0;i<arr.size();i++){\n ans^=arr[i];\n \n }\n return ans;\n }\n};", "memory": "20600" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
0
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n \n int m=nums.size();\n int flag;\nfor(int i=0;i<(m+1);i++)\n{flag=0;\nfor(int j=0;j<m;j++) \n {if(nums[j]==i)\n {flag=1;\n break;}}\n if(flag==0)\n {return i;}}\n return -1;\n }\n\n};", "memory": "20700" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
0
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int m=nums.size();\n int flag;\nfor(int i=0;i<(m+1);i++)\n{flag=0;\nfor(int j=0;j<m;j++) \n {if(nums[j]==i)\n {flag=1;\n break;}}\n if(flag==0)\n {return i;}}\n return -1;\n \n }\n};", "memory": "20700" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
2
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int n=nums.size();\n int es=n*(n+1)/2;\n for(int i=0; i<n; i++)\n es-=nums[i];\n return es;\n }\n};", "memory": "20800" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
2
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int n = nums.size();\n int expected_sum = n * (n + 1) / 2;\n int actual_sum = 0;\n \n for (int i = 0; i < n; i++) {\n actual_sum += nums[i];\n }\n \n return expected_sum - actual_sum;\n }\n};\n", "memory": "20800" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
2
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int n=nums.size(); \n vector<int> ans(n+1,1);\n for(int i=0;i<n;i++){\n ans[nums[i]]=0;\n } \n for(int i=0;i<n+1;i++){\n if(ans[i]==1)return i;\n }\n return -1;\n }\n};", "memory": "20900" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
2
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int size = nums.size();\n vector<bool>bools(size+1, false);\n for(int i = 0; i < size; i++){\n bools[nums[i]] = true;\n }\n if(bools[0] == true) {\n vector<bool>::iterator it = find(bools.begin(), bools.end(), false);\n return int(it-bools.begin());\n } else{ return 0; }\n \n }\n};", "memory": "20900" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
2
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int n=nums.size();\n vector<int> hash(n + 1, 0);\n for(int i=0;i<n;i++){\n hash[nums[i]]++;\n }\n for(int i=0;i<=n;i++){\n if(hash[i]==0){\n return i;\n }\n }\n return -1;\n }\n // int missingNumber(vector<int>& nums) {\n // int n=nums.size();\n // int ans=0;\n // ans=(n*(n+1))/2;\n // for(int i=0;i<n;i++){\n // ans-=nums[i];\n // }\n // return ans;\n // }\n\n // int missingNumber(vector<int>& nums) {\n // int n=nums.size();\n // int ans=0;\n // for(int i=0;i<n;i++){\n // ans=ans^nums[i];\n // }\n // for(int i=0;i<=n;i++){\n // ans=ans^i;\n // }\n // return ans;\n // }\n};", "memory": "21000" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
2
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int n = nums.size();\n int arr[n+1];\n memset(arr, 0, (n+1)*sizeof(int));\n for(int i = 0; i < nums.size(); i++)\n {\n arr[nums[i]]++;\n }\n\n for(int i = 0; i < n+1; i++)\n {\n if(arr[i]==0)\n return i;\n }\n return 1;\n }\n};\nint speedUp = [] {\n std::ios::sync_with_stdio(0);\n std::cin.tie(0);\n return 0;\n}();", "memory": "21000" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
2
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int size = nums.size();\n vector<int> hashArray(size+1,-1);\n for(int i=0;i<size;i++){\n hashArray[nums[i]] = nums[i];\n }\n for(int i=0;i<hashArray.size();i++){\n if(hashArray[i]==-1) return i;\n }\n return -1;\n }\n};", "memory": "21100" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
2
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int n = nums.size();\n vector<int>v(n+1,-1);\n for(int i =0;i<nums.size();i++){\n v[nums[i]] = nums[i];\n }\n for(int i =0;i<v.size();i++){\n if(v[i]==-1)return i;\n }\n return 0;\n }\n};\n\n", "memory": "21100" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
2
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int n = nums.size();\n vector<int>hash(n+1,0);\n\n for(int i=0; i<n; i++){\n hash[nums[i]]++;\n }\n for(int i=0; i<=n; i++){\n if ( hash[i]!=1){\n return i;\n }\n }\n\n return n; }\n};", "memory": "21200" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
2
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n // //using natural number sum\n // int n=nums.size();\n // int og_sum=n*(n+1)/2;\n // int sum=0;\n // //finding sum of elements in the vector\n // for(int i=0;i<nums.size();i++){\n // sum=sum+nums[i];\n // }\n // return (og_sum-sum);\n sort(nums.begin(),nums.end());\n for(int i=1;i<nums.size();i++){\n if(nums[i]-nums[i-1]==2)\n return(nums[i-1]+1);\n else if(nums[0]!=0)\n return 0;\n else if(nums[nums.size()-1]!=nums.size())\n return nums.size();\n }\n if(nums.size()==1&&nums[0]==1)\n return 0;\n else \n return 1;\n }\n};", "memory": "21200" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
3
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int n = 0;\n while(n<nums.size()){\n if(n != nums[n]){\n break;\n }\n n++;\n }\n return n;\n }\n};\n", "memory": "21300" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
3
{ "code": "#include <vector>\nusing namespace std;\n\nclass Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int n = nums.size();\n \n \n vector<int> hash(n + 1, 0);\n\n \n for (int i = 0; i < n; i++) {\n hash[nums[i]] = 1;\n }\n\n for (int i = 0; i <= n; i++) {\n if (hash[i] == 0)\n return i;\n }\n \n return -1; \n}\n};", "memory": "21300" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
3
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int n = nums.size();\n sort(nums.begin(), nums.end());\n\n if(nums[0] != 0) return 0;\n if(nums[n-1] != n) return n;\n\n for(int i = 1; i < n; i++){\n if(nums[i] != i){\n return i;\n }\n }\n return 0;\n }\n};", "memory": "21400" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
3
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int i;\n for(i = 0; i < nums.size(); i++){\n if(nums[i] != i){\n return i;\n }\n }\n return i;\n }\n};", "memory": "21400" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
3
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int s=nums.size();\n \n int n=0;\n\n \n sort(nums.begin(),nums.end());\n \n\n for(int j=0;j<s;j++){\n if(nums[j]!=j){\n return j;\n }\n }\n \n return s;\n \n\n \n }\n};", "memory": "21500" }
268
<p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1] <strong>Output:</strong> 8 <strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
3
{ "code": "class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n int c = nums.size();\n sort(nums.begin(), nums.end());\n int i;\n for (i = 0; i < c; i++) {\n if (nums[i] != i) {\n return i;\n }\n }\n return i;\n }\n};", "memory": "21500" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
0
{ "code": "class Solution {\npublic:\n unordered_map<int, unordered_map<int, bool>> dp;\n bool can(vector<int>& stones, int unit, int i) {\n if(i >= stones.size() - 1) return true;\n if(dp.count(i) && dp[i].count(unit)) {\n return dp[i][unit];\n }\n \n for(int step = unit-1; step <= unit+1; step++) {\n if(step <= 0) continue;\n\n int nextPos = stones[i] + step;\n for(int j = i+1; j < stones.size(); j++) {\n if(nextPos == stones[j]) {\n if(can(stones, step, j)) {\n return dp[i][unit] = true;\n }\n }\n }\n }\n return dp[i][unit] = false;\n }\n\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n if(n == 1) return true;\n if(stones[1] - stones[0] != 1) return false;\n return can(stones, 1, 1);\n }\n};", "memory": "29128" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
0
{ "code": "class Solution {\npublic:\n unordered_map<int, unordered_map<int, bool>> memo;\n\n bool canCrossHelper(vector<int>& stones, int index, int jump) {\n if (index == stones.size() - 1)\n return true;\n if (memo.count(index) && memo[index].count(jump))\n return memo[index][jump];\n for (int step = jump - 1; step <= jump + 1; ++step) {\n if (step > 0) {\n int nextPosition = stones[index] + step;\n auto it =\n lower_bound(stones.begin(), stones.end(), nextPosition);\n\n if (it != stones.end() && *it == nextPosition) {\n int nextIndex = it - stones.begin();\n\n if (canCrossHelper(stones, nextIndex, step)) {\n return memo[index][jump] = true;\n }\n }\n }\n }\n\n return memo[index][jump] = false;\n }\n\n bool canCross(vector<int>& stones) {\n\n if (stones[1] != 1)\n return false;\n return canCrossHelper(stones, 1, 1);\n }\n};\n", "memory": "29128" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
0
{ "code": "class Solution {\npublic:\nset<int>st;\nmap<pair<int,int>,bool>dp;\n bool fn(int i, int last, int jump){\n if(dp.find({i,jump})!=dp.end())\n return dp[{i,jump}];\n if(st.find(i)==st.end()||i<0||i>last)\n return dp[{i,jump}]=0;\n if(i==last)\n return dp[{i,jump}]=1;\n int a=0, b=0, c=0;\n if(jump-1+i>0&&jump-1>0)\n a=fn(i+jump-1,last,jump-1);\n if(a)\n return 1;\n if(i+jump>0&&jump>0)\n b=fn(i+jump,last,jump);\n if(b)\n return 1;;\n if(i+jump+1>0)\n c=fn(i+jump+1,last,jump+1);\n return dp[{i,jump}]=c;\n\n}\n bool canCross(vector<int>& stones) {\n for(auto i:stones)\n st.insert(i);\n return fn(0,stones[stones.size()-1],0);\n }\n};", "memory": "33979" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool helper(vector<int>& stones , int i , int k, unordered_map<string,bool>& dp){\n if(i == stones.size()-1){\n return true;\n }\n \n string key = to_string(i) + \",\" + to_string(k);\n if(dp.find(key) != dp.end()){\n return dp[key];\n }\n\n for(int ind = i+1 ; ind<stones.size(); ind++){\n int diff = stones[ind] - stones[i];\n if(diff > k+1){\n break;\n }\n\n\n for(int dir=-1 ; dir<=1; dir++){\n if(diff == k+dir){\n if(helper(stones , ind , k+dir , dp)){\n return dp[key] = true;\n };\n }\n }\n\n }\n\n return dp[key] = false;\n }\n bool canCross(vector<int>& stones) {\n //base case\n if(stones[1] != 1) return false;\n unordered_map<string,bool> dp;\n return helper(stones , 1 , 1 , dp);\n }\n};", "memory": "33979" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool ans;\n bool jump(int u, int j, vector<vector<bool>> &dp, vector<vector<bool>> &vis, vector<int>&v){\n int n=v.size();\n if(u==n-1){\n vis[u][j]=true;\n dp[u][j]=true;\n ans=true;\n return true;\n }\n if(vis[u][j]) return dp[u][j];\n\n bool f=false;\n for(int i=u+1;i<n; i++){\n if(v[i]-v[u]>j+1) break;\n if(v[i]-v[u]>=j-1 && v[i]-v[u]<=j+1) \n f |= jump(i,v[i]-v[u],dp,vis,v);\n }\n // cout<<u<<\",\"<<j<<\",\"<<f<<\" \";\n vis[u][j]=true;\n return dp[u][j]=f;\n }\n\n bool canCross(vector<int>& v) {\n int n=v.size();\n vector<vector<bool>> dp(n,vector<bool>(n,false)), vis(n,vector<bool>(n,false));\n // dp[0][0]=true;\n ans=false;\n return jump(0,0,dp,vis,v);\n // return ans;\n\n }\n};\n\n// 0,1,3,5,6,8,12,17\n// 0,1,2,2,3,\n\n", "memory": "38830" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
1
{ "code": "struct pair_hash {\n inline std::size_t operator()(const std::pair<int,int> & v) const {\n return v.first*31+v.second;\n }\n};\nclass Solution {\npublic:\n\tbool canCross(vector<int>& stones) {\n\t\tunordered_set<int> s;\n\t\tmemo = new unordered_set<pair<int, int>, pair_hash>;\n\t\tfor(int a : stones) s.insert(a);\n\t\tbool ans = can(s, 1, 1, stones[stones.size()-1]);\n\t\tdelete memo;\n\t\treturn ans;\n\t}\n\nprivate:\n\tbool can(unordered_set<int>& stones, int pos, int k, int dest){\n\t\tif(memo->find({pos, k})!=memo->end()) return false;\n\t\tif(stones.find(pos)==stones.end()) return false;\n\t\tif(pos == dest) return true;\n\n\t\tmemo->insert({pos, k});\n\t\treturn can(stones, pos + k, k, dest) || can(stones, pos + k + 1, k+1, dest) || can(stones, pos + k-1, k-1, dest);\n\t}\n\tunordered_set<pair<int,int>, pair_hash>* memo;\n};\n", "memory": "38830" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n if (stones[1] != 1) {\n return false;\n }\n std::vector<std::unordered_map<int, bool>> dp(n + 1);\n dp[0][0] = true;\n for (int i = 1; i < n; ++i) {\n int k = i - 1;\n while (k >= 0) {\n int dist = stones[i] - stones[k];\n bool cur=false, prev=false, next= false;\n if (dp[k].find(dist) != dp[k].end()) {\n cur = dp[k][dist];\n }\n if (dp[k].find(dist + 1) != dp[k].end()) {\n next = dp[k][dist + 1];\n }\n if (dp[k].find(dist - 1) != dp[k].end()) {\n prev = dp[k][dist - 1];\n }\n cur |= next || prev;\n if (cur) {\n dp[i][dist] = true;\n }\n if (dist > 2e6) {\n break;\n }\n --k;\n }\n } \n bool ans = false;\n for (auto it : dp[n - 1]) {\n ans |= it.second;\n }\n return ans;\n }\n};\n", "memory": "43681" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n if (stones[1] != 1) return false;\n vector<unordered_set<int>> dp(n, unordered_set<int>());\n dp[1].insert(1);\n for (int i = 2; i < n; i++) {\n for (int j = 1; j < i; j++) {\n for (auto k: dp[j]) {\n // if i is reachable from j\n if (stones[j] + k - 1 <= stones[i] && stones[i] <= stones[j] + k + 1)\n dp[i].insert(stones[i] - stones[j]);\n }\n }\n }\n return !dp[n-1].empty();\n }\n};", "memory": "43681" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
1
{ "code": "#include <vector>\n#include <unordered_map>\n#include <unordered_set>\n\nclass Solution {\npublic:\n bool canCross(std::vector<int>& stones) {\n if (stones[1] != 1) {\n return false;\n }\n\n std::unordered_map<int, int> memo;\n for (int i = 0; i < stones.size(); ++i) {\n memo[stones[i]] = i;\n }\n\n std::unordered_map<int, std::unordered_set<int>> units;\n units[1].insert(1);\n\n for (int i = 1; i < stones.size(); ++i) {\n int stone = stones[i];\n for (int jump : units[stone]) {\n for (int k = jump - 1; k <= jump + 1; ++k) {\n if (k > 0 && memo.find(stone + k) != memo.end()) {\n units[stone + k].insert(k);\n }\n }\n }\n }\n\n return !units[stones.back()].empty();\n }\n};\n", "memory": "48533" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n if(stones[1]-stones[0] != 1) return false;\n unordered_map<int, int> stone_map;\n for(int i=0;i<stones.size();i++) {\n stone_map[stones[i]] = i;\n }\n unordered_map<int, unordered_set<int> > m;\n m[1].insert(1);\n for(int i=1;i<stones.size()-1;i++) {\n for(auto it = m[i].begin(); it!=m[i].end(); it++) {\n for(int j=-1;j<=1;j++) {\n int last_jump = *it;\n if(j+last_jump > 0) {\n if (stone_map.find(j+ last_jump + stones[i])!=stone_map.end()) {\n int index_jumped = stone_map[last_jump + stones[i] + j];\n m[index_jumped].insert(j + last_jump);\n }\n }\n }\n }\n }\n if (m.find(stones.size()-1)!=m.end()) return true;\n return false;\n }\n};", "memory": "48533" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n\n if (stones[1] != 1)\n return false;\n\n unordered_map<int, unordered_set<int>> dp;\n\n for (int stone : stones) {\n dp[stone] = unordered_set<int>();\n }\n dp[stones[0]].insert(1);\n\n for (int i = 0; i < stones.size(); i++) {\n int stone = stones[i];\n\n for (int jump : dp[stone]) {\n int reach = stone + jump;\n\n if (reach == stones.back()) {\n return true;\n }\n\n if (dp.find(reach) != dp.end()) {\n\n if (jump - 1 > 0)\n dp[reach].insert(jump - 1);\n dp[reach].insert(jump);\n dp[reach].insert(jump + 1);\n }\n }\n }\n\n return false;\n }\n};\n", "memory": "53384" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n if (stones[1] != 1) return false; // If the first jump is not 1, frog can't make the first jump\n\n // Hash map to store the possible jumps at each stone\n unordered_map<int, unordered_set<int>> stoneJumps;\n for (int stone : stones) {\n stoneJumps[stone] = unordered_set<int>();\n }\n\n // The frog starts on the first stone with a jump of 1 unit\n stoneJumps[stones[0]].insert(1);\n\n // Iterate over each stone\n for (int stone : stones) {\n // Check all possible jumps that can reach this stone\n for (int jump : stoneJumps[stone]) {\n int reach = stone + jump;\n if (reach == stones.back()) return true; // If the frog can reach the last stone\n\n // If the next stone is within the stones set, update its possible jumps\n if (stoneJumps.count(reach)) {\n if (jump - 1 > 0) stoneJumps[reach].insert(jump - 1);\n stoneJumps[reach].insert(jump);\n stoneJumps[reach].insert(jump + 1);\n }\n }\n }\n\n return false;\n }\n};", "memory": "53384" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n if (n >= 2)\n {\n if (stones[1] != 1)\n {\n return false;\n }\n\n if (n == 2)\n {\n return true; \n }\n }\n\n map<pair<int,int>, bool> dict;\n set<pair<int, int>> s; \n s.insert(pair<int,int>(1,1));\n dict[pair<int,int>(1,1)] = true;\n\n while (s.size() > 0)\n {\n set<pair<int,int>> news;\n\n for (auto elem : s)\n {\n for (int i = elem.first+1; i < n; i++)\n {\n int jump = stones[i] - stones[elem.first];\n if ((jump > 0) && ((jump == elem.second) || (jump == (elem.second + 1)) || (jump == (elem.second - 1))))\n {\n if (i == n-1)\n {\n return true;\n }\n\n if (dict.find(pair<int,int>(i, jump)) == dict.end())\n {\n news.insert(pair<int,int>(i, jump));\n dict[pair<int,int>(i, jump)] = true;\n }\n }\n }\n }\n s = news;\n }\n\n return false;\n }\n};", "memory": "58235" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool rec(set<int>& stone_hai, int i, int last_step, unordered_map<int, unordered_map<int, int>>& memo, int target) {\n // Pruning\n if (i > target || i < 0 || last_step <= 0) return false;\n if (stone_hai.find(i) == stone_hai.end()) return false;\n \n // Base case\n if (i == target) return true;\n \n // Check memoization\n if (memo[i].find(last_step) != memo[i].end()) return memo[i][last_step];\n \n // Save and call\n bool first_step = rec(stone_hai, i + last_step, last_step, memo, target);\n bool second_step = rec(stone_hai, i + last_step - 1, last_step - 1, memo, target);\n bool third_step = rec(stone_hai, i + last_step + 1, last_step + 1, memo, target);\n \n // Save and return\n return memo[i][last_step] = first_step || second_step || third_step;\n }\n \n bool canCross(vector<int>& stones) {\n int n = stones.size();\n set<int> stone_hai;\n for (auto it : stones) stone_hai.insert(it);\n int target = stones[n - 1];\n \n // Memoization table\n unordered_map<int, unordered_map<int, int>> memo;\n \n return rec(stone_hai, 1, 1, memo, target);\n }\n};", "memory": "58235" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\n vector<int>a;\n int n;\n int dp[2010][5010];\n unordered_map<int,int>mp;\n\n int rec(int x,int j){\n if(x==n-1){\n return 1;\n \n }\n \n //if(x==n)return 1;\n\n if(dp[x][j]!=-1)return dp[x][j];\n\n int ans=0;\n if(mp.find(a[x]+j)!=mp.end())ans=rec(mp[a[x]+j],j);\n if(mp.find(a[x]+j+1)!=mp.end()) ans=max(ans,rec(mp[a[x]+j+1],j+1));\n if(j>1&&(mp.find(a[x]+j-1)!=mp.end())) ans=max(ans,rec(mp[a[x]+j-1],j-1));\n //use of mp[a[x]+j+1] in rec(mp[a[x]+j+1],j+1)) to find the index at which a[x]+j+1 value is there\n\n return dp[x][j]=ans;\n\n }\npublic:\n bool canCross(vector<int>& stones) {\n n=stones.size();\n for(int i=0;i<n;i++){\n a.push_back(stones[i]);\n }\n\n //a=stones;\n memset(dp,-1,sizeof(dp));\n if(a[1]!=1)return false;\n //return true;\n for(int i=0;i<n;i++){\n mp[stones[i]]=i;\n }\n\n int k=rec(1,1);\n if(k==1)return true;\n else return false;\n \n }\n};", "memory": "63086" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n //int dp[2001][2001];\n unordered_map<int,unordered_map<int,int>>dp;\n\n int solve(unordered_map<int,int>&stones,int i,int k,int n){\n //cout<<i<<\" \";\n if(i>n || i<0 || k<0) return 0;\n\n if(i==n) return 1;\n\n int a=0;\n\n if(dp[i].find(k)!=dp[i].end() && dp.find(i)!=dp.end()) return dp[i][k];\n\n if(stones.find(i)==stones.end()) return 0;\n\n if(i==0) return solve(stones,i+1,k,n);\n\n a=solve(stones,i+k,k,n);\n a|=solve(stones,i+k+1,k+1,n);\n\n if(k-1>0) a|=solve(stones,i+k-1,k-1,n);\n\n return dp[i][k]=a;\n }\n\n bool canCross(vector<int>& stones) {\n unordered_map<int,int>mp,idx;\n int mx=stones.back();\n\n for(auto i:stones) ++mp[i];\n\n return solve(mp,0,1,mx);\n }\n};", "memory": "63086" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& a) {\n long n = a.size();\n \n unordered_map<long,long>idx;\n\n\n for(int i=0;i<n;i++)\n {\n idx[a[i]] = i+1;\n }\n\n long dp[2000][2000];\n memset(dp,0,sizeof(dp));\n long N = INT_MAX;\n\n for(int i=0;i<n;i++)dp[i][n-1] = 1;\n\n for(int i=n-3;i>=0;i--)\n {\n for(int j=i+1;j<n;j++)\n { \n int dis = a[j]-a[i];\n\n if(dis-1>2000)continue;\n \n int id1 = idx[a[j]+dis-1];\n int id2 = idx[a[j]+dis];\n int id3 = idx[a[j]+dis+1];\n\n \n\n if(id1&&id1!=j)\n {\n dp[i][j] = dp[i][j]||dp[j][id1-1];\n\n \n }\n\n if(id2&&id2!=j)\n {\n dp[i][j] = dp[i][j]||dp[j][id2-1];\n \n }\n\n\n if(id3&&id3!=j)\n {\n dp[i][j] = dp[i][j]||dp[j][id3-1];\n }\n\n \n }\n }\n\n \n\n return dp[0][1]&&a[1]==1;\n \n\n\n \n }\n};", "memory": "67938" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "#include <vector>\n#include <unordered_map>\n#include <unordered_set>\nusing namespace std;\n\nclass Solution {\npublic:\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n unordered_map<int, unordered_set<int>> mp;\n \n // Initialize the map with stone positions and possible jumps\n for (int i = 0; i < n; i++) {\n mp[stones[i]] = unordered_set<int>();\n }\n \n // The frog starts at the first stone and can only make a jump of 0\n mp[stones[0]].insert(0);\n \n for (int i = 0; i < n; i++) {\n int stone = stones[i];\n unordered_set<int> steps = mp[stone];\n for (int k : steps) {\n if (k - 1 > 0 && mp.find(stone + k - 1) != mp.end()) {\n mp[stone + k - 1].insert(k - 1);\n }\n if (k > 0 && mp.find(stone + k) != mp.end()) {\n mp[stone + k].insert(k);\n }\n if (k + 1 > 0 && mp.find(stone + k + 1) != mp.end()) {\n mp[stone + k + 1].insert(k + 1);\n }\n }\n }\n \n // Check if the frog can reach the last stone\n return !mp[stones.back()].empty();\n }\n};\n", "memory": "67938" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n const int n = stones.size();\n vector<vector<char>> dp(n, vector<char>(n+5, -1));\n function<bool(int,int)> solve = [&](int i, int j) {\n if (j == 0) return false;\n if (i == n-1) return true;\n char& res = dp[i][j];\n if (res == -1) {\n res = 0;\n int ni = lower_bound(stones.begin(), stones.end(), stones[i] + j) - stones.begin();\n if (ni == n || stones[ni] - stones[i] != j) return false;\n for (int jj = j-1; jj <= j+1; ++jj) {\n if (solve(ni,jj)) {\n res = 1;\n break;\n }\n }\n }\n return bool(res);\n };\n return solve(0, 1);\n }\n};", "memory": "72789" }