id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int neg_index=1;\n int pos_index=0;\n int n = nums.size();\n int ans[n];\n for (int i=0;i<n;i++){\n if (nums[i]<0){\n ans[neg_index] = nums[i];\n neg_index+=2;\n }\n else{\n ans[pos_index] = nums[i];\n pos_index+=2;\n }\n }\n vector<int>ans2;\n for (int i=0;i<n;i++){\n ans2.push_back(ans[i]);\n }\n return ans2;\n }\n};", "memory": "133547" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> op;\n int p=0;\n for(int i=0;i<nums.size();i++)\n {\n \n if(nums[i]>0)\n {\n \n op.push_back(nums[i]);\n // p++;\n }\n \n }\n for(int i=0;i<nums.size();i++){\n if(nums[i]<0)\n {\n op.insert(op.begin()+p+1,nums[i]);\n p+=2;\n }\n }\n\n return op;\n }\n};", "memory": "133547" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>ans;\n int n = nums.size() ;\n int temp1[n];\n int temp2[n];\n int a = 0,b=0,c=0;\n while(a<n){\n if(nums[a] >0){\n temp1[b] = nums[a];\n b++;a++;\n }else{\n temp2[c] = nums[a]; \n c++;a++;\n }\n }\n // ans.push_back(temp1[0]);\n int i = 0,j = 0,k=0;\n while(i<n){\n ans.push_back(temp1[j]);\n i++;j++;\n ans.push_back(temp2[k]);\n i++;k++;\n\n }\n return ans;\n }\n};", "memory": "133782" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "\nauto init = []() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\nclass Solution {\npublic:\n \n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> ans;\n int i=0,j=0;\n int n=nums.size();\n while(i<n && j<n){\n while(nums[i]<0){\n i++;\n }\n while(nums[j]>0){\n j++;\n }\n ans.push_back(nums[i++]);\n \n ans.push_back(nums[j++]);\n \n } \n return ans; \n }\n};", "memory": "133782" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int p1=0,p2=0;\n if (nums.size()==0 || nums.size()==2)\n {\n if (nums[0]>0)\n return nums;\n else\n return {nums[1],nums[0]};\n }\n while (nums[p1]<0)\n p1++;\n while(nums[p2]>0)\n p2++;\n vector<int> v1;\n v1.push_back(nums[p1]);\n p1++;\n int choice =1;\n while (v1.size()<nums.size())\n {\n if (choice==1)\n {\n while(nums[p2]>0)\n p2++;\n v1.push_back(nums[p2]);p2++;\n choice =0;\n }\n else\n {\n choice =1;\n while (nums[p1]<0)\n p1++;\n v1.push_back(nums[p1]);p1++;\n }\n }\n return v1;\n\n }\n};\nauto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();", "memory": "134017" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"avx2\")\nstatic auto _ = [](){\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n inline int sign(int n) const {\n return (n > 0) - (n < 0);\n }\n\n std::vector<int> rearrangeArray(std::vector<int>& nums) {\n std::vector<int> result;\n\n std::size_t p = 0;\n std::size_t n = 0;\n while (result.size() != nums.size()) {\n while (sign(nums[p]) == -1) {\n ++p;\n }\n result.push_back(nums[p++]);\n\n while (sign(nums[n]) == 1) {\n ++n;\n }\n result.push_back(nums[n++]);\n }\n\n return result;\n\n /*\n std::vector<int> negatives;\n std::vector<int> positives;\n\n for (auto n : nums) {\n const int n_sign = (n > 0) - (n < 0);\n if (n_sign == 1) {\n positives.push_back(n);\n } else {\n negatives.push_back(n);\n }\n }\n\n \n for (std::size_t i = 0; i < positives.size(); ++i) {\n nums[i * 2] = positives[i];\n nums[i * 2 + 1] = negatives[i];\n }\n\n return nums;\n */\n }\n};", "memory": "134017" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "int speedUp = [] {\n std::ios::sync_with_stdio(0);\n std::cin.tie(0);\n return 0;\n}();\nclass Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n = nums.size();\n int res[n];\n vector<int> vres;\n int pos = 0, neg = 1;\n for(int i=0; i<n; i++) {\n if(nums[i] >= 0) { \n res[pos] = nums[i]; \n pos += 2;\n }\n else { \n res[neg] = nums[i]; \n neg += 2;\n }\n }\n for(auto i : res) vres.push_back(i);\n return vres;\n }\n};", "memory": "134252" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int size=nums.size();\n vector<int> pos(size);\n vector<int> neg(size);\n int p=0,n=0;\n for(int i=0;i<size;i++){\n if(nums[i]>=0){\n pos[p]=nums[i];\n p++;\n }\n else{\n neg[n]=nums[i];\n n++;\n }\n }\n p=0;\n n=0;\n for(int j=0;j<size;j++){\n if(j%2==0){\n nums[j]=pos[p];\n p++;\n }\n else{\n nums[j]=neg[n];\n n++;\n }\n }\n return nums;\n }\n};", "memory": "134487" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> nums1(nums.size());\n vector<int> nums2(nums.size());\n int k=0;\n int l=0;\n for(int i=0;i<nums.size();i++) {\n if(nums[i]<0) {\n nums1[k] = nums[i];\n k++;\n }\n if(nums[i]>=0) {\n nums2[l] = nums[i];\n l++;\n }\n }\n k=0;\n l=0;\n for(int i=0;i<nums.size();i++) {\n if(i%2==0) {\n nums[i] = nums2[k];\n k++;\n }\n else {\n nums[i] = nums1[l];\n l++;\n }\n }\n return nums;\n }\n};", "memory": "134722" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n auto neg = stable_partition(nums.begin(), nums.end(), [](int n){ return n > 0; });\n auto pos = nums.begin();\n vector<int> res;\n while (neg != nums.end()) {\n res.push_back(*pos++);\n res.push_back(*neg++);\n }\n return res;\n }\n};", "memory": "134957" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n=nums.size();\n vector<int> p(n,0);\n vector<int> n_(n,0);\n int n_c=0,p_c=0;\n for(int i=0;i<n;i++){\n if(nums[i]<0){\n n_[n_c]=nums[i];\n n_c++;\n }\n else{\n p[p_c]=nums[i];\n p_c++;\n }\n }\n p_c=0;\n n_c=0;\n for(int i=0;i<n;i++){\n if(i%2==0){\n nums[i]=p[p_c];\n p_c++;\n }\n else{\n nums[i]=n_[n_c];\n n_c++;\n }\n }\n return nums;\n }\n};", "memory": "135192" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\n // void helper(vector<int>& nums, vector<int> & ans){\n // int size = nums.size();\n // vector<int> visited(size, 0);\n \n // for(int k = 0 ; k < size; k++){\n \n // if(k % 2 == 0){\n // int j = 0;\n // for(int i = 0; i < size; i++){\n // if(nums[i] > 0 && !visited[i] ) {\n // j = i;\n // break;\n // }\n\n \n // }\n // ans[k]=nums[j];\n // visited[j]=1;\n \n // }\n // else { \n // int j = 0;\n // for(int i = 0; i < size; i++){\n // if(nums[i] < 0 && !visited[i] ) {\n // j = i;\n // break;\n // }\n\n \n // }\n // ans[k]=nums[j];\n // visited[j]=1;\n // }\n \n // }}\n\n // better solution for thiss question 2 O(N)\n\n void helper(vector<int>& nums ){\n int size = nums.size();\n vector<int> pos(size/2, 0);\n vector<int> neg(size/2, 0);\n int idx1=0;\n int idx2 =0;\n\n for(int i=0; i < size; i++ ){\n if(nums[i] < 0){\n neg[idx1] = nums[i];\n idx1++;\n }else {\n pos[idx2] = nums[i];\n idx2++;\n }\n } \n idx1 = 0;\n idx2 = 0;\n\n for(int i = 0; i < size ; i++ ){\n if(i % 2 == 0 ){\n nums[i] = pos[idx1++];\n\n }else {\n\n nums[i] = neg[idx2++];\n }\n \n }}\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> ans(nums.size(), 0);\n // helper(nums, ans);\n // return ans;\n helper( nums);\n return nums;\n }\n};", "memory": "135192" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int neg = 0;\n int pos = 0;\n int i = 0;\n int n = nums.size();\n\n // Find the first negative and positive indices\n while (nums[neg] >= 0) neg++;\n while (nums[pos] < 0) pos++;\n\n // Alternately push positive and negative numbers to the back\n for (int i = 0; i < n; i++) {\n if (i % 2 == 0) {\n nums.push_back(nums[pos]);\n pos++;\n while (pos < n && nums[pos] < 0) pos++; // Find the next positive number\n } else {\n nums.push_back(nums[neg]);\n neg++;\n while (neg < n && nums[neg] >= 0) neg++; // Find the next negative number\n }\n }\n\n // Erase the original first half of the array\n nums.erase(nums.begin(), nums.begin() + n);\n\n return nums;\n }\n};\n", "memory": "135427" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n vector<int> pos;\n vector<int> neg;\n int n = nums.size();\n for(int i = 0; i < n ;i++)\n {\n if(nums[i] < 0)\n {\n neg.push_back(nums[i]);\n }\n else\n {\n pos.push_back(nums[i]);\n }\n }\n for(int i = 0 ; i < n; i++)\n {\n int z=i/2;\n if(i%2 == 0)\n {\n nums[i] = pos[z];\n }\n else\n {\n nums[i] = neg[z];\n }\n }\n return nums;\n }\n};", "memory": "135662" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios_base::sync_with_stdio(0);\n vector<int>p;\n vector<int>n;\n for(int i=0;i<nums.size();i++){\n if(nums[i]<0) n.push_back(nums[i]);\n else if(nums[i]>0) p.push_back(nums[i]); \n } \n \n\n for(int i=0;i<p.size();i++){\n nums[2*i]=p[i];\n nums[2*i+1]=n[i];\n }\n \n return nums;\n }\n};", "memory": "135897" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution{\npublic:\n vector<int> rearrangeArray(vector<int>& nums){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n int itr = nums.size()/2;\n vector<int> p_vec, n_vec;\n for(int &num:nums){\n if(num<0){\n n_vec.push_back(num);\n }else{\n p_vec.push_back(num);\n }\n }\n nums.clear();\n for(int i = 0; i < itr; ++i){\n nums.push_back(p_vec[i]);\n nums.push_back(n_vec[i]);\n }\n return nums;\n }\n};", "memory": "136132" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n vector<int>vcp,vcn;\n int n=nums.size();\n for(int i=0;i<nums.size();i++){\n if(nums[i]<0)\n vcn.push_back(nums[i]);\n else\n vcp.push_back(nums[i]);\n }\n nums.clear();\n int i=0,j=0,f=0;\n while((i+j)<n){\n if(f==0 && i< vcp.size()){\n nums.push_back(vcp[i]);\n i++;\n f=1;\n if(j==vcn.size()){\n f=0;\n }\n }\n else if(f==1 && j<vcn.size()) {\n nums.push_back(vcn[j]);\n j++;\n f=0;\n if(i==vcp.size()){\n f=1;\n }\n }\n }\n return nums;\n }\n};", "memory": "136367" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> pos;\n vector<int> neg;\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] > 0) {\n pos.push_back(nums[i]);\n } else {\n neg.push_back(nums[i]);\n }\n }\n \n for (int i = 0; i < (nums.size())/2; i++) {\n nums[2*i]=pos[i];\n nums[2*i+1]=neg[i];\n }\n \n return nums;\n }\n};\n", "memory": "136367" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> pos;\n vector<int> neg;\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n\n // Separate the positive and negative numbers\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] > 0) {\n pos.push_back(nums[i]);\n } else {\n neg.push_back(nums[i]);\n }\n }\n \n // Simply alternate between positive and negative without assuming ne\n \n for (int i = 0; i < (nums.size())/2; i++) {\n nums[2*i]=pos[i];\n nums[2*i+1]=neg[i];\n }\n \n return nums;\n }\n};\n", "memory": "136602" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n=nums.size();\n vector<int> vp;\n vector<int> vn;\n for( auto x: nums){\n if(x>0) vp.push_back(x);\n else vn.push_back(x);\n }\n for(int i=0;i<n/2 ;i++){\n nums[2*i]=vp[i];\n nums[2*i+1]=vn[i];\n }\n return nums;\n\n }\n};", "memory": "136837" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>positive;\n vector<int>negative;\n for(int i =0; i< nums.size(); i++)\n {\n if(nums[i]>0)\n {\n positive.push_back(nums[i]);\n }\n else\n {\n negative.push_back(nums[i]);\n }\n }\n int i=0;\n int j=0;\n int p=0;\n int n=1;\n while(i<positive.size() && j <negative.size())\n {\n nums[p]=positive[i];\n nums[n]=negative[j];\n p+=2;\n n+=2;\n i++;\n j++;\n }\n return nums;\n }\n};", "memory": "136837" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> positive;\n vector<int> negative;\n for(int i=0; i<nums.size(); i++){\n if(nums[i] >= 0)\n positive.push_back(nums[i]);\n else\n negative.push_back(nums[i]);\n }\n\n int pindex=0, nindex=0;\n for (int i = 0; i < nums.size(); i++) {\n if (i % 2 == 0) \n nums[i] = positive[pindex++];\n else \n nums[i] = negative[nindex++];\n }\n return nums;\n }\n};", "memory": "137072" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>pos;\n vector<int>neg;\n for(int x = 0; x < nums.size(); x++)\n {\n if(nums[x] > 0)\n {\n pos.push_back(nums[x]);\n }\n else\n {\n neg.push_back(nums[x]);\n }\n }\n\n for(int x = 0; x < nums.size() / 2;x++)\n {\n nums[x * 2] = pos[x];\n nums[x * 2 + 1] = neg[x];\n }\n\n return nums;\n }\n};", "memory": "137072" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> positive;\n vector<int> negative;\n for(int i=0; i<nums.size(); i++){\n if(nums[i] >= 0)\n positive.push_back(nums[i]);\n else\n negative.push_back(nums[i]);\n }\n int j=0, pindex=0, nindex=0;\n // while(pindex < positive.size() && nindex < negative.size()){\n // if(j%2==0)\n // nums[j] = positive[pindex++];\n // else\n // {\n // nums[j] = negative[nindex++];\n // }\n // j++;\n // }\n for (int i = 0; i < nums.size(); i++) {\n if (i % 2 == 0) {\n nums[i] = positive[pindex++];\n } else {\n nums[i] = negative[nindex++];\n }\n }\n return nums;\n }\n};", "memory": "137307" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> pos;\n vector<int> neg;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]<0)\n {\n neg.push_back(nums[i]);\n }\n else{\n pos.push_back(nums[i]);\n }\n }\n int k=0;\n for(int i=0;i<nums.size();i++)\n { \n nums[i]=pos[k];\n k++;\n i++;\n }\n int l=0;\n for(int i=1;i<nums.size();i++)\n { \n nums[i]=neg[l];\n l++;\n i++;\n }\n return nums;\n }\n};", "memory": "137307" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& a) {\n\n int n = a.size();\n queue<int> pos, neg; // max 2 element will there at anytime in these queues\n queue<int> posIdx, negIdx; // max 2 element will there at anytime in these queues\n // Space Complexity - O(1)\n for(int i = 0; i < n; i++) {\n if(a[i] < 0) neg.push(a[i]);\n else pos.push(a[i]);\n if(i&1) {\n negIdx.push(i);\n }else {\n posIdx.push(i);\n }\n while(posIdx.size() && pos.size() && posIdx.size() > 2) {\n a[posIdx.front()] = pos.front();\n pos.pop();\n posIdx.pop();\n }\n while(negIdx.size() && neg.size() && negIdx.size() > 2) {\n a[negIdx.front()] = neg.front();\n neg.pop();\n negIdx.pop();\n }\n }\n while(posIdx.size() && pos.size()) {\n a[posIdx.front()] = pos.front();\n pos.pop();\n posIdx.pop();\n }\n while(negIdx.size() && neg.size()) {\n a[negIdx.front()] = neg.front();\n neg.pop();\n negIdx.pop();\n }\n return a;\n }\n};", "memory": "137542" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& a) {\n\n int n = a.size();\n queue<int> pos, neg; // max 2 element will there at anytime in these queues\n queue<int> posIdx, negIdx; // max 2 element will there at anytime in these queues\n // Space Complexity - O(1)\n for(int i = 0; i < n; i++) {\n if(a[i] < 0) neg.push(a[i]);\n else pos.push(a[i]);\n if(i&1) {\n negIdx.push(i);\n }else {\n posIdx.push(i);\n }\n while(posIdx.size() && pos.size() && posIdx.size() > 2) {\n a[posIdx.front()] = pos.front();\n pos.pop();\n posIdx.pop();\n }\n while(negIdx.size() && neg.size() && negIdx.size() > 2) {\n a[negIdx.front()] = neg.front();\n neg.pop();\n negIdx.pop();\n }\n }\n while(posIdx.size() && pos.size()) {\n a[posIdx.front()] = pos.front();\n pos.pop();\n posIdx.pop();\n }\n while(negIdx.size() && neg.size()) {\n a[negIdx.front()] = neg.front();\n neg.pop();\n negIdx.pop();\n }\n return a;\n }\n};", "memory": "137777" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> v;\n int j= 0;\n vector<int> negative;\n for(int i = 0;i<nums.size();i++){\n if(nums[i]<0) negative.push_back(nums[i]);\n }\n \n for(int i = 0;i<nums.size();i++){\n if(nums[i]>0){\n v.push_back(nums[i]);\n v.push_back(negative[j++]);\n }\n } \n return v;\n }\n};", "memory": "137777" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios_base::sync_with_stdio(0); cin.tie(0);\n queue<int> q1 , q2;\n vector<int> ans;\n for(auto &x: nums){\n if(x>0) q1.push(x);\n if(x<0) q2.push(x);\n }\n ans.push_back(q1.front());\n q1.pop();\n while(!q1.empty() && !q2.empty()){\n if(ans[ans.size()-1]<0){\n ans.push_back(q1.front());\n q1.pop();\n }\n if(ans[ans.size()-1]>0){\n ans.push_back(q2.front());\n q2.pop();\n }\n }\n while(!q1.empty()){\n ans.push_back(q1.front());\n q1.pop();\n }\n while(!q2.empty()){\n ans.push_back(q2.front());\n q2.pop();\n }\n return ans;\n }\n};", "memory": "138012" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>neg;\n int k=0;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]<0)\n {\n neg.push_back(nums[i]);\n k++;\n }\n }\n int j=0;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]>=0)\n {\n nums[j]=nums[i];\n j++;\n }\n }\n // nums.size()=j;\n for(int i=0;i<neg.size();i++)cout<<neg[i]<<\" \";\n for(int i=0;i<k;i++)cout<<nums[i]<<\" \";\n int i=0;\n int l=0;\n int m=0;\n vector<int> ans;\n while(i<j && l<neg.size())\n {\n if(i<j)\n ans.push_back(nums[i++]);\n if(l<neg.size())\n ans.push_back(neg[l++]);\n }\n while(i<j && m<nums.size())\n {\n nums[m++]=nums[i++];\n }\n while(l<neg.size() && m<nums.size())\n {\n nums[m++]=neg[l++];\n }\n return ans;\n }\n};", "memory": "138012" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> pos;\n vector<int> neg;\n int n = nums.size();\n vector<int> res(n);\n for(const int& it : nums){\n if(it >= 0)\n pos.push_back(it);\n else\n neg.push_back(it);\n }\n int npos = pos.size();\n int nneg = neg.size();\n int i = 0, j = 0, k = 0;\n while(i < npos and j < nneg){\n res[k++] = pos[i++];\n res[k++] = neg[j++];\n }\n for(;i < npos ;i++)\n res[k++] = pos[i];\n for(; j < nneg; j++)\n res[k++] = neg[j];\n\n return res;\n\n }\n};\n\nstatic const auto it = [](){\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();", "memory": "138247" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector <int> ans;\n stack <int> pos;\n stack <int> neg;\n for(int i= nums.size()-1; i>=0; --i) {\n if(nums[i]>0) pos.push(nums[i]);\n else neg.push(nums[i]);\n }\n while(neg.size()>0) {\n ans.push_back(pos.top());\n pos.pop();\n ans.push_back(neg.top());\n neg.pop();\n }\n return ans;\n }\n};", "memory": "138247" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector <int> ans;\n stack <int> pos;\n stack <int> neg;\n for(int i= nums.size()-1; i>=0; --i) {\n if(nums[i]>0) pos.push(nums[i]);\n else neg.push(nums[i]);\n }\n while(neg.size()>0) {\n ans.push_back(pos.top());\n pos.pop();\n ans.push_back(neg.top());\n neg.pop();\n }\n return ans;\n }\n};", "memory": "138482" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n queue<int> positives;\n queue<int> negatives;\n \n \n for (int num : nums) {\n if (num > 0) {\n positives.push(num); \n } else {\n negatives.push(num); \n }\n }\n \n \n vector<int> result;\n\n \n while (!positives.empty() && !negatives.empty()) {\n \n result.push_back(positives.front());\n positives.pop();\n\n \n result.push_back(negatives.front());\n negatives.pop();\n }\n\n return result;\n}\n};", "memory": "138482" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector <int> ans;\n stack <int> pos;\n stack <int> neg;\n for(int i= nums.size()-1; i>=0; --i) {\n if(nums[i]>0) pos.push(nums[i]);\n else neg.push(nums[i]);\n }\n while(neg.size()>0) {\n ans.push_back(pos.top());\n pos.pop();\n ans.push_back(neg.top());\n neg.pop();\n }\n return ans;\n }\n};", "memory": "138717" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>v;\n vector<int>n;\n int pos = 1;\n for(int i = 0; i < nums.size(); i++)\n {\n if(nums[i] > 0)\n {\n v.push_back(nums[i]);\n }\n else\n {\n n.push_back(nums[i]);\n }\n }\n //now the positives are in v and the negatives are in n\n\n for(int j = 0; j < n.size(); j++)\n {\n v.insert(v.begin() + pos, n[j]);\n pos += 2;\n }\n\n return v;\n }\n};", "memory": "138717" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>p,n;\n for(int x : nums){\n if(x>0) p.push_back(x);\n else n.push_back(x);\n }\n int pos=1;\n for(int x : n){\n p.insert(p.begin()+pos,x);\n pos += 2;\n }\n return p;\n }\n};", "memory": "138952" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> p, n;\n for(auto i:nums)\n if(i>=0)\n p.push_back(i);\n else\n n.push_back(i);\n int idx=1;\n for(auto i:n){\n p.insert(p.begin()+idx, i);\n idx+=2;\n }\n return p;\n }\n};", "memory": "139187" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "#include <chrono>\n\nauto init = []()\n{\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();\n\nclass Solution\n{\npublic:\n vector<int> rearrangeArray(vector<int> &nums)\n {\n queue<int> pos, neg;\n for (int i = 0; i < nums.size(); i++)\n {\n if (nums[i] < 0)\n {\n neg.push(nums[i]);\n }\n else\n {\n pos.push(nums[i]);\n }\n }\n int mode = 1;\n vector<int> ans;\n // mode=1 means pos and mode=0 means neg\n for (int i = 0; i < nums.size(); i++)\n {\n if (mode == 1)\n {\n int curr = pos.front();\n ans.push_back(curr);\n pos.pop();\n mode = 0;\n }\n else\n {\n int curr = neg.front();\n ans.push_back(curr);\n neg.pop();\n mode = 1;\n }\n }\n return ans;\n }\n};", "memory": "139187" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "#include <chrono>\n\nauto init = []()\n{\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();\n\nclass Solution\n{\npublic:\n vector<int> rearrangeArray(vector<int> &nums)\n {\n queue<int> pos, neg;\n for (int i = 0; i < nums.size(); i++)\n {\n if (nums[i] < 0)\n {\n neg.push(nums[i]);\n }\n else\n {\n pos.push(nums[i]);\n }\n }\n int mode = 1;\n vector<int> ans;\n // mode=1 means pos and mode=0 means neg\n while(!neg.empty()||!pos.empty())\n {\n if (mode == 1)\n {\n int curr = pos.front();\n ans.push_back(curr);\n pos.pop();\n mode = 0;\n }\n else\n {\n int curr = neg.front();\n ans.push_back(curr);\n neg.pop();\n mode = 1;\n }\n }\n return ans;\n }\n};", "memory": "139422" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> v1;\n vector<int> v2;\n\n for(int i = 0;i<nums.size();++i){\n\n if(nums[i]<0) v1.push_back(nums[i]);\n else v2.push_back(nums[i]);\n\n }\n\n vector<int> ans(nums.size());\n\n for(int i = 0;i<nums.size();i+=2){\n ans[i] = v2[i/2];\n ans[i+1] = v1[i/2];\n\n }\n\n return nums = ans;\n\n }\n};", "memory": "139657" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n = nums.size();\n vector<int> neg;\n vector<int> pos;\n vector<int> ans(n);\n for(int i = 0; i < n; i++){\n if(nums[i] > 0){\n pos.push_back(nums[i]);\n }\n else{\n neg.push_back(nums[i]);\n }\n }\n int l = 0;\n int m = 0;\n for(int i = 0; i < n; i++){\n if(i % 2 == 0){\n nums[i] = pos[l++];\n }\n if(i % 2 == 1){\n nums[i] = neg[m++];\n }\n }\n return nums;\n }\n};", "memory": "139892" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n \n vector<int> pos;\n vector<int> neg;\n\n for(int i=0 ; i<nums.size() ; i++){\n\n if(nums[i]<0){\n neg.push_back(nums[i]);\n }\n else{\n pos.push_back(nums[i]);\n }\n\n }\n\n int chance = 0;\n int p = 0, n = 0;\n\n vector<int> temp = nums;\n nums.clear();\n\n for(int i=0 ; i<temp.size() ; i++){\n\n if(chance == 0){\n nums.push_back(pos[p++]);\n chance = 1;\n }\n else{\n nums.push_back(neg[n++]);\n chance = 0;\n }\n\n }\n\n return nums;\n }\n};", "memory": "140127" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n \n vector<int> pos;\n vector<int> neg;\n\n for(int i=0 ; i<nums.size() ; i++){\n\n if(nums[i]<0){\n neg.push_back(nums[i]);\n }\n else{\n pos.push_back(nums[i]);\n }\n\n }\n\n int chance = 0;\n int p = 0, n = 0;\n\n vector<int> temp = nums;\n nums.clear();\n\n for(int i=0 ; i<temp.size() ; i++){\n\n if(chance == 0){\n nums.push_back(pos[p++]);\n chance = 1;\n }\n else{\n nums.push_back(neg[n++]);\n chance = 0;\n }\n\n }\n\n return nums;\n }\n};", "memory": "140127" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\n\n void merge(vector<int> &nums , vector<int> pos , vector<int> neg){\n int l1 = 0 ;\n int l2 = 0 ;\n\n bool val = false ;\n int i = 0 ;\n while(i<nums.size()){\n if(!val){\n nums[i] = pos[l1] ;\n i++ ;\n l1++ ;\n val = true ;\n }\n else{\n nums[i] = neg[l2] ;\n l2++ ;\n i++ ;\n val = false;\n }\n }\n }\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n = nums.size() ;\n vector<int> pos ;\n vector<int> neg ;\n\n\n for(int i = 0 ;i<n ;i++){\n if(nums[i]>0){\n pos.push_back(nums[i]) ;\n }\n else neg.push_back(nums[i]) ;\n }\n\n merge(nums,pos,neg) ;\n\n return nums;\n \n }\n};", "memory": "140362" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n=nums.size();\n vector<int> ans(n);\n int pos=0, neg=1;\n // equal no.of pos and neg\n // for(int i=0;i<n;i++)\n // {\n // if(nums[i]>0)\n // {\n // ans[pos]=nums[i];\n // pos=pos+2;\n // }\n // else\n // {\n // ans[neg]=nums[i];\n // neg=neg+2;\n // }\n // }\n // return ans;\n \n // unequal no.of pos and neg\n vector<int>poss, negi;\n for(int i=0;i<n;i++)\n {\n if(nums[i]>0)\n {\n poss.push_back(nums[i]);\n }\n else\n negi.push_back(nums[i]);\n }\n if(poss.size()>negi.size())\n {\n for(int i=0;i<negi.size();i++)\n {\n nums[i*2]=poss[i];\n nums[i*2+1]=negi[i];\n }\n int ind=negi.size()*2;\n for(int i=negi.size()*2;i<n;i++)\n {\n nums[ind]=poss[i];\n ind++;\n }\n }\n else\n {\n for(int i=0;i<poss.size();i++)\n {\n nums[i*2]=poss[i];\n nums[i*2+1]=negi[i];\n }\n int ind=poss.size()*2;\n for(int i=poss.size()*2;i<n;i++)\n {\n nums[ind]=negi[i];\n ind++;\n }\n }\n return nums;\n }\n};", "memory": "140597" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios_base::sync_with_stdio(false);\n vector<int>res1;\n vector<int>res2;\n vector<int>res;\n for(auto num:nums){\n if(num<0)res1.push_back(num);\n else res2.push_back(num);\n }\n int i=0,j=0;\n while(i<res1.size() && j<res2.size()){\n res.push_back(res2[i++]);\n res.push_back(res1[j++]);\n }\n return res;\n }\n};", "memory": "140832" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n // brute force\n\n vector<int> p;\n vector<int> n;\n\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] < 0) {\n n.push_back(nums[i]);\n } else {\n p.push_back(nums[i]);\n }\n }\n\n vector<int> ans;\n int i = 0;\n int ne = nums.size()/2;\n\n while (ne--) {\n ans.push_back(p[i]);\n ans.push_back(n[i]);\n i++;\n }\n\n return ans;\n }\n};", "memory": "142007" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> x={};\n vector<int> y={};\n for (int i=0;i<nums.size();i++){\n if (nums[i]>0){\n x.push_back(nums[i]);\n }\n else {\n y.push_back(nums[i]);\n }\n }\n vector<int> result={};\n for (int i=0;i<(nums.size())/2;i++){\n result.push_back(x[i]);\n result.push_back(y[i]);\n }\n return result;\n }\n};", "memory": "142007" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>pos;\n vector<int>neg;\n int n=nums.size();\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]>0)\n {\n pos.push_back(nums[i]);\n }\n else\n {\n neg.push_back(nums[i]);\n }\n }\n int i=0,j=0;\n int n1=pos.size();\n int n2=neg.size();\n vector<int>temp;\n while(i<n1 && j<n2)\n {\n temp.push_back(pos[i]);\n i++;\n temp.push_back(neg[j]);\n j++;\n }\n return temp;\n }\n};", "memory": "142242" }
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,-2,-5,2,-4] <strong>Output:</strong> [3,-2,1,-5,2,-4] <strong>Explanation:</strong> The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,1] <strong>Output:</strong> [1,-1] <strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul> <p>&nbsp;</p> It is not required to do the modifications in-place.
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n=nums.size();\n vector<int> p;\n vector<int> ne;\n for(int i=0;i<n;i++){\n if(nums[i]<0){\n ne.push_back(nums[i]);\n }\n else{\n p.push_back(nums[i]);\n }\n }\n vector<int> ans;\n for(int i=0;i<ne.size();i++){\n ans.push_back(p[i]);\n ans.push_back(ne[i]);\n }\n return ans;\n\n }\n};", "memory": "142242" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"Ofast\", \"inline\", \"unroll-loops\")\nclass Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n=pref.size();\n if(n==1) return pref;\n vector<int> arr(n);\n arr[0] = pref[0];\n for(int i=1;i<n;i++){\n arr[i] = pref[i]^pref[i-1];\n }\n return arr;\n }\n};\nauto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();", "memory": "78300" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n=pref.size();\n vector<int>result(n);\n result[0] = pref[0];\n for(int i=1;i<n;i++){\n result[i] = pref[i-1]^pref[i];\n } \n return result;\n }\n};\n/*\npref array se result nikalna hai \nphle alag space le ke krte hai \n*/", "memory": "78400" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n = pref.size(), xxor = pref[0];\n vector<int> ans(n, pref[0]);\n for(int i = 1; i < n; ++i){\n ans[i] = pref[i]^xxor;\n xxor ^= ans[i];\n }\n return ans;\n }\n};", "memory": "78400" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int size = pref.size();\n vector<int> ans(size);\n\n ans[0] = pref[0];\n for (int i = 1; i < size; i++) {\n ans[i] = pref[i] ^ pref[i - 1];\n }\n return ans;\n }\n};", "memory": "78500" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n vector<int> ans(pref.size(), 0);\n\n for (int i = 0; i < pref.size(); i++) {\n if (i == 0)\n ans[i] = pref[i];\n else {\n ans[i] = pref[i - 1] ^ pref[i];\n }\n }\n\n return ans;\n }\n};", "memory": "78500" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n = pref.size();\n if(pref.size() == 1) return pref;\n vector<int> ans(n);\n ans[0] = pref[0];\n for(int i=1;i<n;i++)\n {\n ans[i] = pref[i-1]^pref[i];\n }\n return ans;\n    }\n}; ", "memory": "78600" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int xr=pref[0];\n vector<int> a;\n int s=0;\n \n \n vector<int> ps(pref.size(),xr); \n for(int i=1;i<pref.size();i++){ \n ps[i]=xr^pref[i];\n xr=pref[i];\n }\n return ps;\n }\n};", "memory": "78600" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n = pref.size();\n for (int i = n - 1; i > 0; --i) {\n pref[i] ^= pref[i - 1];\n }\n return pref;\n }\n};\n ", "memory": "78700" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int temp = -1;\n int n = pref.size();\n vector<int> ans;\n for (int i = 0; i < n; i++) {\n if (i == 0) {\n temp = pref[i];\n pref[i]=temp;\n\n continue;\n } else {\n int res = temp ^ pref[i];\n temp = pref[i];\n pref[i]=res;\n }\n }\n return pref;\n }\n};", "memory": "78700" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n vector<int> res(pref.size());\n res[0] = pref[0];\n for (int i = 1; i < pref.size(); i++) {\n res[i] = pref[i] ^ pref[i - 1];\n }\n return res;\n }\n};", "memory": "78800" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n = pref.size(), curr = pref[0];\n vector<int> ans(n);\n ans[0] = pref[0];\n\n for(int i = 1; i < n; i++){\n int temp = pref[i] ^ curr;\n ans[i] = temp;\n curr = curr ^ temp;\n }\n\n return ans;\n }\n};", "memory": "78800" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n=pref.size();\n vector<int>ans(n);\n vector<int>sum(n);\n ans[0]=pref[0];\n sum[0]=pref[0];\n for(int i=1;i<n;i++)\n {\n ans[i]=sum[i-1]^pref[i];\n sum[i]=sum[i-1]^ans[i];\n }\n return ans;\n\n\n }\n};", "memory": "81000" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int a =0 ; vector<int>arr(pref.size()) ; \n arr[0]= pref[0] ; \n a= a^ pref[0] ; \n for(int i=1;i<pref.size();i++){\n \n arr[i]=pref[i]^pref[i-1] ;\n\n } vector<int>ans(pref.size()) ; \n ans[0]= arr[0] ; \n for(int i=1;i<arr.size();i++){\n ans[i]= arr[i]^arr[i-1] ; \n } return arr ; \n }\n};", "memory": "81100" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n ios::sync_with_stdio(0);\n vector<int> v;\n if(pref.empty())\n return {};\n \n v.push_back(pref[0]);\n for(int i=1; i<pref.size(); i++){\n v.push_back(pref[i-1]^pref[i]);\n }\n\n return v;\n }\n};", "memory": "81600" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n vector<int>arr;\n arr.push_back(pref[0]);\n for(int i=1;i<pref.size();i++){\n arr.push_back(pref[i-1]^pref[i]);\n }\n return arr;\n }\n};", "memory": "81700" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n = pref.size();\n\n vector<int> res;\n res.push_back(pref[0]);\n\n for (int i=1; i<n; i++)\n res.push_back(pref[i] ^ pref[i-1]);\n\n return res;\n }\n};", "memory": "81800" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n vector<int> arr;\n arr.push_back(pref[0]);\n for(int i=1;i<pref.size();i++){\n arr.push_back(pref[i]^pref[i-1]);\n }\n\n return arr;\n }\n};", "memory": "81800" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n vector<int>ans;\n ans.push_back(pref[0]);\n for(int i=0;i<pref.size()-1;i++){\n int bit=(pref[i]^pref[i+1]);\n ans.push_back(bit);\n }\n return ans;\n }\n};", "memory": "81900" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes the <strong>bitwise-xor</strong> operation.</p> <p>It can be proven that the answer is <strong>unique</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> pref = [5,2,0,3,1] <strong>Output:</strong> [5,7,2,3,2] <strong>Explanation:</strong> From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> pref = [13] <strong>Output:</strong> [13] <strong>Explanation:</strong> We have pref[0] = arr[0] = 13. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= pref.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= pref[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n vector<int> ret;\n ret.push_back(pref[0]);\n\n for (int i = 1; i < pref.size(); i++) {\n ret.push_back(pref[i-1] ^ pref[i]);\n }\n\n return ret;\n }\n};", "memory": "81900" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int stoneGameVII(vector<int>& stones) {\n for(int i = 1; i < size(stones); ++i) stones[i] += stones[i-1];\n stones.insert(stones.begin(), 0);\n\n int n = size(stones);\n int dp[n-1];\n memset(dp, 0, sizeof(dp));\n\n // base case:\n for(int i = 0; i < n-1; ++i) dp[i] = 0;\n\n // recursive:\n int aturn = n%2 != 0;\n for(int j = 2; j < n; ++j) {\n for(int tmpi = 1, tmpj = j; tmpj < n; ++tmpi, ++tmpj) {\n if(aturn) {\n int op1 = stones[tmpj] - stones[tmpi] + dp[tmpi];\n int op2 = stones[tmpj-1] - stones[tmpi-1] + dp[tmpi-1];\n dp[tmpi-1] = max(op1, op2);\n } else {\n int op1 = -(stones[tmpj] - stones[tmpi]) + dp[tmpi];\n int op2 = -(stones[tmpj-1] - stones[tmpi-1]) + dp[tmpi-1];\n dp[tmpi-1] = min(op1, op2);\n }\n }\n aturn = !aturn;\n }\n\n return dp[0];\n }\n};", "memory": "16786" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
0
{ "code": "#include <vector>\n\nclass Solution {\n public:\n int stoneGameVII(std::vector<int>& stones) {\n for (std::size_t i = 1; i < stones.size(); ++i) {\n stones[i] += stones[i - 1];\n }\n\n std::vector<int> diffs(stones.size(), 0);\n for (std::size_t i = 1; i < diffs.size(); ++i) {\n for (std::size_t j = 0; j < diffs.size() - i; ++j) {\n diffs[j] = std::max(\n stones[j + i] - stones[j] - diffs[j + 1],\n stones[j + i - 1] - (j > 0 ? stones[j - 1] : 0) - diffs[j]);\n }\n }\n\n return diffs[0];\n }\n};\n", "memory": "16786" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int n;\n int dp[1001][1001][2];\n vector<int> pre;\n int solve(int i, int j, bool flag, vector<int>& stones)\n {\n if(i>j)\n {\n return 0;\n }\n\n if(dp[i][j][flag]!=-1)\n {\n return dp[i][j][flag];\n }\n int diff;\n if(flag)\n {\n diff=-1;\n }\n else\n {\n diff=INT_MAX;\n }\n\n // int sum=0;\n // for(int idx=i;idx<=j;idx++)\n // {\n // sum+=stones[idx];\n // }\n\n int first = pre[j+1] - pre[i + 1];\n int last = pre[j] - pre[i];\n\n // Bsically finding Alice-Bob\n // Alices score in positive and Bobs score negative\n if(flag)\n {\n diff=max(diff,(first)+solve(i+1,j,0,stones));\n diff=max(diff,(last)+solve(i,j-1,0,stones));\n }\n else\n {\n diff=min(diff,-(first)+solve(i+1,j,1,stones));\n diff=min(diff,-(last)+solve(i,j-1,1,stones));\n }\n\n return dp[i][j][flag]=diff;\n }\n int stoneGameVII(vector<int>& stones)\n {\n n=stones.size();\n\n // Make a function which returns difference\n // Alice tries to maximize the difference\n // Bob tries to minimize the difference\n pre.resize(n+1);\n pre[0]=0;\n for(int i=1;i<=n;i++)\n {\n pre[i]=pre[i-1]+stones[i-1];\n }\n memset(dp,-1,sizeof(dp));\n return solve(0,n-1,true ,stones);\n }\n};", "memory": "25960" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n \n int dp[1002][1002][2] ; \n\n int rec(int l , int r , int ch , vector <int> &arr){\n if(l >= r){\n return 0 ; \n }\n if(dp[l][r][ch] != -1){\n return dp[l][r][ch] ; \n }\n int ans = -1e9 ; \n int ansx = 1e9 ; \n if(ch == 0){\n ans = max({ans , arr[r] - arr[l] + rec(l+1 , r , ch^1, arr) , arr[r-1] - arr[l-1] + rec(l , r-1 , ch^1 , arr) } ) ; \n }\n else if(ch == 1){\n ansx = min({ansx , -(arr[r] - arr[l]) + rec(l+1 , r , ch^1 , arr) , -(arr[r-1] - arr[l-1]) + rec(l , r-1 ,ch^1, arr) }) ; \n }\n\n if(ch == 1){\n return dp[l][r][ch] = ansx ; \n }\n return dp[l][r][ch] = ans ; \n }\n\n\n int stoneGameVII(vector<int>& arr) {\n int n = (int)arr.size() ; \n vector <int> pre(n+1 , 0) ; \n for(int i =1 ; i <= n ; i++){\n pre[i] = pre[i-1] + arr[i-1] ; \n }\n memset(dp , -1 , sizeof(dp)) ; \n int ans = rec(1 , n , 0 , pre) ; \n return ans ; \n\n }\n};", "memory": "25960" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
1
{ "code": "using ll = long long;\nusing pl = pair<ll, ll>;\nusing vl = vector<ll>;\nusing vvl = vector<vector<ll>>;\nusing vll = vector<pair<ll, ll>>;\n\n#define all(a) a.begin(), a.end()\n#define rall(a) a.rbegin(), a.rend()\n\n\nclass Solution {\npublic:\n\nint stoneGameVII(vector<int>& a) {\n\n ll n = a.size();vl pre(n+1,0);\n for(ll i=0;i<n;i++)pre[i+1] = (pre[i]+a[i]);ll dp[1008][1008][2];memset(dp,-1ll,sizeof dp);\n\n function<ll(ll,ll,ll)> f = [&](ll i,ll j,ll move){\n\n\n if(i>j)return 0ll;\n\n if(dp[i][j][move]!=-1)return dp[i][j][move];\n \n if(!move){\n ll res = max(f(i+1,j,move^1)+(pre[j+1]-pre[i+1]),f(i,j-1,move^1)+pre[j]-pre[i]);\n return dp[i][j][move] = res;\n }else{\n ll res = min(f(i+1,j,move^1)-(pre[j+1]-pre[i+1]),f(i,j-1,move^1)-(pre[j]-pre[i]));\n return dp[i][j][move] = res;\n }\n\n return 0ll;\n\n };\n\n return (int)f(0ll,n-1,0ll);\n \n}\n\n};", "memory": "35134" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
1
{ "code": "class Solution {\n static int solve(const std::vector<int>& stones, const int N, std::vector<int>& cache, const int l, const int r) {\n // ok, so the way the DP works, is that we are always entering as alice.\n const int SN = r - l + 1;\n\n // We pick the last and it's done\n if (SN == 1) {\n return 0;\n } else if (SN == 2) {\n // ok, for two we pick the smallest and bob ends up with the biggest\n return std::max(stones[l], stones[l+1]);\n }\n\n // Maybe adda special case for SN == 3?\n\n const int key = l*N + r;\n if (cache[key] != -1) {\n return cache[key];\n }\n\n\n const int leftChoice = std::min(\n stones[l+1] + solve(stones, N, cache, l+2, r),\n stones[r] + solve(stones, N, cache, l+1, r-1)\n );\n const int rightChoice = std::min(\n stones[r-1] + solve(stones, N, cache, l, r-2),\n stones[l] + solve(stones, N, cache, l+1, r-1)\n );\n\n\n const int ans = std::max(leftChoice, rightChoice);\n cache[key] = ans;\n return ans;\n }\npublic:\n static constexpr int stoneGameVII(const std::vector<int>& stones) {\n const int N = stones.size();\n\n std::vector<int> cache(N*N, -1);\n\n return solve(stones, N, cache, 0, N-1);\n }\n};", "memory": "35134" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
1
{ "code": "inline const auto optimize = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\n static constexpr int solve(const std::vector<int>& stones, const int N,\n std::vector<int>& cache, const int l, const int r) {\n // Maybe adda special case for SN == 3?\n\n const int key = l * N + r;\n if (cache[key] != -1) {\n return cache[key];\n }\n\n const int solve1 = solve(stones, N, cache, l + 2, r);\n const int solve2 = solve(stones, N, cache, l + 1, r - 1);\n const int solve3 = solve(stones, N, cache, l, r-2);\n\n \n const int leftChoice =\n std::min(stones[l + 1] + solve1,\n stones[r] + solve2);\n const int rightChoice =\n std::min(stones[r - 1] + solve3,\n stones[l] +solve2);\n\n const int ans = std::max(leftChoice, rightChoice);\n cache[key] = ans;\n return ans;\n }\n\npublic:\n static constexpr int stoneGameVII(const std::vector<int>& stones) {\n const int N = stones.size();\n\n std::vector<int> cache(N * N, -1);\n for (int i = 0; i < N; i++) {\n cache[i * N + i] = 0;\n }\n for (int i = 0; i < N - 1; i++) {\n cache[i * N + i + 1] = std::max(stones[i], stones[i + 1]);\n }\n\n return solve(stones, N, cache, 0, N - 1);\n }\n};", "memory": "44308" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n vector<vector<int>> dp(n, vector<int>(n, 0));\n for(int len = 1;len<n;len++)\n {\n for(int i=0;i+len<n;i++)\n {\n int j = i+len;\n int total = 0;\n for(int k=i;k<=j;k++) total += stones[k];\n dp[i][j] = max(dp[i][j], total - stones[i] - dp[i+1][j]);\n dp[i][j] = max(dp[i][j], total - stones[j] - dp[i][j-1]);\n }\n }\n return dp[0][n-1];\n }\n};", "memory": "53481" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int solve(vector<int>& stones, int start, int end, int totalSum,vector<vector<int>>&dp) {\n if (start >= end || totalSum<=0) return 0; \n if(dp[start][end]!=-1)return dp[start][end]; \n int scoreIfStart = totalSum - stones[start] - solve(stones, start + 1, end, totalSum - stones[start],dp);\n int scoreIfEnd = totalSum -stones[end] - solve(stones, start, end - 1, totalSum - stones[end],dp);\n \n return dp[start][end] = max(scoreIfStart,scoreIfEnd);\n }\n int stoneGameVII(vector<int>& stones) {\n int end = stones.size()-1;\n vector<vector<int>>dp(end+1,vector<int>(end+1,-1));\n int sum = accumulate(stones.begin(),stones.end(),0);\n return solve(stones,0,end,sum,dp);\n }\n};", "memory": "62655" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<vector<int>>dp;\n int helper(int i,int j,vector<int>& stones,int sum){\n \n if(i>j)return 0;\n if(dp[i][j]!=-1)return dp[i][j];\n int left=sum-stones[i] -helper(i+1,j,stones,sum-stones[i]);\n int right=sum-stones[j]-helper(i,j-1,stones,sum-stones[j]);\n\n return dp[i][j]=max(left,right);\n\n\n \n }\n int stoneGameVII(vector<int>& stones) {\n \n int sum=0;\n int n=stones.size();\n dp.resize(n,vector<int>(n,-1));\n for(auto & stone: stones){\n sum+=stone; }\n\n return helper(0,n-1,stones,sum);\n }\n};", "memory": "71829" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<vector<int>>dp;\n int helper(int i,int j,vector<int>& stones,int sum){\n \n if(i>j)return 0;\n if(dp[i][j]!=-1)return dp[i][j];\n int left=sum-stones[i] -helper(i+1,j,stones,sum-stones[i]);\n int right=sum-stones[j]-helper(i,j-1,stones,sum-stones[j]);\n\n return dp[i][j]=max(left,right);\n\n\n \n }\n int stoneGameVII(vector<int>& stones) {\n \n int sum=0;\n int n=stones.size();\n dp.resize(n,vector<int>(n,-1));\n for(auto & stone: stones){\n sum+=stone; }\n\n return helper(0,n-1,stones,sum);\n }\n};", "memory": "81003" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n vector<vector<int>> dp_alice ;\n vector<vector<int>> dp_bob ;\n\n int minimise(vector<int>& stones, int l, int r, int sum) {\n if ( l >= r ) return 0 ;\n if ( dp_bob[l][r] != -1 ) return dp_bob[l][r] ;\n if ( r-l == 1 ) return max(stones[l],stones[r]) ;\n int left = sum-stones[l]-maximise(stones,l+1,r,sum-stones[l]) ;\n int right = sum-stones[r]-maximise(stones,l,r-1,sum-stones[r]) ;\n dp_bob[l][r] = max(left,right) ;\n return dp_bob[l][r] ;\n }\n\n int maximise(vector<int>& stones, int l, int r, int sum) {\n if ( l >= r ) return 0 ;\n if ( dp_alice[l][r] != -1 ) return dp_alice[l][r] ;\n \n if ( r-l == 1 ) return max(stones[l],stones[r]) ;\n int left = sum-stones[l]-minimise(stones,l+1,r,sum-stones[l]) ;\n int right = sum-stones[r]-minimise(stones,l,r-1,sum-stones[r]) ;\n dp_alice[l][r] = max(left,right) ;\n return dp_alice[l][r] ;\n }\n\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size(), sum = 0 ;\n for (int n : stones) sum+= n ;\n dp_bob = dp_alice = vector<vector<int>>(n,vector<int>(n,-1)) ; \n return maximise(stones,0,n-1,sum) ; \n \n }\n};\n \n", "memory": "117698" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n vector<vector<int>> array(n, vector<int>(n, 0));\n vector<vector<int>> sum(n, vector<int>(n, 0));\n for (int i=0; i<n; i++) {\n //array[i][i] = stones[i];\n sum[i][i] = stones[i];\n }\n for (int i=n-1; i>=1; i--) {\n for (int j=0; j<i; j++)\n sum[j][j+n-i] = stones[j] + sum[j+1][j+n-i];\n }\n\n for (int i=n-1; i>=1; i--) {\n for (int j=0; j<i; j++) {\n array[j][j+n-i] = max(sum[j][j+n-i-1]-array[j][j+n-i-1], sum[j+1][j+n-i]-array[j+1][j+n-i]);\n }\n }\n\n return array[0][n-1];\n }\n};", "memory": "126871" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n pair<int,int> f(int i,int j,vector<vector<pair<int,int>>>&dp,vector<int>&pre){\n if(i>=j)return {0,0};\n if(dp[i][j].first!=-1)return dp[i][j];\n int first=INT_MIN;\n int second;\n first=f(i+1,j,dp,pre).second+pre[j]-pre[i];\n second=f(i+1,j,dp,pre).first;\n int curr=f(i,j-1,dp,pre).second+pre[j-1]-pre[i-1];\n int curz=f(i,j-1,dp,pre).first;\n if(curr-curz>first-second){\n first=curr;\n second=curz;\n }\n return dp[i][j]={first,second};\n }\n int stoneGameVII(vector<int>& stones) {\n int n=stones.size();\n vector<int>pre(n+1,0);\n int val=0;\n vector<vector<pair<int,int>>>dp(n+1,vector<pair<int,int>>(n+1,{-1,-1}));\n for(int i=0;i<n;i++){\n val+=stones[i];\n pre[i+1]=val;\n }\n // for(int i=0;i<(int)pre.size();i++)cout<<pre[i]<<\" \";\n // cout<<endl;\n pair<int,int>p=f(1,n,dp,pre);\n return p.first-p.second;\n }\n};", "memory": "136045" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int stoneGameVII(vector<int>& stones) {\n const int n = stones.size();\n\n vector<vector<int>> dp(n, vector<int>(n, 0));\n vector<vector<int>> sums(n, vector<int>(n, 0));\n\n int sum = 0;\n for (int i = 0; i < n; ++i) {\n for (int j = i; j < n; ++j) {\n sum += stones[j];\n sums[i][j] = sum;\n //cout << \"sums[\" << i << \"][\" << j << \"] = \" << sums[i][j] << endl;\n }\n sum = 0;\n }\n \n for (int len = 2; len <= n; ++len) {\n for (int i = 0; i < n - (len - 1); ++i) {\n int j = i + (len - 1);\n \n dp[i][j] = max(\n sums[i + 1][j] - dp[i + 1][j],\n sums[i][j - 1] - dp[i][j - 1]);\n\n //cout << \"dp[\" << i << \"][\" << j << \"] = \" << dp[i][j] << endl;\n }\n }\n\n return dp[0][n - 1];\n }\n};", "memory": "145219" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "#include <vector>\n#include <algorithm>\nusing namespace std;\n\nclass Solution {\npublic:\n vector<vector<int>> sum;\n vector<vector<int>> memo;\n\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n sum.resize(n, vector<int>(n, 0));\n memo.resize(n, vector<int>(n, -1));\n \n // Calculate the sum array\n for (int i = 0; i < n; ++i) {\n sum[i][i] = stones[i];\n for (int j = i + 1; j < n; ++j) {\n sum[i][j] = sum[i][j - 1] + stones[j];\n }\n }\n\n return stoneGameVIIUtil(0, n - 1, stones);\n }\n\n int stoneGameVIIUtil(int i, int j, vector<int>& stones) {\n if (i >= j) {\n return 0;\n }\n if (memo[i][j] != -1) {\n return memo[i][j];\n }\n \n int a = sum[i + 1][j] - stoneGameVIIUtil(i + 1, j, stones);\n int b = sum[i][j - 1] - stoneGameVIIUtil(i, j - 1, stones);\n\n memo[i][j] = max(a, b);\n return memo[i][j];\n }\n};\n", "memory": "154393" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "typedef long long int ll;\n\nclass Solution {\npublic:\n int stoneGameVII(vector<int>& arr) {\n \n ll n = arr.size();\n\n const ll INF = 1e15; \n\n vector<ll> csum(n, 0);\n\n vector< vector<ll> > dp( n, vector<ll>( n, INF));\n\n for( ll i=0, tot=0; i<n; i++) {\n tot += arr[i];\n csum[i] = tot;\n }\n\n auto getSum = [&]( ll l, ll r)->ll {\n if(l>r) {\n return 0;\n }\n return csum[r] - (l?csum[l-1]:0);\n };\n\n function<ll(ll,ll)> solve;\n\n solve = [&]( ll i, ll j)->ll {\n if(i>j) {\n return 0;\n }\n if(dp[i][j]==INF) {\n ll t = j-i+1;\n if((n-t)%2==0) {\n dp[i][j] = max(\n getSum( i+1, j) + solve( i+1, j),\n getSum( i, j-1) + solve( i, j-1)\n );\n } else {\n dp[i][j] = min(\n (-1)*getSum( i+1, j) + solve( i+1, j),\n (-1)*getSum( i, j-1) + solve( i, j-1)\n );\n }\n }\n return dp[i][j];\n };\n\n return solve( 0, n-1);\n }\n};", "memory": "163566" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp;\n vector<vector<int>> sum;\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n for(int i = 0; i < n; i++){\n dp.push_back(vector<int>(n, INT_MAX));\n sum.push_back(vector<int>(n));\n }\n for(int i = 0; i < n; i++){\n sum[i][i] = stones[i];\n for(int j = i+1; j < n; j++){\n sum[i][j] = sum[i][j-1] + stones[j];\n }\n }\n return recursion(0, n-1);\n }\n int recursion(int left, int right){\n if(left == right) return 0;\n if(dp[left][right] != INT_MAX) return dp[left][right];\n\n int take_right = sum[left][right-1] - recursion(left, right-1);\n int take_left = sum[left+1][right] - recursion(left+1, right);\n\n dp[left][right] = max(take_right, take_left);\n \n return dp[left][right];\n }\n};", "memory": "163566" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "// Idea: DP:\n// let f[p][i][j] be the result of subproblem A[i...j] when the active player is\n// p, p in [0,1].\n// then the DP/Bellman equation is:\n// f[p][i][j] = max of: sum(A[i+1 ... j]) - f[1-p][i+1][j],\n// sum(A[i ... j-1]) - f[1-p][i][j-1].\nclass Solution {\n constexpr static const int Inf = INT_MIN;\n\npublic:\n int stoneGameVII(vector<int>& stones) {\n const int n = stones.size();\n vector<vector<vector<int>>> f(\n 2, vector<vector<int>>(n, vector<int>(n, Inf)));\n vector<int> psum(n);\n partial_sum(stones.begin(), stones.end(), psum.begin());\n\n return solve(psum, f, 0, 0, n - 1);\n }\n\nprivate:\n static int solve(const vector<int>& psum, vector<vector<vector<int>>>& f,\n int player, int i, int j) {\n if (f[player][i][j] != Inf) { // look it up in memo\n return f[player][i][j];\n }\n if (i > j) {\n return 0;\n }\n if (i == j) {\n return f[player][i][j] = 0;\n }\n // sum(A[i...j]) = psum[j] - psum[i-1];\n return f[player][i][j] =\n max(psum[j] - psum[i] - solve(psum, f, 1 - player, i + 1, j),\n psum[j - 1] - (i - 1 >= 0 ? psum[i - 1] : 0) -\n solve(psum, f, 1 - player, i, j - 1));\n }\n};", "memory": "172740" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "// Idea: DP:\n// let f[p][i][j] be the result of subproblem A[i...j] when the active player is\n// p, p in [0,1].\n// then the DP/Bellman equation is:\n// f[p][i][j] = max of: sum(A[i+1 ... j]) - f[1-p][i+1][j],\n// sum(A[i ... j-1]) - f[1-p][i][j-1].\n// complexity: O(2 * 2 n^2) = O(n^2)\nclass Solution {\n constexpr static const int Inf = INT_MIN;\n\npublic:\n int stoneGameVII(vector<int>& stones) {\n const int n = stones.size();\n vector<vector<vector<int>>> f(\n 2, vector<vector<int>>(n, vector<int>(n, Inf)));\n vector<int> psum(n);\n partial_sum(stones.begin(), stones.end(), psum.begin());\n\n return solve(psum, f, 0, 0, n - 1);\n }\n\nprivate:\n static int solve(const vector<int>& psum, vector<vector<vector<int>>>& f,\n int player, int i, int j) {\n if (f[player][i][j] != Inf) { // look it up in memo\n return f[player][i][j];\n }\n if (i > j) {\n return 0;\n }\n if (i == j) {\n return f[player][i][j] = 0;\n }\n // sum(A[i...j]) = psum[j] - psum[i-1];\n return f[player][i][j] =\n max(psum[j] - psum[i] - solve(psum, f, 1 - player, i + 1, j),\n psum[j - 1] - (i - 1 >= 0 ? psum[i - 1] : 0) -\n solve(psum, f, 1 - player, i, j - 1));\n }\n};", "memory": "172740" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int dp(vector<int>& stones,int i,int j,int isbob,vector<vector<vector<int>>>&memo,int sum){\n if(i>j){\n return 0;\n }\n if(memo[isbob][i][j]!=-1){\n return memo[isbob][i][j];\n }\n int ans=(isbob?INT_MAX:INT_MIN);\n if(isbob){\n ans=min(dp(stones,i+1,j,0,memo,sum-stones[i])-(sum-stones[i]),dp(stones,i,j-1,0,memo,sum-stones[j])-(sum-stones[j]));\n }\n else{\n ans=max(dp(stones,i+1,j,1,memo,sum-stones[i])+(sum-stones[i]),dp(stones,i,j-1,1,memo,sum-stones[j])+(sum-stones[j]));\n }\n memo[isbob][i][j]=ans;\n return memo[isbob][i][j];\n }\n int stoneGameVII(vector<int>& stones) {\n int n=stones.size();\n vector<vector<vector<int>>> memo(2,vector<vector<int>>(n+1,vector<int>(n+1,-1)));\n \n int sum=0;\n for(int i=0;i<n;i++){\n sum+=stones[i];\n }\n return dp(stones,0,n-1,0,memo,sum);\n \n\n \n }\n};", "memory": "181914" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int n;\n int solve(vector<int>& stones,int start,int end,int player,vector<int>&presum,vector<vector<vector<int>>>&dp){\n if(start>=end)return 0;\n if(dp[player][start][end]!=-1)return dp[player][start][end];\n if(player==0){\n int p1=solve(stones,start+1,end,1,presum,dp)+presum[end]-presum[start];\n int p2=solve(stones,start,end-1,1,presum,dp)+presum[end-1]-presum[start]+stones[start];\n return dp[player][start][end]= max(p1,p2);\n }\n else{\n int p1=solve(stones,start+1,end,0,presum,dp)-(presum[end]-presum[start]);\n int p2=solve(stones,start,end-1,0,presum,dp)-(presum[end-1]-presum[start]+stones[start]);\n return dp[player][start][end]=min(p1,p2);\n }\n return 0;\n }\n int stoneGameVII(vector<int>& stones) {\n n=stones.size();\n vector<int>presum(n);\n vector<vector<vector<int>>>dp(2,vector<vector<int>>(n+1,vector<int>(n+1,-1)));\n presum[0]=stones[0];\n for(int i=1;i<n;i++)presum[i]=presum[i-1]+stones[i];\n for(auto it:presum)cout<<it<<\" \";\n return solve(stones,0,n-1,0,presum,dp);\n }\n // 5 8 9 13 15 \n};", "memory": "191088" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<int> &a, int i, int j, int sum, bool isAliceTurn, vector<vector<int>> &dp) {\n if (i > j) {\n return 0;\n }\n if (dp[i][j] != -1) {\n return dp[i][j];\n }\n if (isAliceTurn) {\n int op1 = solve(a, i + 1, j, sum - a[i], !isAliceTurn, dp) + (sum - a[i]);\n int op2 = solve(a, i, j - 1, sum - a[j], !isAliceTurn, dp) + (sum - a[j]);\n return dp[i][j] = max(op1, op2);\n } else {\n int op1 = solve(a, i + 1, j, sum - a[i], !isAliceTurn, dp) - (sum - a[i]);\n int op2 = solve(a, i, j - 1, sum - a[j], !isAliceTurn, dp) - (sum - a[j]);\n return dp[i][j] = min(op1, op2);\n }\n }\n \n int stoneGameVII(vector<int>& a) {\n int n = a.size();\n int sum = 0;\n for (auto i : a) {\n sum += i;\n }\n vector<vector<int>> dp(1001, vector<int>(1001, -1));\n return solve(a, 0, n - 1, sum, true, dp);\n }\n};\n", "memory": "200261" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>>dp;\n int solve(vector<int>& stones, int i, int j, int sum){\n if(i>=j){\n return 0;\n }\n if(sum<=0){\n return 0;\n }\n if(dp[i][j]!=-1){\n return dp[i][j];\n }\n int front = sum-stones[i]-solve(stones,i+1,j,sum-stones[i]);\n int back = sum-stones[j]-solve(stones,i,j-1,sum-stones[j]);\n \n int profit = max(front,back);\n return dp[i][j] = profit;\n }\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n int sum = 0;\n for(int i=0;i<n;i++){\n sum+=stones[i];\n }\n dp.clear();\n dp.resize(1001,vector<int>(1001,-1));\n \n int value = solve(stones,0,n-1,sum);\n return value;\n }\n};", "memory": "209435" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int stoneGameVII(vector<int>& stones) {\n vector<vector<int>> dp(1005, vector<int>(1005, 0));\n for(int i = stones.size() - 1; i >= 0; i--) {\n int sum = stones[i];\n for(int j = i + 1; j < stones.size(); j++) {\n sum += stones[j];\n // cout << sum << \" \" << i << \" \" << j << endl;\n dp[i][j] = max(dp[i][j], sum - stones[i] - dp[i + 1][j]);\n dp[i][j] = max(dp[i][j], sum - stones[j] - dp[i][j - 1]);\n }\n }\n return dp[0][stones.size() - 1];\n }\n};", "memory": "218609" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\n unordered_map<int, int> dp;\n\n int solver(int left, int right, bool player, int total, vector<int>& stones) {\n if (left == right) {\n return 0;\n } else if (dp.contains(left * 10000 + right * 10 + player)) {\n return dp[left * 10000 + right * 10 + player];\n }\n\n int best {};\n if (player) {\n best = max( solver(left + 1, right, !player, total - stones[left], stones) + total - stones[left], \n solver(left, right - 1, !player, total - stones[right], stones) + total - stones[right]);\n } else {\n best = min( solver(left + 1, right, !player, total - stones[left], stones) - total + stones[left], \n solver(left, right - 1, !player, total - stones[right], stones) - total + stones[right]);\n }\n\n return dp[left * 10000 + right * 10 + player] = best;\n }\n\n\n\npublic:\n int stoneGameVII(vector<int>& stones) {\n int total {accumulate(stones.begin(), stones.end(), 0)};\n return solver(0, stones.size() - 1, true, total, stones);\n }\n};", "memory": "227783" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\n unordered_map<int, int> dp;\n\n int solver(int left, int right, int total, vector<int>& stones) {\n if (left == right) {\n return 0;\n } else if (dp.contains(left * 1000 + right)) {\n return dp[left * 1000 + right];\n }\n\n int take_left {total - stones[left] - solver(left + 1, right, total - stones[left], stones)};\n int take_right {total - stones[right] - solver(left, right - 1, total - stones[right], stones)};\n\n return dp[left * 1000 + right] = max(take_left, take_right);\n }\n\npublic:\n int stoneGameVII(vector<int>& stones) {\n int total {accumulate(stones.begin(), stones.end(), 0)};\n return solver(0, stones.size() - 1, total, stones);\n }\n};", "memory": "236956" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n pair<int, int> dynamicthink(vector<vector<vector<pair<int,int>>>>& dp, int curr, int start, int end, int total, vector<int>& stones) {\n if (start >= end) {\n return {0, 0};\n }\n if (dp[start][end][curr].first != -1) {\n return dp[start][end][curr];\n }\n\n pair<int, int> removeStart = dynamicthink(dp, curr ^ 1, start + 1, end, total - stones[start], stones);\n pair<int, int> removeEnd = dynamicthink(dp, curr ^ 1, start, end - 1, total - stones[end], stones);\n\n if (curr == 0) {\n if (total - stones[start] + removeStart.first - removeStart.second > total - stones[end] + removeEnd.first - removeEnd.second) {\n dp[start][end][curr] = removeStart;\n dp[start][end][curr].first += total - stones[start];\n } else {\n dp[start][end][curr] = removeEnd;\n dp[start][end][curr].first += total - stones[end];\n }\n } else {\n if (abs(total - stones[start] + removeStart.second - removeStart.first) >= abs(total - stones[end] + removeEnd.second - removeEnd.first)) {\n dp[start][end][curr] = removeStart;\n dp[start][end][curr].second += total - stones[start];\n } else {\n dp[start][end][curr] = removeEnd;\n dp[start][end][curr].second += total - stones[end];\n }\n }\n\n return dp[start][end][curr];\n }\n\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n vector<vector<vector<pair<int,int>>>> dp(n, vector<vector<pair<int,int>>>(n, vector<pair<int,int>>(2, {-1, -1})));\n pair<int,int> a = dynamicthink(dp, 0, 0, n - 1, accumulate(stones.begin(), stones.end(), 0), stones);\n return (a.first - a.second);\n }\n};\n", "memory": "246130" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n vector<vector<vector<pair<int,int>>>> dp(n, vector<vector<pair<int,int>>>(n, vector<pair<int,int>>(2, {0, 0})));\n vector<int> prefixSum(n + 1, 0);\n \n // Compute prefix sum\n for (int i = 0; i < n; ++i) {\n prefixSum[i + 1] = prefixSum[i] + stones[i];\n }\n\n for (int length = 2; length <= n; ++length) {\n for (int i = 0; i + length - 1 < n; ++i) {\n int j = i + length - 1;\n int total = prefixSum[j + 1] - prefixSum[i]; // Total sum from i to j\n \n for (int k = 0; k < 2; ++k) {\n pair<int, int> a = dp[i + 1][j][k ^ 1];\n pair<int, int> b = dp[i][j - 1][k ^ 1];\n \n if (k == 0) { // Player 1's turn\n if (total - stones[i] + a.first - a.second >= total - stones[j] + b.first - b.second) {\n dp[i][j][k] = a;\n dp[i][j][k].first += total - stones[i];\n } else {\n dp[i][j][k] = b;\n dp[i][j][k].first += total - stones[j];\n }\n } else { // Player 2's turn\n if (abs(total - stones[i] + a.second - a.first) >= abs(total - stones[j] + b.second - b.first)) {\n dp[i][j][k] = a;\n dp[i][j][k].second += total - stones[i];\n } else {\n dp[i][j][k] = b;\n dp[i][j][k].second += total - stones[j];\n }\n }\n }\n }\n }\n\n return dp[0][n-1][0].first - dp[0][n-1][0].second;\n }\n};\n", "memory": "246130" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p> <p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p> <p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [5,3,1,4,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [7,90,5,1,100,10,10,2] <strong>Output:</strong> 122</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stones.length</code></li> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= stones[i] &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<vector<pair<int, int>>>> memo; \n\n int getSum(vector<int>& sums, int l, int r) {\n return sums[r] - ((l > 0) ? sums[l - 1] : 0);\n };\n\n pair<int, int> dfs(int l, int r, int turn, pair<int, int> score, vector<int>& sums) { \n if (l == r) \n return {0, 0};\n else if (memo[l][r][turn].first != -1)\n return memo[l][r][turn];\n\n pair<int, int> ret;\n\n if (turn == 0) { \n auto [aliceL, bobL] = dfs(l + 1, r, 1, score, sums);\n aliceL += getSum(sums, l + 1, r);\n\n auto [aliceR, bobR] = dfs(l, r - 1, 1, score, sums);\n aliceR += getSum(sums, l, r - 1);\n\n int takeLeft = aliceL - bobL; \n int takeRight = aliceR - bobR;\n\n if (takeLeft > takeRight)\n ret = {aliceL, bobL}; \n else \n ret = {aliceR, bobR};\n } else { \n auto [aliceL, bobL] = dfs(l + 1, r, 0, score, sums);\n bobL += getSum(sums, l + 1, r);\n\n auto [aliceR, bobR] = dfs(l, r - 1, 0, score, sums);\n bobR += getSum(sums, l, r - 1);\n\n int takeLeft = bobL - aliceL; \n int takeRight = bobR - aliceR;\n\n if (takeLeft > takeRight)\n ret = {aliceL, bobL}; \n else \n ret = {aliceR, bobR};\n }\n\n memo[l][r][turn] = ret;\n return ret;\n }\n\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n memo.resize(n, vector<vector<pair<int, int>>>(n, vector<pair<int, int>>(2, {-1, -1})));\n\n vector<int> sums(n, 0); \n sums[0] = stones[0];\n\n for (int i = 1; i < n; i++) \n sums[i] = sums[i - 1] + stones[i];\n\n auto [alice, bob] = dfs(0, n - 1, 0, {0, 0}, sums);\n return abs(alice - bob); \n }\n};", "memory": "255304" }