id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int, int> sumCount;\n \n for(int a : nums1){\n for(int b : nums2){\n sumCount[a+b]++;\n }\n }\n \n int result = 0;\n \n for(int c : nums3){\n for(int d : nums4){\n int target = -(c+d);\n if(sumCount.find(target) != sumCount.end()){\n result += sumCount[target];\n }\n }\n }\n \n return result;\n }\n};", "memory": "28300" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int,int>sumcount;\n int count =0;\n for(int a: nums1){\n for(int b: nums2){\n ++sumcount[a+b];\n }\n }\n\n for(int c:nums3){\n for(int d: nums4){\n int target = -(c+d);\n if(sumcount.find(target)!=sumcount.end()){\n count+= sumcount[target];\n }\n }\n }\n return count;\n \n }\n};", "memory": "28300" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n int res = 0;\n\n unordered_map <int, int> mp;\n\n for (int i=0; i<nums1.size(); i++)\n {\n for (int j=0; j<nums2.size(); j++)\n {\n // add all possible pair of sums from nums1, nums2 to mp\n int sum = nums1[i] + nums2[j];\n mp[sum]++;\n }\n }\n\n for (int i=0; i<nums3.size(); i++)\n {\n for (int j=0; j<nums4.size(); j++)\n {\n int sum = nums3[i] + nums4[j];\n\n // search if -sum is already available in the map\n if (mp.find(-sum) != mp.end())\n {\n res += mp[-sum];\n }\n }\n }\n\n return res;\n }\n};", "memory": "28400" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int, int> sumCount;\n\n for(int a : nums1){\n for(int b : nums2){\n sumCount[a+b]++;\n }\n }\n\n int count = 0;\n\n for(int c : nums3){\n for(int d : nums4){\n int target = -(c+d);\n if(sumCount.find(target) != sumCount.end()){\n count += sumCount[target];\n }\n }\n }\n return count;\n }\n};", "memory": "28500" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int, int> hash;\n int cnt = 0;\n\n // Calculate all possible sums of nums1[i] + nums2[j] and store in the hash map\n for (int i = 0; i < nums1.size(); i++)\n for (int j = 0; j < nums2.size(); j++) hash[nums1[i] + nums2[j]]++;\n\n // Check for all possible sums of nums3[k] + nums4[l] and see if their negative exists in the hash map\n for (int k = 0; k < nums3.size(); k++) {\n for (int l = 0; l < nums4.size(); l++) {\n int target = -(nums3[k] + nums4[l]);\n if (hash.find(target) != hash.end()) cnt += hash[target];\n }\n }\n\n return cnt;\n }\n};", "memory": "28500" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
1
{ "code": "class Solution {\n public:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3,\n vector<int>& nums4) {\n int ans = 0;\n unordered_map<int, int> count;\n\n for (const int a : nums1)\n for (const int b : nums2)\n ++count[a + b];\n\n for (const int c : nums3)\n for (const int d : nums4)\n if (const auto it = count.find(-c - d); it != count.cend())\n ans += it->second;\n\n return ans;\n }\n};", "memory": "28600" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n // n1 + n2 = -(n3 + n4)\n int res = 0;\n\n unordered_map <int, int> mp;\n\n for (int i=0; i<nums1.size(); i++)\n {\n for (int j=0; j<nums2.size(); j++)\n {\n // add all possible pair of sums from nums1, nums2 to mp\n int sum = nums1[i] + nums2[j];\n mp[sum]++;\n }\n }\n\n for (int i=0; i<nums3.size(); i++)\n {\n for (int j=0; j<nums4.size(); j++)\n {\n int sum = nums3[i] + nums4[j];\n\n // search if -sum is already available in the map\n if (mp.find(-sum) != mp.end())\n {\n res += mp[-sum];\n }\n }\n }\n\n return res;\n }\n};", "memory": "28600" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int, int> firstTwoSum;\n for(int i : nums1)\n {\n for(int j : nums2)\n {\n firstTwoSum[i+j]++;\n }\n }\n int ans = 0, thirdFourthSum;\n for(int i : nums3)\n {\n for(int j : nums4)\n {\n thirdFourthSum = i + j;\n ans += firstTwoSum[-thirdFourthSum];\n }\n }\n return ans;\n }\n};", "memory": "28700" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {\n map<int, int> mp;\n \n for(int i:A) {\n for(int j:B) {\n mp[i+j]++;\n }\n }\n \n int count = 0;\n for(int k:C) {\n for(int l:D) {\n int target = -(k+l);\n count += mp[target];\n }\n }\n return count;\n }\n};\n", "memory": "28800" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& n1, vector<int>& n2, vector<int>& n3, vector<int>& n4) {\n map<int,int>mp;\n for(int k : n1){\n for(int l : n2){\n mp[k+l]++;\n }\n }\n int cnt = 0;\n for(int m : n3){\n for(int n : n4){\n cnt+= mp[-(m+n)];\n }\n }\n return cnt;\n }\n};", "memory": "28900" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n // int cnt = 0;\n\n // for(auto i: nums1)\n // {\n // for(auto j: nums2)\n // {\n // for(auto k: nums3)\n // {\n // for(auto l: nums4)\n // {\n // if(i + j + k + l == 0)\n // {\n // cnt++;\n // }\n // }\n // }\n // }\n // }\n\n // return cnt;\n // }\n\n // TIME = O(N^4)\n // SPACE = O(1)\n\n\n // better approach\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n int cnt = 0;\n\n map<int,int> mp;\n\n for(auto k: nums3)\n {\n for(auto l: nums4)\n {\n mp[k+l]++;\n }\n }\n\n for(auto i: nums1)\n {\n for(auto j: nums2)\n {\n cnt += mp[-(i+j)];\n }\n }\n\n return cnt;\n }\n};", "memory": "28900" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n int n = nums1.size();\n\n map<long, int>cnt;\n\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n long long x = nums1[i];\n long long y = nums2[j];\n\n cnt[x + y]++;\n }\n }\n int ans = 0;\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n long long x = nums3[i];\n long long y = nums4[j];\n\n long long res = x + y;\n\n ans += cnt[-res];\n }\n }\n\n return ans;\n }\n};", "memory": "29000" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n map<int,int> mp;\n for(int k : nums3)\n for(int l : nums4)\n mp[k + l]++;\n int count = 0;\n for(int i : nums1)\n for(int j : nums2)\n count += mp[-(i + j)];\n return count;\n }\n};", "memory": "29100" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n int n = nums1.size();\n int count = 0;\n unordered_map<long long,int> mp;\n\n // find 2 pairs - as arrays are different ans it becomes easier \n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n long long sum = (long long)nums1[i]+nums2[j];\n mp[sum]++;\n }\n }\n\n for(int k=0;k<n;k++){\n for(int l=0;l<n;l++){\n long long sum = (long long)nums3[k] + nums4[l];\n if(mp.find(-sum) != mp.end()){\n count += mp[-sum];\n }\n }\n }\n\n return count;\n }\n};", "memory": "29200" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4){\n unordered_map<long long, int> sumCount;\n int n = nums1.size();\n int count = 0;\n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n long long sum = (long long)nums3[i] + nums4[j];\n sumCount[sum]++;\n }\n }\n \n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n long long target = -((long long)nums1[i] + nums2[j]);\n if(sumCount.find(target) != sumCount.end()){\n count += sumCount[target];\n }\n }\n }\n return count;\n }\n};\n\n\n\n\n\n\n", "memory": "29300" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n int n = nums1.size();\n\n unordered_map<long, int>cnt;\n\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n long long x = nums1[i];\n long long y = nums2[j];\n\n cnt[x + y]++;\n }\n }\n int ans = 0;\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n long long x = nums3[i];\n long long y = nums4[j];\n\n long long res = x + y;\n\n ans += cnt[-res];\n }\n }\n\n return ans;\n }\n};", "memory": "29400" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n int n = nums1.size();\n unordered_map<long int, int> mp;\n \n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n mp[nums1[i] + nums2[j]]++;\n } \n }\n \n int ans = 0;\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n ans += mp[-1*(nums3[i] + nums4[j])];\n } \n }\n return ans;\n \n \n }\n};", "memory": "29500" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "#include <unordered_map>\n#include <vector>\n\nclass Solution {\n public:\n int fourSumCount(\n std::vector<int>& nums1, std::vector<int>& nums2,\n std::vector<int>& nums3, std::vector<int>& nums4) {\n std::unordered_map<long long, int> countsOfTwoPairs{};\n\n std::unordered_map<int, int> countsOfThreePairs{};\n for (const auto num : nums4) {\n const auto it = countsOfThreePairs.find(-num);\n if (it != countsOfThreePairs.end()) {\n ++it->second;\n } else {\n countsOfThreePairs.emplace(-num, 1);\n }\n }\n\n int count{0};\n for (const auto i : nums1) {\n for (const auto j : nums2) {\n const auto twoPairsSum = static_cast<long long>(i) + j;\n auto it = countsOfTwoPairs.find(twoPairsSum);\n if (it == countsOfTwoPairs.end()) {\n int count{0};\n for (const auto k : nums3) {\n const auto threePairsSum = twoPairsSum + k;\n const auto it = countsOfThreePairs.find(threePairsSum);\n if (it != countsOfThreePairs.end()) count += it->second;\n }\n it = countsOfTwoPairs.emplace(twoPairsSum, count).first;\n }\n count += it->second;\n }\n }\n\n return count;\n }\n};\n", "memory": "30200" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void fillMap(vector<int>& A, vector<int>& B, unordered_map<int,int> &m)\n {\n int n = A.size();\n for(int i = 0; i < n; ++i)\n for(int j = 0; j < n; ++j)\n ++m[A[i] + B[j]];\n \n }\n int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {\n unordered_map<int,int> m1, m2;\n fillMap(A, B, m1);\n fillMap(C, D, m2);\n int res = 0;\n for(auto it = m1.begin(); it != m1.end(); ++it)\n {\n auto it2 = m2.find(-it->first);\n if(it2 != m2.end())\n res += it->second*it2->second;\n }\n return res;\n }\n};", "memory": "31500" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\n unordered_map<int, int> makeMap(vector<int>& nums1, vector<int>& nums2) {\n unordered_map<int, int> M;\n int n = nums1.size();\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n int sum = nums1[i] + nums2[j];\n if(M.find(sum) != M.end()) M[sum] += 1;\n else M[sum] = 1;\n }\n }\n return M;\n }\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n int n = nums1.size();\n unordered_map<int, int> M = makeMap(nums1, nums2);\n unordered_map<int, int> N = makeMap(nums3, nums4);\n \n int ans = 0;\n \n for(auto it = M.begin(); it != M.end(); it++) {\n int temp = -(it->first);\n if(N.find(temp) != N.end()) {\n ans += (N[temp] * (it->second));\n }\n }\n return ans;\n }\n};", "memory": "31500" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int,int>mp;\n for(auto it:nums1){\n for(auto p:nums2){\n mp[it+p]++;\n }\n }\n unordered_map<int,int>mp2;\n for(auto it:nums3){\n for(auto p:nums4){\n mp2[it+p]++;\n }\n }\n int cnt=0;\n for(auto it:mp){\n for(auto p:mp2){\n if(it.first+p.first==0){\n cnt+=abs(mp[it.first]*mp2[p.first]);\n }\n }\n }\n return cnt;\n }\n};", "memory": "31600" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n using count = int;\n using value = int;\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n const std::unordered_map<value, count> mapValue12 {getValueMap(nums1, nums2)};\n const std::unordered_map<value, count> mapValue34 {getValueMap(nums3, nums4)};\n\n int sum {0};\n for (const auto& [value12, count12] : mapValue12)\n {\n if (const auto& it = mapValue34.find(-value12); it != mapValue34.end()) \n {\n sum += count12 * it->second;\n }\n }\n return sum;\n }\n\n std::unordered_map<value, count> getValueMap(vector<int>& nums1, vector<int>& nums2)\n {\n std::unordered_map<value, count> mapValue;\n for (const auto& num1 : nums1)\n { \n for (const auto& num2 : nums2) \n {\n mapValue[num1 + num2]++;\n }\n }\n return mapValue;\n }\n};", "memory": "31700" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n using count = int;\n using value = int;\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n const std::unordered_map<value, count> mapValue12 {getValueMap(nums1, nums2)};\n const std::unordered_map<value, count> mapValue34 {getValueMap(nums3, nums4)};\n\n int sum {0};\n for (const auto& [value12, count12] : mapValue12)\n {\n if (const auto& it = mapValue34.find(-value12); it != mapValue34.end()) \n {\n sum += count12 * it->second;\n }\n }\n return sum;\n }\n\n std::unordered_map<value, count> getValueMap(vector<int>& nums1, vector<int>& nums2)\n {\n std::unordered_map<value, count> mapValue;\n for (const auto& num1 : nums1)\n { \n for (const auto& num2 : nums2) \n {\n mapValue[num1 + num2]++;\n }\n }\n return mapValue;\n }\n};", "memory": "31800" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n using count = int;\n using value = int;\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n const std::unordered_map<value, count> mapValue12 {getValueMap(nums1, nums2)};\n const std::unordered_map<value, count> mapValue34 {getValueMap(nums3, nums4)};\n\n int sum {0};\n for (const auto& [value12, count12] : mapValue12)\n {\n if (const auto& it = mapValue34.find(-value12); it != mapValue34.end()) \n {\n sum += count12 * it->second;\n }\n }\n return sum;\n }\n\n std::unordered_map<value, count> getValueMap(vector<int>& nums1, vector<int>& nums2)\n {\n std::unordered_map<value, count> mapValue;\n for (const auto& num1 : nums1)\n { \n for (const auto& num2 : nums2) \n {\n mapValue[num1 + num2]++;\n }\n }\n return mapValue;\n }\n};", "memory": "31800" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int, int> hash1;\n unordered_map<int, int> hash2;\n int N = nums1.size();\n for (int i = 0; i < N; ++i) {\n for (int j = 0; j < N; ++j) {\n int tmp = nums1[i] + nums2[j];\n ++hash1[tmp]; \n int tmp1 = nums3[i] + nums4[j];\n ++hash2[tmp1];\n }\n }\n int ans = 0;\n for (auto& p : hash1) {\n if (hash2.find(-p.first) != hash2.end()) {\n ans += p.second * hash2[-p.first]; \n }\n }\n return ans;\n }\n};", "memory": "31900" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n int i,j,k,l;\n i = 0;\n j = 0;\n k = 0;\n l = 0;\n unordered_map<int,int> m_12;\n unordered_map<int,int> m_34;\n for(int i = 0; i < nums1.size(); i++)\n {\n for(int j = 0; j < nums2.size(); j++)\n {\n m_12[nums1[i]+nums2[j]] += 1;\n }\n }\n for(int i = 0; i < nums3.size(); i++)\n {\n for(int j = 0; j < nums4.size(); j++)\n {\n m_34[nums3[i]+nums4[j]] += 1;\n }\n }\n int re = 0;\n for(auto it = m_12.begin(); it != m_12.end(); it++)\n {\n int tmp = it->first;\n auto it2 = m_34.find(-tmp);\n if(it2 != m_34.end())\n {\n int m = it->second;\n int n = it2->second;\n re = re + m*n;\n }\n }\n return re;\n }\n};", "memory": "31900" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3,\n vector<int>& nums4) {\n unordered_map<int, int> sum1;\n unordered_map<int, int> sum2;\n int res = 0;\n\n for (auto num1 : nums1) {\n for (auto num2 : nums2) {\n sum1[num1 + num2]++;\n }\n }\n\n for (auto num1 : nums3) {\n for (auto num2 : nums4) {\n sum2[num1 + num2]++;\n }\n }\n\n for (auto num1 : sum1) {\n res += sum2[-num1.first] * num1.second;\n }\n\n return res;\n }\n};", "memory": "32000" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n ios_base::sync_with_stdio(false), cin.tie(NULL);\n unordered_map<int, int> m1, m2;\n int n = nums1.size(), ans = 0, sum, count;\n for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) {\n m1[nums1[i] + nums2[j]]++;\n m2[nums3[i] + nums4[j]]++;\n }\n\n for(auto i : m1) ans += i.second * m2[-i.first];\n return ans;\n\n }\n};", "memory": "32100" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n ios_base::sync_with_stdio(false), cin.tie(NULL);\n unordered_map<int, int> m1, m2;\n int n = nums1.size(), ans = 0, sum, count;\n for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) {\n m1[nums1[i] + nums2[j]]++;\n m2[nums3[i] + nums4[j]]++;\n }\n\n for(auto &i : m1) ans += i.second * m2[-i.first];\n return ans;\n\n }\n};", "memory": "32100" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void func(vector<int>& nums1, vector<int>& nums2, map<int, int>& mp1){\n int n = nums1.size();\n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n mp1[nums1[i] + nums2[j]]++; \n }\n }\n }\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n \n int n = nums1.size();\n\n map<int, int> mp1, mp2;\n\n func(nums1, nums2, mp1);\n func(nums3, nums4, mp2);\n\n int ans = 0;\n for(auto i : mp1){\n auto itr = mp2.find(-i.first);\n if(itr != mp2.end()){\n ans += i.second * itr->second;\n }\n }\n return ans;\n }\n};", "memory": "32299" }
454
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p> <ul> <li><code>0 &lt;= i, j, k, l &lt; n</code></li> <li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two tuples are: 1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length</code></li> <li><code>n == nums2.length</code></li> <li><code>n == nums3.length</code></li> <li><code>n == nums4.length</code></li> <li><code>1 &lt;= n &lt;= 200</code></li> <li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n map<int, int> mp1;\n map<int, int> mp2;\n int n = nums1.size();\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n mp1[nums1[i] + nums2[j]]++;\n mp2[nums3[i] + nums4[j]]++;\n }\n }\n\n // for(auto &c:mp1)cout << c.first << \" \" << c.second << \" // \" << endl;\n // for(auto &c:mp2)cout << c.first << \" \" << c.second << \" // \" << endl;\n int ans = 0;\n for(auto &c:mp1){\n for(auto &v:mp2){\n if(c.first + v.first == 0)ans += c.second*v.second;\n }\n }\n return(ans);\n\n }\n};", "memory": "32400" }
3,249
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p> <p>You can apply the following operation on the array <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> element of the array and <strong>flip</strong> a bit in its <strong>binary</strong> representation. Flipping a bit means changing a <code>0</code> to <code>1</code> or vice versa.</li> </ul> <p>Return <em>the <strong>minimum</strong> number of operations required to make the bitwise </em><code>XOR</code><em> of <strong>all</strong> elements of the final array equal to </em><code>k</code>.</p> <p><strong>Note</strong> that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,3,4], k = 1 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can do the following operations: - Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes [2,1,2,4]. - Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes [6,1,2,4]. The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. It can be shown that we cannot make the XOR equal to k in less than 2 operations. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,0,2,0], k = 0 <strong>Output:</strong> 0 <strong>Explanation:</strong> The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. </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>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= k &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int countBitsToChange(int num1, int num2) {\n int xorResult = num1 ^ num2;\n int count = 0;\n while (xorResult > 0) {\n count += xorResult & 1;\n xorResult >>= 1;\n }\n return count;\n }\n\n\n int minOperations(vector<int>& nums, int k) {\n int ans=0;\n int n=nums.size();\n for(int i=0;i<n;i++){\n ans^=nums[i];\n }\n int ansi=countBitsToChange(ans,k);\n return ansi;\n }\n};", "memory": "91600" }
3,249
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p> <p>You can apply the following operation on the array <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> element of the array and <strong>flip</strong> a bit in its <strong>binary</strong> representation. Flipping a bit means changing a <code>0</code> to <code>1</code> or vice versa.</li> </ul> <p>Return <em>the <strong>minimum</strong> number of operations required to make the bitwise </em><code>XOR</code><em> of <strong>all</strong> elements of the final array equal to </em><code>k</code>.</p> <p><strong>Note</strong> that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,3,4], k = 1 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can do the following operations: - Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes [2,1,2,4]. - Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes [6,1,2,4]. The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. It can be shown that we cannot make the XOR equal to k in less than 2 operations. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,0,2,0], k = 0 <strong>Output:</strong> 0 <strong>Explanation:</strong> The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. </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>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= k &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int k) {\n for (int num : nums) {\n k ^= num;\n }\n int count = 0;\n while (k != 0) {\n count += k&1;\n k >>= 1;\n }\n return count;\n }\n};", "memory": "91700" }
3,249
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p> <p>You can apply the following operation on the array <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> element of the array and <strong>flip</strong> a bit in its <strong>binary</strong> representation. Flipping a bit means changing a <code>0</code> to <code>1</code> or vice versa.</li> </ul> <p>Return <em>the <strong>minimum</strong> number of operations required to make the bitwise </em><code>XOR</code><em> of <strong>all</strong> elements of the final array equal to </em><code>k</code>.</p> <p><strong>Note</strong> that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,3,4], k = 1 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can do the following operations: - Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes [2,1,2,4]. - Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes [6,1,2,4]. The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. It can be shown that we cannot make the XOR equal to k in less than 2 operations. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,0,2,0], k = 0 <strong>Output:</strong> 0 <strong>Explanation:</strong> The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. </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>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= k &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int k) {\n int result =0;\n for(int i=0;i<nums.size();i++){\n result = result ^ nums[i];\n }\n\n result = result^k;\n return __builtin_popcount(result);\n \n \n \n }\n};", "memory": "91800" }
3,249
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p> <p>You can apply the following operation on the array <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> element of the array and <strong>flip</strong> a bit in its <strong>binary</strong> representation. Flipping a bit means changing a <code>0</code> to <code>1</code> or vice versa.</li> </ul> <p>Return <em>the <strong>minimum</strong> number of operations required to make the bitwise </em><code>XOR</code><em> of <strong>all</strong> elements of the final array equal to </em><code>k</code>.</p> <p><strong>Note</strong> that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,3,4], k = 1 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can do the following operations: - Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes [2,1,2,4]. - Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes [6,1,2,4]. The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. It can be shown that we cannot make the XOR equal to k in less than 2 operations. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,0,2,0], k = 0 <strong>Output:</strong> 0 <strong>Explanation:</strong> The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. </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>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= k &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int k) {\n int n = nums.size();\n int Oxor = 0;\n for(int i=0;i<n;i++){\n Oxor ^= nums[i];\n }\n int count=0;\n while(Oxor||k){\n int x = Oxor&1;\n int y = k&1;\n if((x^y)==1) count +=1; \n\n Oxor >>= 1;\n k >>= 1; \n }\n return count;\n }\n};", "memory": "91900" }
3,249
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p> <p>You can apply the following operation on the array <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> element of the array and <strong>flip</strong> a bit in its <strong>binary</strong> representation. Flipping a bit means changing a <code>0</code> to <code>1</code> or vice versa.</li> </ul> <p>Return <em>the <strong>minimum</strong> number of operations required to make the bitwise </em><code>XOR</code><em> of <strong>all</strong> elements of the final array equal to </em><code>k</code>.</p> <p><strong>Note</strong> that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,3,4], k = 1 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can do the following operations: - Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes [2,1,2,4]. - Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes [6,1,2,4]. The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. It can be shown that we cannot make the XOR equal to k in less than 2 operations. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,0,2,0], k = 0 <strong>Output:</strong> 0 <strong>Explanation:</strong> The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. </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>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= k &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int k) {\n int n = nums.size();\n int Oxor = 0;\n for(int i=0;i<n;i++){\n Oxor ^= nums[i];\n }\n int count=0;\n while(Oxor||k){\n int x = Oxor&1;\n int y = k&1;\n if((x^y)==1) count +=1; \n\n Oxor >>= 1;\n k >>= 1; \n }\n return count;\n }\n};", "memory": "91900" }
3,249
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p> <p>You can apply the following operation on the array <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> element of the array and <strong>flip</strong> a bit in its <strong>binary</strong> representation. Flipping a bit means changing a <code>0</code> to <code>1</code> or vice versa.</li> </ul> <p>Return <em>the <strong>minimum</strong> number of operations required to make the bitwise </em><code>XOR</code><em> of <strong>all</strong> elements of the final array equal to </em><code>k</code>.</p> <p><strong>Note</strong> that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,3,4], k = 1 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can do the following operations: - Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes [2,1,2,4]. - Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes [6,1,2,4]. The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. It can be shown that we cannot make the XOR equal to k in less than 2 operations. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,0,2,0], k = 0 <strong>Output:</strong> 0 <strong>Explanation:</strong> The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. </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>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= k &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int k) {\n int zor=0;\n for(int i=0; i<nums.size(); i++) zor=zor^nums[i];\n if(zor==k) return 0;\n int cnt=0;\n while(zor!=0 || k!=0){\n if((zor&1)!= (k&1)) cnt++;\n zor=zor>>1;\n k=k>>1;\n }\n return cnt;\n }\n};", "memory": "92000" }
3,249
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p> <p>You can apply the following operation on the array <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> element of the array and <strong>flip</strong> a bit in its <strong>binary</strong> representation. Flipping a bit means changing a <code>0</code> to <code>1</code> or vice versa.</li> </ul> <p>Return <em>the <strong>minimum</strong> number of operations required to make the bitwise </em><code>XOR</code><em> of <strong>all</strong> elements of the final array equal to </em><code>k</code>.</p> <p><strong>Note</strong> that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,3,4], k = 1 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can do the following operations: - Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes [2,1,2,4]. - Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes [6,1,2,4]. The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. It can be shown that we cannot make the XOR equal to k in less than 2 operations. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,0,2,0], k = 0 <strong>Output:</strong> 0 <strong>Explanation:</strong> The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. </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>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= k &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int k) {\n int cnt = 0;\n for(int i=0;i<32;i++){\n int target_bit = (k>>i)&1;\n int set_bit = 0;\n for(auto a:nums){\n set_bit += (a>>i)&1;\n }\n\n if(target_bit){\n if(!(set_bit&1)) cnt++;\n }\n else{\n if(set_bit&1) cnt++;\n }\n }\n return cnt;\n }\n};", "memory": "92000" }
3,249
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p> <p>You can apply the following operation on the array <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> element of the array and <strong>flip</strong> a bit in its <strong>binary</strong> representation. Flipping a bit means changing a <code>0</code> to <code>1</code> or vice versa.</li> </ul> <p>Return <em>the <strong>minimum</strong> number of operations required to make the bitwise </em><code>XOR</code><em> of <strong>all</strong> elements of the final array equal to </em><code>k</code>.</p> <p><strong>Note</strong> that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,3,4], k = 1 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can do the following operations: - Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes [2,1,2,4]. - Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes [6,1,2,4]. The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. It can be shown that we cannot make the XOR equal to k in less than 2 operations. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,0,2,0], k = 0 <strong>Output:</strong> 0 <strong>Explanation:</strong> The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. </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>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= k &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int k) {\n int zor=0;\n for(int i=0; i<nums.size(); i++) zor=zor^nums[i];\n if(zor==k) return 0;\n else return __builtin_popcount(zor^k);\n // int cnt=0;\n // while(zor!=0 || k!=0){\n // if((zor&1)!= (k&1)) cnt++;\n // zor=zor>>1;\n // k=k>>1;\n // }\n // return cnt;\n }\n};", "memory": "92100" }
3,249
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p> <p>You can apply the following operation on the array <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> element of the array and <strong>flip</strong> a bit in its <strong>binary</strong> representation. Flipping a bit means changing a <code>0</code> to <code>1</code> or vice versa.</li> </ul> <p>Return <em>the <strong>minimum</strong> number of operations required to make the bitwise </em><code>XOR</code><em> of <strong>all</strong> elements of the final array equal to </em><code>k</code>.</p> <p><strong>Note</strong> that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,3,4], k = 1 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can do the following operations: - Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes [2,1,2,4]. - Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes [6,1,2,4]. The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. It can be shown that we cannot make the XOR equal to k in less than 2 operations. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,0,2,0], k = 0 <strong>Output:</strong> 0 <strong>Explanation:</strong> The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. </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>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= k &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int k) {\n int cnt = 0;\n for(int i=0;i<32;i++){\n int target_bit = (k>>i)&1;\n int set_bit = 0;\n for(auto a:nums){\n set_bit += (a>>i)&1;\n }\n\n if(target_bit){\n if(!(set_bit&1)) cnt++;\n }\n else{\n if(set_bit&1) cnt++;\n }\n }\n return cnt;\n }\n};", "memory": "92100" }
3,249
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p> <p>You can apply the following operation on the array <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> element of the array and <strong>flip</strong> a bit in its <strong>binary</strong> representation. Flipping a bit means changing a <code>0</code> to <code>1</code> or vice versa.</li> </ul> <p>Return <em>the <strong>minimum</strong> number of operations required to make the bitwise </em><code>XOR</code><em> of <strong>all</strong> elements of the final array equal to </em><code>k</code>.</p> <p><strong>Note</strong> that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,3,4], k = 1 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can do the following operations: - Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes [2,1,2,4]. - Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes [6,1,2,4]. The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. It can be shown that we cannot make the XOR equal to k in less than 2 operations. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,0,2,0], k = 0 <strong>Output:</strong> 0 <strong>Explanation:</strong> The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. </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>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= k &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int k) {\n \n vector<int>bits(32,0);\n \n for(int i =0;i<nums.size();i++)\n {\n int num = nums[i];\n \n for(int j = 0;j<32;j++)\n {\n int bit = (num>>j)&1;\n bits[j] += bit;\n }\n }\n \n int ans = 0;\n for(int i = 0;i<32;i++)\n {\n int bit = (k>>i)&1;\n if(bit==1)\n {\n if(bits[i]%2==0)\n {\n ans++;\n }\n }\n else\n {\n if(bits[i]%2!=0)\n {\n ans++;\n }\n }\n }\n \n return ans;\n }\n};", "memory": "92200" }
3,249
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p> <p>You can apply the following operation on the array <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> element of the array and <strong>flip</strong> a bit in its <strong>binary</strong> representation. Flipping a bit means changing a <code>0</code> to <code>1</code> or vice versa.</li> </ul> <p>Return <em>the <strong>minimum</strong> number of operations required to make the bitwise </em><code>XOR</code><em> of <strong>all</strong> elements of the final array equal to </em><code>k</code>.</p> <p><strong>Note</strong> that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,3,4], k = 1 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can do the following operations: - Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes [2,1,2,4]. - Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes [6,1,2,4]. The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. It can be shown that we cannot make the XOR equal to k in less than 2 operations. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,0,2,0], k = 0 <strong>Output:</strong> 0 <strong>Explanation:</strong> The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. </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>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= k &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int> freq1(32),freq2(32);\n int j=0;\n while(k){\n if(k%2!=0) freq1[j]=1;\n j++;\n k/=2;\n }\n\n for(auto x:nums){\n k=x;\n j=0;\n while(k){\n if(k%2!=0) freq2[j]++;\n j++;\n k/=2;\n }\n }\n int ans=0;\n for(int i=0;i<32;i++){\n int one=freq2[i];\n int zero=n-freq2[i];\n\n int x,y=0;\n if(one%2==0) x=0;\n else x=1;\n // if(i==0) cout<<x<<\" \"<<endl;\n if(freq1[i]==1){\n if(x==0) ans++;\n\n }else if(freq1[i]==0){\n if(x==1){\n ans++;\n }\n }\n }\n return ans;\n }\n};", "memory": "92300" }
3,249
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p> <p>You can apply the following operation on the array <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> element of the array and <strong>flip</strong> a bit in its <strong>binary</strong> representation. Flipping a bit means changing a <code>0</code> to <code>1</code> or vice versa.</li> </ul> <p>Return <em>the <strong>minimum</strong> number of operations required to make the bitwise </em><code>XOR</code><em> of <strong>all</strong> elements of the final array equal to </em><code>k</code>.</p> <p><strong>Note</strong> that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,3,4], k = 1 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can do the following operations: - Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes [2,1,2,4]. - Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes [6,1,2,4]. The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. It can be shown that we cannot make the XOR equal to k in less than 2 operations. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,0,2,0], k = 0 <strong>Output:</strong> 0 <strong>Explanation:</strong> The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. </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>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= k &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int k) {\n int xor_a = 0;\n for(int i = 0; i < nums.size(); i++){\n xor_a = xor_a^nums[i];\n }\n string s1 = bitset<64> (xor_a).to_string();\n string s2 = bitset<64> (k).to_string();\n int count = 0;\n for(int i = 0; i < s1.length(); i++){\n if(s1[i]!=s2[i]){\n count++;\n }\n }\n return count;\n }\n};", "memory": "92400" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int n = obstacles.size();\n vector<int> ret; ret.reserve(n);\n vector<int> dp; dp.reserve(n);\n for (int h : obstacles) {\n if (dp.empty() || dp.back() <= h) {\n dp.push_back(h);\n ret.push_back(dp.size());\n } else {\n int i = upper_bound(begin(dp), end(dp), h) - begin(dp);\n dp[i] = h;\n ret.push_back(i + 1);\n }\n }\n return ret;\n }\n};", "memory": "119100" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int n = obstacles.size();\n vector<int> ret; ret.reserve(n);\n vector<int> dp; dp.reserve(n);\n for (int h : obstacles) {\n if (dp.empty() || dp.back() <= h) {\n dp.push_back(h);\n ret.push_back(dp.size());\n } else {\n int i = upper_bound(begin(dp), end(dp), h) - begin(dp);\n dp[i] = h;\n ret.push_back(i + 1);\n }\n }\n return ret;\n }\n};\n\nstatic const int _ = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();", "memory": "119300" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
0
{ "code": "#ifdef LOCAL\n #include \"../../lib/bits.h\"\n #include \"../../lib/leetcode.h\"\n #include \"../../lib/debug.h\"\n#else\n #define debug(...) 1\n#endif\n\nusing ll = long long;\n\nclass Solution {\npublic:\n vector<int> solve1(vector<int>& obstacles) {\n const int n = obstacles.size();\n\n vector<int> tails{0};\n tails.reserve(n);\n \n const auto find = [&](int v) -> int {\n int l = 0, r = (int)tails.size() - 1;\n int ans = 0;\n while (l <= r) {\n const int m = l + (r - l) / 2;\n if (tails[m] <= v) {\n ans = m;\n l = m + 1;\n } else {\n r = m - 1;\n }\n }\n\n return ans;\n };\n\n vector<int> ans;\n ans.reserve(n);\n\n for (int v : obstacles) {\n const int l = find(v) + 1;\n if (l == tails.size()) {\n tails.emplace_back(v);\n } else {\n tails[l] = v;\n }\n\n ans.emplace_back(l);\n }\n\n return ans;\n }\n\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n return solve1(obstacles);\n }\n};\n\n// =============================\n// https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position\n\n#ifdef LOCAL\n\nint main(int argc, char *argv[]) {\n std::ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n using namespace leetcode;\n\n auto obstacles = read_vec<int>();\n const auto result = (new Solution())->longestObstacleCourseAtEachPosition(obstacles);\n debug(result);\n\n return 0;\n}\n\n#endif", "memory": "119800" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& a) {\n int n=a.size();\n vector <int> ans (n,1);\n vector <int> lst;\n lst.push_back(a[0]);\n for (int i=1;i<n;i++)\n {\n if (lst.back()<=a[i])\n {\n lst.push_back(a[i]);\n ans[i]=lst.size();\n }\n else\n {\n int pos=upper_bound(lst.begin(),lst.end(),a[i])-lst.begin();\n // minimize for lis\n lst[pos]=a[i];\n ans[i]=pos+1;\n }\n }\n return ans;\n\n\n }\n};", "memory": "120100" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
0
{ "code": "\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& nums) {\n \n int n = nums.size();\n \n // lis store elements of longest increasing subsequence till ith\n \n vector<int> lis;\n \n // ans[i] store, no of elements satisfying the condition including ith\n \n vector<int> ans(n);\n\n for(int i = 0; i < n; i++)\n {\n int idx = upper_bound(lis.begin(), lis.end(), nums[i]) - lis.begin();\n \n ans[i] = idx + 1;\n \n if(idx == lis.size())\n lis.push_back(nums[i]);\n \n else\n {\n lis[idx] = nums[i];\n }\n }\n \n return ans;\n }\n};\n", "memory": "120200" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& nums) {\n vector<int> lis;\n for (int i = 0; i < nums.size(); ++i) {\n int x = nums[i];\n if (lis.empty() || lis[lis.size() - 1] <= x) { \n lis.push_back(x);\n nums[i] = lis.size();\n } else {\n int idx = upper_bound(lis.begin(), lis.end(), x) - lis.begin(); \n lis[idx] = x; \n nums[i] = idx + 1;\n }\n }\n return nums;\n }\n};", "memory": "120200" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obs) {\n int n=obs.size();\n vector<int> result(n, 1);\n vector<int> LIS;\n\n for(int i=0;i<obs.size();i++){\n\n int idx = upper_bound(LIS.begin(),LIS.end(),obs[i]) - LIS.begin();\n\n if(idx == LIS.size()){\n LIS.push_back(obs[i]);\n }\n else{\n LIS[idx]=obs[i];\n }\n\n result[i] = idx+1;\n }\n\n return result;\n }\n};", "memory": "120300" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int n = obstacles.size();\n\n vector<int> ans(n, 1);\n vector<int> lis {obstacles[0]}; \n\n for (int i = 1; i < n; i++) {\n int j = upper_bound(lis.begin(), lis.end(), obstacles[i]) - lis.begin();\n \n if (j == lis.size())\n lis.push_back(obstacles[i]);\n else \n lis[j] = obstacles[i];\n\n ans[i] = j + 1;\n }\n\n return ans; \n }\n};", "memory": "120300" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int n=obstacles.size();\n vector<int>lis;\n vector<int>res(n);\n for(int i=0;i<n;i++)\n {\n int idx=upper_bound(begin(lis),end(lis),obstacles[i])-begin(lis);\n if(idx==lis.size()) lis.push_back(obstacles[i]);\n else lis[idx]=obstacles[i];\n res[i]=idx+1;\n }\n return res;\n \n \n }\n};", "memory": "120400" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
2
{ "code": "// Seen\nclass Solution {\n public:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n vector<int> dpArr = {};\n int len = obstacles.size();\n vector<int> ans(len);\n\n ans[0] = 1, dpArr = {obstacles[0]};\n\n for(int i:dpArr) cout<<i<<',';\n cout<<'\\n';\n\n for (int i = 1; i < len; i++) {\n int idx = upper_bound(dpArr.begin(), dpArr.end(), obstacles[i]) - dpArr.begin();\n\n if (idx == (int)dpArr.size()) {\n dpArr.push_back(obstacles[i]);\n } else {\n dpArr[idx] = obstacles[i];\n }\n\n\n ans[i] = idx+1;\n }\n\n return ans;\n }\n};", "memory": "120500" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
2
{ "code": "//m0\n\n//[A] easy dp_ending[i] in O(n^2), now require O(nlogn) for 1964. Find the Longest Valid Obstacle Course at Each Position hard.\n\n\n//[B] think of a tree:\n\n//1. a tree actually building & forking the increasing subsequences from the root to a leaf.\n\n//2. for [2, 6, 8, 3, 4, 5, 1], the 3 paths to any leaf are: sub1 = [2, 6, 8], sub2 = [2, 3, 4, 5], sub3 = [1] --> for process, see hiepit's sol.\n\n\n//[C] implement [B] by a tree array:\n\n//1. for eg., for node \"3\", once we found the parent node \"2\" by binary search,\n\n//2. don't delete another forked branch ([2, 6, 8]) from the tree array.\n\n//3. like lazy update / lazy propagation, just keep the old forked branch.\n\n//4. we can still fork branches on any node of the tree array. --> the order is still correct.\n\n\n//[D] final answer\n\n//1. recommend not to use ans = final tree_array.size().\n\n//2. for more clear semantics, ans = max(forked leaf index j) for all j,\n//2.a. since it's more like enumerating LIS_dp_ending[j].\n\n\n//[E] lower_bound or upper_bound?\n\n//1. strictly increasing --> lower_bound(), 'cuz we replace the iterator's value.\n\n//2. non-decreasing --> upper_bound() for replacing *it.\n\n\n//[reference] hiepit's solution by greedy & binary search in O(nlogn):\n//https://leetcode.com/problems/longest-increasing-subsequence/solutions/1326308/c-python-dp-binary-search-bit-segment-tree-solutions-picture-explain-o-nlogn\n\nstatic auto _ = [](){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int n = obstacles.size();\n\n vector<int> lis_ending(n);\n\n vector<int> tree_arr;\n for(int i=0;i<n;i++){\n const int h = obstacles[i];\n\n auto it = upper_bound(tree_arr.begin(), tree_arr.end(), h);\n\n //if empty, v.begin() == v.end()\n if(it == tree_arr.end()){\n tree_arr.push_back(h);\n\n lis_ending[i] = tree_arr.size();\n }else{\n *it = h;\n\n int j = it - tree_arr.begin();\n\n lis_ending[i] = j - (0-1);\n }\n }\n return lis_ending;\n }\n};", "memory": "120600" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int i;\n vector<int> b(obstacles.size() + 1, INT_MAX);\n vector<int> ans(obstacles.size(), 0);\n b[0] = INT_MIN;\n for(i = 0 ; i < obstacles.size() ; i ++){\n vector<int>::iterator got = upper_bound(b.begin(), b.end(), obstacles[i]);\n got --;\n int sz = got - b.begin();\n b[sz + 1] = min(b[sz + 1], obstacles[i]);\n ans[i] = sz + 1;\n }\n return ans;\n }\n};", "memory": "121400" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int binSearch (vector<int>& nums, int target) {\n int a = 0, b = nums.size()-1;\n while (a < b) {\n int m = (a+b+1)/2;\n if (nums[m] <= target) { a = m; }\n else { b = m - 1; }\n }\n\n if (nums[a] <= target) return a;\n return -1;\n }\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& nums) {\n vector<int> ret(nums.size()); int cont = 0;\n vector<int> dp(nums.size(), INT_MAX);\n\n for (int i = 0; i < nums.size(); i++) {\n int j = binSearch(dp, nums[i]);\n\n if (j == -1) {\n dp[0] = nums[i];\n ret[i] = 1;\n }\n else {\n dp[j+1] = nums[i];\n ret[i] = j+2;\n }\n\n \n }\n \n return ret;\n }\n};", "memory": "121500" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int n = obstacles.size();\n\n vector<int> dp(n + 2, numeric_limits<int>::max());\n dp[0] = numeric_limits<int>::min();\n\n vector<int> ans(n);\n for (int i = 0; i < n; ++i) {\n int nxt_index = upper_bound(dp.begin(), dp.end(), obstacles[i]) - dp.begin();\n ans[i] = nxt_index;\n dp[nxt_index] = min(dp[nxt_index], obstacles[i]);\n }\n\n return ans;\n }\n};", "memory": "121600" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int ceilIdx(vector<int>&tail,int l, int r, int x){\n while(r>l){\n int m= l + (r-l)/2;\n if(tail[m]>=x)r=m;\n else l=m+1;\n }\n return r;\n }\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& arr) {\n int n=arr.size();\n vector<int>tail(n);\n int length=1;\n tail[0]=arr[0];\n vector<int> lis(n,1);\n\n // for(int i=1;i<n;i++){\n // for(int j=0;j<i;j++){\n // if(arr[j]<=arr[i]){\n // lis[i]=max(lis[i],lis[j]+1);\n // }\n // }\n // }\n\n for(int i=1;i<n;i++){\n if(arr[i]>=tail[length-1]){\n tail[length]=arr[i];\n length++;\n lis[i]=length;\n }\n else{\n // for(int i=0;i<length;i++){\n // cout<<tail[i]<<\" \";\n // }\n // cout<<endl;\n // cout<<arr[i]<<\" \";\n // int c = ceilIdx(tail, 0, length-1, arr[i]);\n int c=upper_bound(tail.begin(),tail.begin() + length,arr[i])-tail.begin();\n cout<<c<<endl;\n lis[i]=c+1;\n tail[c]=arr[i];\n }\n }\n\n return lis;\n }\n};", "memory": "121700" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
2
{ "code": "#define ll long long\n#define pb push_back\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obs) {\n ll n = obs.size();\n vector<int>ans(n);\n vector<ll>store;\n for(ll i=0;i<n;i++){\n ll indi = upper_bound(store.begin(),store.end(),obs[i])-store.begin();\n ans[i]=indi+1;\n if(indi==store.size())store.pb(obs[i]);\n else store[indi]=obs[i];\n }\n return ans;\n }\n};", "memory": "122800" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n vector<int> res;\n\n int i = 0;\n\n for (auto num: obstacles) {\n auto idx = std::upper_bound(\n begin(obstacles), begin(obstacles) + i, num) - begin(obstacles);\n\n if (idx == i) i++;\n\n res.push_back(idx + 1);\n\n obstacles[idx] = num;\n }\n\n return res;\n }\n};", "memory": "123000" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
2
{ "code": "// Time: O(NlogN)\n// Space: O(1)\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& A) {\n vector<int> ans;\n int len = 0;\n for (int n : A) {\n int i = upper_bound(begin(A), begin(A) + len, n) - begin(A); // this number `n` should be put at `lis[i]`\n A[i] = n;\n len = max(i + 1, len); // updating the length of LIS\n ans.push_back(i + 1);\n }\n return ans;\n }\n};", "memory": "123200" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
2
{ "code": "const static auto speedup = [] () {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n \n return 1;\n} ();\n\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n vector<int> res;\n\n int i = 0;\n\n for (auto num: obstacles) {\n auto idx = std::upper_bound(\n begin(obstacles), begin(obstacles) + i, num) - begin(obstacles);\n\n if (idx == i) i++;\n\n res.push_back(idx + 1);\n\n obstacles[idx] = num;\n }\n\n return res;\n }\n};", "memory": "123300" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& nums) {\n int n=nums.size();\n vector<int>dp(n,1);\n // for(int i=1;i<n;i++){\n // for(int j=0;j<i;j++){ //represent previous \n // if(nums[j]<=nums[i]){\n // dp[i]=max(dp[j]+1,dp[i]);\n // }\n // }\n // }\n vector<int>result(n);\n vector<int>lis;\n lis.push_back(nums[0]);\n result[0]=1;\n for(int i=1;i<n;i++){\n auto it = upper_bound(lis.begin(), lis.end(),nums[i]) - lis.begin();\n\n // result[it]=;\n if(it==lis.size()){\n lis.push_back(nums[i]);\n }\n else {\n lis[it]=nums[i];\n }\n result[i]=it+1;\n\n }\n return result;\n }\n};", "memory": "124100" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int n=obstacles.size();\n vector<int>ans(obstacles.size());\n vector<int>dp(n,1);\n // for (int i=1;i<n;i++){\n // for (int j=0;j<i;j++){\n // if (obstacles[j]<=obstacles[i]){\n // dp[i]=max(dp[i],1+dp[j]);\n // }\n // }\n // }\n vector<int> v;\n for(int i=0;i<n;i++){\n if(v.size()==0||v.back()<=obstacles[i]){\n v.push_back(obstacles[i]);\n dp[i] =v.size();\n }else{\n int ind = upper_bound(v.begin(),v.end(),obstacles[i])-v.begin();\n v[ind] = obstacles[i];\n dp[i] = ind+1;\n }\n }\n return dp;\n \n }\n};", "memory": "124400" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int n=obstacles.size();\n vector<int>ans(obstacles.size());\n vector<int>dp(n,1);\n // for (int i=1;i<n;i++){\n // for (int j=0;j<i;j++){\n // if (obstacles[j]<=obstacles[i]){\n // dp[i]=max(dp[i],1+dp[j]);\n // }\n // }\n // }\n vector<int> v;\n for(int i=0;i<n;i++){\n if(v.size()==0||v.back()<=obstacles[i]){\n v.push_back(obstacles[i]);\n dp[i] =v.size();\n }else{\n int ind = upper_bound(v.begin(),v.end(),obstacles[i])-v.begin();\n v[ind] = obstacles[i];\n dp[i] = ind+1;\n }\n }\n return dp;\n \n }\n};", "memory": "124400" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) \n {\n vector<int>dp;\n vector<int>ans;\n for(auto x:obstacles)\n {\n auto k=lower_bound(dp.begin(),dp.end(),x);\n if(k==dp.end())\n {\n dp.push_back(x);\n ans.push_back(dp.size());\n }\n else\n {\n auto u=upper_bound(dp.begin(),dp.end(),x);\n if(*k==x)\n {\n if(u==dp.end())\n {\n dp.push_back(x);\n ans.push_back(dp.size());\n }\n else\n {\n *u=x;\n ans.push_back(u-dp.begin()+1);\n }\n }\n else\n {\n if(u==dp.end())\n {\n dp.push_back(x);\n ans.push_back(dp.size());\n }\n else\n {\n *k=x;\n ans.push_back(k-dp.begin()+1);\n }\n }\n \n }\n \n }\n return ans;\n }\n};", "memory": "126000" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "\nclass FenwickTree {\npublic:\n FenwickTree(int size) : tree(size + 1, 0) {}\n \n void update(int index, int value) {\n while (index < tree.size()) {\n tree[index] = max(tree[index], value);\n index += index & -index;\n }\n }\n \n int query(int index) {\n int maxVal = 0;\n while (index > 0) {\n maxVal = max(maxVal, tree[index]);\n index -= index & -index;\n }\n return maxVal;\n }\n \nprivate:\n vector<int> tree;\n};\n\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int n = obstacles.size();\n \n // Normalize the heights\n vector<int> sortedObstacles = obstacles;\n sort(sortedObstacles.begin(), sortedObstacles.end());\n for (int& height : obstacles) {\n height = lower_bound(sortedObstacles.begin(), sortedObstacles.end(), height) - sortedObstacles.begin() + 1;\n }\n \n FenwickTree bit(n);\n vector<int> result(n);\n \n for (int i = 0; i < n; ++i) {\n int currentHeight = obstacles[i];\n int maxLength = bit.query(currentHeight);\n result[i] = maxLength + 1;\n bit.update(currentHeight, result[i]);\n }\n \n return result;\n }\n};\n", "memory": "126700" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n std::vector<int> longestObstacleCourseAtEachPosition(std::vector<int>& obstacles) {\n int n = obstacles.size();\n std::vector<int> res; \n std::vector<int> dp(n + 1, INT_MAX); \n\n for (int i = 0; i < n; ++i) {\n int height = obstacles[i];\n\n int index = upper_bound(dp.begin(), dp.end(), height) - dp.begin();\n\n res.push_back(index + 1);\n\n dp[index] = height;\n }\n\n return res;\n }\n};\n\n\n\n/*\n//My solution - correct but gives TLE cuz O(n^2)\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int n = obstacles.size();\n vector<int> dp(n, 1); \n\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < i; ++j) {\n if (obstacles[i] >= obstacles[j]) {\n dp[i] = max(dp[i], dp[j] + 1);\n }\n }\n }\n\n return dp;\n }\n};\n*/", "memory": "127000" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obs) {\n int n = obs.size();\n vector<int> v(n, INT_MAX);\n vector<int> res;\n for(auto & o : obs){\n int idx = upper_bound(v.begin(), v.end(), o) - v.begin();\n v[idx] = o;\n res.push_back(idx+1);\n }\n return res;\n }\n};", "memory": "127100" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "//DP + Binary\n//I don't understand it\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int n = obstacles.size();\n vector<int> res; \n vector<int> dp(n + 1, INT_MAX); \n\n for (int i = 0; i < n; ++i) {\n int height = obstacles[i];\n\n int index = upper_bound(dp.begin(), dp.end(), height) - dp.begin();\n\n res.push_back(index + 1);\n\n dp[index] = height;\n }\n\n return res;\n }\n};\n\n/*\n//My solution - correct but gives TLE cuz O(n^2)\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int n = obstacles.size();\n vector<int> dp(n, 1); \n\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < i; ++j) {\n if (obstacles[i] >= obstacles[j]) {\n dp[i] = max(dp[i], dp[j] + 1);\n }\n }\n }\n\n return dp;\n }\n};\n*/", "memory": "127200" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n \n\n // For every index i, we are interested in the longest increasing subsequence in obstacles[0..i] which includes i\n\n stack<int> st;\n\n vector<int> ans;\n int n = obstacles.size();\n\n // At each position, remember the answer we got from there\n // arr[k] = the last number of the sequence we used to build a subsequence of lenght k\n\n vector<int> answers(n + 1, INT_MAX);\n for (int i = 0; i < obstacles.size(); i++) {\n\n cout << \"i: \" << i << '\\n';\n\n int l = 1, r = answers.size() - 1;\n\n while(l != r) {\n int mid = (l + r) / 2;\n if (answers[mid] <= obstacles[i]) {\n // cout << \"GOING RIGHT AFTER: \" << mid << '\\n';\n l = mid + 1;\n }\n else {\n // cout << \"GOING LEFT AFTER: \" << mid << '\\n';\n r = mid;\n }\n }\n\n // The answer is now in l\n // cout << \"l: \" << l << '\\n';\n\n answers[l] = min(answers[l], obstacles[i]);\n ans.push_back(l);\n }\n\n return ans;\n }\n};", "memory": "127300" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int n = obstacles.size();\n vector<int> dp(n, 1); // dp[i] will store the longest valid course ending at i\n vector<int> longest_valid; // This will store the final result\n \n vector<int> lis; // This stores the longest increasing subsequence\n\n for (int i = 0; i < n; ++i) {\n // Use binary search to find the position in lis where obstacles[i] can be placed\n int idx = upper_bound(lis.begin(), lis.end(), obstacles[i]) - lis.begin();\n \n // If idx == lis.size(), obstacles[i] is larger than all elements in lis, so append it\n if (idx == lis.size()) {\n lis.push_back(obstacles[i]);\n } else {\n // Otherwise, replace the element at idx with obstacles[i]\n lis[idx] = obstacles[i];\n }\n \n // The length of the longest valid obstacle course ending at i is idx + 1\n longest_valid.push_back(idx + 1);\n }\n\n return longest_valid;\n }\n};", "memory": "129800" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> lis(vector<int>nums){\n vector<int> dp;\n vector<int> ans;\n for(int i=0;i<nums.size();i++){\n if(dp.empty() || nums[i]>=dp.back()){\n dp.push_back(nums[i]);\n ans.push_back(dp.size());\n }\n else{\n int upperBound = upper_bound(dp.begin(),dp.end(),nums[i]) - dp.begin();\n dp[upperBound] = nums[i];\n ans.push_back(upperBound+1);\n }\n }\n return ans;\n }\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n return lis(obstacles);\n }\n};", "memory": "129900" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "#ifdef LOCAL\n #include \"../../lib/bits.h\"\n #include \"../../lib/leetcode.h\"\n #include \"../../lib/debug.h\"\n#else\n #define debug(...) 1\n#endif\n\nusing ll = long long;\n\nclass Solution {\npublic:\n vector<int> solve1(vector<int>& obstacles) {\n const auto lis = [](vector<int>& a) -> vector<int> {\n vector<int> tails{0};\n const auto bs = [&](int v) -> int {\n int l = 0, r = (int)tails.size() - 1;\n int ans = 0;\n while (l <= r) {\n const int m = l + (r - l) / 2;\n if (tails[m] <= v) {\n ans = m;\n l = m + 1;\n } else {\n r = m - 1;\n }\n }\n\n return ans;\n };\n\n vector<int> ans;\n for (int v : a) {\n const int l = bs(v) + 1;\n if (l == tails.size()) {\n tails.emplace_back(v);\n } else {\n tails[l] = v;\n }\n\n ans.emplace_back(l);\n }\n\n return ans;\n };\n\n return lis(obstacles);\n }\n\n class SegmentTree {\n public:\n SegmentTree(vector<int>& a) {\n array.swap(a);\n tree.assign(array.size() * 4, 0);\n build(1, 0, array.size() - 1);\n }\n\n SegmentTree(const int n) {\n array.assign(n, 0);\n tree.assign(array.size() * 4, 0);\n build(1, 0, array.size() - 1);\n }\n \n int combine(int l, int r) {\n return max(l, r);\n }\n \n void build(int v, int tl, int tr) {\n if (tl == tr) {\n tree[v] = array[tl];\n return;\n }\n \n int mid = (tl + tr) / 2;\n build(v * 2, tl, mid);\n build(v * 2 + 1, mid + 1, tr);\n tree[v] = combine(tree[v * 2], tree[v * 2 + 1]);\n }\n \n int get(int l, int r) {\n return get(1, 0, array.size() - 1, l, r);\n }\n \n int get(int v, int tl, int tr, int l, int r) {\n if (l > r) {\n return 0;\n }\n \n if (tl == l && tr == r) {\n return tree[v];\n }\n \n int mid = (tl + tr) / 2;\n int left_ans = get(v * 2, tl, mid, l, min(mid, r));\n int right_ans = get(v * 2 + 1, mid + 1, tr, max(mid + 1, l), r);\n return combine(left_ans, right_ans);\n }\n \n void update(int index, int value) {\n update(1, 0, array.size() - 1, index, value);\n }\n \n void update(int v, int tl, int tr, int index, int value) {\n if (tl == tr) {\n tree[v] = value;\n return;\n }\n \n int mid = (tl + tr) / 2;\n if (index <= mid) {\n update(v * 2, tl, mid, index, value);\n } else {\n update(v * 2 + 1, mid + 1, tr, index, value);\n }\n \n tree[v] = combine(tree[v * 2], tree[v * 2 + 1]);\n }\n \n public:\n vector<int> tree;\n vector<int> array;\n };\n\n vector<int> solve2(vector<int>& obstacles) {\n const int n = obstacles.size();\n\n vector<pair<int, int>> a;\n for (int i = 0; i < n; ++i) {\n a.emplace_back(obstacles[i], i);\n }\n\n sort(a.begin(), a.end());\n\n vector<int> ans(n);\n SegmentTree st(n);\n for (const auto& [v, i] : a) {\n int l = 1;\n if (i > 0) {\n l = st.get(0, i - 1) + 1;\n }\n \n st.update(i, l);\n ans[i] = l;\n }\n\n return ans;\n }\n\n template <typename T, T val = numeric_limits<T>::max()> class Min {\n public:\n static T default_val() { return val; };\n static T combine(const T a, const T b) { return min(a, b); }\n };\n\n template <typename T, T val = numeric_limits<T>::min()> class Max {\n public:\n static T default_val() { return val; };\n static T combine(const T a, const T b) { return max(a, b); }\n };\n\n template <typename T, typename P> class FenwickTreeMinMax {\n public:\n FenwickTreeMinMax(int n) {\n this->n = n;\n tree.assign(n, P::default_val());\n }\n\n FenwickTreeMinMax(const vector<int>& a) : FenwickTreeMinMax(a.size()) {\n for (int i = 0; i < a.size(); ++i) {\n update(i, a[i]);\n }\n }\n\n T get(int r) {\n T ans = P::default_val();\n for (; r >= 0; r = (r & (r + 1)) - 1) {\n ans = P::combine(ans, tree[r]);\n }\n\n return ans;\n }\n\n void update(int i, T val) {\n for (; i < n; i = i | (i + 1)) {\n tree[i] = P::combine(tree[i], val);\n }\n }\n\n public:\n vector<T> tree;\n int n = 0;\n };\n\n vector<int> solve3(vector<int>& obstacles) {\n const int n = obstacles.size();\n\n vector<pair<int, int>> a;\n for (int i = 0; i < n; ++i) {\n a.emplace_back(obstacles[i], i);\n }\n\n sort(a.begin(), a.end());\n\n vector<int> ans(n);\n FenwickTreeMinMax<int, Max<int, 0>> ft(n);\n for (const auto& [v, i] : a) {\n int l = 1;\n if (i > 0) {\n l = ft.get(i - 1) + 1;\n }\n \n ft.update(i, l);\n ans[i] = l;\n }\n\n return ans;\n }\n\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n // return solve1(obstacles);\n\n // debug(solve1(obstacles));\n // return solve2(obstacles);\n return solve3(obstacles);\n }\n};\n\n// =============================\n// https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position\n\n#ifdef LOCAL\n\nint main(int argc, char *argv[]) {\n std::ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n using namespace leetcode;\n\n auto obstacles = read_vec<int>();\n const auto result = (new Solution())->longestObstacleCourseAtEachPosition(obstacles);\n debug(result);\n\n return 0;\n}\n\n#endif", "memory": "141200" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "#ifdef LOCAL\n #include \"../../lib/bits.h\"\n #include \"../../lib/leetcode.h\"\n #include \"../../lib/debug.h\"\n#else\n #define debug(...) 1\n#endif\n\nusing ll = long long;\n\nclass Solution {\npublic:\n vector<int> solve1(vector<int>& obstacles) {\n const auto lis = [](vector<int>& a) -> vector<int> {\n vector<int> tails{0};\n const auto bs = [&](int v) -> int {\n int l = 0, r = (int)tails.size() - 1;\n int ans = 0;\n while (l <= r) {\n const int m = l + (r - l) / 2;\n if (tails[m] <= v) {\n ans = m;\n l = m + 1;\n } else {\n r = m - 1;\n }\n }\n\n return ans;\n };\n\n vector<int> ans;\n for (int v : a) {\n const int l = bs(v) + 1;\n if (l == tails.size()) {\n tails.emplace_back(v);\n } else {\n tails[l] = v;\n }\n\n ans.emplace_back(l);\n }\n\n return ans;\n };\n\n return lis(obstacles);\n }\n\n class SegmentTree {\n public:\n SegmentTree(vector<int>& a) {\n array.swap(a);\n tree.assign(array.size() * 4, 0);\n build(1, 0, array.size() - 1);\n }\n\n SegmentTree(const int n) {\n array.assign(n, 0);\n tree.assign(array.size() * 4, 0);\n build(1, 0, array.size() - 1);\n }\n \n int combine(int l, int r) {\n return max(l, r);\n }\n \n void build(int v, int tl, int tr) {\n if (tl == tr) {\n tree[v] = array[tl];\n return;\n }\n \n int mid = (tl + tr) / 2;\n build(v * 2, tl, mid);\n build(v * 2 + 1, mid + 1, tr);\n tree[v] = combine(tree[v * 2], tree[v * 2 + 1]);\n }\n \n int get(int l, int r) {\n return get(1, 0, array.size() - 1, l, r);\n }\n \n int get(int v, int tl, int tr, int l, int r) {\n if (l > r) {\n return 0;\n }\n \n if (tl == l && tr == r) {\n return tree[v];\n }\n \n int mid = (tl + tr) / 2;\n int left_ans = get(v * 2, tl, mid, l, min(mid, r));\n int right_ans = get(v * 2 + 1, mid + 1, tr, max(mid + 1, l), r);\n return combine(left_ans, right_ans);\n }\n \n void update(int index, int value) {\n update(1, 0, array.size() - 1, index, value);\n }\n \n void update(int v, int tl, int tr, int index, int value) {\n if (tl == tr) {\n tree[v] = value;\n return;\n }\n \n int mid = (tl + tr) / 2;\n if (index <= mid) {\n update(v * 2, tl, mid, index, value);\n } else {\n update(v * 2 + 1, mid + 1, tr, index, value);\n }\n \n tree[v] = combine(tree[v * 2], tree[v * 2 + 1]);\n }\n \n public:\n vector<int> tree;\n vector<int> array;\n };\n\n vector<int> solve2(vector<int>& obstacles) {\n const int n = obstacles.size();\n\n vector<pair<int, int>> a;\n for (int i = 0; i < n; ++i) {\n a.emplace_back(obstacles[i], i);\n }\n\n sort(a.begin(), a.end());\n\n vector<int> ans(n);\n SegmentTree st(n);\n for (const auto& [v, i] : a) {\n int l = 1;\n if (i > 0) {\n l = st.get(0, i - 1) + 1;\n }\n \n st.update(i, l);\n ans[i] = l;\n }\n\n return ans;\n }\n\n struct FenwickTreeMax {\n vector<int> bit;\n int n;\n const int minf = 0;\n FenwickTreeMax(int n) {\n this->n = n;\n bit.assign(n, minf);\n }\n\n FenwickTreeMax(vector<int> a) : FenwickTreeMax(a.size()) {\n for (size_t i = 0; i < a.size(); i++) {\n update(i, a[i]);\n }\n }\n\n int getmax(int r) {\n int ret = minf;\n for (; r >= 0; r = (r & (r + 1)) - 1) {\n ret = max(ret, bit[r]);\n }\n\n return ret;\n }\n\n void update(int idx, int val) {\n for (; idx < n; idx = idx | (idx + 1)) {\n bit[idx] = max(bit[idx], val);\n }\n }\n };\n\n vector<int> solve3(vector<int>& obstacles) {\n const int n = obstacles.size();\n\n vector<pair<int, int>> a;\n for (int i = 0; i < n; ++i) {\n a.emplace_back(obstacles[i], i);\n }\n\n sort(a.begin(), a.end());\n\n vector<int> ans(n);\n FenwickTreeMax ft(n);\n for (const auto& [v, i] : a) {\n int l = 1;\n if (i > 0) {\n l = ft.getmax(i - 1) + 1;\n }\n \n ft.update(i, l);\n ans[i] = l;\n }\n\n return ans;\n }\n\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n // return solve1(obstacles);\n\n // debug(solve1(obstacles));\n // return solve2(obstacles);\n return solve3(obstacles);\n }\n};\n\n// =============================\n// https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position\n\n#ifdef LOCAL\n\nint main(int argc, char *argv[]) {\n std::ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n using namespace leetcode;\n\n auto obstacles = read_vec<int>();\n const auto result = (new Solution())->longestObstacleCourseAtEachPosition(obstacles);\n debug(result);\n\n return 0;\n}\n\n#endif", "memory": "141300" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "#ifdef LOCAL\n #include \"../../lib/bits.h\"\n #include \"../../lib/leetcode.h\"\n #include \"../../lib/debug.h\"\n#else\n #define debug(...) 1\n#endif\n\nusing ll = long long;\n\nclass Solution {\npublic:\n vector<int> solve1(vector<int>& obstacles) {\n const auto lis = [](vector<int>& a) -> vector<int> {\n vector<int> tails{0};\n const auto bs = [&](int v) -> int {\n int l = 0, r = (int)tails.size() - 1;\n int ans = 0;\n while (l <= r) {\n const int m = l + (r - l) / 2;\n if (tails[m] <= v) {\n ans = m;\n l = m + 1;\n } else {\n r = m - 1;\n }\n }\n\n return ans;\n };\n\n vector<int> ans;\n for (int v : a) {\n const int l = bs(v) + 1;\n if (l == tails.size()) {\n tails.emplace_back(v);\n } else {\n tails[l] = v;\n }\n\n ans.emplace_back(l);\n }\n\n return ans;\n };\n\n return lis(obstacles);\n }\n\n class SegmentTree {\n public:\n SegmentTree(vector<int>& a) {\n array.swap(a);\n tree.assign(array.size() * 4, 0);\n build(1, 0, array.size() - 1);\n }\n\n SegmentTree(const int n) {\n array.assign(n, 0);\n tree.assign(array.size() * 4, 0);\n build(1, 0, array.size() - 1);\n }\n \n int combine(int l, int r) {\n return max(l, r);\n }\n \n void build(int v, int tl, int tr) {\n if (tl == tr) {\n tree[v] = array[tl];\n return;\n }\n \n int mid = (tl + tr) / 2;\n build(v * 2, tl, mid);\n build(v * 2 + 1, mid + 1, tr);\n tree[v] = combine(tree[v * 2], tree[v * 2 + 1]);\n }\n \n int get(int l, int r) {\n return get(1, 0, array.size() - 1, l, r);\n }\n \n int get(int v, int tl, int tr, int l, int r) {\n if (l > r) {\n return 0;\n }\n \n if (tl == l && tr == r) {\n return tree[v];\n }\n \n int mid = (tl + tr) / 2;\n int left_ans = get(v * 2, tl, mid, l, min(mid, r));\n int right_ans = get(v * 2 + 1, mid + 1, tr, max(mid + 1, l), r);\n return combine(left_ans, right_ans);\n }\n \n void update(int index, int value) {\n update(1, 0, array.size() - 1, index, value);\n }\n \n void update(int v, int tl, int tr, int index, int value) {\n if (tl == tr) {\n tree[v] = value;\n return;\n }\n \n int mid = (tl + tr) / 2;\n if (index <= mid) {\n update(v * 2, tl, mid, index, value);\n } else {\n update(v * 2 + 1, mid + 1, tr, index, value);\n }\n \n tree[v] = combine(tree[v * 2], tree[v * 2 + 1]);\n }\n \n public:\n vector<int> tree;\n vector<int> array;\n };\n\n vector<int> solve2(vector<int>& obstacles) {\n const int n = obstacles.size();\n\n // vector<pair<int, int>> a;\n // for (int i = 0; i < n; ++i) {\n // a.emplace_back(obstacles[i], i);\n // }\n\n // sort(a.begin(), a.end());\n\n vector<int> order(n);\n for (int i = 0; i < n; ++i) {\n order[i] = i;\n }\n\n sort(order.begin(), order.end(), [&](int l, int r) -> bool {\n if (obstacles[l] == obstacles[r]) {\n return l < r;\n }\n\n return obstacles[l] < obstacles[r];\n });\n\n vector<int> ans(n);\n SegmentTree st(n);\n // for (const auto& [v, i] : a) {\n // int l = 1;\n // if (i > 0) {\n // l = st.get(0, i - 1) + 1;\n // }\n \n // st.update(i, l);\n // ans[i] = l;\n // }\n\n for (int i : order) {\n int l = 1;\n if (i > 0) {\n l = st.get(0, i - 1) + 1;\n }\n \n st.update(i, l);\n ans[i] = l;\n }\n\n return ans;\n }\n\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n // return solve1(obstacles);\n\n // debug(solve1(obstacles));\n return solve2(obstacles);\n }\n};\n\n// =============================\n// https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position\n\n#ifdef LOCAL\n\nint main(int argc, char *argv[]) {\n std::ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n using namespace leetcode;\n\n auto obstacles = read_vec<int>();\n const auto result = (new Solution())->longestObstacleCourseAtEachPosition(obstacles);\n debug(result);\n\n return 0;\n}\n\n#endif", "memory": "141800" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "#ifdef LOCAL\n #include \"../../lib/bits.h\"\n #include \"../../lib/leetcode.h\"\n #include \"../../lib/debug.h\"\n#else\n #define debug(...) 1\n#endif\n\nusing ll = long long;\n\nclass Solution {\npublic:\n vector<int> solve1(vector<int>& obstacles) {\n const auto lis = [](vector<int>& a) -> vector<int> {\n vector<int> tails{0};\n const auto bs = [&](int v) -> int {\n int l = 0, r = (int)tails.size() - 1;\n int ans = 0;\n while (l <= r) {\n const int m = l + (r - l) / 2;\n if (tails[m] <= v) {\n ans = m;\n l = m + 1;\n } else {\n r = m - 1;\n }\n }\n\n return ans;\n };\n\n vector<int> ans;\n for (int v : a) {\n const int l = bs(v) + 1;\n if (l == tails.size()) {\n tails.emplace_back(v);\n } else {\n tails[l] = v;\n }\n\n ans.emplace_back(l);\n }\n\n return ans;\n };\n\n return lis(obstacles);\n }\n\n class SegmentTree {\n public:\n SegmentTree(vector<int>& a) {\n array.swap(a);\n tree.assign(array.size() * 4, 0);\n build(1, 0, array.size() - 1);\n }\n\n SegmentTree(const int n) {\n array.assign(n, 0);\n tree.assign(array.size() * 4, 0);\n build(1, 0, array.size() - 1);\n }\n \n int combine(int l, int r) {\n return max(l, r);\n }\n \n void build(int v, int tl, int tr) {\n if (tl == tr) {\n tree[v] = array[tl];\n return;\n }\n \n int mid = (tl + tr) / 2;\n build(v * 2, tl, mid);\n build(v * 2 + 1, mid + 1, tr);\n tree[v] = combine(tree[v * 2], tree[v * 2 + 1]);\n }\n \n int get(int l, int r) {\n return get(1, 0, array.size() - 1, l, r);\n }\n \n int get(int v, int tl, int tr, int l, int r) {\n if (l > r) {\n return 0;\n }\n \n if (tl == l && tr == r) {\n return tree[v];\n }\n \n int mid = (tl + tr) / 2;\n int left_ans = get(v * 2, tl, mid, l, min(mid, r));\n int right_ans = get(v * 2 + 1, mid + 1, tr, max(mid + 1, l), r);\n return combine(left_ans, right_ans);\n }\n \n void update(int index, int value) {\n update(1, 0, array.size() - 1, index, value);\n }\n \n void update(int v, int tl, int tr, int index, int value) {\n if (tl == tr) {\n tree[v] = value;\n return;\n }\n \n int mid = (tl + tr) / 2;\n if (index <= mid) {\n update(v * 2, tl, mid, index, value);\n } else {\n update(v * 2 + 1, mid + 1, tr, index, value);\n }\n \n tree[v] = combine(tree[v * 2], tree[v * 2 + 1]);\n }\n \n public:\n vector<int> tree;\n vector<int> array;\n };\n\n vector<int> solve2(vector<int>& obstacles) {\n const int n = obstacles.size();\n\n // vector<pair<int, int>> a;\n // for (int i = 0; i < n; ++i) {\n // a.emplace_back(obstacles[i], i);\n // }\n\n // sort(a.begin(), a.end());\n\n vector<int> order(n);\n for (int i = 0; i < n; ++i) {\n order[i] = i;\n }\n\n sort(order.begin(), order.end(), [&](int l, int r) -> bool {\n if (obstacles[l] == obstacles[r]) {\n return l < r;\n }\n\n return obstacles[l] < obstacles[r];\n });\n\n vector<int> ans(n);\n SegmentTree st(n);\n // for (const auto& [v, i] : a) {\n // int l = 1;\n // if (i > 0) {\n // l = st.get(0, i - 1) + 1;\n // }\n \n // st.update(i, l);\n // ans[i] = l;\n // }\n\n for (int i : order) {\n int l = 1;\n if (i > 0) {\n l = st.get(0, i - 1) + 1;\n }\n \n st.update(i, l);\n ans[i] = l;\n }\n\n return ans;\n }\n\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n // return solve1(obstacles);\n\n debug(solve1(obstacles));\n return solve2(obstacles);\n }\n};\n\n// =============================\n// https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position\n\n#ifdef LOCAL\n\nint main(int argc, char *argv[]) {\n std::ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n using namespace leetcode;\n\n auto obstacles = read_vec<int>();\n const auto result = (new Solution())->longestObstacleCourseAtEachPosition(obstacles);\n debug(result);\n\n return 0;\n}\n\n#endif", "memory": "141900" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int n = obstacles.size();\n vector<pair<int, int>> v;\n for (int i = 0; i < n; i++) {\n v.push_back({obstacles[i], i});\n }\n sort(v.begin(), v.end());\n vector<int> nums(n);\n for (int i = 0; i < v.size(); i++) {\n pair<int, int> p = v[i];\n nums[p.second] = i;\n }\n vector<int> LIS = {nums[0]}, ans = {1};\n for (int i = 1; i < n; i++) {\n if (nums[i] > LIS.back()) {\n LIS.push_back(nums[i]);\n ans.push_back(LIS.size());\n }\n else {\n int ind = lower_bound(LIS.begin(), LIS.end(), nums[i]) - LIS.begin();\n LIS[ind] = nums[i];\n ans.push_back(ind + 1);\n }\n }\n return ans;\n }\n};", "memory": "149600" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "class Solution {\n struct BITree {\n vector<int> tree;\n int n;\n\n BITree(int n) {\n this->n = n;\n tree.resize(n+1, 0);\n }\n\n int query(int idx) {\n int res = 0;\n \n idx++;\n while (idx > 0) {\n res = max(res, tree[idx]);\n idx -= idx&(-idx);\n }\n return res;\n }\n\n void update(int idx, int val) {\n idx++;\n while (idx <= n) {\n tree[idx] = max(tree[idx], val);\n idx += idx&(-idx);\n }\n }\n };\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n int n = obstacles.size(), idx = 0;\n unordered_map<int, int> map;\n vector<int> temp = obstacles;\n sort(temp.begin(), temp.end());\n for (int h: temp) {\n if (!map.count(h)) map[h] = idx++;\n }\n\n BITree* bit = new BITree(idx);\n vector<int> res(n);\n for (int i = 0; i < n; i++) {\n res[i] = bit->query(map[obstacles[i]])+1;\n bit->update(map[obstacles[i]], res[i]);\n }\n return res;\n }\n};", "memory": "155000" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "#ifdef LOCAL\n #include \"../../lib/bits.h\"\n #include \"../../lib/leetcode.h\"\n #include \"../../lib/debug.h\"\n#else\n #define debug(...) 1\n#endif\n\nusing ll = long long;\n\nclass Solution {\npublic:\n vector<int> solve1(vector<int>& obstacles) {\n const auto lis = [](vector<int>& a) -> vector<int> {\n vector<int> tails{0};\n const auto bs = [&](int v) -> int {\n int l = 0, r = (int)tails.size() - 1;\n int ans = 0;\n while (l <= r) {\n const int m = l + (r - l) / 2;\n if (tails[m] <= v) {\n ans = m;\n l = m + 1;\n } else {\n r = m - 1;\n }\n }\n\n return ans;\n };\n\n vector<int> ans;\n for (int v : a) {\n const int l = bs(v) + 1;\n if (l == tails.size()) {\n tails.emplace_back(v);\n } else {\n tails[l] = v;\n }\n\n ans.emplace_back(l);\n }\n\n return ans;\n };\n\n return lis(obstacles);\n }\n\n class Node {\n public:\n Node() {}\n void apply(int v) {\n this->v = v;\n }\n\n static Node combine(const Node& l, const Node& r) {\n return Node().pull(l, r);\n }\n\n Node& pull(const Node& l, const Node& r) {\n v = max(l.v, r.v);\n return *this;\n }\n\n public:\n int v = 0;\n };\n\n template <typename T, typename N> class SegTree {\n public:\n SegTree(vector<T>& a) {\n array.swap(a);\n tree.resize(array.size() * 4);\n build(1, 0, array.size() - 1);\n }\n\n SegTree(const int n) {\n array.resize(n);\n tree.resize(array.size() * 4);\n build(1, 0, array.size() - 1);\n }\n \n void build(int v, int tl, int tr) {\n if (tl == tr) {\n tree[v].apply(array[tl]);\n return;\n }\n \n int mid = (tl + tr) / 2;\n build(v * 2, tl, mid);\n build(v * 2 + 1, mid + 1, tr);\n tree[v].pull(tree[v * 2], tree[v * 2 + 1]);\n }\n \n N get(int l, int r) {\n return get(1, 0, array.size() - 1, l, r);\n }\n \n N get(int v, int tl, int tr, int l, int r) {\n if (l > r) {\n return N();\n }\n \n if (tl == l && tr == r) {\n return tree[v];\n }\n\n int mid = (tl + tr) / 2;\n return N::combine(\n get(v * 2, tl, mid, l, min(mid, r)), \n get(v * 2 + 1, mid + 1, tr, max(mid + 1, l), r)\n );\n }\n \n void update(int index, T value) {\n update(1, 0, array.size() - 1, index, value);\n }\n \n void update(int v, int tl, int tr, int index, T value) {\n if (tl == tr) {\n tree[v].apply(value);\n return;\n }\n \n int mid = (tl + tr) / 2;\n if (index <= mid) {\n update(v * 2, tl, mid, index, value);\n } else {\n update(v * 2 + 1, mid + 1, tr, index, value);\n }\n \n tree[v].pull(tree[v * 2], tree[v * 2 + 1]);\n }\n \n public:\n vector<N> tree;\n vector<T> array;\n };\n\n vector<int> solve2(vector<int>& obstacles) {\n const int n = obstacles.size();\n\n vector<pair<int, int>> a;\n for (int i = 0; i < n; ++i) {\n a.emplace_back(obstacles[i], i);\n }\n\n sort(a.begin(), a.end());\n\n vector<int> ans(n);\n SegTree<int, Node> st(n);\n for (const auto& [v, i] : a) {\n int l = 1;\n if (i > 0) {\n l = st.get(0, i - 1).v + 1;\n }\n\n st.update(i, l);\n ans[i] = l;\n }\n\n return ans;\n }\n\n template <typename T, T val = numeric_limits<T>::max()> class Min {\n public:\n static T default_val() { return val; };\n static T combine(const T a, const T b) { return min(a, b); }\n };\n\n template <typename T, T val = numeric_limits<T>::min()> class Max {\n public:\n static T default_val() { return val; };\n static T combine(const T a, const T b) { return max(a, b); }\n };\n\n template <typename T, typename P> class FenwickTreeMinMax {\n public:\n FenwickTreeMinMax(int n) {\n this->n = n;\n tree.assign(n, P::default_val());\n }\n\n FenwickTreeMinMax(const vector<T>& a) : FenwickTreeMinMax(a.size()) {\n for (int i = 0; i < a.size(); ++i) {\n update(i, a[i]);\n }\n }\n\n T get(int r) {\n T ans = P::default_val();\n for (; r >= 0; r = (r & (r + 1)) - 1) {\n ans = P::combine(ans, tree[r]);\n }\n\n return ans;\n }\n\n void update(int i, T val) {\n for (; i < n; i = i | (i + 1)) {\n tree[i] = P::combine(tree[i], val);\n }\n }\n\n public:\n vector<T> tree;\n int n = 0;\n };\n\n vector<int> solve3(vector<int>& obstacles) {\n const int n = obstacles.size();\n\n vector<pair<int, int>> a;\n for (int i = 0; i < n; ++i) {\n a.emplace_back(obstacles[i], i);\n }\n\n sort(a.begin(), a.end());\n\n vector<int> ans(n);\n FenwickTreeMinMax<int, Max<int, 0>> ft(n);\n for (const auto& [v, i] : a) {\n int l = 1;\n if (i > 0) {\n l = ft.get(i - 1) + 1;\n }\n \n ft.update(i, l);\n ans[i] = l;\n }\n\n return ans;\n }\n\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n // return solve1(obstacles);\n\n // debug(solve1(obstacles));\n return solve2(obstacles);\n // return solve3(obstacles);\n }\n};\n\n// =============================\n// https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position\n\n#ifdef LOCAL\n\nint main(int argc, char *argv[]) {\n std::ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n using namespace leetcode;\n\n auto obstacles = read_vec<int>();\n const auto result = (new Solution())->longestObstacleCourseAtEachPosition(obstacles);\n debug(result);\n\n return 0;\n}\n\n#endif", "memory": "156300" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "#ifdef LOCAL\n #include \"../../lib/bits.h\"\n #include \"../../lib/leetcode.h\"\n #include \"../../lib/debug.h\"\n#else\n #define debug(...) 1\n#endif\n\nusing ll = long long;\n\nclass Solution {\npublic:\n vector<int> solve1(vector<int>& obstacles) {\n const auto lis = [](vector<int>& a) -> vector<int> {\n vector<int> tails{0};\n const auto bs = [&](int v) -> int {\n int l = 0, r = (int)tails.size() - 1;\n int ans = 0;\n while (l <= r) {\n const int m = l + (r - l) / 2;\n if (tails[m] <= v) {\n ans = m;\n l = m + 1;\n } else {\n r = m - 1;\n }\n }\n\n return ans;\n };\n\n vector<int> ans;\n for (int v : a) {\n const int l = bs(v) + 1;\n if (l == tails.size()) {\n tails.emplace_back(v);\n } else {\n tails[l] = v;\n }\n\n ans.emplace_back(l);\n }\n\n return ans;\n };\n\n return lis(obstacles);\n }\n\n class SegmentTree {\n public:\n SegmentTree(vector<int>& a) {\n array.swap(a);\n tree.assign(array.size() * 4, 0);\n build(1, 0, array.size() - 1);\n }\n\n SegmentTree(const int n) {\n array.assign(n, 0);\n tree.assign(array.size() * 4, 0);\n build(1, 0, array.size() - 1);\n }\n \n int combine(int l, int r) {\n return max(l, r);\n }\n \n void build(int v, int tl, int tr) {\n if (tl == tr) {\n tree[v] = array[tl];\n return;\n }\n \n int mid = (tl + tr) / 2;\n build(v * 2, tl, mid);\n build(v * 2 + 1, mid + 1, tr);\n tree[v] = combine(tree[v * 2], tree[v * 2 + 1]);\n }\n \n int get(int l, int r) {\n return get(1, 0, array.size() - 1, l, r);\n }\n \n int get(int v, int tl, int tr, int l, int r) {\n if (l > r) {\n return 0;\n }\n \n if (tl == l && tr == r) {\n return tree[v];\n }\n \n int mid = (tl + tr) / 2;\n int left_ans = get(v * 2, tl, mid, l, min(mid, r));\n int right_ans = get(v * 2 + 1, mid + 1, tr, max(mid + 1, l), r);\n return combine(left_ans, right_ans);\n }\n \n void update(int index, int value) {\n update(1, 0, array.size() - 1, index, value);\n }\n \n void update(int v, int tl, int tr, int index, int value) {\n if (tl == tr) {\n tree[v] = value;\n return;\n }\n \n int mid = (tl + tr) / 2;\n if (index <= mid) {\n update(v * 2, tl, mid, index, value);\n } else {\n update(v * 2 + 1, mid + 1, tr, index, value);\n }\n \n tree[v] = combine(tree[v * 2], tree[v * 2 + 1]);\n }\n \n public:\n vector<int> tree;\n vector<int> array;\n };\n\n vector<int> solve2(vector<int>& obstacles) {\n const int n = obstacles.size();\n\n vector<pair<int, int>> a;\n for (int i = 0; i < n; ++i) {\n a.emplace_back(obstacles[i], i);\n }\n\n sort(a.begin(), a.end());\n\n vector<int> ans(n);\n SegmentTree st(n);\n for (const auto& [v, i] : a) {\n int l = 1;\n if (i > 0) {\n l = st.get(0, i - 1) + 1;\n }\n \n st.update(i, l);\n ans[i] = l;\n }\n\n return ans;\n }\n\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n // return solve1(obstacles);\n\n // debug(solve1(obstacles));\n return solve2(obstacles);\n }\n};\n\n// =============================\n// https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position\n\n#ifdef LOCAL\n\nint main(int argc, char *argv[]) {\n std::ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n using namespace leetcode;\n\n auto obstacles = read_vec<int>();\n const auto result = (new Solution())->longestObstacleCourseAtEachPosition(obstacles);\n debug(result);\n\n return 0;\n}\n\n#endif", "memory": "156500" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "#ifdef LOCAL\n #include \"../../lib/bits.h\"\n #include \"../../lib/leetcode.h\"\n #include \"../../lib/debug.h\"\n#else\n #define debug(...) 1\n#endif\n\nusing ll = long long;\n\nclass Solution {\npublic:\n vector<int> solve1(vector<int>& obstacles) {\n const auto lis = [](vector<int>& a) -> vector<int> {\n vector<int> tails{0};\n const auto bs = [&](int v) -> int {\n int l = 0, r = (int)tails.size() - 1;\n int ans = 0;\n while (l <= r) {\n const int m = l + (r - l) / 2;\n if (tails[m] <= v) {\n ans = m;\n l = m + 1;\n } else {\n r = m - 1;\n }\n }\n\n return ans;\n };\n\n vector<int> ans;\n for (int v : a) {\n const int l = bs(v) + 1;\n if (l == tails.size()) {\n tails.emplace_back(v);\n } else {\n tails[l] = v;\n }\n\n ans.emplace_back(l);\n }\n\n return ans;\n };\n\n return lis(obstacles);\n }\n\n class Node {\n public:\n Node(int v) {\n this->v = v;\n }\n\n static Node combine(const Node& l, const Node& r) {\n return Node(max(l.v, r.v));\n }\n\n public:\n int v = 0;\n };\n\n template <typename T, typename N> class SegTree {\n public:\n SegTree(vector<T>& a) {\n array.swap(a);\n tree.assign(array.size() * 4, 0);\n build(1, 0, array.size() - 1);\n }\n\n SegTree(const int n) {\n array.assign(n, 0);\n tree.assign(array.size() * 4, 0);\n build(1, 0, array.size() - 1);\n }\n \n void build(int v, int tl, int tr) {\n if (tl == tr) {\n tree[v] = N(array[tl]);\n return;\n }\n \n int mid = (tl + tr) / 2;\n build(v * 2, tl, mid);\n build(v * 2 + 1, mid + 1, tr);\n tree[v] = N::combine(tree[v * 2], tree[v * 2 + 1]);\n }\n \n N get(int l, int r) {\n return get(1, 0, array.size() - 1, l, r);\n }\n \n N get(int v, int tl, int tr, int l, int r) {\n if (l > r) {\n return 0;\n }\n \n if (tl == l && tr == r) {\n return tree[v];\n }\n\n int mid = (tl + tr) / 2;\n return N::combine(\n get(v * 2, tl, mid, l, min(mid, r)), \n get(v * 2 + 1, mid + 1, tr, max(mid + 1, l), r)\n );\n }\n \n void update(int index, T value) {\n update(1, 0, array.size() - 1, index, value);\n }\n \n void update(int v, int tl, int tr, int index, T value) {\n if (tl == tr) {\n tree[v] = value;\n return;\n }\n \n int mid = (tl + tr) / 2;\n if (index <= mid) {\n update(v * 2, tl, mid, index, value);\n } else {\n update(v * 2 + 1, mid + 1, tr, index, value);\n }\n \n tree[v] = N::combine(tree[v * 2], tree[v * 2 + 1]);\n }\n \n public:\n vector<N> tree;\n vector<T> array;\n };\n\n vector<int> solve2(vector<int>& obstacles) {\n const int n = obstacles.size();\n\n vector<pair<int, int>> a;\n for (int i = 0; i < n; ++i) {\n a.emplace_back(obstacles[i], i);\n }\n\n sort(a.begin(), a.end());\n\n vector<int> ans(n);\n SegTree<int, Node> st(n);\n for (const auto& [v, i] : a) {\n int l = 1;\n if (i > 0) {\n l = st.get(0, i - 1).v + 1;\n }\n \n st.update(i, l);\n ans[i] = l;\n }\n\n return ans;\n }\n\n template <typename T, T val = numeric_limits<T>::max()> class Min {\n public:\n static T default_val() { return val; };\n static T combine(const T a, const T b) { return min(a, b); }\n };\n\n template <typename T, T val = numeric_limits<T>::min()> class Max {\n public:\n static T default_val() { return val; };\n static T combine(const T a, const T b) { return max(a, b); }\n };\n\n template <typename T, typename P> class FenwickTreeMinMax {\n public:\n FenwickTreeMinMax(int n) {\n this->n = n;\n tree.assign(n, P::default_val());\n }\n\n FenwickTreeMinMax(const vector<T>& a) : FenwickTreeMinMax(a.size()) {\n for (int i = 0; i < a.size(); ++i) {\n update(i, a[i]);\n }\n }\n\n T get(int r) {\n T ans = P::default_val();\n for (; r >= 0; r = (r & (r + 1)) - 1) {\n ans = P::combine(ans, tree[r]);\n }\n\n return ans;\n }\n\n void update(int i, T val) {\n for (; i < n; i = i | (i + 1)) {\n tree[i] = P::combine(tree[i], val);\n }\n }\n\n public:\n vector<T> tree;\n int n = 0;\n };\n\n vector<int> solve3(vector<int>& obstacles) {\n const int n = obstacles.size();\n\n vector<pair<int, int>> a;\n for (int i = 0; i < n; ++i) {\n a.emplace_back(obstacles[i], i);\n }\n\n sort(a.begin(), a.end());\n\n vector<int> ans(n);\n FenwickTreeMinMax<int, Max<int, 0>> ft(n);\n for (const auto& [v, i] : a) {\n int l = 1;\n if (i > 0) {\n l = ft.get(i - 1) + 1;\n }\n \n ft.update(i, l);\n ans[i] = l;\n }\n\n return ans;\n }\n\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {\n // return solve1(obstacles);\n\n // debug(solve1(obstacles));\n return solve2(obstacles);\n // return solve3(obstacles);\n }\n};\n\n// =============================\n// https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position\n\n#ifdef LOCAL\n\nint main(int argc, char *argv[]) {\n std::ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n using namespace leetcode;\n\n auto obstacles = read_vec<int>();\n const auto result = (new Solution())->longestObstacleCourseAtEachPosition(obstacles);\n debug(result);\n\n return 0;\n}\n\n#endif", "memory": "156500" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "class BIT\n{\n public: \n int size;\n vector<int> tree;\n vector<int> *arr;\n BIT(vector<int> *arr)\n {\n this->size = (*arr).size();\n this->arr = arr;\n tree.resize(size+1,0);\n }\n void update(int i, int maxValue)\n {\n i++;\n while(i<=size)\n {\n tree[i] = max(tree[i], maxValue+1);\n i += (i&(-i));\n }\n }\n int prefixMax(int i)\n {\n i++;\n int maxi = 0;\n while(i>0)\n {\n maxi = max(maxi, tree[i]);\n i -= (i&(-i));\n }\n return maxi;\n }\n};\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& arr) {\n int n = arr.size();\n vector<int> temp = arr;\n unordered_map<int,int> indexes;\n sort(temp.begin(), temp.end());\n int index = 0;\n indexes[temp[0]] = 0;\n for(int i=1;i<n;i++)\n {\n if(temp[i]!=temp[i-1]) index++;\n indexes[temp[i]] = index;\n }\n vector<int> a(index+1,0);\n BIT tree(&a);\n vector<int> result(n,0);\n for(int i=0;i<n;i++)\n {\n int maxValue = tree.prefixMax(indexes[arr[i]]);\n result[i] = maxValue + 1;\n a[indexes[arr[i]]] = maxValue + 1;\n tree.update(indexes[arr[i]], maxValue);\n }\n return result;\n }\n};", "memory": "156900" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "class BIT\n{\n public: \n int size;\n vector<int> tree;\n BIT(int size)\n {\n this->size = size;\n tree.resize(size+1,0);\n }\n void update(int i, int maxValue)\n {\n i++;\n while(i<=size)\n {\n tree[i] = max(tree[i], maxValue+1);\n i += (i&(-i));\n }\n }\n int prefixMax(int i)\n {\n i++;\n int maxi = 0;\n while(i>0)\n {\n maxi = max(maxi, tree[i]);\n i -= (i&(-i));\n }\n return maxi;\n }\n};\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& arr) {\n int n = arr.size();\n vector<int> temp = arr;\n unordered_map<int,int> indexes;\n sort(temp.begin(), temp.end());\n int index = 0;\n indexes[temp[0]] = 0;\n for(int i=1;i<n;i++)\n {\n if(temp[i]!=temp[i-1]) index++;\n indexes[temp[i]] = index;\n }\n vector<int> a(index+1,0);\n BIT tree(index+1);\n vector<int> result(n,0);\n for(int i=0;i<n;i++)\n {\n int maxValue = tree.prefixMax(indexes[arr[i]]);\n result[i] = maxValue + 1;\n a[indexes[arr[i]]] = maxValue + 1;\n tree.update(indexes[arr[i]], maxValue);\n }\n return result;\n }\n};", "memory": "156900" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "class BIT\n{\n public: \n int size;\n vector<int> tree;\n vector<int> *arr;\n BIT(int size)\n {\n this->size = size;\n tree.resize(size+1,0);\n }\n void update(int i, int maxValue)\n {\n i++;\n while(i<=size)\n {\n tree[i] = max(tree[i], maxValue+1);\n i += (i&(-i));\n }\n }\n int prefixMax(int i)\n {\n i++;\n int maxi = 0;\n while(i>0)\n {\n maxi = max(maxi, tree[i]);\n i -= (i&(-i));\n }\n return maxi;\n }\n};\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& arr) {\n int n = arr.size();\n vector<int> temp = arr;\n unordered_map<int,int> indexes;\n sort(temp.begin(), temp.end());\n int index = 0;\n indexes[temp[0]] = 0;\n for(int i=1;i<n;i++)\n {\n if(temp[i]!=temp[i-1]) index++;\n indexes[temp[i]] = index;\n }\n vector<int> a(index+1,0);\n BIT tree(index+1);\n vector<int> result(n,0);\n for(int i=0;i<n;i++)\n {\n int maxValue = tree.prefixMax(indexes[arr[i]]);\n result[i] = maxValue + 1;\n a[indexes[arr[i]]] = maxValue + 1;\n tree.update(indexes[arr[i]], maxValue);\n }\n return result;\n }\n};", "memory": "157000" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "#define debug(x) cout<<#x\" => \"<<(x)<<'\\n';\ntemplate <typename T> void _print(vector<T>& v){for(T x:v) {cout<<x<<\" \";}cout<<'\\n';}\n\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& a) {\n int n = a.size();\n vector<int>b = {a[0]}, ans = {1};\n unordered_map<int, int>mp;\n mp[a[0]] = 1;\n for(int i=1;i<n;i++){\n if(b.back() <= a[i]){\n mp[a[i]] = mp[b.back()] + 1;\n b.push_back(a[i]);\n }else{\n int it = upper_bound(b.begin(), b.end(), a[i]) - b.begin();\n if(!it){\n mp[a[i]] = 1;\n }else{\n mp[a[i]] = mp[b[it - 1]] + 1;\n }\n b[it] = a[i];\n }\n ans.push_back(mp[a[i]]);\n }\n return ans;\n }\n};", "memory": "157200" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "class segTree{\n public: \n vector<int> seg;\n segTree(int n){\n seg.resize(4*n,0);\n }\n void build(int idx, int left, int right, vector<int>& v){\n if(left==right){\n seg[idx] = v[left];\n return;\n }\n int mid = left + (right-left)/2;\n build(2*idx+1,left,mid,v);\n build(2*idx+2,mid+1,right,v);\n seg[idx] = max(seg[2*idx+1],seg[2*idx+2]);\n }\n void update(int idx, int left, int right, int pos, int val){\n if(left == right){\n seg[idx] = max(seg[idx],val);\n return;\n }\n int mid = left + (right-left)/2;\n if(mid >= pos) update(2*idx+1,left,mid,pos,val);\n else update(2*idx+2,mid+1,right,pos,val);\n seg[idx] = max(seg[2*idx+1],seg[2*idx+2]);\n }\n int query(int idx, int left, int right,int l, int r){\n \n if(left > r || right < l) return 0;\n\n if(left >= l && right <= r) return seg[idx];\n\n int mid = left + (right-left)/2;\n\n int lv = query(2*idx+1,left,mid,l,r);\n int rv = query(2*idx+2,mid+1,right,l,r);\n return max(lv,rv);\n }\n};\nclass Solution {\npublic:\n void resizE(vector<int>& v){\n map<int,int> mp;\n for(auto &i : v) mp[i];\n int c = 0;\n for(auto &it : mp) it.second = c++;\n for(auto &i : v) i = mp[i];\n }\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& v) {\n resizE(v);\n int m = *max_element(v.begin(),v.end()),n=v.size();\n segTree st(m+9);\n vector<int> res(n,0);\n // for(auto it : v) cout << it << \" \";cout << endl;\n for(int i=0;i<n;i++){\n // query [0,v[i]]\n res[i] = st.query(0,0,m+8,0,v[i]) + 1;\n // update(v[i],res[i])\n st.update(0,0,m+8,v[i],res[i]);\n }\n return res;\n }\n};", "memory": "160000" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "class SegmentTree {\n public:\n vector<int> seg;\n\n SegmentTree(int n) {\n seg.resize(4 * n);\n }\n\n void build(int ind, int low, int high, vector<int> &nums) {\n if (low == high) {\n seg[ind] = nums[low];\n return;\n }\n int mid = (low + high) / 2;\n build(2 * ind + 1, low, mid, nums);\n build(2 * ind + 2, mid + 1, high, nums);\n\n seg[ind] = max(seg[2 * ind + 1], seg[2 * ind + 2]);\n }\n\n int query(int ind, int low, int high, int l, int r) {\n if (low >= l && high <= r) {\n return seg[ind]; \n }\n if (high < l || low > r) {\n return INT_MIN;\n }\n\n int mid = (low + high) / 2;\n int left = query(2 * ind + 1, low, mid, l, r);\n int right = query(2 * ind + 2, mid + 1, high, l, r);\n\n return max(left, right);\n }\n\n void pointUpdate(int ind, int low, int high, int node, int val) {\n if (low == high) {\n seg[ind] = max(seg[ind], val);\n return;\n }\n int mid = (low + high) / 2;\n if (node >= low && node <= mid) \n pointUpdate(2 * ind + 1, low, mid, node, val);\n else\n pointUpdate(2 * ind + 2, mid + 1, high, node, val);\n \n seg[ind] = max(seg[2 * ind + 1], seg[2 * ind + 2]);\n }\n};\n\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& obs) {\n int n = obs.size();\n vector<pair<int, int>> prs;\n for (int i = 0; i < n; i++) {\n prs.push_back({obs[i], i});\n }\n sort(prs.begin(), prs.end());\n vector<int> nums(n);\n int prev = 0;\n for (int i = 1; i < n; i++) {\n int c_val = prev;\n int ind = prs[i].second;\n if (prs[i].first != prs[i - 1].first)\n c_val++;\n nums[ind] = c_val;\n prev = c_val;\n }\n SegmentTree st(n + 1);\n vector<int> m_val(n);\n st.build(0, 0, n - 1, m_val);\n vector<int> ans(n, 1);\n for (int i = 0; i < n; i++) {\n int c_max = st.query(0, 0, n - 1, 0, nums[i]);\n ans[i] += c_max;\n st.pointUpdate(0, 0, n - 1, nums[i], ans[i]);\n }\n return ans;\n }\n};", "memory": "160400" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int m;\n inline int lsb(int x) { return x & (-x); }\n inline void fmax(int& x, int y) { if (x < y) x = y; }\n void update(vector<int>& bit, int i, int val) {\n for (;i <= m; i += lsb(i)) {\n fmax(bit[i], val);\n }\n } \n int query(const vector<int>& bit, int i) {\n int ret = 0;\n for (; i > 0; i -= lsb(i)) {\n fmax(ret, bit[i]);\n }\n return ret;\n }\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& o) {\n vector<int> a = o;\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n unordered_map<int, int> idx;\n m = a.size();\n for (int i = 0; i < m; ++i) {\n idx[a[i]] = i + 1;\n }\n\n vector<int> bit(m + 1, 0);\n vector<int> ans;\n for (int i : o) {\n int j = idx[i];\n int l = query(bit, j) + 1;\n ans.push_back(l);\n update(bit, j, l);\n }\n return ans;\n }\n};", "memory": "160500" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "#define debug(x) cout<<#x\" => \"<<(x)<<'\\n';\ntemplate <typename T> void _print(vector<T>& v){for(T x:v) {cout<<x<<\" \";}cout<<'\\n';}\n\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& a) {\n int n = a.size();\n vector<int>b = {a[0]}, ans = {1};\n map<int, int>mp;\n mp[a[0]] = 1;\n for(int i=1;i<n;i++){\n if(b.back() <= a[i]){\n mp[a[i]] = mp[b.back()] + 1;\n b.push_back(a[i]);\n }else{\n int it = upper_bound(b.begin(), b.end(), a[i]) - b.begin();\n if(!it){\n mp[a[i]] = 1;\n }else{\n mp[a[i]] = mp[b[it - 1]] + 1;\n }\n b[it] = a[i];\n }\n ans.push_back(mp[a[i]]);\n }\n return ans;\n }\n};", "memory": "160600" }
2,096
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p> <p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p> <ul> <li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li> <li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li> <li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li> <li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li> </ul> <p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> obstacles = [1,2,3,2] <strong>Output:</strong> [1,2,3,3] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>1</u>], [1] has length 1. - i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2. - i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3. - i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> obstacles = [2,2,1] <strong>Output:</strong> [1,2,1] <strong>Explanation: </strong>The longest valid obstacle course at each position is: - i = 0: [<u>2</u>], [2] has length 1. - i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2. - i = 2: [2,2,<u>1</u>], [1] has length 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> obstacles = [3,1,5,6,4,2] <strong>Output:</strong> [1,1,2,3,2,2] <strong>Explanation:</strong> The longest valid obstacle course at each position is: - i = 0: [<u>3</u>], [3] has length 1. - i = 1: [3,<u>1</u>], [1] has length 1. - i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid. - i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid. - i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid. - i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == obstacles.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li> </ul>
3
{ "code": "const int N=1e7+20;\nint arr[N];\n\nvoid update(int ind,int val){\n for(++ind;ind<=N;ind+=ind&-ind){\n arr[ind]=max(arr[ind],val);\n }\n}\n\nint get(int ind){\n int val=INT_MIN;\n for(++ind;ind>0;ind-=ind&-ind){\n val=max(val,arr[ind]);\n }\n\n return val;\n}\n\nclass Solution {\npublic:\n vector<int> longestObstacleCourseAtEachPosition(vector<int>& v) {\n int n=v.size();\n vector<int> ans;\n \n for(int i=0;i<n;i++){\n int val=get(v[i])+1;\n ans.push_back(val);\n update(v[i],val);\n }\n\n memset(arr,0,sizeof(arr));\n return ans;\n\n }\n};", "memory": "162300" }
1,792
<p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p> <p>An array&#39;s subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p> <p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,5,2,6], k = 2 <strong>Output:</strong> [2,6] <strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4 <strong>Output:</strong> [2,3,3,4] </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>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n vector<int> answer;\n answer.reserve(nums.size());\n for (int i = 0; i < nums.size(); i++) {\n while(answer.size() && answer.back() > nums[i] && (answer.size() + nums.size() - i - 1) >= k) {\n answer.pop_back();\n }\n if (answer.size() == k) {\n continue;\n }\n // cout << nums[i] << endl;\n answer.push_back(nums[i]);\n }\n return answer;\n }\n};", "memory": "109100" }
1,792
<p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p> <p>An array&#39;s subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p> <p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,5,2,6], k = 2 <strong>Output:</strong> [2,6] <strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4 <strong>Output:</strong> [2,3,3,4] </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>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n vector<int> answer;\n answer.reserve(nums.size());\n for (int i = 0; i < nums.size(); i++) {\n while(answer.size() && answer.back() > nums[i] && (answer.size() + nums.size() - i - 1) >= k) {\n answer.pop_back();\n }\n if (answer.size() == k) {\n continue;\n }\n // cout << nums[i] << endl;\n answer.push_back(nums[i]);\n }\n return answer;\n }\n};", "memory": "109300" }
1,792
<p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p> <p>An array&#39;s subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p> <p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,5,2,6], k = 2 <strong>Output:</strong> [2,6] <strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4 <strong>Output:</strong> [2,3,3,4] </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>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) \n {\n int n = nums.size();\n \n vector<int> r(n);\n int index = 0;\n\n for (int i=0; i<n; i++)\n {\n int x = nums[i];\n int t = max(0, k+i-n);\n while (index>t && x<r[index-1]) index--;\n r[index++] = x;\n }\n \n r.resize(k); \n return r;\n }\n};\n\nstatic auto speedUp = []() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();", "memory": "111300" }
1,792
<p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p> <p>An array&#39;s subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p> <p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,5,2,6], k = 2 <strong>Output:</strong> [2,6] <strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4 <strong>Output:</strong> [2,3,3,4] </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>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack<int> st;\n vector<int> ans(k);\n for(int i=0;i<nums.size();i++){\n // cout<<\" i = \"<<i<<\"\\n\";\n // cout<<\"st size \"<<st.size()<<\"\\n\";\n while(!st.empty() && st.top() > nums[i] && nums.size() - i- 1 + st.size() >= k){\n st.pop();\n }\n if(st.size() < k){\n st.push(nums[i]);\n }\n }\n int t=k-1;\n // while(st.size()){\n // cout<<st.top()<<\"\\n\";\n // st.pop();\n // }\n while(!st.empty() && t>=0){\n cout<<\"t = \"<<t<<\"\\n\";\n ans[t]=st.top();\n t--;\n st.pop();\n }\n return ans;\n }\n};", "memory": "111600" }
1,792
<p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p> <p>An array&#39;s subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p> <p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,5,2,6], k = 2 <strong>Output:</strong> [2,6] <strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4 <strong>Output:</strong> [2,3,3,4] </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>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack<int> st;\n int n = nums.size();\n for(int i=0;i<n;i++){\n while(!st.empty() && nums[i] < st.top() && st.size() + n-i > k){\n st.pop();\n }\n if(st.size() < k) st.push(nums[i]);\n }\n\n vector<int> ans(k);\n while(!st.empty()) ans[--k] = st.top(), st.pop();\n\n return ans;\n }\n};", "memory": "111700" }
1,792
<p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p> <p>An array&#39;s subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p> <p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,5,2,6], k = 2 <strong>Output:</strong> [2,6] <strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4 <strong>Output:</strong> [2,3,3,4] </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>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack<int> st;\n\n for(int i=0;i<nums.size();i++){\n while(!st.empty() && nums[i]<nums[st.top()] && st.size() + nums.size() - i>k){\n st.pop(); \n }\n\n if(st.size()<k){\n st.push(i);\n }\n }\n\n int i=k-1;\n vector<int> ans(k);\n while(!st.empty()){\n ans[i--]=nums[st.top()];\n st.pop();\n }\n return ans;\n }\n};", "memory": "111800" }
1,792
<p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p> <p>An array&#39;s subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p> <p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,5,2,6], k = 2 <strong>Output:</strong> [2,6] <strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4 <strong>Output:</strong> [2,3,3,4] </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>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n int n = nums.size();\n stack<int> st;\n for(int i=0;i<n;i++){\n\n while(!st.empty() && st.top() > nums[i] && (n-i)+st.size() > k){\n st.pop();\n }\n if(st.size() < k)\n st.push(nums[i]);\n }\n vector<int> ans(k);\n int i = k-1;\n while(!st.empty()){\n int e = st.top();\n st.pop();\n ans[i--] = e;\n }\n return ans;\n }\n};", "memory": "111900" }
1,792
<p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p> <p>An array&#39;s subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p> <p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,5,2,6], k = 2 <strong>Output:</strong> [2,6] <strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4 <strong>Output:</strong> [2,3,3,4] </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>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n stack<int> st;\n \n // Iterate through the input array\n for(int i = 0; i < nums.size(); i++) {\n // Pop from the stack if the current number is smaller than the stack's top\n // and if there are enough numbers left to still make a subsequence of length k.\n while(!st.empty() && st.top() > nums[i] && nums.size() - i > k - st.size()) {\n st.pop();\n }\n // Push the current element onto the stack if there is space left in the subsequence.\n if(st.size() < k) {\n st.push(nums[i]);\n }\n }\n \n // Collect the result from the stack\n vector<int> ans(k);\n for(int i = k - 1; i >= 0; i--) {\n ans[i] = st.top();\n st.pop();\n }\n \n return ans;\n }\n};\n", "memory": "112000" }
1,792
<p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p> <p>An array&#39;s subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p> <p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,5,2,6], k = 2 <strong>Output:</strong> [2,6] <strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4 <strong>Output:</strong> [2,3,3,4] </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>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> mostCompetitive(vector<int>& nums, int k) {\n vector<int> incSeqs;\n for (int i = 0; i < nums.size(); ++i) {\n const int remain = nums.size() - i;\n const int shift = max(0, k - remain);\n auto it = upper_bound(incSeqs.begin() + shift, incSeqs.end(), nums[i]);\n incSeqs.resize(it-incSeqs.begin());\n if (incSeqs.size() < k) {\n incSeqs.emplace_back(nums[i]);\n }\n }\n return incSeqs;\n }\n};\n", "memory": "112100" }