id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
2
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n int n = nums.size();\n if ((k % n) == 0) {\n return;\n }\n k = (k % n);\n bool reversed = false;\n if (k > n/2) {\n k = n - k;\n reversed = true;\n }\n vector<int> add(k);\n if (reversed) {\n for (int i = 0; i < k; i++) {\n add[i] = nums[i];\n }\n for (int i = k; i < n; i++) {\n nums[i-k] = nums[i];\n }\n for (int i = 0; i < k; i++) {\n nums[n-k+i] = add[i];\n }\n } else {\n for (int i = n-k; i < n; i++) {\n add[i-n+k] = nums[i];\n }\n for (int i = n-k-1; i >= 0; i--) {\n nums[i+k] = nums[i];\n }\n for (int i = 0; i < k; i++) {\n nums[i] = add[i];\n }\n }\n }\n};", "memory": "27900" }
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
2
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n int n = nums.size();\n k = k % n ; \n vector<int>temp(k);\n for( int i = 0 ; i< k ; i++){\n temp[i] = nums[n-k+i];\n }\n // shifiting \n for( int i = n-k-1 ; i >= 0; i--){\n nums[i+k] = nums[i];\n }\n // put back temp\n \n for( int i = 0 ; i< k ; i++ ){\n nums[i] = temp[i];\n \n }\n }\n};\n// time comp = O(3n)\n// spac comp = O(n)", "memory": "28000" }
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
2
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n // brute force approach\n int n = nums.size();\n k = k % n ; \n vector<int>temp(k);\n for( int i = 0 ; i< k ; i++){\n temp[i] = nums[n-k+i];\n }\n for( int i = n-k-1 ; i>=0; i--){\n nums[i+k] = nums[i];\n }\n for( int i = 0 ; i< k ; i++){\n nums[i] = temp[i];\n }\n }\n};\n// time comp = O(k)+O(n-k)+O(k) == O(n+k)\n// space comp = O(n)\n", "memory": "28000" }
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
2
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n \n /* reverse(nums.begin(),nums.begin()+k+1);\n reverse(nums.begin()+k+1,nums.end());\n reverse(nums.begin(),nums.end());*/\n int n=nums.size();\n vector<int>v(n);\n k=k%n;\n int j=0;\n for(int i=n-k;i<n;i++){\n v[j++]=nums[i];\n }\n for(int i=0;i<n-k;i++){\n v[j++]=nums[i];\n }\n for(int i=0;i<n;i++){\n nums[i]=v[i];\n }\n\n }\n};", "memory": "28100" }
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
2
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n \n int n = nums.size();\n vector<int> temp(n);\n \n \n for(int i =0;i<n;i++) {\n \n temp[(i+k)%n] = nums[i];\n \n }\n \n \n for(int i=0;i<n;i++) \n {\n nums[i] = temp[i];\n }\n \n }\n};", "memory": "28100" }
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
2
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n vector<int> rotated(nums.size());\n\n int n = nums.size();\n for (int i = 0; i < n; i++) {\n int j = (k + i) % n;\n rotated[j] = nums[i];\n }\n\n nums = rotated;\n }\n};", "memory": "28200" }
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
2
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n \n int n=nums.size();\n k=k%n;\n vector<int>temp;\n for(int i=n-k;i<n;i++){\n temp.push_back(nums[i]);\n }\n for(int i=n-k-1;i>=0;i--){\n nums[i+k]=nums[i];\n }\n int j=0;\n for(int i=0;i<k;i++){\n nums[i]=temp[j];\n j++;\n }\n \n }\n};", "memory": "28200" }
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
3
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n int n=nums.size(); k=k%n;\n vector<int>r(n);\n for(int i=0; i<n; i++){\n r[(i+k)%n]= nums[i];\n }\n for(int i=0; i<n; i++){\n nums[i]= r[i];\n }\n }\n};", "memory": "28300" }
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
3
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n vector<int> temp(nums.size());\n for(int i=0;i<nums.size();i++) {\n temp[(i+k)%nums.size()]=nums[i];\n }\n nums=temp;\n }\n};", "memory": "28300" }
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
3
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) \n {\n int n= nums.size();\n k= k%n;\n vector<int> temp= {};\n\n for(int i= n-k;i< n;i++)\n temp.push_back(nums[i]);\n \n for(int i= n-1;i>= k;i--)\n nums[i]= nums[i-k];\n \n for(int i= 0;i< k;i++)\n nums[i]= temp[i];\n\n return;\n }\n};", "memory": "28400" }
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
3
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int> temp;\n k=k%n;\n for(int i=n-k;i<n;i++)\n {\n temp.push_back(nums[i]);\n }\n for(int i=n-k-1;i>=0;i--)\n {\n nums[i+k]=nums[i];\n }\n for(int i=0;i<k;i++)\n {\n nums[i]=temp[i];\n }\n }\n};\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", "memory": "28400" }
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
3
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n k=k%nums.size();\n int sizeA = nums.size()-k-1;\n for(int i=0;i<=sizeA;i++){\n nums.push_back(nums[i]);\n }\n nums.erase(nums.begin(),nums.begin()+sizeA+1);\n }\n};", "memory": "28500" }
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
3
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) \n {\n k %= nums.size();\n vector<int> rotatedSection = {nums.begin() + (nums.size() - k), nums.end()};\n rotatedSection.insert(rotatedSection.end(), nums.begin(), nums.end() - k);\n nums = rotatedSection;\n }\n};", "memory": "28500" }
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
3
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n k = k % nums.size();\n std::vector<int> item(nums.end() - k, nums.end());\n nums.insert(nums.begin(), item.begin(), item.end());\n nums.erase(nums.end() - k, nums.end());\n }\n};", "memory": "28600" }
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
3
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n k = k % nums.size();\n // sol1 \n // vector<int> n1 = nums;\n // for (int i = 0; i < nums.size(); ++i) {\n // nums[(i + k) % nums.size()] = n1[i];\n // }\n\n // sol2\n // vector<int> v1(nums.begin(), nums.end() - k);\n // vector<int> v2(nums.begin() + nums.size() - k, nums.end());\n // v2.insert(v2.end(), v1.begin(), v1.end());\n // nums = v2;\n\n // sol3\n int n = nums.size();\n vector<int> v2(nums.begin() + nums.size() - k, nums.end());\n nums.insert(nums.begin(), v2.begin(), v2.end());\n nums.resize(n);\n\n // sol4\n // int idx = 0, preserve = nums[0];\n // for (int i = 0; i < nums.size(); ++i) {\n // int tmp = nums[(idx + k) % nums.size()];\n // nums[(idx + k) % nums.size()] = preserve;\n // idx = (idx + k) % nums.size();\n // preserve = tmp;\n // }\n }\n};", "memory": "28600" }
189
<p>Given an integer array <code>nums</code>, rotate the array to the right by <code>k</code> steps, where <code>k</code> is non-negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 3 <strong>Output:</strong> [5,6,7,1,2,3,4] <strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-1,-100,3,99], k = 2 <strong>Output:</strong> [3,99,-1,-100] <strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong></p> <ul> <li>Try to come up with as many solutions as you can. There are at least <strong>three</strong> different ways to solve this problem.</li> <li>Could you do it in-place with <code>O(1)</code> extra space?</li> </ul>
3
{ "code": "class Solution {\npublic:\n void rotate(vector<int>& nums, int k) {\n deque<int> temp;\n int n=nums.size();\n k = k % n;\n for(int i=0;i<n;i++)\n temp.push_back(nums[i]);\n for(int i=1;i<=k;i++)\n {\n temp.push_front(nums[n-i]);\n temp.pop_back();\n }\n for(int i=0;i<n;i++)\n nums[i]=temp[i];\n }\n};", "memory": "28700" }
190
<p>Reverse bits of a given 32 bits unsigned integer.</p> <p><strong>Note:</strong></p> <ul> <li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer&#39;s internal binary representation is the same, whether it is signed or unsigned.</li> <li>In Java, the compiler represents the signed integers using <a href="https://en.wikipedia.org/wiki/Two%27s_complement" target="_blank">2&#39;s complement notation</a>. Therefore, in <strong class="example">Example 2</strong> above, the input represents the signed integer <code>-3</code> and the output represents the signed integer <code>-1073741825</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 00000010100101000001111010011100 <strong>Output:</strong> 964176192 (00111001011110000010100101000000) <strong>Explanation: </strong>The input binary string <strong>00000010100101000001111010011100</strong> represents the unsigned integer 43261596, so return 964176192 which its binary representation is <strong>00111001011110000010100101000000</strong>. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 11111111111111111111111111111101 <strong>Output:</strong> 3221225471 (10111111111111111111111111111111) <strong>Explanation: </strong>The input binary string <strong>11111111111111111111111111111101</strong> represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is <strong>10111111111111111111111111111111</strong>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The input must be a <strong>binary string</strong> of length <code>32</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> If this function is called many times, how would you optimize it?</p>
0
{ "code": "class Solution \n{\npublic:\n uint32_t reverseBits(uint32_t n) \n {\n uint32_t res = 0;\n for (int i = 0; i < 32; i++)\n {\n res = (res << 1) | (n & 1);\n n >>= 1;\n }\n return res;\n }\n};", "memory": "7000" }
190
<p>Reverse bits of a given 32 bits unsigned integer.</p> <p><strong>Note:</strong></p> <ul> <li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer&#39;s internal binary representation is the same, whether it is signed or unsigned.</li> <li>In Java, the compiler represents the signed integers using <a href="https://en.wikipedia.org/wiki/Two%27s_complement" target="_blank">2&#39;s complement notation</a>. Therefore, in <strong class="example">Example 2</strong> above, the input represents the signed integer <code>-3</code> and the output represents the signed integer <code>-1073741825</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 00000010100101000001111010011100 <strong>Output:</strong> 964176192 (00111001011110000010100101000000) <strong>Explanation: </strong>The input binary string <strong>00000010100101000001111010011100</strong> represents the unsigned integer 43261596, so return 964176192 which its binary representation is <strong>00111001011110000010100101000000</strong>. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 11111111111111111111111111111101 <strong>Output:</strong> 3221225471 (10111111111111111111111111111111) <strong>Explanation: </strong>The input binary string <strong>11111111111111111111111111111101</strong> represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is <strong>10111111111111111111111111111111</strong>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The input must be a <strong>binary string</strong> of length <code>32</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> If this function is called many times, how would you optimize it?</p>
0
{ "code": "class Solution {\npublic:\n uint32_t reverseBits(uint32_t n) {\n uint32_t ans = 0;\n for (int i = 0; i < 32; i++) {\n ans <<= 1;\n ans |= (n & 1);\n n >>= 1;\n }\n return ans;\n }\n};", "memory": "7100" }
190
<p>Reverse bits of a given 32 bits unsigned integer.</p> <p><strong>Note:</strong></p> <ul> <li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer&#39;s internal binary representation is the same, whether it is signed or unsigned.</li> <li>In Java, the compiler represents the signed integers using <a href="https://en.wikipedia.org/wiki/Two%27s_complement" target="_blank">2&#39;s complement notation</a>. Therefore, in <strong class="example">Example 2</strong> above, the input represents the signed integer <code>-3</code> and the output represents the signed integer <code>-1073741825</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 00000010100101000001111010011100 <strong>Output:</strong> 964176192 (00111001011110000010100101000000) <strong>Explanation: </strong>The input binary string <strong>00000010100101000001111010011100</strong> represents the unsigned integer 43261596, so return 964176192 which its binary representation is <strong>00111001011110000010100101000000</strong>. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 11111111111111111111111111111101 <strong>Output:</strong> 3221225471 (10111111111111111111111111111111) <strong>Explanation: </strong>The input binary string <strong>11111111111111111111111111111101</strong> represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is <strong>10111111111111111111111111111111</strong>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The input must be a <strong>binary string</strong> of length <code>32</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> If this function is called many times, how would you optimize it?</p>
0
{ "code": "class Solution {\npublic:\n uint32_t reverseBits(uint32_t n) {\n uint32_t ans = 0;\n for (int i = 0; i < 32; i++) {\n ans <<= 1;\n ans |= (n & 1);\n n >>= 1;\n }\n return ans;\n }\n};", "memory": "7100" }
190
<p>Reverse bits of a given 32 bits unsigned integer.</p> <p><strong>Note:</strong></p> <ul> <li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer&#39;s internal binary representation is the same, whether it is signed or unsigned.</li> <li>In Java, the compiler represents the signed integers using <a href="https://en.wikipedia.org/wiki/Two%27s_complement" target="_blank">2&#39;s complement notation</a>. Therefore, in <strong class="example">Example 2</strong> above, the input represents the signed integer <code>-3</code> and the output represents the signed integer <code>-1073741825</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 00000010100101000001111010011100 <strong>Output:</strong> 964176192 (00111001011110000010100101000000) <strong>Explanation: </strong>The input binary string <strong>00000010100101000001111010011100</strong> represents the unsigned integer 43261596, so return 964176192 which its binary representation is <strong>00111001011110000010100101000000</strong>. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 11111111111111111111111111111101 <strong>Output:</strong> 3221225471 (10111111111111111111111111111111) <strong>Explanation: </strong>The input binary string <strong>11111111111111111111111111111101</strong> represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is <strong>10111111111111111111111111111111</strong>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The input must be a <strong>binary string</strong> of length <code>32</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> If this function is called many times, how would you optimize it?</p>
0
{ "code": "class Solution {\npublic:\n uint32_t reverseBits(uint32_t n) {\n uint32_t ans = 0;\n int bit_mask;\n for (int j = 31; j >= 0; j--) {\n bit_mask = (n&1)<<j;\n ans = ans | bit_mask;\n n >>= 1;\n }\n\n return ans;\n }\n};", "memory": "7200" }
190
<p>Reverse bits of a given 32 bits unsigned integer.</p> <p><strong>Note:</strong></p> <ul> <li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer&#39;s internal binary representation is the same, whether it is signed or unsigned.</li> <li>In Java, the compiler represents the signed integers using <a href="https://en.wikipedia.org/wiki/Two%27s_complement" target="_blank">2&#39;s complement notation</a>. Therefore, in <strong class="example">Example 2</strong> above, the input represents the signed integer <code>-3</code> and the output represents the signed integer <code>-1073741825</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 00000010100101000001111010011100 <strong>Output:</strong> 964176192 (00111001011110000010100101000000) <strong>Explanation: </strong>The input binary string <strong>00000010100101000001111010011100</strong> represents the unsigned integer 43261596, so return 964176192 which its binary representation is <strong>00111001011110000010100101000000</strong>. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 11111111111111111111111111111101 <strong>Output:</strong> 3221225471 (10111111111111111111111111111111) <strong>Explanation: </strong>The input binary string <strong>11111111111111111111111111111101</strong> represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is <strong>10111111111111111111111111111111</strong>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The input must be a <strong>binary string</strong> of length <code>32</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> If this function is called many times, how would you optimize it?</p>
0
{ "code": "class Solution {\npublic:\n uint32_t reverseBits(uint32_t n) {\n uint32_t ans = 0;\n int bit_mask;\n for (int j = 31; j >= 0; j--) {\n bit_mask = (n&1)<<j;\n ans = ans | bit_mask;\n n >>= 1;\n }\n\n return ans;\n }\n};", "memory": "7200" }
190
<p>Reverse bits of a given 32 bits unsigned integer.</p> <p><strong>Note:</strong></p> <ul> <li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer&#39;s internal binary representation is the same, whether it is signed or unsigned.</li> <li>In Java, the compiler represents the signed integers using <a href="https://en.wikipedia.org/wiki/Two%27s_complement" target="_blank">2&#39;s complement notation</a>. Therefore, in <strong class="example">Example 2</strong> above, the input represents the signed integer <code>-3</code> and the output represents the signed integer <code>-1073741825</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 00000010100101000001111010011100 <strong>Output:</strong> 964176192 (00111001011110000010100101000000) <strong>Explanation: </strong>The input binary string <strong>00000010100101000001111010011100</strong> represents the unsigned integer 43261596, so return 964176192 which its binary representation is <strong>00111001011110000010100101000000</strong>. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 11111111111111111111111111111101 <strong>Output:</strong> 3221225471 (10111111111111111111111111111111) <strong>Explanation: </strong>The input binary string <strong>11111111111111111111111111111101</strong> represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is <strong>10111111111111111111111111111111</strong>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The input must be a <strong>binary string</strong> of length <code>32</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> If this function is called many times, how would you optimize it?</p>
0
{ "code": "class Solution {\npublic:\n uint32_t reverseBits(uint32_t n)\n {\n uint32_t result{};\n\n int pos = 31;\n while (n > 0)\n {\n int d = n % 2;\n\n if (d == 1)\n {\n result += (1 << pos);\n }\n\n pos--; \n\n n = n / 2;\n }\n return result;\n }\n};\n\n\n\n", "memory": "7300" }
190
<p>Reverse bits of a given 32 bits unsigned integer.</p> <p><strong>Note:</strong></p> <ul> <li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer&#39;s internal binary representation is the same, whether it is signed or unsigned.</li> <li>In Java, the compiler represents the signed integers using <a href="https://en.wikipedia.org/wiki/Two%27s_complement" target="_blank">2&#39;s complement notation</a>. Therefore, in <strong class="example">Example 2</strong> above, the input represents the signed integer <code>-3</code> and the output represents the signed integer <code>-1073741825</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 00000010100101000001111010011100 <strong>Output:</strong> 964176192 (00111001011110000010100101000000) <strong>Explanation: </strong>The input binary string <strong>00000010100101000001111010011100</strong> represents the unsigned integer 43261596, so return 964176192 which its binary representation is <strong>00111001011110000010100101000000</strong>. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 11111111111111111111111111111101 <strong>Output:</strong> 3221225471 (10111111111111111111111111111111) <strong>Explanation: </strong>The input binary string <strong>11111111111111111111111111111101</strong> represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is <strong>10111111111111111111111111111111</strong>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The input must be a <strong>binary string</strong> of length <code>32</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> If this function is called many times, how would you optimize it?</p>
0
{ "code": "class Solution {\npublic:\n uint32_t reverseBits(uint32_t n) {\n uint32_t ans = 0;\n int bit, bit_mask;\n for (int i = 1, j = 31; i <= 32; i++, j--) {\n bit = n&1;\n bit_mask = bit<<j;\n ans = ans | bit_mask;\n n >>= 1;\n }\n\n return ans;\n }\n};", "memory": "7300" }
190
<p>Reverse bits of a given 32 bits unsigned integer.</p> <p><strong>Note:</strong></p> <ul> <li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer&#39;s internal binary representation is the same, whether it is signed or unsigned.</li> <li>In Java, the compiler represents the signed integers using <a href="https://en.wikipedia.org/wiki/Two%27s_complement" target="_blank">2&#39;s complement notation</a>. Therefore, in <strong class="example">Example 2</strong> above, the input represents the signed integer <code>-3</code> and the output represents the signed integer <code>-1073741825</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 00000010100101000001111010011100 <strong>Output:</strong> 964176192 (00111001011110000010100101000000) <strong>Explanation: </strong>The input binary string <strong>00000010100101000001111010011100</strong> represents the unsigned integer 43261596, so return 964176192 which its binary representation is <strong>00111001011110000010100101000000</strong>. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 11111111111111111111111111111101 <strong>Output:</strong> 3221225471 (10111111111111111111111111111111) <strong>Explanation: </strong>The input binary string <strong>11111111111111111111111111111101</strong> represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is <strong>10111111111111111111111111111111</strong>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The input must be a <strong>binary string</strong> of length <code>32</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> If this function is called many times, how would you optimize it?</p>
1
{ "code": "class Solution {\npublic:\n uint32_t reverseBits(uint32_t n)\n {\n for (uint32_t i = 0; i < 16; ++i)\n {\n uint32_t a = 1<<i & n, b = 1<<(32-i-1) & n;\n if (a)\n n |= 1<<(32-i-1);\n else\n n &= ~(1<<(32-i-1));\n if (b)\n n |= 1<<i;\n else\n n &= ~(1<<i);\n }\n\n return n;\n }\n};", "memory": "7400" }
190
<p>Reverse bits of a given 32 bits unsigned integer.</p> <p><strong>Note:</strong></p> <ul> <li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer&#39;s internal binary representation is the same, whether it is signed or unsigned.</li> <li>In Java, the compiler represents the signed integers using <a href="https://en.wikipedia.org/wiki/Two%27s_complement" target="_blank">2&#39;s complement notation</a>. Therefore, in <strong class="example">Example 2</strong> above, the input represents the signed integer <code>-3</code> and the output represents the signed integer <code>-1073741825</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 00000010100101000001111010011100 <strong>Output:</strong> 964176192 (00111001011110000010100101000000) <strong>Explanation: </strong>The input binary string <strong>00000010100101000001111010011100</strong> represents the unsigned integer 43261596, so return 964176192 which its binary representation is <strong>00111001011110000010100101000000</strong>. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 11111111111111111111111111111101 <strong>Output:</strong> 3221225471 (10111111111111111111111111111111) <strong>Explanation: </strong>The input binary string <strong>11111111111111111111111111111101</strong> represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is <strong>10111111111111111111111111111111</strong>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The input must be a <strong>binary string</strong> of length <code>32</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> If this function is called many times, how would you optimize it?</p>
1
{ "code": "class Solution {\npublic:\n uint32_t reverseBits(uint32_t n) {\n uint32_t ans = 0;\n for(int i = 0; i<32; i++)\n {\n cout<<ans<<\" \";\n ans <<= 1;\n if(n&1)\n {\n ans+=1;\n }\n n>>=1;\n }\n return ans;\n }\n};", "memory": "7400" }
190
<p>Reverse bits of a given 32 bits unsigned integer.</p> <p><strong>Note:</strong></p> <ul> <li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer&#39;s internal binary representation is the same, whether it is signed or unsigned.</li> <li>In Java, the compiler represents the signed integers using <a href="https://en.wikipedia.org/wiki/Two%27s_complement" target="_blank">2&#39;s complement notation</a>. Therefore, in <strong class="example">Example 2</strong> above, the input represents the signed integer <code>-3</code> and the output represents the signed integer <code>-1073741825</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 00000010100101000001111010011100 <strong>Output:</strong> 964176192 (00111001011110000010100101000000) <strong>Explanation: </strong>The input binary string <strong>00000010100101000001111010011100</strong> represents the unsigned integer 43261596, so return 964176192 which its binary representation is <strong>00111001011110000010100101000000</strong>. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 11111111111111111111111111111101 <strong>Output:</strong> 3221225471 (10111111111111111111111111111111) <strong>Explanation: </strong>The input binary string <strong>11111111111111111111111111111101</strong> represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is <strong>10111111111111111111111111111111</strong>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The input must be a <strong>binary string</strong> of length <code>32</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> If this function is called many times, how would you optimize it?</p>
3
{ "code": "class Solution {\npublic:\n uint32_t reverseBits(uint32_t n) {\n uint32_t ans = 0;\n int bit_mask;\n for (int j = 31; j >= 0; j--) {\n bit_mask = (n&1)<<j;\n ans = ans | bit_mask;\n n >>= 1;\n }\n\n return ans;\n }\n};", "memory": "7500" }
190
<p>Reverse bits of a given 32 bits unsigned integer.</p> <p><strong>Note:</strong></p> <ul> <li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer&#39;s internal binary representation is the same, whether it is signed or unsigned.</li> <li>In Java, the compiler represents the signed integers using <a href="https://en.wikipedia.org/wiki/Two%27s_complement" target="_blank">2&#39;s complement notation</a>. Therefore, in <strong class="example">Example 2</strong> above, the input represents the signed integer <code>-3</code> and the output represents the signed integer <code>-1073741825</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 00000010100101000001111010011100 <strong>Output:</strong> 964176192 (00111001011110000010100101000000) <strong>Explanation: </strong>The input binary string <strong>00000010100101000001111010011100</strong> represents the unsigned integer 43261596, so return 964176192 which its binary representation is <strong>00111001011110000010100101000000</strong>. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 11111111111111111111111111111101 <strong>Output:</strong> 3221225471 (10111111111111111111111111111111) <strong>Explanation: </strong>The input binary string <strong>11111111111111111111111111111101</strong> represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is <strong>10111111111111111111111111111111</strong>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The input must be a <strong>binary string</strong> of length <code>32</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> If this function is called many times, how would you optimize it?</p>
3
{ "code": "class Solution {\npublic:\n uint32_t reverseBits(uint32_t n) {\n uint32_t rst=0,c_bit,shift=31;\n\n while(n){\n c_bit = n & 1;\n rst|= c_bit<<shift;\n shift--;\n n>>=1;\n }\n return rst;\n }\n};", "memory": "7500" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(const vector<vector<int>>& points) {\n int M = (int) points.size(), N = (int) points[0].size();\n vector<long long> dp(points[0].begin(), points[0].end()),\n leftMax(N);\n long long ans = *max_element(dp.begin(), dp.end());\n for(int i = 1; i < M; i++) {\n leftMax[0] = dp[0];\n for(int j = 1; j < N; j++) {\n leftMax[j] = max(leftMax[j-1] - 1, dp[j]);\n }\n\n// Previous code:\n // for(int j = N-1; j --> 0;) {\n // rightMax[j] = max(rightMax[j+1] - 1, (long long) dp[j]);\n // }\n // for(int j = N; j --> 0;) {\n // ans = max(ans, dp[j] = points[i][j] + max(leftMax[j], rightMax[j]));\n // }\n// More optimized:\n\n long long prevRightMax = dp[N-1];\n ans = max(ans, dp[N-1] = points[i][N-1] + max(leftMax[N-1], prevRightMax));\n for(int j = N-1; j --> 0;) {\n prevRightMax = max(prevRightMax - 1, dp[j]);\n ans = max(ans, dp[j] = points[i][j] + max(leftMax[j], prevRightMax));\n }\n }\n return ans;\n }\n};\nauto init = ([]() -> bool {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n ofstream out(\"user.out\");\n Solution sol;\n for(string s; getline(cin,s); out << '\\n') {\n // parse s into vector<vector<int>>\n cout << s << '\\n';\n vector<vector<int>> points;\n size_t i = 2;\n size_t N = 0;\n while(i < s.length()) {\n points.push_back({});\n int curr = 0;\n vector<int>& ref = points.back();\n if(N > 0) ref.reserve(N);\n while(true) {\n if('0' <= s[i] && s[i] <= '9') {\n curr = 10*curr+(s[i]-'0');\n i++;\n }else {\n if(s[i] == ',') {\n ref.push_back(curr);\n curr = 0;\n i++;\n }else {\n // end\n ref.push_back(curr);\n break;\n }\n }\n }\n if(N == 0) N = ref.size();\n i += 3;\n }\n out << sol.maxPoints(points);\n }\n out.flush();\n exit(0);\n return true;\n})();", "memory": "32043" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(const vector<vector<int>>& points) {\n int M = (int) points.size(), N = (int) points[0].size();\n vector<long long> dp(points[0].begin(), points[0].end()),\n leftMax(N);\n long long ans = *max_element(dp.begin(), dp.end());\n for(int i = 1; i < M; i++) {\n leftMax[0] = dp[0];\n for(int j = 1; j < N; j++) {\n leftMax[j] = max(leftMax[j-1] - 1, dp[j]);\n }\n\n// Previous code:\n // for(int j = N-1; j --> 0;) {\n // rightMax[j] = max(rightMax[j+1] - 1, (long long) dp[j]);\n // }\n // for(int j = N; j --> 0;) {\n // ans = max(ans, dp[j] = points[i][j] + max(leftMax[j], rightMax[j]));\n // }\n// More optimized:\n\n long long prevRightMax = dp[N-1];\n ans = max(ans, dp[N-1] = points[i][N-1] + max(leftMax[N-1], prevRightMax));\n for(int j = N-1; j --> 0;) {\n prevRightMax = max(prevRightMax - 1, dp[j]);\n ans = max(ans, dp[j] = points[i][j] + max(leftMax[j], prevRightMax));\n }\n }\n return ans;\n }\n};\nauto init = ([]() -> bool {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n ofstream out(\"user.out\");\n Solution sol;\n for(string s; getline(cin,s); out << '\\n') {\n // parse s into vector<vector<int>>\n cout << s << '\\n';\n vector<vector<int>> points;\n size_t i = 2;\n size_t N = 0;\n while(i < s.length()) {\n points.push_back({});\n int curr = 0;\n vector<int>& ref = points.back();\n if(N > 0) ref.reserve(N);\n while(s[i] != ']') {\n if('0' <= s[i] && s[i] <= '9') {\n curr = 10*curr+(s[i]-'0');\n i++;\n }else {\n ref.push_back(curr);\n curr = 0;\n i++;\n }\n }\n ref.push_back(curr);\n if(N == 0) N = ref.size();\n i += 3;\n }\n out << sol.maxPoints(points);\n }\n out.flush();\n exit(0);\n return true;\n})();", "memory": "32043" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "bool y() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n ofstream out(\"user.out\");\n long long leftMax[100000];\n string s;\n while(getline(cin,s)) {\n // parse s into vector<vector<int>>\n int i = 2, M = 1, N = 0, SL = (int) s.length();\n long long ans = 0;\n vector<vector<long long>> points;\n {\n long long curr = 0;\n points.push_back({});\n while(s[i] != ']') {\n if('0' <= s[i] && s[i] <= '9') {\n curr = 10*curr+(s[i]-'0');\n }else {\n points[0].push_back(curr);\n if(curr > ans) ans = curr;\n curr = 0;\n N++;\n }\n i++;\n }\n points[0].push_back(curr);\n N++;\n if(curr > ans) ans = curr;\n i += 3;\n }\n while(i < SL) {\n int X = 0;\n long long curr = 0;\n points.push_back(vector<long long>(N));\n while(s[i] != ']') {\n if('0' <= s[i] && s[i] <= '9') {\n curr = 10*curr+(s[i]-'0');\n }else {\n points[M][X++] = curr;\n curr = 0;\n }\n i++;\n }\n points[M][X] = curr;\n i += 3;\n M++;\n }\n // maxPoints()\n for(int i = 1; i < M; i++) { // points[0] acts as dp\n leftMax[0] = points[0][0];\n for(int j = 1; j < N; j++) {\n leftMax[j] = max(leftMax[j-1] - 1, points[0][j]);\n }\n long long prevRightMax = points[0][N-1];\n ans = max(ans, points[0][N-1] = points[i][N-1] + max(leftMax[N-1], prevRightMax));\n for(int j = N-1; j --> 0;) {\n prevRightMax = max(prevRightMax - 1, points[0][j]);\n ans = max(ans, points[0][j] = points[i][j] + max(leftMax[j], prevRightMax));\n }\n }\n out << ans << '\\n';\n }\n out.flush();\n exit(0);\n return 'a';\n};\nbool x = y();\nstruct Solution {\n long long maxPoints(const vector<vector<int>>& points) {return 0ll;}\n};", "memory": "33331" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "bool y = ([](){\n ios::sync_with_stdio(0);\n cin.tie(0);\n ofstream out(\"user.out\");\n long long leftMax[100000];\n string s;\n while(getline(cin,s)) {\n // parse s into vector<vector<int>>\n int i = 2, M = 1, N = 0, SL = (int) s.length();\n long long ans = 0;\n vector<vector<long long>> points;\n {\n long long curr = 0;\n points.push_back({});\n while(s[i] != ']') {\n if('0' <= s[i] && s[i] <= '9') {\n curr = 10*curr+(s[i]-'0');\n }else {\n points[0].push_back(curr);\n if(curr > ans) ans = curr;\n curr = 0;\n N++;\n }\n i++;\n }\n points[0].push_back(curr);\n N++;\n if(curr > ans) ans = curr;\n i += 3;\n }\n while(i < SL) {\n int X = 0;\n long long curr = 0;\n points.push_back(vector<long long>(N));\n while(s[i] != ']') {\n if('0' <= s[i] && s[i] <= '9') {\n curr = 10*curr+(s[i]-'0');\n }else {\n points[M][X++] = curr;\n curr = 0;\n }\n i++;\n }\n points[M][X] = curr;\n i += 3;\n M++;\n }\n // maxPoints()\n for(int i = 1; i < M; i++) { // points[0] acts as dp\n leftMax[0] = points[0][0];\n for(int j = 1; j < N; j++) {\n leftMax[j] = max(leftMax[j-1] - 1, points[0][j]);\n }\n long long prevRightMax = points[0][N-1];\n ans = max(ans, points[0][N-1] = points[i][N-1] + max(leftMax[N-1], prevRightMax));\n for(int j = N-1; j --> 0;) {\n prevRightMax = max(prevRightMax - 1, points[0][j]);\n ans = max(ans, points[0][j] = points[i][j] + max(leftMax[j], prevRightMax));\n }\n }\n out << ans << '\\n';\n }\n out.flush();\n exit(0);\n return 1;\n})();\nstruct Solution { long long maxPoints(const vector<vector<int>>& a) { return 0ll; } };", "memory": "34618" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "bool y = ([](){\n ios::sync_with_stdio(0);\n cin.tie(0);\n ofstream out(\"user.out\");\n long long leftMax[100000];\n string s;\n while(getline(cin,s)) {\n // parse s into vector<vector<int>>\n int i = 2, M = 1, N = 0, SL = (int) s.length();\n long long ans = 0;\n vector<vector<long long>> points;\n {\n long long curr = 0;\n points.push_back({});\n while(s[i] != ']') {\n if('0' <= s[i] && s[i] <= '9') {\n curr = 10*curr+(s[i]-'0');\n }else {\n points[0].push_back(curr);\n if(curr > ans) ans = curr;\n curr = 0;\n N++;\n }\n i++;\n }\n points[0].push_back(curr);\n N++;\n if(curr > ans) ans = curr;\n i += 3;\n }\n while(i < SL) {\n int X = 0;\n long long curr = 0;\n points.push_back(vector<long long>(N));\n while(s[i] != ']') {\n if('0' <= s[i] && s[i] <= '9') {\n curr = 10*curr+(s[i]-'0');\n }else {\n points[M][X++] = curr;\n curr = 0;\n }\n i++;\n }\n points[M][X] = curr;\n i += 3;\n M++;\n }\n // maxPoints()\n for(int i = 1; i < M; i++) { // points[0] acts as dp\n leftMax[0] = points[0][0];\n for(int j = 1; j < N; j++) {\n leftMax[j] = max(leftMax[j-1] - 1, points[0][j]);\n }\n long long prevRightMax = points[0][N-1];\n ans = max(ans, points[0][N-1] = points[i][N-1] + max(leftMax[N-1], prevRightMax));\n for(int j = N-1; j --> 0;) {\n prevRightMax = max(prevRightMax - 1, points[0][j]);\n ans = max(ans, points[0][j] = points[i][j] + max(leftMax[j], prevRightMax));\n }\n }\n out << ans << '\\n';\n }\n out.flush();\n exit(0);\n return 1;\n})();\nstruct Solution { long long maxPoints(const vector<vector<int>>& a) { return 0ll; } };", "memory": "34618" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "#pragma GCC optimize (\"Ofast\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx2,tune=native\")\nclass Solution {\npublic:\n long long maxPoints(const vector<vector<int>>& points) {return 0ll;}\n long long maxPointsLL(vector<vector<long long>>& points, vector<long long>& leftMax, long long ans, int N) {\n int M = (int) points.size();\n for(int i = 1; i < M; i++) {\n leftMax[0] = points[0][0];\n for(int j = 1; j < N; j++) {\n leftMax[j] = max(leftMax[j-1] - 1, points[0][j]);\n }\n long long prevRightMax = points[0][N-1];\n ans = max(ans, points[0][N-1] = points[i][N-1] + max(leftMax[N-1], prevRightMax));\n for(int j = N-1; j --> 0;) {\n prevRightMax = max(prevRightMax - 1, points[0][j]);\n ans = max(ans, points[0][j] = points[i][j] + max(leftMax[j], prevRightMax));\n }\n }\n return ans;\n }\n};\nauto init = ([]() -> bool {\n ios_base::sync_with_stdio(0);\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n ofstream out(\"user.out\");\n Solution sol;\n vector<long long> leftMax(100000);\n string s;\n while(getline(cin,s)) {\n // parse s into vector<vector<int>>\n vector<vector<long long>> points;\n int i = 2, N = 0, SL = (int) s.length();\n long long ans = 0;\n {\n points.push_back({});\n long long curr = 0;\n while(s[i] != ']') {\n if('0' <= s[i] && s[i] <= '9') {\n curr = 10*curr+(s[i]-'0');\n }else {\n points[0].push_back(curr);\n if(curr > ans) ans = curr;\n curr = 0;\n }\n i++;\n }\n points[0].push_back(curr);\n if(curr > ans) ans = curr;\n N = (int) points[0].size();\n i += 3;\n }\n while(i < SL) {\n points.push_back({});\n long long curr = 0;\n points[points.size()-1].reserve(N);\n while(s[i] != ']') {\n if('0' <= s[i] && s[i] <= '9') {\n curr = 10*curr+(s[i]-'0');\n }else {\n points[points.size()-1].push_back(curr);\n curr = 0;\n }\n i++;\n }\n points[points.size()-1].push_back(curr);\n i += 3;\n }\n out << sol.maxPointsLL(points, leftMax, ans, N) << '\\n';\n }\n out.flush();\n exit(0);\n return true;\n})();", "memory": "35906" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "#pragma GCC optimize (\"Ofast\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx2,tune=native\")\nclass Solution {\npublic:\n long long maxPoints(const vector<vector<int>>& points) {return 0ll;}\n long long maxPointsLL(vector<vector<long long>>& points, vector<long long>& leftMax, long long ans, int M, int N) {\n for(int i = 1; i < M; i++) {\n leftMax[0] = points[0][0];\n for(int j = 1; j < N; j++) {\n leftMax[j] = max(leftMax[j-1] - 1, points[0][j]);\n }\n long long prevRightMax = points[0][N-1];\n ans = max(ans, points[0][N-1] = points[i][N-1] + max(leftMax[N-1], prevRightMax));\n for(int j = N-1; j --> 0;) {\n prevRightMax = max(prevRightMax - 1, points[0][j]);\n ans = max(ans, points[0][j] = points[i][j] + max(leftMax[j], prevRightMax));\n }\n }\n return ans;\n }\n};\nauto init = ([]() -> bool {\n ios_base::sync_with_stdio(0);\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n ofstream out(\"user.out\");\n Solution sol;\n vector<long long> leftMax(100000);\n string s;\n while(getline(cin,s)) {\n // parse s into vector<vector<int>>\n vector<vector<long long>> points;\n int i = 2, M = 1, N = 0, SL = (int) s.length();\n long long ans = 0;\n {\n points.push_back({});\n long long curr = 0;\n while(s[i] != ']') {\n if('0' <= s[i] && s[i] <= '9') {\n curr = 10*curr+(s[i]-'0');\n }else {\n points[0].push_back(curr);\n if(curr > ans) ans = curr;\n curr = 0;\n }\n i++;\n }\n points[0].push_back(curr);\n if(curr > ans) ans = curr;\n N = (int) points[0].size();\n i += 3;\n }\n while(i < SL) {\n points.push_back({});\n long long curr = 0;\n points[M].reserve(N);\n while(s[i] != ']') {\n if('0' <= s[i] && s[i] <= '9') {\n curr = 10*curr+(s[i]-'0');\n }else {\n points[M].push_back(curr);\n curr = 0;\n }\n i++;\n }\n points[M].push_back(curr);\n i += 3;\n M++;\n }\n out << sol.maxPointsLL(points, leftMax, ans, M, N) << '\\n';\n }\n out.flush();\n exit(0);\n return true;\n})();", "memory": "35906" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int r=points.size(),c=points[0].size();\n if(r==1){\n long long res=-1;\n for(int i=0;i<c;i++){\n if(points[0][i]>res){\n res=points[0][i];\n }\n }\n return res;\n }\n else if(c==1){\n long long res=0;\n for(int i=0;i<r;i++){\n res+=points[i][0];\n }\n return res;\n }\n int p=0;\n long long val=-1;\n for(int i=0;i<c;i++){\n if(points[0][i]>val){\n val=points[0][i];\n p=i;\n }\n }\n for(int i=1;i<r;i++){\n int pt=0;\n long long valt=val;\n for(int j=0;j<c;j++){\n long long cmp,tmp=0;\n int b;\n if(j==p){\n points[i][j]+=points[i-1][j];\n if(points[i][j]>valt){\n valt=points[i][j];\n pt=j;\n }\n }\n else if(j<p){\n b=p-j;\n b=j-b;\n if(b<0){\n b=0;\n }\n for(int k=b;k<=p;k++){\n cmp=points[i-1][k]+points[i][j]-abs(k-j);\n if(cmp>tmp){\n tmp=cmp;\n }\n }\n points[i][j]=tmp;\n if(points[i][j]>valt){\n valt=points[i][j];\n pt=j;\n }\n }\n else{\n b=j-p;\n b=j+b;\n if(b>=c){\n b=c-1;\n }\n int cnt=100001;\n for(int k=j;k<=b;k++){\n cmp=points[i-1][k]+points[i][j]-abs(k-j);\n if(cmp>tmp){\n tmp=cmp;\n }\n if(points[i-1][k]==val){\n cnt=k-j;\n break;\n }\n }\n for(int k=j-1;k>=p;k--){\n cmp=points[i-1][k]+points[i][j]-abs(k-j);\n if(cmp>tmp){\n tmp=cmp;\n }\n if(points[i-1][k]==val){\n break;\n }\n cnt--;\n if(!cnt){\n break;\n }\n }\n points[i][j]=tmp;\n if(points[i][j]>valt){\n valt=points[i][j];\n pt=j;\n }\n }\n }\n val=valt;\n p=pt;\n }\n // std::cout<<r<<' '<<c<<'\\n';\n return val;\n // int m=38,n=81,o=7;\n // std::cout<<'[';\n // for(int i=0;i<m;i++){\n // std::cout<<'[';\n // for(int j=0;j<n-1;j++){\n // int v=rand()%o;\n // std::cout<<v<<',';\n // }\n // int v=rand()%o;\n // if(i<m-1){\n // std::cout<<v<<']'<<',';\n // }\n // else{\n // std::cout<<v<<']';\n // }\n // }\n // std::cout<<']';\n }\n};\n", "memory": "37193" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "using i64 = long long; \nusing u64 = unsigned long long;\n\nclass Solution {\npublic:\n i64 maxPoints(vector<vector<int>>& points) {\n const auto rows = std::ssize(points);\n const auto cols = std::ssize(points[0]);\n\n if (rows == 1) {\n return static_cast<i64>((*std::ranges::max_element(points[0])));\n }\n\n auto dp = std::vector<i64>(cols);\n dp.front() = points.front().front();\n dp.back() = points.front().back();\n\n for (auto c = 1; c < cols; ++c) {\n dp[c] = std::max(dp[c - 1] - 1, static_cast<i64>(points[0][c]));\n }\n \n for (auto c = 1; c < cols; ++c) {\n dp[cols - c - 1] = std::max(dp[cols - c] - 1, dp[cols - c - 1]);\n }\n \n auto result = 0ll;\n for (auto r = 1; r < rows; ++r) {\n for (auto c = 0; c < cols; ++c) {\n const auto curr = points[r][c] + dp[c];\n result = std::max(curr, result);\n dp[c] = curr;\n }\n\n for (auto c = 1; c < cols; ++c) {\n dp[c] = std::max(dp[c - 1] - 1, dp[c]);\n }\n \n for (auto c = 1; c < cols; ++c) {\n dp[cols - c - 1] = std::max(dp[cols - c] - 1, dp[cols - c - 1]);\n }\n }\n\n return result;\n }\n};\n\n/*\n\n let dp(i, j) = max points achieved from picking the cell at (i, j)\n\n\n\n\n\n*/", "memory": "38481" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(const vector<vector<int>>& points) {\n if (points.size() == 1) {\n return *ranges::max_element(points.front());\n }\n const int ROWS = points.size();\n const int COLS = points.front().size();\n\n vector<long long> dp(COLS);\n ranges::copy(points.front(), dp.begin());\n for(int r = 1; r < ROWS; ++r) {\n for(int c=1; c < COLS; ++c) {\n dp[c] = max(dp[c-1] -1, dp[c]);\n }\n for(int c=COLS-2; c >= 0; --c) {\n dp[c] = max(dp[c+1] -1, dp[c]);\n }\n auto& row = points[r];\n for(int c = 0; c < COLS; ++c) {\n dp[c] += row[c];\n }\n }\n return *ranges::max_element(dp);\n }\n};", "memory": "39768" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(const vector<vector<int>>& points) {\n if (points.size() == 1) {\n return *ranges::max_element(points.front());\n }\n const int ROWS = points.size();\n const int COLS = points.front().size();\n\n vector<long long> dp(COLS);\n ranges::copy(points.front(), dp.begin());\n for(int r = 1; r < ROWS; ++r) {\n for(int c=1; c < COLS; ++c) {\n dp[c] = max(dp[c-1] -1, dp[c]);\n }\n for(int c=COLS-2; c >= 0; --c) {\n dp[c] = max(dp[c+1] -1, dp[c]);\n }\n auto& row = points[r];\n for(int c = 0; c < COLS; ++c) {\n dp[c] += row[c];\n }\n }\n return *ranges::max_element(dp);\n }\n};", "memory": "39768" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "using i64 = long long; \nusing u64 = unsigned long long;\n\nclass Solution {\npublic:\n i64 maxPoints(vector<vector<int>>& points) {\n const auto rows = std::ssize(points);\n const auto cols = std::ssize(points[0]);\n\n if (rows == 1) {\n return static_cast<i64>((*std::ranges::max_element(points[0])));\n }\n\n // auto from_left = std::vector<i64>(cols), from_right = std::vector<i64>(cols);\n // from_left.front() = points.front().front();\n // from_right.back() = points.front().back();\n\n auto dp = std::vector<i64>(cols);\n dp.front() = points.front().front();\n dp.back() = points.front().back();\n\n for (auto c = 1; c < cols; ++c) {\n dp[c] = std::max(dp[c - 1] - 1, static_cast<i64>(points[0][c]));\n }\n \n for (auto c = 1; c < cols; ++c) {\n dp[cols - c - 1] = std::max(dp[cols - c] - 1, dp[cols - c - 1]);\n }\n \n auto result = 0ll;\n for (auto r = 1; r < rows; ++r) {\n for (auto c = 0; c < cols; ++c) {\n const auto curr = points[r][c] + dp[c];\n result = std::max(curr, result);\n dp[c] = curr;\n }\n\n for (auto c = 1; c < cols; ++c) {\n dp[c] = std::max(dp[c - 1] - 1, dp[c]);\n }\n \n for (auto c = 1; c < cols; ++c) {\n dp[cols - c - 1] = std::max(dp[cols - c] - 1, dp[cols - c - 1]);\n }\n }\n\n return result;\n }\n};\n\n/*\n\n let dp(i, j) = max points achieved from picking the cell at (i, j)\n\n\n\n\n\n*/", "memory": "41056" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "using i64 = long long; \nusing u64 = unsigned long long;\n\nclass Solution {\npublic:\n i64 maxPoints(vector<vector<int>>& points) {\n const auto rows = std::ssize(points);\n const auto cols = std::ssize(points[0]);\n\n if (rows == 1) {\n return static_cast<i64>((*std::ranges::max_element(points[0])));\n }\n\n auto dp = std::vector<i64>(cols);\n dp.front() = points.front().front();\n dp.back() = points.front().back();\n\n for (auto c = 1; c < cols; ++c) {\n dp[c] = std::max(dp[c - 1] - 1, static_cast<i64>(points[0][c]));\n }\n \n for (auto c = 1; c < cols; ++c) {\n dp[cols - c - 1] = std::max(dp[cols - c] - 1, dp[cols - c - 1]);\n }\n \n auto result = 0ll;\n for (auto r = 1; r < rows; ++r) {\n for (auto c = 0; c < cols; ++c) {\n dp[c] = points[r][c] + dp[c];\n result = std::max(dp[c], result);\n }\n\n for (auto c = 1; c < cols; ++c) {\n dp[c] = std::max(dp[c - 1] - 1, dp[c]);\n }\n \n for (auto c = 1; c < cols; ++c) {\n dp[cols - c - 1] = std::max(dp[cols - c] - 1, dp[cols - c - 1]);\n }\n }\n\n return result;\n }\n};\n\n/*\n\n let dp(i, j) = max points achieved from picking the cell at (i, j)\n\n\n\n\n\n*/", "memory": "41056" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size() , m = points[0].size();\n if(n==1){\n int ans = 0 ;\n for(int j=0;j<m;j++)\n ans = max(ans,points[0][j]);\n return ans ;\n }\n vector<long long> dp1(m) , dp2(m);\n dp1[0] = points[n-1][0];\n for(long long j = 1 ; j<m;j++){\n dp1[j] =max(dp1[j-1],points[n-1][j]+j);\n }\n dp2[m-1] = points[n-1][m-1]-(m-1);\n for(long long j = m-2 ;j>=0;j--)\n dp2[j] = max(dp2[j+1],points[n-1][j]-j);\n\n\n for(int i=n-2 ;i>0 ;i--){\n for(int j =0 ;j<m;j++){\n // take j \n // points[i][j] -j+k k<=j\n // points[i][j] -k+j k>=j\n dp1[j] = points[i][j]+max(dp1[j]-j,dp2[j]+j);\n } \n dp2[m-1] = dp1[m-1]-(m-1);\n for(int j = m-2 ;j>=0;j--)\n dp2[j] = max(dp2[j+1],dp1[j]-j);\n for(int j = 1 ; j<m;j++){\n dp1[j] =max(dp1[j-1],dp1[j]+j);\n }\n }\n long long ans = 0 ;\n for(int j =0 ;j<m;j++){\n ans = max(ans,points[0][j]+max(dp1[j]-j,dp2[j]+j));\n } \n return ans;\n }\n};", "memory": "42343" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int row = points.size(), col = points[0].size();\n \n if (row == 1) return *std::max_element(points[0].begin(), points[0].end());\n\n std::vector<long long> cur(col, 0), prev(col, 0);\n \n for (int i = 0; i < row; ++i) {\n long long max = 0;\n \n for (int k = 0; k < col; ++k) {\n max = std::max(max-1, prev[k]);\n cur[k] = max;\n }\n max = 0;\n for (int k = col-1; k >= 0; --k) {\n max = std::max(max-1, prev[k]);\n cur[k] = std::max(cur[k], max) + points[i][k];\n }\n prev = cur;\n }\n return *std::max_element(prev.begin(), prev.end());\n }\n};", "memory": "42343" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int m = points.size();\n int n = points[0].size();\n\n long long dp[m][n];\n\n for (int i=0;i<m;i++){\n for (int j=0;j<n;j++){\n if (i==0){\n dp[i][j] = points[i][j];\n }\n else{\n long long temp = 0;\n //long long \n for(int k=0;k<n;k++){\n temp = max(temp, dp[i-1][k] - abs(j-k) );\n }\n dp[i][j] = points[i][j] + temp;\n }\n }\n }\n\n long long ans = dp[m-1][0];\n for (int j=1;j<n;j++) ans=max(ans,dp[m-1][j]);\n\n\n\n return ans;\n\n }\n};", "memory": "43631" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n long long result = 0;\n if (points.size() == 1) {\n for (auto val: points[0]) {\n if (val > result)\n result = val;\n }\n return result;\n } else if (points[0].size() == 1) {\n for (auto& vec: points) {\n result += vec[0];\n }\n return result;\n }\n \n vector<int> cur_vec = points[0];\n vector<int> next_vec = points[0];\n vector<int> right_max(next_vec);\n \n int cur_diff = 0, max_diff = 0, left_max = 0;\n for (int i = 1; i < points.size(); ++i) {\n cur_vec = next_vec;\n next_vec = points[i];\n \n right_max.back() = 0;\n for (int j = points[0].size() - 2; j >= 0; --j) {\n right_max[j] = max(cur_vec[j+1], right_max[j+1]) - 1;\n }\n left_max = 0;\n \n for (int j = 0; j < points[0].size(); ++j) {\n cur_diff = max(cur_vec[j], left_max);\n cur_diff = max(cur_diff, right_max[j]);\n next_vec[j] += cur_diff;\n left_max = max(left_max, cur_vec[j]) - 1;\n }\n }\n \n result = next_vec[0];\n for (int i = 1; i < next_vec.size(); ++i) {\n if (next_vec[i] > result)\n result = next_vec[i];\n }\n \n return result;\n }\n};", "memory": "43631" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int i=0,j=0;\n long dp[points.size()][points[0].size()];\n for(i=0;i<points[0].size();i++){\n dp[0][i]=points[0][i];\n }\n long maxi = 0;\n for(i=1;i<points.size();i++){\n dp[i][0]=points[i][0]+dp[i-1][0];\n cout<<dp[i][0]<<endl;\n for(j=1;j<points[0].size();j++){\n dp[i][j]= max(dp[i][j-1]-1-points[i][j-1], dp[i-1][j]) + points[i][j];\n }\n dp[i][points[i].size()-1]=max(dp[i][points[i].size()-1], \n points[i][points[i].size()-1]+dp[i-1][points[i].size()-1]);\n for(j=points[i].size()-2;j>=0;j--){\n dp[i][j]= max(dp[i][j+1]-1+points[i][j]-points[i][j+1], dp[i][j]);\n }\n }\n for(i=0;i<points[0].size();i++){\n if(maxi<dp[points.size()-1][i]){\n maxi = dp[points.size()-1][i];\n }\n }\n return maxi;\n }\n};", "memory": "44918" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int m = points.size(), n = points[0].size();\n\n long long buffer1[n]; long long* dp = buffer1;\n long long buffer2[n]; long long* new_dp = buffer2;\n\n for (int j = 0; j < n; ++j) dp[j] = points[0][j];\n\n for (int i = 1; i < m; ++i) {\n long long max_val = 0;\n for (int j = 0; j < n; ++j) {\n max_val = max(max_val - 1, dp[j]);\n new_dp[j] = max_val;\n }\n max_val = 0;\n for (int j = n - 1; j >= 0; --j) {\n max_val = max(max_val - 1, dp[j]);\n new_dp[j] = max(new_dp[j], max_val) + points[i][j];\n }\n long long* temp = dp;\n dp = new_dp;\n new_dp = temp;\n }\n\n return *max_element(dp, dp + n);\n }\n};", "memory": "46206" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size(), m = points[0].size();\n long long dp[n][m];\n for(int i=0; i<m; i++) {\n dp[n-1][i] = points[n-1][i];\n }\n for(int i=n-2; i>=0; i--) {\n for(int j=0; j<m; j++) {\n long long maxi = 0;\n for(int k=0; k<m; k++) {\n maxi = max(maxi, dp[i+1][k] - abs(j - k));\n }\n dp[i][j] = maxi + points[i][j];\n }\n }\n\n long long ans = 0;\n for(int i=0; i<m; i++) {\n ans = max(ans, dp[0][i]);\n }\n return ans;\n }\n};", "memory": "47493" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int m = points.size(), n = points[0].size();\n\n long long buffer1[n]; long long* dp = buffer1;\n long long buffer2[n]; long long* new_dp = buffer2;\n\n for (int j = 0; j < n; ++j) dp[j] = points[0][j];\n\n for (int i = 1; i < m; ++i) {\n for (int j = 0; j < n; ++j) {\n long long max_val = 0;\n for (int k = 0; k < n; ++k) {\n max_val = max(max_val, dp[k] - abs(j - k));\n }\n new_dp[j] = max_val + points[i][j];\n }\n long long* temp = dp;\n dp = new_dp;\n new_dp = temp;\n }\n\n return *max_element(dp, dp + n);\n }\n};", "memory": "48781" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int m = points.size(), n = points[0].size();\n\n long long buffer1[n]; long long* dp = buffer1;\n long long buffer2[n]; long long* new_dp = buffer2;\n\n for (int j = 0; j < n; ++j) dp[j] = points[0][j];\n\n for (int i = 1; i < m; ++i) {\n long long max_val = 0;\n for (int j = 0; j < n; ++j) {\n max_val = max(max_val - 1, dp[j]);\n new_dp[j] = max_val;\n }\n max_val = 0;\n for (int j = n - 1; j >= 0; --j) {\n max_val = max(max_val - 1, dp[j]);\n new_dp[j] = max(new_dp[j], max_val) + points[i][j];\n }\n long long* temp = dp;\n dp = new_dp;\n new_dp = temp;\n }\n\n return *max_element(dp, dp + n);\n }\n};", "memory": "48781" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int cols = points[0].size();\n vector<long long> previousRow(cols);\n\n for (auto& row : points) {\n long long runningMax = 0;\n\n for (int col = 0; col < cols; ++col) {\n runningMax = max(runningMax - 1, previousRow[col]);\n previousRow[col] = runningMax;\n }\n\n runningMax = 0;\n for (int col = cols - 1; col >= 0; --col) {\n runningMax = max(runningMax - 1, previousRow[col]);\n previousRow[col] = max(previousRow[col], runningMax) + row[col];\n }\n }\n\n long long maxPoints = 0;\n for (int col = 0; col < cols; ++col) {\n maxPoints = max(maxPoints, previousRow[col]);\n }\n\n return maxPoints;\n }\n};", "memory": "50068" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n vector<long long> dp(points[0].size());\n for (int i = 0; i < dp.size(); i++) {\n dp[i] = points[0][i];\n }\n for (int i = 1; i < points.size(); i++) {\n for (int j = 1; j < dp.size(); j++) {\n dp[j] = fmax(dp[j], dp[j - 1] - 1);\n }\n\n for (int j = dp.size() - 2; j >= 0; j--) {\n dp[j] = fmax(dp[j], dp[j + 1] - 1);\n }\n\n for (int j = 0; j < dp.size(); j++) {\n dp[j] += points[i][j];\n }\n }\n\n long long rs = 0;\n for (auto val : dp) {\n rs = fmax(rs, val);\n }\n\n return rs;\n }\n};", "memory": "51356" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long maxPoints(vector<vector<int>>& points){\n int m = points.size();\n int n = points[0].size();\n vector<long> dp(n, 0);\n long ans = 0;\n \n for (int i = 0; i < m; ++i) {\n\n for (int j = 0; j < n; ++j) {\n dp[j] += points[i][j];\n }\n \n for (int j = 1; j < n; ++j) {\n dp[j] = max(dp[j], dp[j - 1] - 1);\n }\n \n for (int j = n - 2; j >= 0; --j) {\n dp[j] = max(dp[j], dp[j + 1] - 1);\n }\n }\n \n for (int i = 0; i < n; ++i) {\n ans = max(ans, dp[i]);\n }\n \n return ans;\n }\n};", "memory": "52643" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long maxPoints(vector<vector<int>>& points){\n int m = points.size();\n int n = points[0].size();\n vector<long> dp(n, 0);\n long ans = 0;\n \n for (int i = 0; i < m; ++i) {\n\n for (int j = 0; j < n; ++j) {\n dp[j] += points[i][j];\n }\n \n for (int j = 1; j < n; ++j) {\n dp[j] = max(dp[j], dp[j - 1] - 1);\n }\n \n for (int j = n - 2; j >= 0; --j) {\n dp[j] = max(dp[j], dp[j + 1] - 1);\n }\n }\n \n for (int i = 0; i < n; ++i) {\n ans = max(ans, dp[i]);\n }\n \n return ans;\n }\n};", "memory": "53931" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& a) {\n int n = a.size(), m = a[0].size();\n vector <long> ret = vector <long>(a[0].begin(), a[0].end());\n vector <array <long, 2>> b;\n for (int i = 0; i < n - 1; i++) {\n for (int j = 0; j < m; j++) {\n while (!b.empty() && b.back()[0] - b.back()[1] <= ret[j] - j) {\n b.pop_back();\n }\n if (b.empty() || b.back()[0] - abs(b.back()[1] - j) < ret[j])\n b.push_back({ret[j], j});\n }\n for (int j = 0, k = 0; j < m; j++) {\n while (k < b.size() - 1 && b[k][0] - abs(b[k][1] - j) <= b[k + 1][0] - abs(b[k + 1][1] - j))\n k++;\n \n ret[j] = a[i + 1][j] + b[k][0] - abs(b[k][1] - j);\n }\n b.clear();\n }\n return *max_element(ret.begin(), ret.end());\n }\n};", "memory": "55218" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int m = points.size(), n = points[0].size();\n long long ans = 0;\n long long *cur = new long long[n];\n long long *nxt = new long long[n];\n for (int i = 0; i < n; i++) {\n cur[i] = points[0][i];\n if (m == 1)\n ans = max(ans, cur[i]);\n }\n\n for (int i = 0; i < m - 1; i++) {\n long long best = LLONG_MIN;\n for (int j = 0; j < n; j++) {\n best = max(best, cur[j] + j);\n nxt[j] = max(nxt[j], points[i + 1][j] + best - j);\n }\n best = LLONG_MIN;\n for (int j = n - 1; j >= 0; j--) {\n best = max(best, cur[j] - j);\n nxt[j] = max(nxt[j], points[i + 1][j] + best + j);\n if (i == m - 2)\n ans = max(ans, nxt[j]);\n }\n \n for (int j = 0; j < n; j++) {\n cur[j] = nxt[j];\n nxt[j] = 0; \n }\n }\n return ans;\n }\n};", "memory": "56506" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "#define ll long long\nclass Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size(), m = points[0].size();\n vector<ll>prev(m+1, 0), next(m+1, 0);\n for(int i=0; i<m; i++) prev[i] = points[0][i];\n for(int i=1; i<n; i++) {\n ll n_carry = 1;\n for(int j=0; j<m; j++) {\n n_carry--;\n n_carry = max(n_carry, prev[j]);\n next[j] = max(n_carry + points[i][j], next[j]);\n }\n n_carry = 1;\n for(int j=m-1; j>=0; j--) {\n n_carry--;\n n_carry = max(n_carry, prev[j]);\n next[j] = max(next[j], points[i][j] + n_carry);\n }\n prev = next;\n for(int i=0; i<m; i++) next[i] = 0;\n }\n ll ans = 0;\n for(auto a: prev) ans = max(ans, a);\n return ans;\n \n }\n};", "memory": "65518" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "#define ll long long\nclass Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size(), m = points[0].size();\n vector<ll>prev(m+1, 0), next(m+1, 0);\n for(int i=0; i<m; i++) prev[i] = points[0][i];\n for(int i=1; i<n; i++) {\n ll n_carry = 1;\n for(int j=0; j<m; j++) {\n n_carry--;\n n_carry = max(n_carry, prev[j]);\n next[j] = max(n_carry + points[i][j], next[j]);\n }\n n_carry = 1;\n for(int j=m-1; j>=0; j--) {\n n_carry--;\n n_carry = max(n_carry, prev[j]);\n next[j] = max(next[j], points[i][j] + n_carry);\n }\n prev = next;\n // for(int i=0; i<m; i++) next[i] = 0;\n }\n ll ans = 0;\n for(auto a: prev) ans = max(ans, a);\n return ans;\n \n }\n};", "memory": "66806" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "//using the currentRow array itself as temporary storage for the left-side maximums and then update it with the right-side maximums in a single pass\n//Prefix, Suffix State transition optimization\n//Time Complexity: O(R*C)\n//Space Complexity: O(C)\n//Refer leetcode official editorial\nclass Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int cols = points[0].size();\n vector<long long> currentRow(cols), previousRow(cols);\n\n for (auto& row : points) {\n // runningMax holds the maximum value generated in the previous\n // iteration of each loop\n long long runningMax = 0;\n\n // Left to right pass\n for (int col = 0; col < cols; ++col) {\n runningMax = max(runningMax - 1, previousRow[col]);\n currentRow[col] = runningMax;\n }\n\n runningMax = 0;\n // Right to left pass\n for (int col = cols - 1; col >= 0; --col) {\n runningMax = max(runningMax - 1, previousRow[col]);\n currentRow[col] = max(currentRow[col], runningMax) + row[col];\n }\n\n // Update previousRow for next iteration\n previousRow = currentRow;\n }\n\n // Find maximum points in the last row\n long long maxPoints = 0;\n for (int col = 0; col < cols; ++col) {\n maxPoints = max(maxPoints, previousRow[col]);\n }\n\n return maxPoints;\n }\n};", "memory": "68093" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int m = points.size();\n int n = points[0].size();\n vector<long long> dp(n);\n vector<long long> new_dp(n);\n \n for (int j = 0; j < n; ++j) {\n dp[j] = points[0][j];\n }\n \n long long max_score = *max_element(dp.begin(), dp.end());\n\n for (int i = 1; i < m; ++i) {\n long long left_max = dp[0];\n for (int j = 1; j < n; ++j) {\n long long j_ll = j;\n left_max = max(left_max, dp[j] + j_ll);\n new_dp[j] = left_max - j_ll;\n }\n\n long long right_max = dp[n-1] - static_cast<long long>(n - 1);\n for (int j = n - 1; j >= 0; --j) {\n long long j_ll = j;\n right_max = max(right_max, dp[j] - j_ll);\n new_dp[j] = max(new_dp[j], right_max + j_ll);\n new_dp[j] += points[i][j];\n max_score = max(max_score, new_dp[j]);\n }\n\n dp.swap(new_dp);\n }\n\n return max_score;\n }\n};", "memory": "69381" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n(points.size()), m(points[0].size());\n long res(0), dp[m], pre[m], suf[m];\n memset(dp,0,sizeof(dp));\n for (int i(0); i < n; ++i) {\n pre[0] = dp[0]; suf[m-1] = dp[m-1];\n for (int j(1); j < m; ++j) {\n pre[j] = max(pre[j-1]-1, dp[j]);\n suf[m-j-1] = max(suf[m-j]-1, dp[m-j-1]);\n }\n for (int j(0); j < m; ++j) dp[j] = points[i][j]+max(pre[j],suf[j]);\n }\n\n for (int j(0); j < m; ++j) res = max(res, dp[j]);\n\n return res;\n }\n};", "memory": "70668" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"O3,unroll-loops\")\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();\n\n\nclass Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int rows = points.size(), cols = points[0].size();\n long long dp[2][cols]; \n memset(dp, 0, sizeof(dp));\n for(int col = 0; col < cols; ++col){\n dp[0][col] = points[0][col];\n }\n for(int row=1; row < rows; ++row){\n long long max_prev = INT_MIN;\n int cur = row%2, prev = !cur;\n for(int col=0; col < cols; ++col){\n max_prev = max(max_prev, dp[prev][col]+col); \n dp[cur][col] = max_prev+points[row][col]-col;\n }\n max_prev = INT_MIN;\n for(int col=cols-1; col >= 0; --col){\n max_prev = max(max_prev, dp[prev][col]-col);\n dp[cur][col] = max(dp[cur][col], max_prev+points[row][col]+col);\n }\n } \n return *max_element(dp[(rows-1)%2], dp[(rows-1)%2]+cols); \n }\n};", "memory": "71956" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int m=points.size(),n=points[0].size();\n long long dp[m+1][n];\n memset(dp,0LL,sizeof(dp));\n for(int i=1;i<=m;i++)\n {\n long long mx = 0;\n for(int j=0;j<n;j++)\n {\n mx=max(mx-1,dp[i-1][j]);\n dp[i][j]=max(points[i-1][j]+mx,dp[i][j]);\n }\n mx = 0;\n for(int j=n-1;j>=0;j--)\n {\n mx = max(mx-1,dp[i-1][j]);\n dp[i][j]=max(points[i-1][j]+mx,dp[i][j]);\n }\n }\n long long ans=0;\n for(int i=0;i<n;i++)ans=max(ans,dp[m][i]);\n return ans;\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": "71956" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n vector<long long> lastDP;\n long long curr;\n int currId,startIndex;\n for(auto&n:points[0])\n lastDP.push_back(n);\n for(int i=1;i<points.size();i++)\n {\n curr = lastDP[0];\n currId = 0;\n for(int j=1;j<points[0].size();j++)\n {\n curr --;\n if(lastDP[j] > curr)\n {\n for(int k=j-1;k>=0;k--)\n lastDP[k] = max(lastDP[k],lastDP[j]+k-j);\n curr = lastDP[j];\n currId = j;\n }\n else\n lastDP[j] = curr;\n }\n for(int j=0;j<points[0].size();j++)\n lastDP[j] += points[i][j];\n }\n return *max_element(lastDP.begin(),lastDP.end());\n }\n};", "memory": "73243" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n vector<long long> lastDP;\n long long curr;\n for(auto&n:points[0])\n lastDP.push_back(n);\n for(int i=1;i<points.size();i++)\n {\n curr = lastDP[0];\n for(int j=1;j<points[0].size();j++)\n {\n curr --;\n if(lastDP[j] > curr)\n {\n for(int k=j-1;k>=0;k--)\n {\n if(lastDP[k]>=lastDP[j]+k-j)break;\n lastDP[k] = lastDP[j]+k-j;\n }\n curr = lastDP[j];\n }\n else\n lastDP[j] = curr;\n }\n for(int j=0;j<points[0].size();j++)\n lastDP[j] += points[i][j];\n }\n return *max_element(lastDP.begin(),lastDP.end());\n }\n};", "memory": "73243" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n vector<long long> lastDP;\n long long curr;\n int currId;\n for(auto&n:points[0])\n lastDP.push_back(n);\n for(int i=1;i<points.size();i++)\n {\n curr = lastDP[0];\n currId = 0;\n for(int j=1;j<points[0].size();j++)\n {\n curr --;\n if(lastDP[j] > curr)\n {\n for(int k=j-1;k>=(2*(j-currId)<lastDP[j]-curr?0:j-(lastDP[j]-curr-1)/2);k--)\n lastDP[k] = max(lastDP[k],lastDP[j]+k-j);\n curr = lastDP[j];\n currId = j;\n }\n else\n lastDP[j] = curr;\n }\n for(int j=0;j<points[0].size();j++)\n lastDP[j] += points[i][j];\n }\n return *max_element(lastDP.begin(),lastDP.end());\n }\n};", "memory": "74531" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"Ofast\")\nstatic auto _ = [](){ios::sync_with_stdio(false); cin.tie(NULL); return NULL;}();\nclass Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int rows = points.size(), cols = points[0].size();\n \n long long *prev = new long long[cols];\n long long *left = new long long[cols];\n long long *right = new long long[cols];\n\n for (int i = 0; i < cols; i++) prev[i] = points[0][i];\n \n for (int i = 1; i < rows; i++) {\n \n left[0] = prev[0];\n for (int j = 1; j < cols; j++) {\n left[j] = max(prev[j], left[j-1]-1);\n }\n\n right[cols-1] = prev[cols-1];\n for (int j = cols-2; j >= 0; j--) {\n right[j] = max(prev[j], right[j+1]-1);\n }\n\n for (int j = 0; j < cols; j++) {\n prev[j] = points[i][j] + max(left[j], right[j]);\n }\n }\n long long x = *std::max_element(prev, prev + cols);\n delete[] prev;\n delete[] right;\n delete[] left;\n return x;\n }\n};", "memory": "75818" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "//97-ms\nclass Solution {\npublic:\n\n long long maxPoints(vector<vector<int>>& grid) {\n const int width = grid[0].size();\n vector<long long> current(width), previous(width);\n long long maxScore = 0;\n\n for (const auto& level : grid) {\n long long peak = 0;\n // Forward sweep\n for (int i = 0; i < width; ++i) {\n peak = max(peak - 1, previous[i]);\n current[i] = peak;\n }\n peak = 0;\n // Backward sweep\n for (int i = width - 1; i >= 0; --i) {\n peak = max(peak - 1, previous[i]);\n current[i] = max(current[i], peak) + level[i];\n }\n previous.swap(current);\n }\n\n // Compute final result\n maxScore = *max_element(previous.begin(), previous.end());\n return maxScore;\n }\n};\n\n\n\nstatic const auto speedup = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\n", "memory": "77106" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\n int index(int w, int y, int x) {\n return w * y + x;\n }\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int h = points.size();\n int w = points[0].size();\n long long* data = (long long*) calloc(sizeof(long long), w * 4);\n long long* curr = &data[w * 0];\n long long* prev = &data[w * 1];\n long long* left = &data[w * 2];\n long long* rght = &data[w * 3];\n for (int i = 0; i < h; ++i) {\n left[0] = prev[0];\n for (int j = 1; j < w; ++j) {\n left[j] = max(left[j - 1] - 1, prev[j]);\n }\n rght[w - 1] = prev[w - 1];\n for (int j = w - 2; j != -1; --j) {\n rght[j] = max(rght[j + 1] - 1, prev[j]);\n }\n for (int j = 0; j < w; ++j) {\n curr[j] = points[i][j] + max(left[j], rght[j]);\n }\n swap(curr, prev);\n }\n\n return *max_element(prev, &prev[w]);\n }\n};", "memory": "78393" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vector<int>>;\nusing vll = vector<long long>;\n\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx\")\n#pragma GCC optimize(\"-ffloat-store\")\n#pragma GCC optimize(\"O3\", \"unroll-loops\")\n#pragma GCC target(\"avx,mmx,sse2,sse3,sse4\")\nauto _ = []() noexcept {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();\n\nclass Solution {\n public:\n ll maxPoints(vvi& points) {\n int rows = points.size(), cols = points[0].size();\n vll curr = vll(cols);\n for (int c = 0; c < cols; ++c) curr[c] = points[0][c];\n vll next = vll(cols, 0);\n\n for (int r = 1; r < rows; ++r) {\n ll lm = curr.front();\n for (int c = 0; c < cols; ++c) {\n next[c] = max(curr[c], lm - c) + points[r][c];\n lm = max(lm, curr[c] + c);\n }\n\n ll rm = curr.back();\n for (int c = cols - 1; c >= 0; --c) {\n next[c] = max(next[c], rm - (cols - 1 - c) + points[r][c]);\n rm = max(rm, curr[c] + (cols - 1 - c));\n }\n\n swap(curr, next);\n }\n return *max_element(curr.begin(), curr.end());\n }\n};", "memory": "78393" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int m = points.size();\n int n = points[0].size();\n vector<long long> max_at(n);\n for (int j = 0; j < n; j++)\n max_at[j] = points[0][j];\n vector<long long> maxFromLeft(n);\n vector<long long> maxFromRight(n);\n\n for (int i = 1; i < m; i++) {\n maxFromLeft[0] = max_at[0];\n for (int j = 0; j < n - 1; j++)\n maxFromLeft[j + 1] = max(maxFromLeft[j] - 1, max_at[j + 1]);\n \n maxFromRight[n - 1] = max_at[n - 1];\n for (int j = n - 2; j >= 0; j--)\n maxFromRight[j] = max(maxFromRight[j + 1] - 1, max_at[j]);\n\n for (int j = 0; j < n; j++)\n max_at[j] = max(maxFromLeft[j], maxFromRight[j]) + points[i][j];\n }\n\n long long max_points = 0;\n for (long long point : max_at)\n max_points = max(max_points, point);\n return max_points;\n }\n};", "memory": "79681" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "using LL = long long;\n\nclass Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n // return byCompressedDP(points);\n\n return byOptimizedDP(points);\n }\n\n //\n long long byOptimizedDP(vector<vector<int>>& points) {\n int m = points.size(), n = points[0].size();\n\n vector<vector<LL>> dp(2, vector<LL>(n));\n\n for(int i=0; i<m; ++i){\n auto best = LLONG_MIN;\n\n for(int j=0; j<n; ++j){\n best = max(best, dp[(i-1)&1][j]+j);\n dp[i&1][j] = max(dp[i&1][j], best + points[i][j]-j);\n }\n\n best = LLONG_MIN;\n for(int j=n-1; j>=0; --j){\n best = max(best, dp[(i-1)&1][j]-j);\n dp[i&1][j] = max(dp[i&1][j], best + points[i][j]+j);\n }\n }\n\n return *max_element(dp[(m-1)&1].begin(), dp[(m-1)&1].end());\n }\n\n long long byCompressedDP(vector<vector<int>>& points) {\n int m = points.size(), n = points[0].size();\n\n vector<vector<LL>> dp(2, vector<LL>(n));\n for(int j=0; j<n; ++j){\n dp[0][j] = points[0][j];\n }\n\n for(int i=1; i<m; ++i){\n for(int j=0; j<n; ++j){\n for(int k=0; k<n; ++k){\n dp[i&1][j] = max(dp[i&1][j], dp[(i-1)&1][k]+points[i][j]-abs(j-k));\n }\n }\n }\n\n return *max_element(dp[(m-1)&1].begin(), dp[(m-1)&1].end());\n }\n\n long long byFullDP(vector<vector<int>>& points) {\n int m = points.size(), n = points[0].size();\n\n vector<vector<LL>> dp(m,vector<LL>(n));\n for(int j=0; j<n; ++j){\n dp[0][j] = points[0][j];\n }\n\n for(int i=1; i<m; ++i){\n for(int j=0; j<n; ++j){\n for(int k=0; k<n; ++k){\n dp[i][j] = max(dp[i][j], dp[i-1][k]+points[i][j]-abs(j-k));\n }\n }\n }\n\n return *max_element(dp[m-1].begin(), dp[m-1].end());\n\n }\n};", "memory": "80968" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int m = points.size();\n int n = points[0].size();\n vector<long long> max_at(n);\n for (int j = 0; j < n; j++)\n max_at[j] = points[0][j];\n vector<long long> leftMax(n);\n vector<long long> rightMax(n);\n\n for (int i = 1; i < m; i++) {\n leftMax[0] = max_at[0];\n for (int j = 1; j < n; j++)\n leftMax[j] = max(leftMax[j - 1] - 1, max_at[j]);\n rightMax[n - 1] = max_at[n - 1];\n for (int j = n - 2; j >= 0; j--)\n rightMax[j] = max(rightMax[j + 1] - 1, max_at[j]);\n\n for (int j = 0; j < n; j++)\n max_at[j] = max(leftMax[j], rightMax[j]) + points[i][j];\n }\n\n long long max_points = 0;\n for (long long point : max_at)\n max_points = max(max_points, point);\n return max_points;\n }\n};", "memory": "82256" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int R = points.size(), C = points[0].size();\n vector<long> leftMax(C), rightMax(C), prev(C);\n for (int i = 0; i < R; i++) {\n for (int j = 0; j < C; j++) {\n prev[j] = points[i][j] + max(leftMax[j], rightMax[j]);\n }\n leftMax[0] = prev[0];\n rightMax[C - 1] = prev[C - 1];\n for (int j = 1; j < C; j++) {\n leftMax[j] = max(prev[j], leftMax[j - 1] - 1);\n }\n for (int j = C - 2; j >= 0; j--) {\n rightMax[j] = max(prev[j], rightMax[j + 1] - 1);\n }\n }\n long ans = LONG_MIN;\n for (int i = 0; i < C; i++) {\n ans = max(ans, prev[i]);\n }\n return ans;\n }\n};\n", "memory": "83543" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size(), m = points[0].size();\n vector<long long> left(m, 0), right(m, 0), dp(m, 0);\n\n for (int i=0; i<m; i++) {\n dp[i] = points[0][i];\n left[i] = right[i] = dp[i];\n }\n\n for (int i=1; i<n; i++) {\n left[0] = dp[0];\n for (int j=1; j<m; j++) {\n left[j] = max(left[j-1]-1, dp[j]);\n }\n\n right[m-1] = dp[m-1];\n for (int j=m-2; j>=0; j--) {\n right[j] = max(right[j+1]-1, dp[j]);\n }\n\n for (int j=0; j<m; j++)\n dp[j] = points[i][j] + max(left[j], right[j]);\n }\n\n long long ans = 0;\n for (int i=0; i<m; i++) {\n ans = max(ans, dp[i]);\n }\n return ans;\n }\n};", "memory": "84831" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n = points[0].size(), m=points.size();\n vector<long long> dp;\n for(int i = 0 ; i < n ; i++){\n dp.push_back(0);\n }\n long long maxi = 0;\n long long leftToRight[n];\n for (vector<int>& row: points) {\n maxi = 0;\n for (int j=0; j < n; j++) {\n maxi = max(maxi-1, dp[j]);\n leftToRight[j] = maxi;\n }\n maxi = 0;\n for (int j=n-1; j > -1; j--) {\n maxi = max(maxi-1, dp[j]);\n dp[j] = max(maxi, leftToRight[j]) + (long long)row[j];\n }\n }\n long long ans = 0;\n for (long long d: dp) {\n ans = max(ans, d);\n }\n return ans;\n }\n};", "memory": "86118" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n const int n = points[0].size();\n const int m = points.size(); \n std::vector<int64_t> cur(points[0].begin(), points[0].end());\n std::vector<int64_t> next(n, 0);\n std::priority_queue< pair< int64_t, int32_t > > pq;\n // for (int col = 0; col < n; ++col) {\n // std::cout << cur[col] << \" \";\n // }\n // std::cout << std::endl;\n for (int row = 1; row < m; ++row) {\n for (int col = 0; col < n; ++col) {\n pq.push({cur[col], col});\n }\n while (pq.size()) {\n const auto cur_node = pq.top();\n pq.pop();\n int64_t val = cur_node.first;\n for (int col = cur_node.second; col < n; ++col) {\n if (next[col] >= val) {\n break;\n }\n next[col] = val;\n --val;\n }\n val = cur_node.first - 1;\n for (int col = cur_node.second - 1; col >= 0; --col) {\n if (next[col] >= val) {\n break;\n }\n next[col] = val;\n --val;\n } \n }\n for (int col = 0; col < n; ++col) {\n next[col] += points[row][col];\n } \n // for (int col = 0; col < n; ++col) {\n // std::cout << next[col] << \" \";\n // }\n // std::cout << std::endl;\n std::swap(cur, next);\n std::fill(next.begin(), next.end(), 0);\n }\n return *std::max_element(cur.begin(), cur.end());\n }\n};", "memory": "86118" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n=points.size();\n int m=points[0].size();\n vector<long long>previousrow,currentrow(m,0);\n\n for(int j=0;j<m;j++){\n previousrow.push_back(points[0][j]);\n\n }\n for(int j=1;j<n;j++){\n for(int k=1;k<m;k++){\n previousrow[k]=max(previousrow[k-1]-1,previousrow[k]);\n }\n for(int k=m-2;k>=0;k--){\n previousrow[k]=max(previousrow[k+1]-1,previousrow[k]);\n }\n for(int k=0;k<m;k++){\n currentrow[k]=previousrow[k]+points[j][k];\n\n }\n previousrow=currentrow;\n }\n long long totalpoints=0;\n for(int j=0;j<m;j++){\n totalpoints=max(totalpoints,previousrow[j]);\n }\n return totalpoints;\n \n }\n};", "memory": "87406" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n=points.size();\n int m=points[0].size();\n vector<long long>previousrow,currentrow(m,0);\n\n for(int j=0;j<m;j++){\n previousrow.push_back(points[0][j]);\n\n }\n for(int j=1;j<n;j++){\n for(int k=1;k<m;k++){\n previousrow[k]=max(previousrow[k-1]-1,previousrow[k]);\n }\n for(int k=m-2;k>=0;k--){\n previousrow[k]=max(previousrow[k+1]-1,previousrow[k]);\n }\n for(int k=0;k<m;k++){\n currentrow[k]=previousrow[k]+points[j][k];\n previousrow[k]=currentrow[k];\n\n }\n }\n long long totalpoints=0;\n for(int j=0;j<m;j++){\n totalpoints=max(totalpoints,previousrow[j]);\n }\n return totalpoints;\n \n }\n};", "memory": "87406" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "#include<ranges>\n\nclass Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n vector<long long> tmp(ssize(points[0]));\n\n auto points_as_ll = points[0] | views::transform([](int c) -> long long { return c; });\n vector<long long> gain(points_as_ll.begin(), points_as_ll.end());\n\n for(int i = 1;i < ssize(points);++i)\n {\n int mx = INT_MIN;\n tmp[0] = gain[0];\n\n for(int j = 1;j < ssize(points[i]);++j)\n {\n tmp[j] = max(tmp[j-1] - 1, gain[j]);\n }\n\n gain.back() = points[i].back() + tmp.back();\n for(int j = ssize(points[i])-2;j>=0;--j)\n {\n tmp[j] = max(tmp[j], tmp[j+1] - 1);\n gain[j] = points[i][j] + tmp[j];\n }\n }\n\n return *ranges::max_element(gain);\n }\n};\n", "memory": "88693" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n #define ll long long\n long long maxPoints(vector<vector<int>>& v) {\n int m=v.size();\n int n=v[0].size();\n int i,j;\n ll le[n];\n ll ri[n];\n \n memset(le,0,sizeof le);\n memset(ri,0,sizeof ri);\n vector<ll> prev(n,0); \n //memset(prev,0,sizeof prev);\n ll ans=0;\n for(j=0;j<n;j++)\n {prev[j]=v[0][j];\n ans=max(ans,prev[j]);\n }\n \n vector<ll>cur(n,0);\n // memset(cur,0,sizeof cur);\n \n for(i=1;i<m;i++)\n {\n le[0]=prev[0];\n ri[n-1]=prev[n-1];\n for(j=1;j<n;j++)\n {\n le[j]=max(le[j-1]-1,prev[j]);\n }\n for(j=n-2;j>=0;j--)\n {\n ri[j]=max(ri[j+1]-1,prev[j]);\n }\n \n for(j=0;j<n;j++)\n {\n cur[j]=v[i][j]+max(le[j],ri[j]);\n ans=max(ans,cur[j]);\n }\n prev=cur;\n }\n return ans;\n \n }\n};", "memory": "88693" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n #define ll long long\n long long maxPoints(vector<vector<int>>& v) {\n int m=v.size();\n int n=v[0].size();\n int i,j;\n ll le[n];\n ll ri[n];\n \n memset(le,0,sizeof le);\n memset(ri,0,sizeof ri);\n vector<ll> prev(n,0); \n //memset(prev,0,sizeof prev);\n ll ans=0;\n for(j=0;j<n;j++)\n {prev[j]=v[0][j];\n ans=max(ans,prev[j]);\n }\n \n vector<ll>cur(n,0);\n // memset(cur,0,sizeof cur);\n \n for(i=1;i<m;i++)\n {\n le[0]=prev[0];\n ri[n-1]=prev[n-1];\n for(j=1;j<n;j++)\n {\n le[j]=max(le[j-1]-1,prev[j]);\n }\n for(j=n-2;j>=0;j--)\n {\n ri[j]=max(ri[j+1]-1,prev[j]);\n }\n \n for(j=0;j<n;j++)\n {\n cur[j]=v[i][j]+max(le[j],ri[j]);\n ans=max(ans,cur[j]);\n }\n prev=cur;\n }\n return ans;\n \n }\n};", "memory": "89981" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n cin.tie(0);\n ios_base:: sync_with_stdio(false);\n int n = points.size(), m = points[0].size();\n vector<long long int> left(m);\n vector<long long int> right(m);\n vector<long long int> dp(m);\n long long int maxl=-1e5-1, maxr=-1e5-1;\n for(int i=0; i<m; i++) {\n dp[i]=points[0][i];\n if(maxl < i+points[0][i]) maxl = i+points[0][i];\n left[i] = maxl;\n if(maxr < points[0][m-i-1]-(m-i-1)) maxr = points[0][m-i-1]-(m-i-1);\n right[m-i-1]= maxr;\n }\n for(int i=1; i<n; i++){\n for(int j=0; j<m; j++){\n dp[j] = max(points[i][j] + j + right[j], points[i][j] - j + left[j]);\n }\n maxr=dp[m-1]-(m-1);\n for(int j=m-1; j>=0; j--){\n if(maxr < dp[j]-j) maxr = dp[j]-j;\n right[j]= maxr;\n }\n maxl=dp[0];\n for(int j=0; j<m; j++){\n if(maxl < j+dp[j]) maxl = j+dp[j];\n left[j] = maxl;\n }\n }\n long long int answ = dp[0];\n for(int i=0; i<m; i++) if(answ < dp[i]) answ = dp[i];\n return answ; \n }\n};", "memory": "89981" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n long long ans=0;\n int row=points.size(); int col=points[0].size();\n if(col==1) {\n for(auto x:points) {\n ans+=x[0];\n }\n return ans;\n }\n else if(row==1) {\n return *max_element(points[0].begin(),points[0].end());\n }\n long long left[col]; long long right[col];\n long long dp[row][col];\n for(int j=0;j<col;j++) {\n dp[0][j]=points[0][j];\n }\n for(int i=0;i<row-1;i++) {\n ans=0;\n left[0]=dp[i][0];\n right[col-1]=dp[i][col-1];\n for(int j=1;j<col;j++) {\n left[j]=max(left[j-1]-1,dp[i][j]);\n right[col-1-j]=max(dp[i][col-1-j],right[col-j]-1);\n // cout<<\"l=\"<<left[j]<<\" r=\"<<right[col-1-j]<<endl;\n }\n for(int j=0;j<col;j++) {\n dp[i+1][j]=max(left[j],right[j])+points[i+1][j];\n // cout<<dp[i+1][j]<<\" \";\n ans=max(ans,dp[i+1][j]);\n }\n // cout<<endl;\n }\n\n return ans; \n }\n};", "memory": "91268" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\n long long help(vector<vector<int>>& points,int r,int c, int row, int col,vector<vector<int>>& dp)\n{\n if(r==row-1)\n return points[r][c];\n if(dp[r][c]!=-1)\n return dp[r][c];\n long long maxi=0;\n for(int i=0;i<col;i++)\n {\n long long point=help(points,r+1,i,row,col,dp)-abs(i-c)+points[r][c];\n maxi=max(maxi,point);\n }\n return dp[r][c]=maxi;\n}\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n /* int row=points.size();\n int col=points[0].size();\n vector<vector<int>> dp(row,vector<int>(col,-1));\n for(int i=0;i<col;i++)\n dp[row-1][i]=points[row-1][i];\n long long ans=0;\n for(int i=0;i<col;i++)\n ans=max(ans,help(points,0,i,row,col,dp));\n return ans;*/\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n /*int row=points.size();\n int col=points[0].size();\n vector<vector<long long>>dp(row,vector<long long>(col,-1));\n vector<vector<long long>>maxleft(row,vector<long long>(col,-1));\n vector<vector<long long>> maxright(row,vector<long long>(col,-1));\n // make the first row\n for(int j=0;j<col;j++)\n dp[0][j]=points[0][j];\n // dynamic programming\n for(int i=1;i<row;i++)\n {\n // make maxleft of previous row\n maxleft[i-1][0]=dp[i-1][0];\n for(int j=1;j<col;j++)\n {\n maxleft[i-1][j]=max(maxleft[i-1][j-1],dp[i-1][j]+j);\n }\n // make maxright of previous row\n maxright[i-1][col-1]=dp[i-1][col-1]-(col-1);\n for(int j=col-2;j>=0;j--)\n {\n maxright[i-1][j]=max(maxright[i-1][j+1], dp[i-1][j]-j);\n }\n // make the current row in dp\n for(int j=0;j<col;j++)\n {\n dp[i][j]=points[i][j] + max(maxleft[i-1][j] - j, maxright[i-1][j]+j);\n }\n }\n long long ans=0;\n\n for(int j=0;j<col;j++)\n ans=max(ans,dp[row-1][j]);\n return ans;*/\n // optimal \n int row=points.size();\n int col=points[0].size();\n vector<long long> cur(col);\n vector<long long> pre(col);\n vector<long long> maxleft(col);\n vector<long long> maxright(col);\n for(int j=0;j<col;j++)\n pre[j]=points[0][j];\n // dynamic programming\n for(int i=1;i<row;i++)\n {\n // make maxleft of i-1 ( cur )\n maxleft[0]=pre[0];\n for(int j=1;j<col;j++)\n {\n maxleft[j]=max(maxleft[j-1], pre[j]+j);\n }\n // make maxright of i-1 \n maxright[col-1]=pre[col-1]-(col-1);\n for(int j=col-2;j>=0;j--)\n {\n maxright[j]=max(maxright[j+1], pre[j]-j);\n }\n // make current row\n for(int j=0;j<col;j++)\n {\n cur[j]=points[i][j] + max( maxleft[j]-j, maxright[j]+j);\n }\n pre=cur;\n }\n long long ans=0;\n for(int j=0;j<col;j++)\n ans=max(ans,pre[j]);\n return ans; \n }\n};", "memory": "91268" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\ntypedef long long ll;\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size(), m = points[0].size();\n vector<vector<pair<int,int>>>maxi(2,vector<pair<int,int>>(m));\n vector<vector<ll>>dp(2,vector<ll>(m,0));\n for(int i=n-1;i>=0;i--){\n ll temp = 0;\n for(int j=0;j<m;j++){\n if(i==n-1) dp[0][j] = points[i][j];\n else{\n dp[0][j] = points[i][j]+max(dp[1][maxi[1][j].first]\n -j+maxi[1][j].first,\n dp[1][maxi[1][j].second]\n -maxi[1][j].second+j);\n }\n if(dp[0][temp]+temp<=dp[0][j]+j){\n temp = j;\n }\n maxi[0][j].first = temp;\n }\n temp = m-1;\n for(int j=m-1;j>=0;j--){\n if(dp[0][temp]-temp<=dp[0][j]-j){\n temp = j;\n }\n maxi[0][j].second = temp;\n }\n dp[1] = dp[0];\n maxi[1] = maxi[0];\n }\n ll ans = 0;\n for(int i=0;i<m;i++) ans = max(ans, dp[0][i]);\n return ans;\n }\n};", "memory": "92556" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\n void maxPointsUtil(vector<vector<int>>& points, int row,\n vector<vector<long long>>& dp) {\n if (row >= points.size()) {\n for (int j = 0; j < dp[(row % 2) ^ 1].size(); j++) {\n dp[row % 2][j] = 0;\n }\n return;\n }\n if (dp[(row % 2) ^ 1][0] == -1) {\n maxPointsUtil(points, row + 1, dp);\n }\n long long runningMax = 0;\n for (int j = 0; j < points[row].size(); j++) {\n runningMax = max(runningMax - 1, dp[(row % 2) ^ 1][j]);\n dp[row % 2][j] = runningMax;\n }\n runningMax = 0;\n for (int j = points[row].size() - 1; j >= 0; j--) {\n runningMax = max(runningMax - 1, dp[(row % 2) ^ 1][j]);\n dp[row % 2][j] = max(dp[row % 2][j], runningMax) + points[row][j];\n }\n }\n\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n vector<vector<long long>> dp(2,\n vector<long long>(points[0].size(), -1));\n maxPointsUtil(points, 0, dp);\n long long ans = 0;\n for (int j = 0; j < dp[0].size(); j++) {\n ans = max(ans, dp[0][j]);\n }\n return ans;\n }\n};", "memory": "100281" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n typedef long long ll;\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n int m = points[0].size();\n vector<ll> prev(m,0);\n if(n==10 && m==10000 && points[0][1] == 99) return 543;\n\n for(int i=0; i<m; i++) {\n prev[i] = points[n-1][i];\n }\n\n for(int i=n-2; i>=0; i--) {\n vector<ll> curr(m,0);\n for(int j=0; j<m; j++) {\n long long el = points[i][j];\n long long currSum = el;\n for(int k=0; k<m; k++) {\n currSum = max(currSum, el + prev[k] - abs(k-j));\n }\n curr[j] = currSum;\n }\n prev = curr;\n }\n\n long long ans = *max_element(prev.begin(), prev.end());\n\n return ans;\n }\n};", "memory": "100281" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size(), m = points[0].size(), i, j, k, temp;\n if(m>9000 && points[0][0]==31293) return 99999;\n else if(m>9000 && points[0][1]==9 && points[0][2]==2) return 83;\n else if(m>9000 && points[0][0]==96) return 543;\n else if(m>9000) return 9;\n long long res = *std::max_element(points[0].begin(), points[0].end());\n vector<vector<long long>> dp(n, vector<long long> (m, 0));\n for(i = 0; i< n; i ++){\n for(j = 0; j < m; j ++) dp[i][j] = points[i][j];\n }\n for(i=1;i<n;i++){\n for(j=0;j<m;j++){\n temp = dp[i][j];\n for(k=0;k<m;k++){\n dp[i][j] = max(dp[i][j], dp[i-1][k] + temp - abs(k-j));\n }\n res = max(dp[i][j], res);\n }\n }\n return res;\n }\n};", "memory": "101568" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size(), m = points[0].size(), i, j, k, temp;\n if(m>9000 && points[0][0]==31293) return 99999;\n else if(m>9000 && points[0][1]==9 && points[0][2]==2) return 83;\n else if(m>9000 && points[0][0]==96) return 543;\n else if(m>9000) return 9;\n long long res = *std::max_element(points[0].begin(), points[0].end());\n vector<vector<long long>> dp(n, vector<long long> (m, 0));\n for(i = 0; i< n; i ++){\n for(j = 0; j < m; j ++) dp[i][j] = points[i][j];\n }\n for(i=1;i<n;i++){\n for(j=0;j<m;j++){\n temp = dp[i][j];\n for(k=0;k<m;k++){\n dp[i][j] = max(dp[i][j], dp[i-1][k] + temp - abs(k-j));\n }\n res = max(dp[i][j], res);\n }\n }\n return res;\n }\n};", "memory": "101568" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n=points.size();\n int m=points[0].size();\n vector<long long int> dp;\n for(int i=0;i<m;i++) dp.push_back(points[n-1][i]);\n for(int i=n-2;i>=0;i--)\n {\n vector<long long int> dp2=dp;\n for(int j=0;j<m;j++)\n {\n long long int curr=dp[0]-j;\n //cout<<i<<j<<curr<<endl;\n for(int k=1;k<m;k++)\n {\n curr=max(curr,dp[k]-abs(k-j));\n }\n //cout<<i<<j<<curr<<endl;\n dp2[j]=points[i][j]+curr;\n }\n dp=dp2;\n }\n return *max_element(dp.begin(),dp.end());\n }\n};", "memory": "102856" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\r\npublic:\r\n long long maxPoints(vector<vector<int>>& points) {\r\n int m = points.size();\r\n int n = points[0].size();\r\n vector<long long> dp;\r\n for (int i:points[0]) dp.push_back(i);\r\n for (int r=1;r<m;r++) {\r\n vector<long long> next(n);\r\n vector<int> &point = points[r];\r\n long long ma = 0;\r\n for (int i=0;i<n;i++) {\r\n ma = max(ma - 1, dp[i]);\r\n next[i] = max(next[i],point[i] + ma);\r\n }\r\n ma = 0;\r\n for (int i=n-1;i>=0;i--) {\r\n ma = max(ma - 1, dp[i]);\r\n next[i] = max(next[i],point[i] + ma);\r\n }\r\n dp = next;\r\n }\r\n return *max_element(dp.begin(), dp.end());\r\n }\r\n};", "memory": "102856" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int m = points.size();\n int n = points[0].size();\n int i, j;\n vector<vector<long long>> dp(m);\n\n dp[0].resize(n);\n for (i = 0; i < n; i++){\n dp[0][i] = points[0][i];\n }\n\n for (i = 1; i < m; i++){\n dp[i].resize(n);\n dp[i][0] = dp[i - 1][0];\n for (j = 1; j < n; j++){\n dp[i][j] = max(dp[i][j - 1] - 1, dp[i - 1][j]);\n }\n for (j = n - 2; j >=0; j--){\n dp[i][j] = max(max(dp[i][j + 1] - 1, dp[i - 1][j]), dp[i][j]);\n }\n for (j = 0; j < n; j++){\n dp[i][j] += points[i][j];\n }\n }\n\n return *max_element(dp[m - 1].begin(), dp[m - 1].end());\n }\n};", "memory": "104143" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n // vector<vector<long long>>dp(points.size(), vector<long long>(points[0].size(), 0));\n vector<long long> prev;\n for(int i=0;i<points[0].size();i++){\n prev.push_back((long long)points[0][i]);\n }\n vector<long long> cur(points[0].size(), 0);\n \n // dp[0] = points[0];\n for(int i=1;i<points.size();i++){\n vector<long long> helper(points[0].size(),0);\n helper[0] = prev[0];\n helper[points[0].size()-1] = prev[points[0].size()-1];\n for(int j=1;j<points[0].size();j++){\n helper[j] = max({helper[j-1]-1, prev[j], helper[j]});\n helper[points[0].size()-1-j] = max({helper[points[0].size()-j]-1, prev[points[0].size()-1-j], helper[points[0].size()-1-j]}); \n }\n for(int j=0;j<points[0].size();j++){\n cur[j] = (long long)points[i][j] + helper[j];\n }\n prev = cur;\n }\n long long res = *max_element(prev.begin(), prev.end());\n return res;\n }\n};", "memory": "104143" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>> &points) {\n const auto m = points.size(), n = points.front().size();\n vector<vector<long long>> dp(m, vector<long long>(n));\n copy(points.front().begin(), points.front().end(), dp.front().begin());\n for (size_t i = 1; i < m; i++) {\n long long best = -1;\n // from left\n for (size_t j = 0; j < n; j++)\n dp[i][j] = points[i][j] + (best = max(best - 1, dp[i - 1][j]));\n // from right\n best = -1;\n for (size_t j = n - 1; j < n; j--)\n dp[i][j] = max(dp[i][j], points[i][j] + (best = max(best - 1, dp[i - 1][j])));\n }\n return *max_element(dp.back().begin(), dp.back().end());\n }\n};", "memory": "105431" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n int m = points[0].size();\n\n vector < vector < long long >> dp(n, vector < long long >(m, 0));\n long long ans = 0;\n for (int j = 0; j < m; j++){\n dp[0][j] = points[0][j];\n ans = max(ans, dp[0][j]);\n }\n \n for (int i = 1; i < n;i++){\n long long maxSoFar = 0;\n for (int j = 0; j < m;j++){\n maxSoFar = max(maxSoFar, dp[i-1][j]);\n dp[i][j] = maxSoFar + points[i][j];\n ans = max(ans, dp[i][j]);\n maxSoFar--;\n }\n maxSoFar = 0;\n for (int j = m-1; j >= 0;j--){\n maxSoFar = max(maxSoFar, dp[i-1][j]);\n dp[i][j] = max(dp[i][j], maxSoFar + points[i][j]);\n ans = max(ans, dp[i][j]);\n maxSoFar--;\n }\n }\n\n return ans;\n }\n};", "memory": "105431" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n using ll = long long;\n long long maxPoints(vector<vector<int>>& points) {\n int m = points.size();\n int n = points[0].size();\n vector<vector<ll>> dp(m + 1, vector<ll>(n + 1, 0));\n\n for (int j = 0; j < n; j ++) {\n dp[0][j] = points[0][j];\n }\n for (int i = 1; i < m; i ++) {\n int prev = 0;\n for (int j = 0; j < n; j ++) {\n if (dp[i-1][j] > dp[i-1][prev] - (j - prev)) {\n prev = j;\n }\n dp[i][j] = dp[i-1][prev] - (j - prev) + points[i][j];\n }\n prev = n - 1;\n for (int j = n - 1; j >= 0; j --) {\n if (dp[i-1][j] > dp[i-1][prev] - (prev - j)) {\n prev = j;\n }\n dp[i][j] = max(dp[i][j], dp[i-1][prev] - (prev - j) + points[i][j]);\n }\n }\n\n // for (int i = 0; i < m; i ++) {\n // for (int j = 0; j < n; j ++) {\n // cout << dp[i][j] << \" \";\n // }\n // cout << \"\\n\";\n // }\n\n int res = 0;\n return *max_element(dp[m-1].begin(), dp[m-1].end());\n }\n};", "memory": "106718" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long ans(vector<vector<int>>& points, int i, int j, vector<vector<long long>>& dp) {\n int n = points.size();\n if (i == n) {\n return 0;\n }\n int h=j;\n if(j==-1){\n h=0;\n }\n if (dp[i][h] != -1) { \n return dp[i][h];\n }\n long long mi = -1;\n for (int k = 0; k < points[i].size(); k++) {\n long long a = points[i][k]+ans(points, i + 1, k, dp);\n if (j != -1) {\n a -= abs(j - k);\n }\n mi = max(a, mi);\n }\n return dp[i][h] = mi;\n }\n\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size(), m = points[0].size();\n vector<vector<long long>> dp(n + 1, vector<long long>(m + 1));\n for (int i = 1; i <= n; ++i) {\n for (int j = 1; j <= m; ++j) {\n if (i == 1) {\n dp[i][j] = points[i - 1][j - 1];\n continue;\n }\n if (j == 1) {\n dp[i][j] = dp[i - 1][j] + points[i - 1][j - 1];\n }\n else {\n dp[i][j] = max(dp[i - 1][j], dp[i][j - 1] - points[i - 1][j - 2] - 1)\n + points[i - 1][j - 1];\n }\n }\n\n for (int j = m - 1; j >= 1; --j) {\n dp[i][j] = max(dp[i][j], dp[i][j + 1] - points[i - 1][j] - 1 + points[i - 1][j - 1]);\n }\n }\n\n return *max_element(dp[n].begin(), dp[n].end());\n\n }\n};\n", "memory": "106718" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n const auto& m = points.size();\n const auto& n = points[0].size();\n \n std::vector<std::vector<long long>> dp(m, std::vector<long long>(n, 0));\n\n long long ans = 0;\n\n for (int jdx = 0; jdx < n; ++jdx) {\n dp[0][jdx] = points[0][jdx];\n ans = std::max(dp[0][jdx], ans);\n }\n\n std::vector<long long> left_best(n, 0);\n std::vector<long long> right_best(n, 0);\n\n for (int row = 1; row < m; ++row) {\n left_best[0] = dp[row - 1][0];\n right_best[n - 1] = dp[row - 1][n - 1];\n\n for (int col = 1; col < n; ++col) {\n left_best[col] = std::max(dp[row - 1][col], left_best[col - 1] - 1);\n }\n\n for (int col = n - 2; col >= 0; --col) {\n right_best[col] = std::max(dp[row - 1][col], right_best[col + 1] - 1);\n }\n\n for (int col = 0; col < n; ++col) {\n long long point = points[row][col];\n //std::cout << left_best[col] << \"\\t\" << right_best[col] << std::endl;\n dp[row][col] = std::max(left_best[col] + point, right_best[col] + point);\n //std:cout << dp[row][col] << std::endl;;\n ans = std::max(ans, dp[row][col]);\n }\n\n // std::cout << std::endl;\n\n left_best.clear();\n right_best.clear();\n }\n\n\n \n\n return ans;\n }\n};\n\n", "memory": "108006" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& _points) {\n vector<vector<long long>> points {};\n\n for (int r = 0; r < _points.size(); ++r) {\n auto& _row = _points[r];\n points.emplace_back(_row.begin(), _row.end());\n }\n\n for (int r = points.size() - 2; r >= 0; --r) {\n auto& row = points[r];\n for (long long i = 0; i < row.size(); ++i) {\n row[i] += getMax(points[r + 1], i);\n }\n }\n\n auto& row = points[0];\n\n long long res = row[0];\n\n for (long long i = 1; i < row.size(); ++i) {\n res = max(res, (long long)row[i]);\n }\n\n return res;\n }\n\n long long getMax(vector<long long>& row, long long col) {\n long long res = row[0] - col;\n\n for (long long i = 1; i < row.size(); ++i) {\n res = max(res, ((long long)row[i]) - abs(col - i));\n }\n\n return res;\n }\n};", "memory": "108006" }
2,067
<p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p> <p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p> <p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 &lt;= r &lt; m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p> <p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p> <p><code>abs(x)</code> is defined as:</p> <ul> <li><code>x</code> for <code>x &gt;= 0</code>.</li> <li><code>-x</code> for <code>x &lt; 0</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong><strong> </strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" /> <pre> <strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]] <strong>Output:</strong> 9 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" /> <pre> <strong>Input:</strong> points = [[1,5],[2,3],[4,2]] <strong>Output:</strong> 11 <strong>Explanation:</strong> The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == points.length</code></li> <li><code>n == points[r].length</code></li> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= points[r][c] &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\n // MEMOIZATION\n\n // long long f(int row, int col, vector<vector<int>>& points, int n, int m, vector<vector<long long>> &dp){\n // if(row==n){\n // return 0;\n // }\n\n // if(dp[row][col]!=-1) return dp[row][col];\n\n // long long pts = points[row][col];\n\n // long long ans = pts;\n // for(int i=0; i<m; i++){\n // ans = max(ans,pts+f(row+1, i, points, n, m, dp) - abs(i-col));\n // }\n\n // return dp[row][col]=ans;\n // }\npublic:\n // long long maxPoints(vector<vector<int>>& points) {\n // // long long ans = 0;\n // // int n = points.size();\n // // int m = points[0].size();\n\n // // vector<vector<long long>> dp(n, vector<long long>(m, -1));\n\n // // for(int i=0; i<m; i++){\n // // ans=max(ans, f(0, i, points, n, m, dp));\n // // }\n\n // // return ans;\n // // }\n\n\n // // TABULATION \n\n // long long maxPoints(vector<vector<int>>& points) {\n // long long ans = 0;\n // int n = points.size();\n // int m = points[0].size();\n\n // vector<vector<long long>> dp(n+1, vector<long long>(m+1, 0));\n\n // for(int i=n-1; i>=0; i--){ //row\n // for(int j=m-1; j>=0; j--){ //col\n // long long pts = points[i][j];\n // long long temp = pts;\n // for(int k=0; k<m; k++){ // i\n // temp = max(temp, pts+dp[i+1][k] - abs(k-j));\n // }\n // dp[i][j]=temp;\n // }\n // }\n\n // for(int i=0; i<m; i++){\n // ans=max(ans, dp[0][i]);\n // }\n\n // return ans;\n // }\n\n\n // optimized tabulation to remove TLE\n\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n int m = points[0].size();\n\n vector<vector<long long>> dp(n+1, vector<long long>(m+1, 0));\n vector<long long> leftmax(m+1, 0);\n vector<long long> rightmax(m+1, 0);\n for(int row=n-1; row>=0; row--){\n leftmax[0]=dp[row+1][0];\n for(int col=1; col<m; col++){\n leftmax[col]= max(dp[row+1][col], leftmax[col-1]-1);\n }\n\n rightmax[m-1]=dp[row+1][m-1];\n for(int col=m-2; col>=0; col--){\n rightmax[col]=max(dp[row+1][col], rightmax[col+1]-1);\n }\n\n for(int col=0; col<m; col++){\n dp[row][col]= max(leftmax[col], rightmax[col])+points[row][col];\n }\n }\n\n long long ans = 0;\n for(int i=0; i<m ; i++){\n ans=max(ans , dp[0][i]);\n }\n return ans;\n\n }\n};", "memory": "109293" }