id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "// Idea: Greedy, Prefix Sum, Binary Search, Sorting\nclass Solution {\npublic:\n // _BinarySearch\n int maxFrequency(vector<int>& nums, int k) {\n std::ranges::sort(nums);\n const auto& a = nums;\n const int n = a.size();\n // create the prefix sum array, for quick range-sum\n vector<long long> ps(n);\n // Bug: partial_sum()'s accumulator is int32, so it cannot handle int64.\n // partial_sum(a.begin(), a.end(), ps.begin(),\n // [](long long accum, int e) { return accum + e; });\n // cout << \"n: \" << n << \", first: \" << a.front() << \", last: \" <<\n // a.back()\n // << \", total: \" << ps.back() << endl;\n // for (int i = 0; i < n - 1; ++i) {\n // if (ps[i] >= ps[i + 1]) {\n // cout << \"wrong: \" << ps[i] << \" vs \" << ps[i + 1] << endl;\n // }\n // }\n for (long long i = 0, sum = 0; i < n; ++i) {\n sum += a[i];\n ps[i] = sum;\n }\n int res = 1;\n for (int i = n - 1; i >= 1 && res < i + 1; --i) {\n // check if the whole range [0, i] is possible\n // {\n // long long existing = ps[i];\n // long long required = (long long)a[i] * (i + 1);\n // if (existing + k >= required) {\n // cout << \"can make [0, \" << i << \"]\" << endl;\n // return i + 1;\n // } else {\n // cout << \"lacked \" << required - existing + k\n // << \" to make [0, \" << i << \"]\" << endl;\n // }\n // }\n // binary search in [0, i]\n int low = 0;\n int high = i;\n int best = i;\n while (low <= high) {\n int mid = (low + high) / 2;\n long long existing = ps[i] - (mid > 0 ? ps[mid - 1] : 0);\n long long required = (long long)a[i] * (i - mid + 1);\n if (existing + k >= required) { // can make it\n // cout << \"low: \" << low << \", high: \" << high\n // << \", mid: \" << mid << \", existing: \" <<\n // existing\n // << \", required: \" << required << \", Good\" <<\n // endl;\n best = min(best, mid);\n high = mid - 1;\n } else {\n // cout << \"i: \" << i << \", low: \" << low << \", high: \" <<\n // high\n // << \", mid: \" << mid << \", existing: \" << existing\n // << \", required: \" << required\n // << \", failed: diff: \" << required - existing - k\n // << endl;\n low = mid + 1;\n }\n }\n // [best ... i] can be made the same as a[i]\n res = max(res, i - best + 1);\n }\n return res;\n }\n // sliding window\n int maxFrequency_SlidingWindow(vector<int>& A, long k) {\n int i = 0, j = 0;\n sort(A.begin(), A.end());\n for (j = 0; j < A.size(); ++j) {\n k += A[j];\n if (k < (long)A[j] * (j - i + 1))\n k -= A[i++];\n }\n return j - i;\n }\n};", "memory": "113600" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n countingSort(nums);\n int start = 0;\n long long subarraySum = 0;\n int maxFrequency = 1;\n for (int i = 0; i < nums.size(); i++) {\n int subarrayLength = i - start + 1;\n long long subarrayProduct = (long long)nums[i] * subarrayLength;\n subarraySum += nums[i];\n while (subarrayProduct - subarraySum > k) {\n subarraySum -= nums[start];\n start++;\n subarrayLength--;\n subarrayProduct = (long long)nums[i] * subarrayLength;\n }\n maxFrequency =max(maxFrequency, subarrayLength);\n }\n return maxFrequency;\n }\nprivate:\n void countingSort(vector<int>& nums) {\n int maxElement = INT_MIN;\n for (int num : nums)\n maxElement = max(maxElement, num);\n vector<int> countMap(maxElement + 1, 0);\n for (int num : nums) \n countMap[num]++;\n int index = 0;\n for (int i = 0; i <= maxElement; i++) {\n while (countMap[i]-- > 0) {\n nums[index++] = i;\n }\n }\n }\n};\n", "memory": "113700" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int findBest(int target_idx, int k, vector<int>& nums, vector<long>& prefixSum) {\n int target = nums[target_idx];\n \n int i = 0;\n int j = target_idx;\n int result = target_idx;\n \n while(i <= j) {\n int mid = i + (j-i)/2;\n \n long count = (target_idx - mid + 1);\n long windowSum = (count * target);\n long currSum = prefixSum[target_idx] - prefixSum[mid] + nums[mid];\n \n int ops = windowSum - currSum;\n \n if(ops > k) {\n i = mid+1;\n } else {\n result = mid;\n j = mid-1;\n }\n }\n \n return target_idx-result+1;\n }\n \n int maxFrequency(vector<int>& nums, int k) {\n int n = nums.size();\n \n sort(begin(nums), end(nums));\n vector<long> prefixSum(n);\n prefixSum[0] = nums[0];\n \n for(int i = 1; i < n; i++) {\n prefixSum[i] = prefixSum[i-1] + nums[i];\n }\n \n int result = 0;\n \n for(int i = 0; i<n; i++) {\n result = max(result, findBest(i, k, nums, prefixSum));\n }\n \n return result;\n }\n};", "memory": "114400" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int n=nums.size();\n int a=0, b=0;\n sort(nums.begin(), nums.end());\n vector<long long> prefix(n,nums[0]);\n for(int i=1; i<n; i++)\n prefix[i]=nums[i]+prefix[i-1];\n int ans=0;\n while(b<n){\n while((nums[b]*(b-a+1LL)) - (prefix[b]-(a?prefix[a-1]:0LL)) > k){\n a++;\n }\n ans= max(ans, b-a+1);\n b++;\n }\n return ans;\n }\n};", "memory": "114500" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& a, int k) \n {\n int n=a.size();\n sort(a.begin(),a.end());\n vector<long long>pre(n+1,0);\n for(int i=0;i<n;i++)\n {\n pre[i+1]=pre[i]+a[i];\n }\n int ans=0,i=0,j=0;\n while(i<n)\n {\n long long x=(long long)((long long)a[i]*((long long)(i-j)))-(pre[i]-pre[j] );\n if(x<=k){\n ans=max(ans,i-j);\n i++;\n }\n else{\n j++;\n }\n\n }\n\n return ans+1;\n }\n};", "memory": "114600" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int n=nums.size();\n int l=0, r=0;\n sort(nums.begin(), nums.end());\n vector<long long> cumml(n,nums[0]);\n for(int i=1; i<n; i++)\n cumml[i]=nums[i]+cumml[i-1];\n int ans=0;\n while(r<n){\n while((nums[r]*(r-l+1LL)) - (cumml[r]-(l?cumml[l-1]:0LL)) > k){\n l++;\n }\n ans= max(ans, r-l+1);\n r++;\n }\n return ans;\n }\n};", "memory": "114700" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int bSearch(int target_idx, vector<int>& nums, int k, vector<long> &prefixSum) {\n int target= nums[target_idx];\n int l = 0;\n int r = target_idx;\n int best_idx= target_idx;\n while(l<=r) {\n int mid = l+(r-l)/2;\n long count = (target_idx-mid+1);\n long windowSum = count*target;\n long currSum = prefixSum[target_idx]-prefixSum[mid]+nums[mid];\n int ops = windowSum - currSum;\n if(ops>k) {\n l=mid+1;\n } else{\n best_idx = mid;\n r = mid-1;\n }\n }\n return target_idx-best_idx+1;\n }\n int maxFrequency(vector<int>& nums, int k) {\n int n = nums.size();\n sort(nums.begin(), nums.end());\n vector<long> prefixSum(n);\n prefixSum[0] = nums[0];\n for(int i = 1;i<n; i++) {\n prefixSum[i] = prefixSum[i-1]+nums[i];\n }\n int res = 0;\n for(int target=0;target<n;target++) {\n int freq = bSearch(target, nums, k, prefixSum);\n res = max(res, freq);\n }\n return res;\n }\n};", "memory": "114800" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int n=nums.size();\n sort(nums.begin(),nums.end());\n int lo =1;\n int hi = n;\n vector<long long int> pre(n);\n long long int p=0;\n for(int i=0;i<n;i++){\n p+=nums[i];\n pre[i]=p;\n }\n int ans=1;\n while(lo<=hi){\n long long int mid = (lo+hi)/2;\n long long int a = mid*nums[mid-1] - pre[mid-1]; \n int flag=0;\n if(a<=k)flag=1;\n for(int i=mid;i<n;i++){\n a = mid*nums[i] - (pre[i]-pre[i-mid]);\n if(a<=k){\n flag=1;\n }\n if(flag)break;\n }\n if(flag){\n ans=mid;\n lo=mid+1;\n }\n else{\n hi=mid-1;\n }\n }\n return ans;\n }\n};", "memory": "114800" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n vector<long long> pre(nums.size());\n pre[0] = nums[0];\n for(int i = 1; i < nums.size(); i++)\n pre[i] = pre[i - 1] + nums[i];\n long long l = 0, r = 1, ans = 1;\n while(r < nums.size())\n {\n long long req;\n if(l > 0)\n req = pre[r - 1] - pre[l - 1] - nums[r] * (r - l);\n else\n req = pre[r - 1] - nums[r] * (r - l);\n ans = max(ans, (long long)r - l);\n if(req + k < 0)\n l++;\n else\n r++;\n }\n ans = max(ans, (long long)r - l);\n return ans;\n }\n};", "memory": "114900" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int bSearch(int target_idx, int k, vector<int>&nums, vector<long long>&prefix ){\n\n int target = nums[target_idx];\n\n int left = 0;\n int right = target_idx;\n\n int best_idx = target_idx;\n\n while(left<=right){\n int mid = (left+right)/2;\n\n long count = (target_idx - mid +1);\n long windowSum = count*target;\n\n long currSum = prefix[target_idx] - prefix[mid] +nums[mid];\n\n int ops = windowSum - currSum;\n\n\n if(ops > k){\n left = mid+1;\n }\n else{\n best_idx = mid;\n right = mid-1;\n }\n }\n\n return target_idx - best_idx +1;\n\n }\n\n\n\n\n\n int maxFrequency(vector<int>& nums, int k) {\n\n int n = nums.size();\n\n sort(nums.begin(),nums.end());\n\n vector<long long>prefix(n);\n prefix[0] = nums[0];\n\n for(int i =1;i<n;i++){\n prefix[i] = prefix[i-1] + nums[i];\n }\n\n int result = 0;\n\n for(int target_idx =0;target_idx<n;target_idx++){\n result = max(result, bSearch(target_idx,k,nums,prefix));\n }\n\n return result;\n \n }\n};", "memory": "114900" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n unordered_map<int, int> map;\n sort(nums.begin(), nums.end());\n\n int left = 0;\n int maxFreq = 0;\n long long currSum = 0;\n\n for (int right = 0; right < nums.size(); right++) {\n map[nums[right]]++;\n currSum += nums[right];\n\n while (currSum + k < (long long)nums[right] * (right - left + 1)) {\n currSum -= nums[left];\n map[nums[left]]--;\n if (map[nums[left]] == 0) {\n map.erase(nums[left]);\n }\n left++;\n }\n\n maxFreq = max(maxFreq, right - left + 1);\n }\n\n return maxFreq;\n }\n};", "memory": "115200" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end()); // Step 1: Sort the array\n unordered_map<int, int> freqMap; // Step 2: Hash map to store frequency of elements in the window\n long long sum = 0; // Sum of elements in the current window\n int maxFreq = 0, left = 0; // Maximum frequency and left pointer of the sliding window\n \n for (int right = 0; right < nums.size(); ++right) {\n sum += nums[right]; // Add current element to the window's sum\n freqMap[nums[right]]++; // Track frequency of current element\n \n // Step 3: While the cost of making all elements in the window equal to nums[right] exceeds k\n while ((long long)(right - left + 1) * nums[right] - sum > k) {\n sum -= nums[left]; // Remove the leftmost element from the window's sum\n freqMap[nums[left]]--; // Decrease frequency count of the leftmost element\n if (freqMap[nums[left]] == 0) {\n freqMap.erase(nums[left]); // Clean up map when frequency is 0\n }\n left++; // Shrink the window from the left\n }\n \n // Step 4: Update the maximum frequency\n maxFreq = max(maxFreq, right - left + 1);\n }\n \n return maxFreq; // Return the maximum frequency found\n }\n};\n", "memory": "115300" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end()); // Step 1: Sort the array\n unordered_map<int, int> freqMap; // Step 2: Hash map to store frequency of elements in the window\n long long sum = 0; // Sum of elements in the current window\n int maxFreq = 0, left = 0; // Maximum frequency and left pointer of the sliding window\n \n for (int right = 0; right < nums.size(); ++right) {\n sum += nums[right]; // Add current element to the window's sum\n freqMap[nums[right]]++; // Track frequency of current element\n \n // Step 3: While the cost of making all elements in the window equal to nums[right] exceeds k\n while ((long long)(right - left + 1) * nums[right] - sum > k) {\n sum -= nums[left]; // Remove the leftmost element from the window's sum\n freqMap[nums[left]]--; // Decrease frequency count of the leftmost element\n if (freqMap[nums[left]] == 0) {\n freqMap.erase(nums[left]); // Clean up map when frequency is 0\n }\n left++; // Shrink the window from the left\n }\n \n // Step 4: Update the maximum frequency\n maxFreq = max(maxFreq, right - left + 1);\n }\n \n return maxFreq; // Return the maximum frequency found\n }\n};\n", "memory": "115400" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n vector<int> a;\n sort(nums.begin(),nums.end(), greater<int>());\n int l=0;\n for(int i=0;i<nums.size()-1;i++){\n a.push_back(nums[i]-nums[i+1]);\n }\n for(int i=1;i<a.size();i++){\n a[i]+=a[i-1];\n }\n int max=0,p=0,s=0;\n int curr=0;\n for(int i=0;i<a.size();i++){\n if(l>0){\n s=s+a[i]-a[l-1];\n }\n else{\n s=s+a[i];\n }\n if(s>k){\n if(curr>max){\n max=curr;\n }\n if(l==0){\n s-=(i-l+1)*a[l];\n }\n else{\n s-=(i-l+1)*(a[l]-a[l-1]);\n }\n l++;\n curr=i-l;\n }\n curr++;\n }\n if(curr>max){\n max=curr;\n }\n\n return max+1;\n }\n};", "memory": "116900" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.begin() , nums.end());\n vector<int> hash;\n hash.push_back(0);\n for(int i=1 ; i<nums.size() ; i++){\n hash.push_back(nums[i] - nums[i-1]+hash[i-1]);\n }\n int l , r, maxi;\n l=r=maxi=0;\n while(r<nums.size()-1){\n int len=r-l+1;\n long long diff=(nums[r+1]-nums[r]);\n long long x=len*diff;\n if(k>=x){\n k-=len*(nums[r+1]-nums[r]);\n maxi=max(maxi , len);\n r++;\n }\n else{\n k+=(hash[r]-hash[l]);\n l++;\n }\n }\n return maxi+1;\n\n }\n};", "memory": "117100" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "vector<int> fq(1e5+1);\nclass Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n\n for(int i=0; i<=1e5; i++) fq[i]=0;\n int n=nums.size();\n sort(nums.rbegin(), nums.rend());\n vector<long long> pre(n+1, 0);\n for(int i=0; i<n; i++) \n {\n pre[i+1]=pre[i]+nums[i];\n fq[nums[i]]++;\n }\n\n int l=0, r=0;\n int ans=0;\n long long sum=0;\n while(l+ans-1<n)\n {\n while(r<n && (long long)(r-l+1)*nums[l]-(pre[r+1]-pre[l])<=k) ++r;\n ans=max(r-l, ans);\n l+=fq[nums[l]];\n }\n\n return ans;\n }\n};", "memory": "117300" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "vector<int> fq(1e5+1);\nclass Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n\n for(int i=0; i<=1e5; i++) fq[i]=0;\n int n=nums.size();\n sort(nums.rbegin(), nums.rend());\n vector<long long> pre(n+1, 0);\n for(int i=0; i<n; i++) \n {\n pre[i+1]=pre[i]+nums[i];\n fq[nums[i]]++;\n }\n\n int l=0, r=0;\n int ans=0;\n long long sum=0;\n while(l+ans-1<n)\n {\n while(r<n && (long long)(r-l+1)*nums[l]-(pre[r+1]-pre[l])<=k) \n {\n ++r;\n }\n ans=max(r-l, ans);\n l+=fq[nums[l]];\n }\n\n return ans;\n }\n};", "memory": "117600" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k){ \n sort(nums.begin(), nums.end());\n unordered_map<long, long> freqMap;\n long long left = 0, maxFreq = 0;\n long long total = 0;\n\n for (long right = 0; right < nums.size(); ++right) {\n freqMap[nums[right]]++;\n total += nums[right];\n\n while (nums[right] * (right - left + 1) > total + k) {\n total -= nums[left];\n freqMap[nums[left]]--;\n if (freqMap[nums[left]] == 0) {\n freqMap.erase(nums[left]);\n }\n left++;\n }\n\n maxFreq = max(maxFreq, right - left + 1);\n }\n\n return maxFreq;\n \n }\n};", "memory": "119100" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> f(vector<int>& a, vector<long long>& p, long long k) {\n int n = a.size();\n vector<int> res(n, -1);\n\n auto check = [&](int i, int j) {\n long long sum = p[i] - (j > 0 ? p[j - 1] : 0);\n return (long long)a[i] * (i - j + 1) <= k + sum;\n };\n\n for (int i = 0; i < n; ++i) {\n int l = 0, r = i;\n while (r - l > 1) {\n int m = l + (r - l) / 2;\n check(i, m) ? r = m : l = m + 1;\n }\n res[i] = check(i, l) ? l : r;\n }\n return res;\n }\n\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n int n = nums.size();\n vector<long long> pref(n);\n pref[0] = nums[0];\n for (int i = 1; i < n; ++i)\n pref[i] = pref[i - 1] + nums[i];\n\n vector<int> v = f(nums, pref, k);\n\n int ans = 0;\n for (int i = 0; i < n; ++i) {\n ans = max(ans, i - v[i] + 1);\n }\n\n return ans;\n }\n};", "memory": "119200" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // Helper function to check if it's possible to make 'mid' elements equal to nums[i] with k operations\n bool isPossible(vector<int>& nums, int k, int i, int mid, vector<long long>& prefixSum) {\n int startIdx = i - (mid - 1);\n long long sumNeeded = static_cast<long long>(nums[i]) * mid;\n long long currentSum = prefixSum[i] - (startIdx > 0 ? prefixSum[startIdx - 1] : 0);\n\n return sumNeeded - currentSum <= k;\n }\n\n // Binary search function to find the maximum length for making elements equal to nums[i]\n int findMaxFrequency(vector<int>& nums, int k, int i, vector<long long>& prefixSum) {\n int low = 1;\n int high = i + 1;\n\n while (low <= high) {\n int mid = low + (high - low) / 2;\n\n if (isPossible(nums, k, i, mid, prefixSum)) {\n low = mid + 1;\n } else {\n high = mid - 1;\n }\n }\n\n return high;\n }\n\n // Main function to find the maximum frequency of any element in the array\n int maxFrequency(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> maxFreq(n, 0);\n sort(nums.begin(), nums.end());\n\n vector<long long> prefixSum(n);\n prefixSum[0] = nums[0];\n for (int i = 1; i < n; ++i) {\n prefixSum[i] = prefixSum[i - 1] + nums[i];\n }\n\n int result = 0;\n for (int i = 0; i < n; ++i) {\n int currentMaxFreq;\n\n if (i > 0 && nums[i] != nums[i - 1]) {\n currentMaxFreq = findMaxFrequency(nums, k, i, prefixSum);\n } else {\n currentMaxFreq = (i == 0) ? 1 : maxFreq[i - 1] + 1;\n }\n\n maxFreq[i] = currentMaxFreq;\n result = max(result, currentMaxFreq);\n }\n\n return result;\n }\n};\n", "memory": "119300" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // Function to find the appropriate index using binary search\n int bs(int i, int j, int k, vector<int>& nums, vector<long long>& prefixSum) {\n int left = 0, right = j, idx = j;\n while (left <= right) {\n int mid = (left + right) / 2;\n // Use long long to avoid overflow\n long long total = (long long)nums[j] * (j - mid + 1) - (prefixSum[j] - (mid > 0 ? prefixSum[mid - 1] : 0));\n if (total <= k) {\n idx = mid;\n right = mid - 1;\n } else {\n left = mid + 1;\n }\n }\n return idx;\n }\n\n int maxFrequency(vector<int>& nums, int k) {\n int n = nums.size();\n sort(nums.begin(), nums.end());\n\n // Create the prefix sum array\n vector<long long> prefixSum(n, 0); // Use long long to store large sums\n prefixSum[0] = nums[0];\n for (int i = 1; i < n; i++) {\n prefixSum[i] = prefixSum[i - 1] + nums[i];\n }\n\n vector<int> ans(n, 0);\n int maxFreq = 1;\n\n for (int i = 1; i < n; i++) {\n // Binary search to find the valid left index\n int idx = bs(0, i, k, nums, prefixSum);\n\n // Calculate the maximum frequency\n ans[i] = i - idx + 1;\n maxFreq = max(maxFreq, ans[i]);\n }\n\n return maxFreq;\n }\n};\n", "memory": "119400" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end(), greater<int>());\n unordered_map<int, int> freqmap;\n for (auto val : nums)\n {\n freqmap[val]++;\n }\n int i = 0;\n int temp = k;\n int current_size = freqmap[nums[0]];\n int current_max_size = current_size;\n while (i + current_size < nums.size())\n {\n int diff = temp - (nums[i] - nums[i + current_size]);\n if (diff >= 0)\n {\n temp = diff;\n current_size++;\n }\n else\n {\n temp = k;\n i = i + freqmap[nums[i]];\n if (current_size > current_max_size)\n {\n current_max_size = current_size;\n }\n current_size = freqmap[nums[i]];\n }\n }\n if (current_size > current_max_size)\n {\n current_max_size = current_size;\n }\n return current_max_size;\n }\n};", "memory": "121400" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n unordered_map<int,int>mp;\n for(int i=0;i<nums.size();i++){\n mp[nums[i]]++;\n }\n int i=0;\n int co=INT_MIN;\n int cot=0; //count addition needed\n int count=0; //count element\n while(i<nums.size()){\n\n cot=k;\n count=mp[nums[i]];\n for(int j=i-1;j>=0;j--){\n if(nums[i]-nums[j]<=cot){\n count++;\n cot-=(nums[i]-nums[j]);\n }\n else break;\n }\n if(count>co) co=count;\n i+=mp[nums[i]];\n //nm[nums[i]]=-1;\n }\n return co;\n }\n};", "memory": "121500" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int n=nums.size();\n unordered_map<int,int> mpp;\n sort(nums.begin(),nums.end());\n long long int ans=1;\n long long int i=0;\n long long int j=0;\n long long int temp=0;\n long long int sum=0;\n while(j<n){\n sum+=nums[j];\n mpp[nums[j]]++;\n temp=(j-i+1)*nums[j]-sum;\n while(temp>k && i<=j){\n sum-=nums[i];\n mpp[nums[i]]--;\n temp=temp-(nums[j]-nums[i]);\n i++;\n }\n ans=max(ans,j-i+1);\n j++;\n }\n return ans;\n }\n};", "memory": "121600" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n unordered_map<int,int>mpp;\n \n for(int i=0;i<nums.size();i++)\n {\n mpp[nums[i]]++;\n }\n \n sort(nums.begin(),nums.end());\n \n int i=mpp[nums[0]];\n int max=i;\n \n \n int freq;\n for(;i<nums.size();)\n {\n freq=mpp[nums[i]];\n int m=k;\n for(int j=i-1;j>=0;j--)\n {\n if(nums[i]-nums[j]<=m)\n {\n m-=(nums[i]-nums[j]);\n freq++;\n }\n else\n {\n break;\n }\n }\n if(max<freq)\n {\n max=freq;\n }\n i+=mpp[nums[i]];\n \n\n }\n \n return max;\n\n\n }\n};", "memory": "121700" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n map<int,int>mp;\n long long ans=1,temp=0,sum=0,i=0,j=0,n=nums.size();\n while(j<n){\n sum+=nums[j];\n mp[nums[j]]++;\n temp=(j-i+1)*nums[j]-sum;\n while(temp>k && i<=j ){\n sum-=nums[i];\n mp[nums[i]]--;\n temp-=nums[j]-nums[i++];\n }\n ans=max(ans,j-i+1);\n j++;\n }\n return ans;\n }\n};", "memory": "122600" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkIfPangram(string sentence) {\n vector<int> arr(26,0);\n\n int count = 0;\n for(auto it: sentence){\n int index= it - 'a';\n if(arr[index]==0){\n arr[index]++;\n count++;\n }\n }\n if(count == 26) return true;\n else return false;\n }\n};", "memory": "7900" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkIfPangram(string sentence) {\n vector<char>res(26,0);\n for(int i = 0; i < sentence.size();i++){\n res[sentence[i] - 'a']++;\n }\n \n for(int i = 0; i < 26; i++){\n if(res[i] == 0) return false;\n }\n return true;\n }\n};", "memory": "7900" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool checkIfPangram(string sentence) {\n vector<bool>alpha(26,0);\n for(int i=0;i<sentence.size();i++){\n alpha[sentence[i]-'a']=1;\n }\n\n for(int i=0;i<26;i++){\n if(alpha[i]==0)\n return 0;\n }\n\n return 1;\n }\n};", "memory": "8000" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool checkIfPangram(string sentence) {\n vector<char>res(26,0);\n for(int i = 0; i < sentence.size();i++){\n res[sentence[i] - 'a']++;\n }\n \n for(int i = 0; i < 26; i++){\n if(res[i] == 0) return false;\n }\n return true;\n }\n};", "memory": "8000" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool checkIfPangram(string sentence) {\n std::set<char> seen;\n \n for (char c : sentence) {\n seen.insert(c);\n }\n \n return seen.size() == 26;\n }\n};", "memory": "8100" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool checkIfPangram(string sentence) {\n \n set<char>st;\n for(int j=0;j<sentence.size();j++){\n st.insert(sentence[j]);\n }\n if(st.size()==26) return true;\n else return false;\n }\n\n};", "memory": "8100" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution \n{\npublic:\n bool checkIfPangram(string sentence)\n {\n unordered_set<char> set;\n \n for(char c : sentence)\n {\n if(set.find(c) == set.end())\n {\n set.insert(c);\n }\n \n if(set.size() == 26)\n {\n return true;\n }\n \n }\n return false;\n }\n};", "memory": "8200" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool checkIfPangram(string sentence) {\n std::set<char> myset = {};\n for (char& c : sentence) {\n myset.insert(c); \n }\n if(myset.size() < 26) {\n return false;\n }\n return true; \n }\n};", "memory": "8200" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkIfPangram(string sentence) {\n std::unordered_set<char> set;\n for(int i = 97; i <= 122; i++)\n set.insert(i);\n\n for(const auto& c : sentence)\n {\n set.erase(c);\n }\n return set.size() == 0;\n }\n};", "memory": "8300" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkIfPangram(string sentence) {\n std::unordered_set<char> set;\n for(int i = 97; i <= 122; i++)\n set.insert(i);\n\n for(const auto& c : sentence)\n {\n set.erase(c);\n }\n return set.size() == 0;\n }\n};", "memory": "8300" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkIfPangram(string sentence) {\n std::unordered_set<char> set;\n for(int i = 97; i <= 122; i++)\n set.insert(i);\n\n for(const auto& c : sentence)\n {\n set.erase(c);\n }\n return set.size() == 0;\n }\n};", "memory": "8400" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkIfPangram(string sentence) \n {\n std::map<char, int> alphabetMap = {};\n \n for(char c : sentence)\n {\n if(alphabetMap.find(c) == alphabetMap.end())\n {\n alphabetMap[c] = 1;\n }\n }\n \n return (alphabetMap.size() == 26);\n \n }\n};", "memory": "8500" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int mia(char e){\n string alpha=\"abcdefghijklmnopqrstuvwxyz\";\n for(int i=0;i<alpha.size();i++){\n if(alpha[i]==e){\n return i;\n }\n }\n return -1;\n }\n bool checkIfPangram(string sentence) {\n int g;\n vector<int> harsh(26,0);\n vector<int> visited(sentence.size());\n sort(sentence.begin(),sentence.end());\n int c;\n for(int i=0;i<sentence.size();i++){\n if(visited[i]!=1){\n g=mia(sentence[i]);\n c=1;\n for(int j=i+1;j<sentence.size();j++){\n if(sentence[i]==sentence[j]){\n visited[j]=1;\n c++;\n }else{\n visited[i]=1;\n break;\n }\n }\n harsh[g]=c;\n }\n }\n for(int i=0;i<26;i++){\n if(harsh[i]==0){\n return false;\n }\n }\n return true;\n }\n};", "memory": "8800" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "using namespace std;\nclass Solution {\npublic:\n bool checkIfPangram(string sentence) {\n unordered_map<char,int> charMap;\n for (int i=0; i<sentence.size(); i++)\n {\n charMap[sentence[i]]++;\n }\n if( charMap.size() == 26) return true;\n else return false;\n }\n};", "memory": "8900" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkIfPangram(string sentence) {\n int n = sentence.length();\n unordered_map<char, int> mp;\n for (char c : sentence) {\n if (c >= 'a' && c <= 'z') {\n mp[c]++;\n }\n }\n\n for (char c = 'a'; c <= 'z'; c++) {\n if (mp.find(c) == mp.end()) {\n return false;\n }\n }\n\n return true;\n }\n};", "memory": "9000" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkIfPangram(string sentence) {\n unordered_set<char> seen(sentence.begin(), sentence.end());\n return seen.size()==26;\n }\n};", "memory": "9100" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkIfPangram(string sen) {\n unordered_map<char,int>mp;\n int n = sen.length();\n for(int i=0; i<n;i++){\n mp[sen[i]++];\n }\n for(int i='a'; i<='z'; i++){\n cout<<i;\n if(mp.find(i)==mp.end()){\n return false;\n }\n }\n return true;\n }\n};", "memory": "9200" }
1,960
<p>A <strong>pangram</strong> is a sentence where every letter of the English alphabet appears at least once.</p> <p>Given a string <code>sentence</code> containing only lowercase English letters, return<em> </em><code>true</code><em> if </em><code>sentence</code><em> is a <strong>pangram</strong>, or </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;thequickbrownfoxjumpsoverthelazydog&quot; <strong>Output:</strong> true <strong>Explanation:</strong> sentence contains at least one of every letter of the English alphabet. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> sentence = &quot;leetcode&quot; <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= sentence.length &lt;= 1000</code></li> <li><code>sentence</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkIfPangram(string sentence) {\n unordered_map<int,int>mpp;\n for(int i=0;i<sentence.length();i++){\n mpp[sentence[i]]++;\n }\n if(mpp.size()==26) return true;\n else return false;\n }\n};", "memory": "9200" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n stack<TreeNode*> s1,s2;\n TreeNode *node = root1,*tmp;\n while(node){\n tmp = node->left;\n node->left = NULL;\n s1.push(node);\n node = tmp;\n }node = root2,tmp;\n while(node){\n tmp = node->left;\n node->left = NULL;\n s2.push(node);\n node = tmp;\n }\n vector<int> ans;\n while(!s1.empty() && !s2.empty()){\n if(s1.top()->val <= s2.top()->val){\n tmp = s1.top();\n s1.pop();\n ans.push_back(tmp->val);\n node = tmp->right,tmp;\n while(node){\n tmp = node->left;\n node->left = NULL;\n s1.push(node);\n node = tmp;\n }\n }else{\n tmp = s2.top();\n s2.pop();\n ans.push_back(tmp->val);\n node = tmp->right,tmp;\n while(node){\n tmp = node->left;\n node->left = NULL;\n s2.push(node);\n node = tmp;\n }\n }\n }\n\n while(!s1.empty()){\n tmp = s1.top();\n s1.pop();\n ans.push_back(tmp->val);\n node = tmp->right,tmp;\n while(node){\n tmp = node->left;\n node->left = NULL;\n s1.push(node);\n node = tmp;\n }\n }\n\n while(!s2.empty()){\n tmp = s2.top();\n s2.pop();\n ans.push_back(tmp->val);\n node = tmp->right,tmp;\n while(node){\n tmp = node->left;\n node->left = NULL;\n s2.push(node);\n node = tmp;\n }\n }\n return ans;\n }\n};", "memory": "62300" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> getAllElements(TreeNode *root1, TreeNode *root2) {\n vector<int> result;\n stack<TreeNode *> st1, st2;\n while (root1) {\n st1.push(root1);\n root1 = root1->left;\n st1.top()->left = nullptr;\n }\n while (root2) {\n st2.push(root2);\n root2 = root2->left;\n st2.top()->left = nullptr;\n }\n while (!st1.empty() || !st2.empty()) {\n auto &st_min = st1.empty()\n ? st2\n : (st2.empty() || st1.top()->val < st2.top()->val)\n ? st1\n : st2;\n TreeNode *curr = st_min.top();\n st_min.pop();\n result.push_back(curr->val);\n curr = curr->right;\n while (curr) {\n st_min.push(curr);\n curr = curr->left;\n st_min.top()->left = nullptr;\n }\n }\n return result;\n }\n};\n\n", "memory": "62400" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n vector<int> inorderTraversal(TreeNode* root) {\n vector<int> ans;\n if(root==NULL){\n return ans;\n }\n\n TreeNode* curr = root;\n\n while(curr){\n if(curr->left){\n TreeNode* prev = curr->left;\n while(prev->right){\n prev = prev->right;\n }\n prev->right = curr;\n\n TreeNode* temp = curr;\n curr = curr->left;\n temp->left = NULL;\n }\n else{\n ans.push_back(curr->val);\n curr = curr->right;\n }\n }\n\n return ans;\n }\n\n void merge(vector<int>& arr1, vector<int>& arr2, vector<int>& ans){\n int i = 0;\n int j = 0;\n while(i<arr1.size() && j<arr2.size()){\n if(arr1[i]<arr2[j]){\n ans.push_back(arr1[i]);\n i++;\n }\n else{\n ans.push_back(arr2[j]);\n j++;\n }\n }\n while(i<arr1.size()){\n ans.push_back(arr1[i]);\n i++;\n }\n while(j<arr2.size()){\n ans.push_back(arr2[j]);\n j++;\n }\n } \npublic:\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> in1 = inorderTraversal(root1);\n vector<int> in2 = inorderTraversal(root2);\n vector<int> ans;\n merge(in1,in2,ans);\n return ans;\n }\n};", "memory": "64599" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int partials[2][5001], partialsPos[2] = {0, 0};\n void dfs(TreeNode* root, bool isSecond = false) {\n if (!root) return;\n dfs(root->left, isSecond);\n partials[isSecond][partialsPos[isSecond]++] = root->val;\n dfs(root->right, isSecond);\n }\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n dfs(root1);\n dfs(root2, true);\n int i = 0, vLen = partialsPos[0] + partialsPos[1], partialsPointers[2] = {0, 0};\n vector<int> res(vLen);\n while (i < vLen) {\n if (partialsPointers[0] < partialsPos[0] && partials[0][partialsPointers[0]] < partials[1][partialsPointers[1]] || partialsPointers[1] == partialsPos[1]) {\n res[i++] = partials[0][partialsPointers[0]];\n partialsPointers[0]++;\n }\n else {\n res[i++] = partials[1][partialsPointers[1]];\n partialsPointers[1]++;\n }\n }\n return res;\n }\n};", "memory": "81500" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n #include<algorithm>\n vector<int> c;\n void pa(TreeNode* root1)\n {\n if(root1==NULL)\n {\n return;\n }\n else if(root1->right==NULL && root1->left!=NULL)\n {\n c.push_back(root1->val);\n pa(root1->left);\n }\n else if(root1->right!=NULL && root1->left==NULL)\n {\n c.push_back(root1->val);\n pa(root1->right);\n }\n else if(root1->right!=NULL && root1->left!=NULL)\n {\n c.push_back(root1->val);\n pa(root1->right);\n pa(root1->left);\n }\n else\n {\n c.push_back(root1->val);\n return;\n }\n }\n \nclass Solution {\npublic:\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) \n {\n c.clear();\n pa(root1);\n pa(root2);\n sort(c.begin(),c.end());\n return c;\n }\n};", "memory": "82700" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n static vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> res;\n res.reserve(5000 * 2);\n function<void(TreeNode*)> dfs = [&](TreeNode* node) {\n if (node == nullptr) {\n return;\n }\n if (node->left != nullptr) {\n dfs(node->left);\n }\n res.push_back(node->val);\n if (node->right != nullptr) {\n dfs(node->right);\n }\n };\n dfs(root1);\n auto mid = res.begin() + res.size();\n dfs(root2);\n ranges::inplace_merge(res.begin(), mid, res.end());\n return res;\n }\n};", "memory": "82700" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n #include<algorithm>\n vector<int> c;\n void pa(TreeNode* root1)\n {\n if(root1==NULL)\n {\n return;\n }\n else if(root1->right==NULL && root1->left!=NULL)\n {\n c.push_back(root1->val);\n pa(root1->left);\n }\n else if(root1->right!=NULL && root1->left==NULL)\n {\n c.push_back(root1->val);\n pa(root1->right);\n }\n else if(root1->right!=NULL && root1->left!=NULL)\n {\n c.push_back(root1->val);\n pa(root1->right);\n pa(root1->left);\n }\n else\n {\n c.push_back(root1->val);\n return;\n }\n }\n \nclass Solution {\npublic:\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) \n {\n c.clear();\n pa(root1);\n pa(root2);\n sort(c.begin(),c.end());\n return c;\n }\n};", "memory": "82800" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n \n vector<int> ans;\n stack<TreeNode*> s1;\n stack<TreeNode*> s2;\n\n while(root1)\n {\n s1.push(root1);\n root1 = root1->left;\n }\n\n while(root2)\n {\n s2.push(root2);\n root2 = root2->left;\n }\n\n while(!s1.empty() || !s2.empty())\n {\n if(s1.empty())\n {\n ans.push_back(s2.top()->val);\n root2 = s2.top()->right;\n s2.pop();\n }\n else if(s2.empty())\n {\n ans.push_back(s1.top()->val);\n root1 = s1.top()->right;\n s1.pop();\n }\n else if(s1.top()->val >= s2.top()->val)\n {\n ans.push_back(s2.top()->val);\n root2 = s2.top()->right;\n s2.pop();\n }\n else{\n ans.push_back(s1.top()->val);\n root1 = s1.top()->right;\n s1.pop();\n }\n \n while(root1)\n {\n s1.push(root1);\n root1 = root1->left;\n }\n\n while(root2)\n {\n s2.push(root2);\n root2 = root2->left;\n }\n\n }\n return ans;\n }\n};", "memory": "82900" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution1 {\npublic:\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> t1;\n vector<int> t2;\n dfs(root1, t1);\n dfs(root2, t2);\n return merge(t1, t2);\n }\n\n void dfs(TreeNode* root, vector<int>& t) {\n if (!root) return;\n dfs(root->left, t);\n t.push_back(root->val);\n dfs(root->right, t);\n }\n\n vector<int> merge(vector<int>& t1, vector<int>& t2) {\n vector<int> ans;\n int i = 0, j = 0;\n while (i < t1.size() && j < t2.size()) {\n if (t1[i] <= t2[j])\n ans.push_back(t1[i++]);\n else\n ans.push_back(t2[j++]);\n }\n while (i < t1.size()) ans.push_back(t1[i++]);\n while (j < t2.size()) ans.push_back(t2[j++]);\n return ans;\n }\n};\n // below is fastest solution;\nclass Solution {\npublic:\n vector<int> getAllElements(TreeNode *root1, TreeNode *root2) {\n vector<int> result;\n stack<TreeNode *> st1, st2;\n while (root1) {\n st1.push(root1);\n root1 = root1->left;\n //st1.top()->left = nullptr;\n }\n while (root2) {\n st2.push(root2);\n root2 = root2->left;\n //st2.top()->left = nullptr;\n }\n while (!st1.empty() || !st2.empty()) {\n auto &st_min = st1.empty()\n ? st2\n : (st2.empty() || st1.top()->val < st2.top()->val)\n ? st1\n : st2;\n TreeNode *curr = st_min.top();\n st_min.pop();\n result.push_back(curr->val);\n curr = curr->right;\n while (curr) {\n st_min.push(curr);\n curr = curr->left;\n //st_min.top()->left = nullptr;\n }\n }\n return result;\n }\n};", "memory": "83000" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void pushLeft(TreeNode* root,stack<TreeNode*>&st){\n while(root){\n st.push(root);\n root=root->left;\n }\n }\n\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n stack<TreeNode*>st1,st2;\n vector<int>result;\n\n pushLeft(root1,st1);\n pushLeft(root2,st2);\n\n while(!st1.empty()&&!st2.empty()){\n if((st1.top()->val<=st2.top()->val)){\n TreeNode* root=st1.top();\n st1.pop();\n result.push_back(root->val);\n pushLeft(root->right,st1);\n }else{\n TreeNode* root=st2.top();\n st2.pop();\n result.push_back(root->val);\n pushLeft(root->right,st2); \n }\n }\n\n while(!st1.empty()){\n TreeNode* root=st1.top();\n st1.pop();\n result.push_back(root->val);\n pushLeft(root->right,st1);\n }\n\n while(!st2.empty()){\n TreeNode* root=st2.top();\n st2.pop();\n result.push_back(root->val);\n pushLeft(root->right,st2); \n }\n\n return result;\n }\n};", "memory": "83100" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n void addLeftNodes(TreeNode*& node, std::stack<TreeNode*>& s)\n {\n while (node != nullptr)\n {\n s.push(node);\n node = node->left;\n }\n }\npublic:\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2)\n {\n std::vector<int> ans;\n std::stack<TreeNode*> stack1, stack2;\n\n while (root1 != nullptr or root2 != nullptr or\n !stack1.empty() or !stack2.empty())\n {\n addLeftNodes(root1, stack1);\n addLeftNodes(root2, stack2);\n \n if (stack2.empty() or \n (!stack1.empty() and stack1.top()->val <= stack2.top()->val))\n {\n root1 = stack1.top();\n stack1.pop();\n ans.push_back(root1->val);\n root1 = root1->right;\n }\n else\n {\n root2 = stack2.top();\n stack2.pop();\n ans.push_back(root2->val);\n root2 = root2->right;\n }\n }\n return ans;\n }\n};", "memory": "83100" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n\n //\n vector<int>ans;\n stack<TreeNode*>st1;\n stack<TreeNode*>st2;\n\n while(root1){\n st1.push(root1);\n root1=root1->left;\n }\n\n while(root2){\n st2.push(root2);\n root2=root2->left;\n }\n\n while(!st1.empty() && !st2.empty()){\n\n //s1==s2\n\n if(st1.top()->val==st2.top()->val){\n\n ans.push_back(st1.top()->val);\n ans.push_back(st1.top()->val);\n\n root1=st1.top()->right;\n root2=st2.top()->right;\n\n st1.pop(), st2.pop();\n\n }\n //s1>s2\n else if(st1.top()->val<st2.top()->val){\n ans.push_back(st1.top()->val);\n root1=st1.top()->right;\n st1.pop();\n }\n //s1>s2\n else{\n ans.push_back(st2.top()->val);\n root2=st2.top()->right;\n st2.pop();\n }\n\n while(root1){\n st1.push(root1);\n root1=root1->left;\n }\n\n while(root2){\n st2.push(root2);\n root2=root2->left;\n }\n }\n\n //check for which stack is remaining \n while(!st1.empty()){\n ans.push_back(st1.top()->val);\n root1=st1.top()->right;\n st1.pop();\n\n \n while(root1){\n st1.push(root1);\n root1=root1->left;\n }\n }\n\n while(!st2.empty()){\n ans.push_back(st2.top()->val);\n root2=st2.top()->right;\n st2.pop();\n\n while(root2){\n st2.push(root2);\n root2=root2->left;\n }\n }\n\n return ans;\n }\n};", "memory": "83200" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n stack<TreeNode*> st1,st2;\n // st1.push(root1);st2.push(root2);\n vector<int> ans;\n while(root1 ||!st1.empty()|| root2 ||!st2.empty()){\n while(root1){\n st1.push(root1);\n root1=root1->left;\n }\n while(root2){\n st2.push(root2);\n root2=root2->left;\n }\n\n // focus on any one tree and hold the other traversal\n if(st2.empty() || (!st1.empty() && st1.top()->val<=st2.top()->val)){\n root1=st1.top();\n ans.push_back(root1->val);\n st1.pop();\n root1=root1->right;\n }\n // else(st1.empty()||(!st2.empty() && st2.top()->val<=st1.top()->val)){\n else{\n root2=st2.top();\n ans.push_back(root2->val);\n st2.pop();\n root2=root2->right;\n }\n\n\n }return ans;\n }\n};", "memory": "83200" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorderTraversal(TreeNode* root, queue<int>& inorder){\n if(root==nullptr) return;\n inorderTraversal(root->left,inorder);\n inorder.push(root->val);\n inorderTraversal(root->right,inorder);\n }\n void helper(TreeNode* root, queue<int>& inorder, vector<int>& res){\n if(root==nullptr) return;\n helper(root->left,inorder,res);\n while(!inorder.empty() && root->val > inorder.front()){\n res.push_back(inorder.front());\n inorder.pop();\n }\n res.push_back(root->val);\n helper(root->right,inorder,res);\n }\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> res;\n queue<int> inorder;\n inorderTraversal(root1,inorder);\n helper(root2,inorder,res);\n while(!inorder.empty()){\n res.push_back(inorder.front());\n inorder.pop();\n }\n return res;\n }\n};", "memory": "83600" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "\nclass Solution {\npublic:\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> list1, list2;\n inOrderTraversal(root1, list1);\n inOrderTraversal(root2, list2);\n \n // Merge the two sorted lists\n vector<int> result;\n mergeSortedLists(list1, list2, result);\n \n return result;\n }\n\nprivate:\n void inOrderTraversal(TreeNode* root, vector<int>& list) {\n if (!root) return;\n inOrderTraversal(root->left, list);\n list.push_back(root->val);\n inOrderTraversal(root->right, list);\n }\n\n void mergeSortedLists(const vector<int>& list1, const vector<int>& list2, vector<int>& result) {\n result.resize(list1.size() + list2.size());\n merge(list1.begin(), list1.end(), list2.begin(), list2.end(), result.begin());\n }\n};\n", "memory": "83700" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* root,vector<int> &bigarray){\n if(!root) return;\n inorder(root->left,bigarray);\n bigarray.push_back(root->val);\n inorder(root->right,bigarray);\n\n }\n\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> bigarray1;\n vector<int> bigarray2;\n inorder(root1,bigarray1);\n inorder(root2,bigarray2);\n //int n = bigarray1.size();\n //int m = 0;\n vector<int> merged(bigarray1.size()+bigarray2.size());\n merge(bigarray1.begin(),bigarray1.end(),bigarray2.begin(),bigarray2.end(),merged.begin());\n return merged;\n }\n};", "memory": "83800" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* t,vector<int> &v){\n if(!t) return;\n inorder(t->left,v);\n v.push_back(t->val);\n inorder(t->right,v);\n }\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> v1,v2;\n inorder(root1,v1);\n inorder(root2,v2);\n int a = v1.size(),b = v2.size();\n vector<int> V(a+b);\n int i = 0,j = 0,k = 0;\n while(i < a && j < b){\n if(v1[i] >= v2[j]){\n V[k++] = v2[j++];\n }\n else{\n V[k++] = v1[i++];\n }\n }\n while(i < a) V[k++] = v1[i++];\n while(j < b) V[k++] = v2[j++];\n\n return V;\n }\n};", "memory": "83900" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* root, vector<int> &res){\n if(!root) return;\n inorder(root->left, res);\n res.push_back(root->val);\n inorder(root->right, res);\n }\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> res;\n inorder(root1, res);\n inorder(root2, res);\n sort(res.begin(), res.end());\n return res;\n }\n};", "memory": "84000" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void find_nodes(vector<int>&vt , TreeNode* root ){\n \n if(!root) return;\n vt.push_back(root->val);\n find_nodes(vt,root->left);\n find_nodes(vt , root->right);\n // vt.push_back()\n }\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int>vt;\n find_nodes(vt , root1);\n find_nodes(vt , root2);\n sort(vt.begin() , vt.end());\n return vt; \n }\n};", "memory": "84100" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> answer;\n \n insertToVector(root1, answer);\n insertToVector(root2, answer);\n \n sort(answer.begin(), answer.end());\n \n return answer;\n \n }\n \n void insertToVector(TreeNode* current, vector<int>& answer) {\n if (current == nullptr)\n return;\n \n answer.push_back(current->val);\n \n insertToVector(current->left, answer);\n insertToVector(current->right, answer);\n }\n};", "memory": "84200" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> vals;\n order(root1, vals);\n order(root2, vals);\n sort(vals.begin(), vals.end());\n return vals;\n }\n\nprivate:\n void order(TreeNode* node, vector<int>& vals) {\n if (!node) {\n return;\n }\n order(node->left, vals);\n order(node->right, vals);\n vals.push_back(node->val);\n }\n};", "memory": "84300" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n *//**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n\n } */\nclass Solution {\npublic:\n void dfs(TreeNode* root,vector<int>& a1) {\n if (!root) return; \n dfs(root->left,a1); \n dfs(root->right,a1); \n a1.push_back(root->val); \n }\n\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> a1;\n dfs(root1,a1); \n dfs(root2,a1); \n sort(a1.begin(), a1.end()); \n return a1; \n }\n};\n\n", "memory": "84400" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void getEle(TreeNode* root,vector<int> &ans){\n if(root==NULL){\n return ;\n }\n getEle(root->right,ans);\n ans.push_back(root->val);\n getEle(root->left,ans);\n }\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> ans;\n getEle(root1,ans);\n getEle(root2,ans);\n sort(ans.begin(),ans.end());\n return ans;\n }\n};", "memory": "84500" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void f(TreeNode* root,vector<int>& v){\n if(!root) return;\n f(root->left,v);\n v.push_back(root->val);\n f(root->right,v);\n }\n void merge(vector<int>& v1,vector<int>& v2){\n vector<int> temp=v1;\n int i=0,j=0;\n v1.clear();\n while(i<temp.size() && j<v2.size()){\n if(temp[i]<v2[j]){\n v1.push_back(temp[i]);\n i++;\n }else{\n v1.push_back(v2[j]);\n j++;\n }\n }\n while(i<temp.size()){\n v1.push_back(temp[i]);\n i++;\n }\n while(j<v2.size()){\n v1.push_back(v2[j]);\n j++;\n }\n temp.clear();\n\n }\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> l1;\n vector<int> l2;\n f(root1,l1);\n f(root2,l2);\n merge(l1,l2);\n return l1;\n }\n};", "memory": "84600" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void f(TreeNode* root,vector<int>& v){\n if(!root) return;\n f(root->left,v);\n v.push_back(root->val);\n f(root->right,v);\n }\n void merge(vector<int>& v1,vector<int>& v2){\n vector<int> temp=v1;\n int i=0,j=0;\n v1.clear();\n while(i<temp.size() && j<v2.size()){\n if(temp[i]<v2[j]){\n v1.push_back(temp[i]);\n i++;\n }else{\n v1.push_back(v2[j]);\n j++;\n }\n }\n while(i<temp.size()){\n v1.push_back(temp[i]);\n i++;\n }\n while(j<v2.size()){\n v1.push_back(v2[j]);\n j++;\n }\n //temp.clear();\n\n }\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> l1;\n vector<int> l2;\n f(root1,l1);\n f(root2,l2);\n merge(l1,l2);\n return l1;\n }\n};", "memory": "84700" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* t,vector<int> &v){\n if(!t) return;\n inorder(t->left,v);\n v.push_back(t->val);\n inorder(t->right,v);\n }\n vector<int> merge(vector<int> v1,vector<int> v2){\n int a = v1.size(),b = v2.size();\n vector<int> V(a+b);\n int i = 0,j = 0,k = 0;\n while(i < a && j < b){\n if(v1[i] >= v2[j]){\n V[k++] = v2[j++];\n }\n else{\n V[k++] = v1[i++];\n }\n }\n while(i < a) V[k++] = v1[i++];\n while(j < b) V[k++] = v2[j++];\n\n return V;\n }\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> v1,v2;\n inorder(root1,v1);\n inorder(root2,v2);\n return merge(v1,v2);\n }\n};", "memory": "84800" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution \n{\npublic:\n vector<int> result;\n\n void helper(TreeNode *root)\n {\n if(root == NULL)\n {\n return;\n }\n\n result.push_back(root->val);\n\n helper(root->left);\n helper(root->right);\n }\n\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) \n {\n helper(root1);\n helper(root2);\n\n sort(begin(result), end(result));\n\n return result;\n }\n};", "memory": "84900" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> ans;\n\n void inorder(TreeNode* root) {\n if(!root) return;\n inorder(root->left);\n ans.push_back(root->val);\n inorder(root->right);\n }\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n inorder(root1);\n inorder(root2);\n sort(ans.begin(), ans.end());\n return ans;\n }\n};", "memory": "85000" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void InorderTraverse(TreeNode* root, vector<int>& Inorder){\n\n if(!root)\n return;\n\n InorderTraverse(root->left, Inorder);\n Inorder.push_back(root->val);\n InorderTraverse(root->right, Inorder);\n\n }\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n \n vector<int>Inorder1;\n vector<int>Inorder2;\n\n InorderTraverse(root1, Inorder1);\n InorderTraverse(root2, Inorder2);\n\n vector<int>ans;\n int i=0, j=0;\n while(i<Inorder1.size() && j<Inorder2.size()){\n\n if(Inorder1[i]<=Inorder2[j]){\n ans.push_back(Inorder1[i]);\n i++;\n }\n else{\n ans.push_back(Inorder2[j]);\n j++;\n }\n }\n\n while(i<Inorder1.size()){\n ans.push_back(Inorder1[i]);\n i++; \n }\n\n while(j<Inorder2.size()){\n ans.push_back(Inorder2[j]);\n j++; \n }\n\n\n\n return ans;\n }\n};", "memory": "85400" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n #define Node TreeNode\n #define pb push_back\nclass Solution {\npublic:\n void dfs(Node* root,vector<int> &a){\n if(!root)return;\n dfs(root->left,a);\n a.pb(root->val);\n dfs(root->right,a);\n }\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> a1,a2;\n dfs(root1,a1);\n dfs(root2,a2);\n int i=0,j=0;\n int n = a1.size();\n int m = a2.size();\n vector<int> ans;\n while(i<n && j<m){\n if(a1[i]<a2[j]){\n ans.pb(a1[i++]);\n }\n else if(a1[i]>a2[j]){\n ans.pb(a2[j++]);\n }\n else{\n ans.pb(a1[i++]);\n ans.pb(a2[j++]);\n }\n }\n while(i<n)ans.pb(a1[i++]);\n while(j<m)ans.pb(a2[j++]);\n return ans;\n }\n};", "memory": "85400" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> list1;\n vector<int> list2;\n inorder(root1, list1);\n inorder(root2, list2);\n vector<int> result;\n\n int i=0;\n int j=0;\n while (i<list1.size() && j<list2.size()) {\n if (list1[i] < list2[j]) {\n result.push_back(list1[i]);\n i++;\n } else {\n result.push_back(list2[j]);\n j++;\n }\n }\n while (i<list1.size()) {\n result.push_back(list1[i++]);\n }\n while (j<list2.size()) {\n result.push_back(list2[j++]);\n }\n return result;\n }\n\n void inorder(TreeNode* root, vector<int>& list) {\n if (root == nullptr) {\n return;\n }\n inorder(root->left, list);\n list.push_back(root->val);\n inorder(root->right, list);\n }\n};", "memory": "85500" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n \n void bfs(TreeNode *root,vector<int>&ans)\n {\n if(root == NULL)\n {\n return;\n }\n bfs(root->left,ans);\n ans.push_back(root->val);\n bfs(root->right,ans);\n\n }\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> elements1, elements2;\n \n \n bfs(root1, elements1);\n bfs(root2, elements2);\n\n vector<int>result;\n result.insert(result.begin(),elements1.begin(),elements1.end());\n result.insert(result.begin(),elements2.begin(),elements2.end());\n\n sort(result.begin(),result.end());\n\n return result;\n\n }\n};", "memory": "85600" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvoid inorder_traversal(TreeNode* root,vector<int>&res){\n if(root==NULL){\n return;\n }\n stack<TreeNode*>s;\n TreeNode *curr=root;\n while(curr!=NULL||s.empty()==false){\n while(curr!=NULL){\n s.push(curr);\n curr=curr->left;\n }\n res.push_back(s.top()->val);\n curr=s.top()->right;\n s.pop();\n }\n}\n\nvector<int>sorted_merge_two_arrays(vector<int>&a,vector<int>&b){\n if(a.size()==0){\n return b;\n }\n if(b.size()==0){\n return a;\n }\n vector<int>res;\n int i=0,j=0;\n while(i<a.size()&&j<b.size()){\n if(a[i]<b[j]){\n res.push_back(a[i]);\n i++;\n }\n else{\n res.push_back(b[j]);\n j++;\n }\n }\n while(i<a.size()){\n res.push_back(a[i]);\n i++;\n }\n while(j<b.size()){\n res.push_back(b[j]);\n j++;\n }\n return res;\n\n}\n\nvector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int>r1,r2;\n inorder_traversal(root1,r1);\n inorder_traversal(root2,r2);\n return sorted_merge_two_arrays(r1,r2);\n\n}\n};", "memory": "85600" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvoid inorder(TreeNode* root,vector<int>& arr) {\n if(!root) return;\n inorder(root->left,arr);\n arr.push_back(root->val);\n inorder(root->right,arr);\n}\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> arr1,arr2;\n inorder(root1,arr1);\n inorder(root2,arr2);\n vector<int> ans=arr1;\n for(auto it:arr2) ans.push_back(it);\n sort(ans.begin(),ans.end());\n return ans;\n }\n};", "memory": "85700" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void dfs_inorder_1(TreeNode* root1, vector<int> &ans1){\n if(!root1) return;\n dfs_inorder_1(root1->left, ans1);\n ans1.push_back(root1->val);\n cout<< \" \"<< root1->val;\n dfs_inorder_2(root1->right, ans1);\n }\n\n void dfs_inorder_2(TreeNode* root2, vector<int> &ans2){\n if(!root2) return;\n dfs_inorder_1(root2->left, ans2);\n ans2.push_back(root2->val);\n cout<< \" \"<< root2->val;\n dfs_inorder_2(root2->right, ans2);\n }\n\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n vector<int> ans1, ans2, ans3 ;\n // vector<int> ans2 ;\n // vector<int> ans3 ;\n dfs_inorder_1(root1, ans1);cout<<endl;\n dfs_inorder_2(root2, ans2);\n int i1= 0, i2= 0;\n\n while(i1 < ans1.size() || i2 < ans2.size()){\n if(i1 == ans1.size()) {ans3.push_back(ans2[i2]);\n i2++;}\n else if(i2 == ans2.size()) {ans3.push_back(ans1[i1]);\n i1++;} \n else if(ans1[i1]<ans2[i2]){\n ans3.push_back(ans1[i1]);\n i1++;\n }\n else{\n ans3.push_back(ans2[i2]);\n i2++;\n }\n } \n return ans3;\n }\n};", "memory": "85700" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void preorder(TreeNode* root1,vector<int>&vec1)\n {\n if(root1==nullptr)\n {\n return;\n }\n vec1.push_back(root1->val);\n preorder(root1->left,vec1);\n preorder(root1->right,vec1);\n \n }\n void preorder1(TreeNode* root2,vector<int>&vec2)\n {\n if(root2==nullptr)\n {\n return;\n }\n vec2.push_back(root2->val);\n preorder1(root2->left,vec2);\n preorder1(root2->right,vec2);\n \n }\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int>vec1,vec2;\n preorder(root1,vec1);\n preorder1(root2,vec2);\n vector<int>vec(vec1.begin(),vec1.end());\n for(int i=0;i<vec2.size();i++)\n {\n vec.push_back(vec2[i]);\n }\n sort(vec.begin(),vec.end());\n return vec;\n \n\n\n \n }\n};", "memory": "85800" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvoid add(TreeNode* root, vector<int>& temp)\n{\n if (root == nullptr)\n return;\n add(root->left, temp);\n temp.push_back(root->val);\n add(root->right, temp);\n\n\n}\nvector<int>merge(vector<int>&v1, vector<int>v2)\n{\n int i = 0;\n int j = 0;\n vector<int>ans;\n while (i < v1.size() && j < v2.size())\n if (v1[i] < v2[j])\n ans.push_back(v1[i++]);\n else\n ans.push_back(v2[j++]);\n while (i < v1.size())\n ans.push_back(v1[i++]);\n \n while (j < v2.size())\n ans.push_back(v2[j++]);\n \n return ans;\n}\nvector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int>v1, v2;\n add(root1, v1);\n add(root2, v2);\n return merge(v1, v2);\n\n\n}\n};", "memory": "85900" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n #pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\nauto __untie_cin = cin.tie(nullptr);\nauto __unsync_ios_stdio = ios_base::sync_with_stdio(false);\nclass Solution {\npublic:\nvoid add(TreeNode* root, vector<int>& temp)\n{\n if (root == nullptr)\n return;\n add(root->left, temp);\n temp.push_back(root->val);\n add(root->right, temp);\n\n\n}\nvector<int>merge(vector<int>&v1, vector<int>v2)\n{\n int i = 0;\n int j = 0;\n vector<int>ans;\n while (i < v1.size() && j < v2.size())\n if (v1[i] < v2[j])\n ans.push_back(v1[i++]);\n else\n ans.push_back(v2[j++]);\n while (i < v1.size())\n ans.push_back(v1[i++]);\n \n while (j < v2.size())\n ans.push_back(v2[j++]);\n \n return ans;\n}\nvector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int>v1, v2;\n add(root1, v1);\n add(root2, v2);\n return merge(v1, v2);\n\n\n}\n};", "memory": "86000" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n \n vector<int> ans;\n queue<TreeNode*> q;\n if (root1) q.push(root1);\n if (root2) q.push(root2);\n\n while (!q.empty()) {\n TreeNode* now = q.front(); q.pop();\n\n ans.emplace_back(now -> val);\n\n if (now -> left) q.push(now -> left);\n if (now -> right) q.push(now -> right);\n }\n\n sort(ans.begin(), ans.end());\n\n return ans;\n\n }\n};", "memory": "86100" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> ans ; \n \n auto levelOrder = [&](TreeNode* root ) {\n if(root == NULL) return ;\n queue<TreeNode*> nodes ; \n nodes.push(root) ;\n while(!nodes.empty()){\n TreeNode* currentNode = nodes.front() ;\n nodes.pop() ;\n ans.push_back(currentNode->val) ;\n if(currentNode->left != NULL) nodes.push(currentNode->left);\n if(currentNode->right != NULL) nodes.push(currentNode->right);\n }\n } ;\n levelOrder(root1) ;\n levelOrder(root2) ;\n sort(ans.begin() , ans.end());\n return ans; \n }\n};", "memory": "86200" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode * root, vector<int> &in){\n if(root==NULL) return;\n inorder(root->left, in);\n in.push_back(root->val);\n inorder(root->right, in);\n }\n vector<int> mergeSort(vector<int> arr1, vector<int> arr2){\n vector<int> ans;\n int n = arr1.size();\n int m = arr2.size();\n int i=0; int j=0;\n while(i<n && j<m){\n if(arr1[i]<arr2[j]){\n ans.push_back(arr1[i]);\n i++;\n } \n else{\n ans.push_back(arr2[j]);\n j++;\n }\n }\n while(i<n){\n ans.push_back(arr1[i]);\n i++;\n }\n while(j<m){\n ans.push_back(arr2[j]);\n j++;\n }\n return ans;\n }\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> bst1, bst2;\n inorder(root1, bst1);\n inorder(root2, bst2);\n vector<int> bst3 = mergeSort(bst1, bst2);\n return bst3;\n }\n};", "memory": "86300" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void inin(TreeNode* root, vector<int>&v) {\n if(root == NULL) {\n return ;\n }\n inin(root->left, v);\n v.push_back(root->val);\n inin(root->right, v);\n }\n\n void inorder(TreeNode* root1, TreeNode* root2, vector<int>&v) {\n if(root1 == NULL) {\n // inin(root2, v);\n return ;\n }\n if(root2 == NULL) {\n return ;\n }\n if(root1->val < root2->val) {\n inorder(root1->left, root2, v);\n v.push_back(root1->val) ;\n inorder(root1->right, root2, v);\n } else {\n inorder(root1, root2->left, v);\n v.push_back(root2->val);\n inorder(root1, root2->right, v);\n }\n }\n\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int>v1, v2, v;\n inin(root1, v1);\n inin(root2, v2);\n for(int i=0; i<v1.size(); i++) {\n v.push_back(v1[i]);\n }\n for(int i=0; i<v2.size(); i++) {\n v.push_back(v2[i]);\n }\n sort(v.begin(), v.end());\n // inorder(root1, root2, v);\n return v;\n \n }\n};", "memory": "86400" }
1,427
<p>Given two binary search trees <code>root1</code> and <code>root2</code>, return <em>a list containing all the integers from both trees sorted in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> <pre> <strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] <strong>Output:</strong> [0,1,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> <pre> <strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] <strong>Output:</strong> [1,1,8,8] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in each tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n priority_queue<int,vector<int>,greater<int>> p;\nvoid bst(TreeNode* root){\n if(root==NULL)\n return;\n p.push(root->val);\n bst(root->left);\n bst(root->right);\n}\n vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {\n vector<int> v;\n bst(root1);\n bst(root2);\n while(!p.empty()){\n v.push_back(p.top());\n p.pop(); \n }\n return v;\n }\n};", "memory": "86500" }
1,428
<p>Given an array of non-negative integers <code>arr</code>, you are initially positioned at <code>start</code>&nbsp;index of the array. When you are at index <code>i</code>, you can jump&nbsp;to <code>i + arr[i]</code> or <code>i - arr[i]</code>, check if you can reach&nbsp;<strong>any</strong> index with value 0.</p> <p>Notice that you can not jump outside of the array at any time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 5 <strong>Output:</strong> true <strong>Explanation:</strong> All possible ways to reach at index 3 with value 0 are: index 5 -&gt; index 4 -&gt; index 1 -&gt; index 3 index 5 -&gt; index 6 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 0 <strong>Output:</strong> true <strong>Explanation: </strong>One possible way to reach at index 3 with value 0 is: index 0 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [3,0,2,1,2], start = 2 <strong>Output:</strong> false <strong>Explanation: </strong>There is no way to reach at index 1 with value 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= arr[i] &lt;&nbsp;arr.length</code></li> <li><code>0 &lt;= start &lt; arr.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool check(vector<int>& arr, int s, int ind) {\n if (ind < 0 || ind >= arr.size() || arr[ind] == -1) return false;\n if (arr[ind] == 0) return true;\n\n int jump = arr[ind];\n arr[ind] = -1; \n\n \n bool canReachLeft = check(arr, s, ind - jump);\n bool canReachRight = check(arr, s, ind + jump);\n\n \n return canReachLeft || canReachRight;\n }\n\n bool canReach(vector<int>& arr, int s) {\n return check(arr, s, s);\n }\n};\n", "memory": "33330" }
1,428
<p>Given an array of non-negative integers <code>arr</code>, you are initially positioned at <code>start</code>&nbsp;index of the array. When you are at index <code>i</code>, you can jump&nbsp;to <code>i + arr[i]</code> or <code>i - arr[i]</code>, check if you can reach&nbsp;<strong>any</strong> index with value 0.</p> <p>Notice that you can not jump outside of the array at any time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 5 <strong>Output:</strong> true <strong>Explanation:</strong> All possible ways to reach at index 3 with value 0 are: index 5 -&gt; index 4 -&gt; index 1 -&gt; index 3 index 5 -&gt; index 6 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 0 <strong>Output:</strong> true <strong>Explanation: </strong>One possible way to reach at index 3 with value 0 is: index 0 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [3,0,2,1,2], start = 2 <strong>Output:</strong> false <strong>Explanation: </strong>There is no way to reach at index 1 with value 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= arr[i] &lt;&nbsp;arr.length</code></li> <li><code>0 &lt;= start &lt; arr.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n if (start >= 0 && start < arr.size() && arr[start] >= 0) {\n if (arr[start] == 0) return true;\n arr[start] = -arr[start];\n return canReach(arr, start + arr[start]) || \n canReach(arr, start - arr[start]);\n }\n return false;\n }\n};", "memory": "33330" }
1,428
<p>Given an array of non-negative integers <code>arr</code>, you are initially positioned at <code>start</code>&nbsp;index of the array. When you are at index <code>i</code>, you can jump&nbsp;to <code>i + arr[i]</code> or <code>i - arr[i]</code>, check if you can reach&nbsp;<strong>any</strong> index with value 0.</p> <p>Notice that you can not jump outside of the array at any time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 5 <strong>Output:</strong> true <strong>Explanation:</strong> All possible ways to reach at index 3 with value 0 are: index 5 -&gt; index 4 -&gt; index 1 -&gt; index 3 index 5 -&gt; index 6 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 0 <strong>Output:</strong> true <strong>Explanation: </strong>One possible way to reach at index 3 with value 0 is: index 0 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [3,0,2,1,2], start = 2 <strong>Output:</strong> false <strong>Explanation: </strong>There is no way to reach at index 1 with value 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= arr[i] &lt;&nbsp;arr.length</code></li> <li><code>0 &lt;= start &lt; arr.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n\n bool canReach(vector<int>& arr, int start) {\n //Recursive Approach\n // if(start<0 || start>=arr.size()){\n // return false;\n // }\n // if(arr[start]==0){\n // return true;\n // }\n // return canReach(arr,start+arr[start]) || canReach(arr,start-arr[start]);\n\n\n //same approach in optimized way \n // without any repeatition of tasks\n if(start==0 && arr[arr.size()-1]==0){\n return true;\n }\n\n queue<int> q;\n q.push(start);\n while(!q.empty()){\n int cur=q.front();\n q.pop();\n if(arr[cur]==0){\n return true;\n }\n if(cur-arr[cur]>=0){\n q.push(cur-arr[cur]);\n }\n if(cur+arr[cur]<arr.size()){\n q.push(cur+arr[cur]);\n }\n arr[cur]=INT16_MAX; // Mark that node as visited\n }\n return false;\n\n }\n};", "memory": "33590" }
1,428
<p>Given an array of non-negative integers <code>arr</code>, you are initially positioned at <code>start</code>&nbsp;index of the array. When you are at index <code>i</code>, you can jump&nbsp;to <code>i + arr[i]</code> or <code>i - arr[i]</code>, check if you can reach&nbsp;<strong>any</strong> index with value 0.</p> <p>Notice that you can not jump outside of the array at any time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 5 <strong>Output:</strong> true <strong>Explanation:</strong> All possible ways to reach at index 3 with value 0 are: index 5 -&gt; index 4 -&gt; index 1 -&gt; index 3 index 5 -&gt; index 6 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 0 <strong>Output:</strong> true <strong>Explanation: </strong>One possible way to reach at index 3 with value 0 is: index 0 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [3,0,2,1,2], start = 2 <strong>Output:</strong> false <strong>Explanation: </strong>There is no way to reach at index 1 with value 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= arr[i] &lt;&nbsp;arr.length</code></li> <li><code>0 &lt;= start &lt; arr.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n // int i=lower_bound(arr.begin(),arr.end(),0) -;\n //cout<<i<<endl;\n stack<int>st;\n int n=arr.size();\n vector<bool>vis(n,0);\n\n st.push(start);\n\n while(!st.empty())\n {\n int t=st.top();\n st.pop();\n\n if(arr[t]==0)\n return true;\n\n if(vis[t]!=1)\n {\n vis[t]=1;\n if(arr[t]+t<n)\n {\n st.push(arr[t]+t);\n }\n if(t-arr[t]>=0) \n { \n st.push(t-arr[t]);\n }\n }\n }\n\n return false;\n }\n};", "memory": "33590" }
1,428
<p>Given an array of non-negative integers <code>arr</code>, you are initially positioned at <code>start</code>&nbsp;index of the array. When you are at index <code>i</code>, you can jump&nbsp;to <code>i + arr[i]</code> or <code>i - arr[i]</code>, check if you can reach&nbsp;<strong>any</strong> index with value 0.</p> <p>Notice that you can not jump outside of the array at any time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 5 <strong>Output:</strong> true <strong>Explanation:</strong> All possible ways to reach at index 3 with value 0 are: index 5 -&gt; index 4 -&gt; index 1 -&gt; index 3 index 5 -&gt; index 6 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 0 <strong>Output:</strong> true <strong>Explanation: </strong>One possible way to reach at index 3 with value 0 is: index 0 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [3,0,2,1,2], start = 2 <strong>Output:</strong> false <strong>Explanation: </strong>There is no way to reach at index 1 with value 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= arr[i] &lt;&nbsp;arr.length</code></li> <li><code>0 &lt;= start &lt; arr.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n vector<bool> vis(n,false);\n vis[start] = 1;\n queue<int> q;\n q.push(start);\n while(!q.empty()){\n int curr = q.front();\n q.pop();\n if(arr[curr]==0){\n return true;\n }\n if(curr-arr[curr]>=0 && !vis[curr-arr[curr]]){\n vis[curr-arr[curr]] = 1;\n q.push(curr-arr[curr]);\n }\n if(curr+arr[curr]<n && !vis[curr+arr[curr]]){\n vis[curr+arr[curr]] = 1;\n q.push(curr+arr[curr]);\n }\n }\n return false;\n }\n};", "memory": "33850" }
1,428
<p>Given an array of non-negative integers <code>arr</code>, you are initially positioned at <code>start</code>&nbsp;index of the array. When you are at index <code>i</code>, you can jump&nbsp;to <code>i + arr[i]</code> or <code>i - arr[i]</code>, check if you can reach&nbsp;<strong>any</strong> index with value 0.</p> <p>Notice that you can not jump outside of the array at any time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 5 <strong>Output:</strong> true <strong>Explanation:</strong> All possible ways to reach at index 3 with value 0 are: index 5 -&gt; index 4 -&gt; index 1 -&gt; index 3 index 5 -&gt; index 6 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 0 <strong>Output:</strong> true <strong>Explanation: </strong>One possible way to reach at index 3 with value 0 is: index 0 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [3,0,2,1,2], start = 2 <strong>Output:</strong> false <strong>Explanation: </strong>There is no way to reach at index 1 with value 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= arr[i] &lt;&nbsp;arr.length</code></li> <li><code>0 &lt;= start &lt; arr.length</code></li> </ul>
0
{ "code": "static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\nclass Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n vector<bool> vis(n,false);\n vis[start] = 1;\n queue<int> q;\n q.push(start);\n while(!q.empty()){\n int curr = q.front();\n q.pop();\n if(arr[curr]==0){\n return true;\n }\n if(curr-arr[curr]>=0 && !vis[curr-arr[curr]]){\n vis[curr-arr[curr]] = 1;\n q.push(curr-arr[curr]);\n }\n if(curr+arr[curr]<n && !vis[curr+arr[curr]]){\n vis[curr+arr[curr]] = 1;\n q.push(curr+arr[curr]);\n }\n }\n return false;\n }\n};", "memory": "33850" }
1,428
<p>Given an array of non-negative integers <code>arr</code>, you are initially positioned at <code>start</code>&nbsp;index of the array. When you are at index <code>i</code>, you can jump&nbsp;to <code>i + arr[i]</code> or <code>i - arr[i]</code>, check if you can reach&nbsp;<strong>any</strong> index with value 0.</p> <p>Notice that you can not jump outside of the array at any time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 5 <strong>Output:</strong> true <strong>Explanation:</strong> All possible ways to reach at index 3 with value 0 are: index 5 -&gt; index 4 -&gt; index 1 -&gt; index 3 index 5 -&gt; index 6 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 0 <strong>Output:</strong> true <strong>Explanation: </strong>One possible way to reach at index 3 with value 0 is: index 0 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [3,0,2,1,2], start = 2 <strong>Output:</strong> false <strong>Explanation: </strong>There is no way to reach at index 1 with value 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= arr[i] &lt;&nbsp;arr.length</code></li> <li><code>0 &lt;= start &lt; arr.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n queue<int> q {};\n q.push(start);\n while(!q.empty()) {\n int top = q.front();\n q.pop();\n if(arr[top] == 0)\n return true;\n \n if(top + arr[top] < arr.size() && arr[top] > 0) {\n q.push(top + arr[top]);\n }\n if(top - arr[top] >= 0 && arr[top] > 0) {\n q.push(top - arr[top]);\n }\n arr[top] = -1;\n }\n return false;\n }\n};", "memory": "34110" }
1,428
<p>Given an array of non-negative integers <code>arr</code>, you are initially positioned at <code>start</code>&nbsp;index of the array. When you are at index <code>i</code>, you can jump&nbsp;to <code>i + arr[i]</code> or <code>i - arr[i]</code>, check if you can reach&nbsp;<strong>any</strong> index with value 0.</p> <p>Notice that you can not jump outside of the array at any time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 5 <strong>Output:</strong> true <strong>Explanation:</strong> All possible ways to reach at index 3 with value 0 are: index 5 -&gt; index 4 -&gt; index 1 -&gt; index 3 index 5 -&gt; index 6 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 0 <strong>Output:</strong> true <strong>Explanation: </strong>One possible way to reach at index 3 with value 0 is: index 0 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [3,0,2,1,2], start = 2 <strong>Output:</strong> false <strong>Explanation: </strong>There is no way to reach at index 1 with value 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= arr[i] &lt;&nbsp;arr.length</code></li> <li><code>0 &lt;= start &lt; arr.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n queue<int>q;\n q.push(start);\n\n while(!q.empty())\n {\n int curr = q.front();\n q.pop();\n\n if(arr[curr] == 0)\n return true;\n \n if(arr[curr] < 0)\n continue;\n \n if(curr + arr[curr] < arr.size())\n q.push(curr + arr[curr]);\n\n \n if(curr - arr[curr] >= 0)\n q.push(curr - arr[curr]);\n\n arr[curr] *= -1;\n }\n\n\n\n return false;\n }\n};", "memory": "34110" }
1,428
<p>Given an array of non-negative integers <code>arr</code>, you are initially positioned at <code>start</code>&nbsp;index of the array. When you are at index <code>i</code>, you can jump&nbsp;to <code>i + arr[i]</code> or <code>i - arr[i]</code>, check if you can reach&nbsp;<strong>any</strong> index with value 0.</p> <p>Notice that you can not jump outside of the array at any time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 5 <strong>Output:</strong> true <strong>Explanation:</strong> All possible ways to reach at index 3 with value 0 are: index 5 -&gt; index 4 -&gt; index 1 -&gt; index 3 index 5 -&gt; index 6 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 0 <strong>Output:</strong> true <strong>Explanation: </strong>One possible way to reach at index 3 with value 0 is: index 0 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [3,0,2,1,2], start = 2 <strong>Output:</strong> false <strong>Explanation: </strong>There is no way to reach at index 1 with value 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= arr[i] &lt;&nbsp;arr.length</code></li> <li><code>0 &lt;= start &lt; arr.length</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canReach(const std::vector<int>& nums, int start) {\n std::queue<int> q;\n std::vector<bool> visited(nums.size(), false);\n q.push(start);\n\n while (!q.empty()) {\n const auto i = q.front();\n q.pop();\n if (visited[i]) {\n continue;\n }\n visited[i] = true;\n if (!nums[i]) {\n return true;\n }\n if (i - nums[i] >= 0) {\n q.push(i - nums[i]);\n }\n if (i + nums[i] < nums.size()) {\n q.push(i + nums[i]);\n }\n }\n return false;\n }\n};", "memory": "34370" }
1,428
<p>Given an array of non-negative integers <code>arr</code>, you are initially positioned at <code>start</code>&nbsp;index of the array. When you are at index <code>i</code>, you can jump&nbsp;to <code>i + arr[i]</code> or <code>i - arr[i]</code>, check if you can reach&nbsp;<strong>any</strong> index with value 0.</p> <p>Notice that you can not jump outside of the array at any time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 5 <strong>Output:</strong> true <strong>Explanation:</strong> All possible ways to reach at index 3 with value 0 are: index 5 -&gt; index 4 -&gt; index 1 -&gt; index 3 index 5 -&gt; index 6 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 0 <strong>Output:</strong> true <strong>Explanation: </strong>One possible way to reach at index 3 with value 0 is: index 0 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [3,0,2,1,2], start = 2 <strong>Output:</strong> false <strong>Explanation: </strong>There is no way to reach at index 1 with value 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= arr[i] &lt;&nbsp;arr.length</code></li> <li><code>0 &lt;= start &lt; arr.length</code></li> </ul>
1
{ "code": "static bool can_reach(vector<int>& arr, vector<int>& visited, int index) {\n if (index < 0 || index >= arr.size() || visited[index]) return false;\n if (arr[index] == 0) return true;\n visited[index] = 1;\n return can_reach(arr, visited, index - arr[index]) || can_reach(arr, visited, index + arr[index]);\n}\nclass Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n auto visited = vector<int>(arr.size(), 0);\n //if (start - arr[start] >= 0 && arr[start - arr[start]] == 0) return true;\n return can_reach(arr, visited, start);\n }\n};", "memory": "34370" }
1,428
<p>Given an array of non-negative integers <code>arr</code>, you are initially positioned at <code>start</code>&nbsp;index of the array. When you are at index <code>i</code>, you can jump&nbsp;to <code>i + arr[i]</code> or <code>i - arr[i]</code>, check if you can reach&nbsp;<strong>any</strong> index with value 0.</p> <p>Notice that you can not jump outside of the array at any time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 5 <strong>Output:</strong> true <strong>Explanation:</strong> All possible ways to reach at index 3 with value 0 are: index 5 -&gt; index 4 -&gt; index 1 -&gt; index 3 index 5 -&gt; index 6 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 0 <strong>Output:</strong> true <strong>Explanation: </strong>One possible way to reach at index 3 with value 0 is: index 0 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [3,0,2,1,2], start = 2 <strong>Output:</strong> false <strong>Explanation: </strong>There is no way to reach at index 1 with value 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= arr[i] &lt;&nbsp;arr.length</code></li> <li><code>0 &lt;= start &lt; arr.length</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& nums, int start) {\n int n=nums.size();\n vector<int> visited(n,0);\n stack<int> st;\n st.push(start);\n while(!st.empty()){\n int node = st.top();\n st.pop();\n if(nums[node]==0) return true;\n if(!visited[node]){\n visited[node]=1;\n if(nums[node]+node<n)\n st.push(nums[node]+node);\n if(node-nums[node]>=0)\n st.push(node-nums[node]);\n }\n }\n return false;\n }\n};", "memory": "34630" }
1,428
<p>Given an array of non-negative integers <code>arr</code>, you are initially positioned at <code>start</code>&nbsp;index of the array. When you are at index <code>i</code>, you can jump&nbsp;to <code>i + arr[i]</code> or <code>i - arr[i]</code>, check if you can reach&nbsp;<strong>any</strong> index with value 0.</p> <p>Notice that you can not jump outside of the array at any time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 5 <strong>Output:</strong> true <strong>Explanation:</strong> All possible ways to reach at index 3 with value 0 are: index 5 -&gt; index 4 -&gt; index 1 -&gt; index 3 index 5 -&gt; index 6 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 0 <strong>Output:</strong> true <strong>Explanation: </strong>One possible way to reach at index 3 with value 0 is: index 0 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [3,0,2,1,2], start = 2 <strong>Output:</strong> false <strong>Explanation: </strong>There is no way to reach at index 1 with value 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= arr[i] &lt;&nbsp;arr.length</code></li> <li><code>0 &lt;= start &lt; arr.length</code></li> </ul>
1
{ "code": "class Solution {\nprivate:\n bool f(vector<int>&arr,vector<int>&dp,int ind)\n {\n if(ind<0 || ind>=arr.size()||dp[ind]==1)return false;\n if(arr[ind]==0)return true;\n dp[ind]=1;\n return f(arr,dp,ind-arr[ind]) ||f( arr,dp,ind+arr[ind]);\n }\npublic:\n bool canReach(vector<int>& arr, int start) {\n vector<int>dp(arr.size(),0);\n return f(arr,dp,start);\n }\n};", "memory": "34630" }
1,428
<p>Given an array of non-negative integers <code>arr</code>, you are initially positioned at <code>start</code>&nbsp;index of the array. When you are at index <code>i</code>, you can jump&nbsp;to <code>i + arr[i]</code> or <code>i - arr[i]</code>, check if you can reach&nbsp;<strong>any</strong> index with value 0.</p> <p>Notice that you can not jump outside of the array at any time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 5 <strong>Output:</strong> true <strong>Explanation:</strong> All possible ways to reach at index 3 with value 0 are: index 5 -&gt; index 4 -&gt; index 1 -&gt; index 3 index 5 -&gt; index 6 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 0 <strong>Output:</strong> true <strong>Explanation: </strong>One possible way to reach at index 3 with value 0 is: index 0 -&gt; index 4 -&gt; index 1 -&gt; index 3 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [3,0,2,1,2], start = 2 <strong>Output:</strong> false <strong>Explanation: </strong>There is no way to reach at index 1 with value 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= arr[i] &lt;&nbsp;arr.length</code></li> <li><code>0 &lt;= start &lt; arr.length</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& a, int s) {\n int n=a.size(),x;\n vector<int> v(n);\n queue<int> q;\n q.push(s);\n // v[s]=1;\n while(q.size())\n {\n x=q.front();\n q.pop();\n v[x]=1;\n if(!a[x]) return 1;\n if(x+a[x]<n && !v[x+a[x]]) q.push(x+a[x]);\n if(x-a[x]>=0 && !v[x-a[x]]) q.push(x-a[x]);\n }\n return 0;\n }\n};", "memory": "34890" }