id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n \n int m=grid.size();\n int n=grid[0].size();\n\n vector<pair<int,int>>v;\n\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n if(grid[i][j]==1)v.push_back({i,j});\n }\n }\n\n int mini_r=m;\n int maxi_r=-1;\n int mini_c=n;\n int maxi_c=-1;\n\n for(auto &x:v){\n mini_r=min(mini_r,x.first);\n maxi_r=max(maxi_r,x.first);\n mini_c=min(mini_c,x.second);\n maxi_c=max(maxi_c,x.second);\n }\n\n return (maxi_r-mini_r+1)*(maxi_c-mini_c+1);\n\n }\n};", "memory": "157100" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n vector<int> row;\n vector<int> col;\n int l1 = grid[0].size();int l2=0;\n int b1 =grid.size(); int b2=0;\n\n for(int i=0;i<grid.size();i++)\n {\n for(int j=0;j<grid[0].size();j++)\n {\n if(grid[i][j]==1)\n {\n l1 = min(l1,j);\n l2 = max(l2,j);\n b1 = min(b1,i);\n b2 = max(b2,i);\n row.push_back(i);\n col.push_back(j);\n }\n }\n }\n\n // int l2 = *max_element(col.begin(),col.end());\n // int l1 = *min_element(col.begin(),col.end());\n \n // int b2 = *max_element(row.begin(),row.end());\n // int b1 = *min_element(row.begin(),row.end());\n\n int area = ((l2-l1)+1)*((b2-b1)+1);\n\n /*to save space we can also write for loop like this:\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < m; ++j) {\n if (grid[i][j] == 1) {\n l = min(l, j);\n u = min(u, i);\n r = max(r, j);\n d = max(d, i);\n }\n }\n }\n\n */\n\n return area;\n \n }\n};", "memory": "157200" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n vector<int> row, col;\n for(int i=0; i<grid.size(); i++){\n for(int j=0; j<grid[i].size(); j++){\n if(grid[i][j]==1){\n row.push_back(i);\n col.push_back(j);\n }\n }\n }\n int minr=INT_MAX, minc=INT_MAX, maxr=INT_MIN, maxc=INT_MIN;\n for(int i=0; i<row.size(); i++){\n minr= min(minr, row[i]);\n minc= min(minc, col[i]);\n maxr= max(maxr, row[i]);\n maxc= max(maxc, col[i]);\n }\n int len= maxc-minc;\n int wid= maxr-minr;\n return (len+1)*(wid+1);\n }\n};", "memory": "157300" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n vector<int> row;\n vector<int> col;\n\n for(int i=0;i<grid.size();i++)\n {\n for(int j=0;j<grid[0].size();j++)\n {\n if(grid[i][j]==1)\n {\n row.push_back(i);\n col.push_back(j);\n }\n }\n }\n\n int l2 = *max_element(col.begin(),col.end());\n int l1 = *min_element(col.begin(),col.end());\n \n int b2 = *max_element(row.begin(),row.end());\n int b1 = *min_element(row.begin(),row.end());\n\n int area = ((l2-l1)+1)*((b2-b1)+1);\n\n /*to save space we can also write for loop like this:\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < m; ++j) {\n if (grid[i][j] == 1) {\n l = min(l, j);\n u = min(u, i);\n r = max(r, j);\n d = max(d, i);\n }\n }\n }\n\n */\n\n return area;\n \n }\n};", "memory": "157500" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n vector<int> row;\n vector<int> col;\n\n for(int i=0;i<grid.size();i++)\n {\n for(int j=0;j<grid[0].size();j++)\n {\n if(grid[i][j]==1)\n {\n row.push_back(i);\n col.push_back(j);\n }\n }\n }\n\n int l2 = *max_element(col.begin(),col.end());\n int l1 = *min_element(col.begin(),col.end());\n \n int b2 = *max_element(row.begin(),row.end());\n int b1 = *min_element(row.begin(),row.end());\n\n int area = ((l2-l1)+1)*((b2-b1)+1);\n\n /*to save space we can also write for loop like this:\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < m; ++j) {\n if (grid[i][j] == 1) {\n l = min(l, j);\n u = min(u, i);\n r = max(r, j);\n d = max(d, i);\n }\n }\n }\n\n */\n\n return area;\n \n }\n};", "memory": "157600" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n vector<int> row;\n vector<int> col;\n\n for(int i=0;i<grid.size();i++)\n {\n for(int j=0;j<grid[0].size();j++)\n {\n if(grid[i][j]==1)\n {\n row.push_back(i);\n col.push_back(j);\n }\n }\n }\n\n int l2 = *max_element(col.begin(),col.end());\n int l1 = *min_element(col.begin(),col.end());\n \n int b2 = *max_element(row.begin(),row.end());\n int b1 = *min_element(row.begin(),row.end());\n\n int area = ((l2-l1)+1)*((b2-b1)+1);\n\n /*to save space we can also write for loop like this:\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < m; ++j) {\n if (grid[i][j] == 1) {\n l = min(l, j);\n u = min(u, i);\n r = max(r, j);\n d = max(d, i);\n }\n }\n }\n\n */\n\n return area;\n \n }\n};", "memory": "157600" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\n /*\n get the min and max values of i and j that appear for a grid ele 1\n find area\n tc : o(m*n)\n */\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n vector<int> row;\n vector<int> col;\n int l1 = grid[0].size();int l2=0;\n int b1 =grid.size(); int b2=0;\n\n for(int i=0;i<grid.size();i++)\n {\n for(int j=0;j<grid[0].size();j++)\n {\n if(grid[i][j]==1)\n {\n l1 = min(l1,j);\n l2 = max(l2,j);\n b1 = min(b1,i);\n b2 = max(b2,i);\n row.push_back(i);\n col.push_back(j);\n }\n }\n }\n\n // int l2 = *max_element(col.begin(),col.end());\n // int l1 = *min_element(col.begin(),col.end());\n \n // int b2 = *max_element(row.begin(),row.end());\n // int b1 = *min_element(row.begin(),row.end());\n\n int area = ((l2-l1)+1)*((b2-b1)+1);\n\n /*to save space we can also write for loop like this:\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < m; ++j) {\n if (grid[i][j] == 1) {\n l = min(l, j);\n u = min(u, i);\n r = max(r, j);\n d = max(d, i);\n }\n }\n }\n\n */\n\n return area;\n \n }\n};", "memory": "157700" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n vector<int> row;\n vector<int> col;\n for(int i=0;i<grid.size();i++){\n for(int j=0;j<grid[0].size();j++){\n if(grid[i][j]==1){\n row.push_back(i);\n col.push_back(j);\n }\n }\n }\n sort(row.begin(),row.end());\n sort(col.begin(),col.end());\n int rowMin=row[0];\n int rowMax=row[row.size()-1];\n int colMin=col[0];\n int colMax=col[col.size()-1];\n return (colMax-colMin+1)*(rowMax-rowMin+1);\n }\n};", "memory": "157800" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n //Try to store the i and j indexs for grid[i][j] = 1 in these two vector \n vector<int>horizontal_index;\n vector<int>vertical_index;\n \n \n for(int i = 0 ; i< grid.size();i++){\n \n for(int j= 0 ; j < grid[0].size() ;j++){\n \n if(grid[i][j]==1){\n horizontal_index.push_back(j);\n vertical_index.push_back(i);\n }\n }\n }\n \n // Now we can sort the vector and find the min and max i and j for grid[i][j] = 1\n sort(horizontal_index.begin(),horizontal_index.end());\n sort(vertical_index.begin(),vertical_index.end());\n \n //Finally caluculate the area of rec.\n return (horizontal_index[horizontal_index.size()-1]-horizontal_index[0]+1)*(vertical_index[vertical_index.size()-1]-vertical_index[0]+1);\n\n }\n};", "memory": "157900" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n ios::sync_with_stdio(false);\n int n = nums.size();\n long long p1 = nums[0];\n long long p2 = nums[0];\n for(int i =1; i< n; i++){\n long long p3 = max(p1,p2) + nums[i];\n long long p4 = p1 - nums[i];\n p1 = p3;\n p2 = p4;\n }\n return max(p1,p2);\n }\n};\n", "memory": "76196" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "auto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\nclass Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n const int n = nums.size();\n\n if (n == 1) {\n return nums[0];\n }\n\n long long even = nums[0]+nums[1];\n long long odd = nums[0]-nums[1];\n\n for (int i = 2;i < n;i++) {\n long long oldEven = even;\n\n even = max(even, odd)+nums[i];\n odd = oldEven-nums[i];\n }\n\n return max(even, odd);\n }\n};", "memory": "76196" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n long long int sum = 0;\n int n = nums.size();\n vector<vector<long long int>> dp(2,vector<long long>(2,LONG_LONG_MIN));\n dp[0][0] = INT_MIN;\n dp[0][1] = nums[0];\n for(int i=1;i<n;i++){\n dp[i%2][0] = dp[(i+1)%2][1] - nums[i];\n dp[i%2][1] = max(dp[(i+1)%2][0] + nums[i],dp[(i+1)%2][1] + nums[i]);\n }\n return max(dp[(n+1)%2][0], dp[(n+1)%2][1]);\n }\n};", "memory": "77588" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long ff(vector<int> &a,int n,int id,int c,int f,vector<vector<vector<long long>>> &dp){\n if(id>n) return 0;\n if(dp[id][c][f]!=-1) return dp[id][c][f];\n if(c>0){\n if(f){\n return dp[id][c][f]=max(a[id]+ff(a,n,id+1,1,0,dp),a[id]+ff(a,n,id+1,0,1,dp));\n }\n else return dp[id][c][f]=max(-a[id]+ff(a,n,id+1,1,1,dp),-a[id]+ff(a,n,id+1,0,1,dp));\n }\n else{\n if(f){\n return dp[id][c][f]=max(a[id]+ff(a,n,id+1,1,0,dp),a[id]+ff(a,n,id+1,0,1,dp));\n }\n // else return dp[id][c][f]=-a[id]+ff(a,n,id+1,c+1,1,dp);\n }\n return dp[id][c][f]=0;\n }\n long long maximumTotalCost(vector<int>& a) {\n int n=a.size();\n // vector<vector<vector<long long>>> dp(n+1,vector<vector<long long>>(2,vector<long long>(2,0)));\n // return ff(a,n-1,0,0,1,dp);\n vector<vector<long long>> pv(2,vector<long long>(2,0)),ne(2,vector<long long>(2,0));\n for(int i=n-1;i>=0;i--){\n for(int c=0;c<2;c++){\n for(int f=0;f<2;f++){\n if(c==0 && f){\n // dp[i][c][f]=max(a[i]+dp[i+1][1][0],a[i]+dp[i+1][0][1]);\n ne[c][f]=max(a[i]+pv[1][0],a[i]+pv[0][1]);\n }\n else{\n if(f){\n ne[c][f]=max(a[i]+pv[1][0],a[i]+pv[0][1]);\n }\n else ne[c][f]=max(-a[i]+pv[1][1],-a[i]+pv[0][1]);\n }\n }\n }\n pv=ne;\n }\n return pv[0][1];\n }\n};", "memory": "77588" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n int n = nums.size();\n if (n == 1) return nums[0];\n long long dp[n][2];\n dp[1][0] = nums[0] + nums[1];\n dp[1][1] = nums[0] - nums[1];\n\n for (int i = 2; i < n; i++) {\n dp[i][0] = max(dp[i-1][0], dp[i-1][1]) + nums[i];\n dp[i][1] = dp[i-1][0] - nums[i];\n }\n\n return max(dp[n-1][0], dp[n-1][1]);\n }\n};", "memory": "78981" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n int n = (int)nums.size();\n long long dp[n][2];\n dp[0][0] = nums[0];\n dp[0][1] = LONG_LONG_MIN;\n\n for(int i=1;i<n;i++) {\n dp[i][1] = dp[i-1][0]-nums[i];\n dp[i][0] = max(dp[i-1][0],dp[i-1][1])+nums[i];\n }\n return max(dp[n-1][0], dp[n-1][1]);\n }\n};", "memory": "78981" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& a) {\n int n = a.size();\n vector<long long> f(n + 1);\n f[1] = a[0];\n for (int i = 1; i < n; i++) {\n f[i + 1] = max(f[i] + a[i], f[i - 1] + a[i - 1] - a[i]);\n }\n return f[n];\n }\n};\n", "memory": "80373" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "#include<bits/stdc++.h>\nusing namespace std;\nclass Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n \n int n = nums.size();\n vector<long long>dp(n+1,0);\n dp[1] = nums[0];\n\n for(int i = 1; i<n; i++){\n long long op1 = dp[i]+nums[i];\n long long op2 = dp[i-1]+nums[i-1]+ (-1*nums[i]);\n\n dp[i+1] = max(op1, op2);\n }\n return dp[n];\n }\n};", "memory": "81766" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n unordered_map<string,long long> dp;\n \n long long recurse(int l, int r, vector<int>& v){ \n if(l>r){\n return 0;\n }\n \n if(l==r){\n return v[l];\n }\n \n if(dp.find(to_string(l)+\" \"+to_string(r))!=dp.end()){\n return dp[to_string(l)+\" \"+to_string(r)]; \n }\n \n dp[to_string(l)+\" \"+to_string(r)]=max(v[l]+recurse(l+1,r,v),v[l]-v[l+1]+recurse(l+2,r,v));\n \n return dp[to_string(l)+\" \"+to_string(r)];\n }\n \n long long maximumTotalCost(vector<int>& v) { \n \n bool posFound=false;\n \n long long ans=0;\n \n int n=v.size();\n dp.clear();\n \n for(int i=0;i<n;i++){\n if(v[i]>=0){\n ans+=v[i];\n posFound=true;\n }\n \n else if(posFound){\n int r=i;\n while(r<n&&v[r]<0){\n r++;\n }\n \n r--;\n \n ans+=max(recurse(i,r,v),-1*v[i]+recurse(i+1,r,v));\n \n i=r;\n posFound=false;\n }\n \n else{\n int r=i;\n while(r<n&&v[r]<0){\n r++;\n }\n \n r--;\n \n ans+=max(recurse(i,r,v),v[i]+recurse(i+1,r,v));\n \n i=r;\n }\n }\n \n return ans;\n }\n};", "memory": "83158" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "typedef long long ll;\nclass Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n vector<ll> dp{-100000000000000ll, 0};\n for (int i : nums) {\n if (i >= 0) {\n dp[0] = max(dp[0], dp[1]) + i;\n dp[1] = -100000000000000ll;\n } else {\n vector<ll> nx(2);\n nx[0] = max(dp[0], dp[1]) + i;\n nx[1] = dp[0] - i;\n dp = nx;\n }\n }\n return max(dp[0], dp[1]);\n }\n};", "memory": "83158" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "int n;\nvector <int> a;\nlong long dp[100100];\n\nlong long rec(int i){\n if(i==n)\n return 0;\n\n if(dp[i]!=-1)\n return dp[i];\n\n //take this element as it is\n long long ans = a[i]+rec(i+1);\n //take next two elements\n if(i<n-1)\n ans = max(ans, a[i]-a[i+1]+rec(i+2));\n\n return dp[i]=ans;\n}\n\n\nclass Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n memset(dp, -1, sizeof(dp));\n n=nums.size();\n a=nums;\n return rec(0);\n }\n};", "memory": "84551" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "int n;\nvector <int> a;\nlong long dp[100100];\n\nlong long rec(int i){\n if(i==n)\n return 0;\n\n if(dp[i]!=-1)\n return dp[i];\n\n //take this element as it is\n long long ans = a[i]+rec(i+1);\n //take next two elements\n if(i<n-1)\n ans = max(ans, a[i]-a[i+1]+rec(i+2));\n\n return dp[i]=ans;\n}\n\n\nclass Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n memset(dp, -1, sizeof(dp));\n n=nums.size();\n a=nums;\n return rec(0);\n }\n};", "memory": "84551" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "int n;\nvector<long long> dp[2];\nvector<int> a;\n\nlong long recur(int c,int f)\n{\n if(c==n)\n {\n return 0;\n }\n if(dp[f][c]==-1)\n {\n if(f==0)\n return dp[f][c]=max(recur(c+1,0),recur(c+1,1))+a[c];\n else\n return dp[f][c]=recur(c+1,0)+a[c]*(-1);\n }\n else\n {\n return dp[f][c];\n }\n}\nclass Solution {\npublic:\n long long maximumTotalCost(vector<int>& num) {\n n=num.size();\n a=num;\n dp[0].clear();\n dp[0].resize(n,-1);\n dp[1].clear();\n dp[1].resize(n,-1);\n long long res=recur(0,0);\n // for(int i=0;i<2;i++)\n // {\n // for(int j=0;j<n;j++)\n // cout<<dp[i][j]<<\" \";\n // cout<<endl;\n // }\n return res;\n }\n};", "memory": "85943" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& a) {\n int n = a.size() ;\n vector<pair<long long,long long>> dp(n) ;\n dp[n-1] = {a[n-1],-a[n-1]} ;\n\n for(int e=n-2 ;e>=0 ;e--){\n dp[e] = {a[e]+max(dp[e+1].first,dp[e+1].second),-a[e]+dp[e+1].first} ;\n }\n\n return dp[0].first ;\n }\n};", "memory": "85943" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int n;\n long long dp[100000][2];\n long long f(int i, bool minus, vector<int>& nums){\n if(i == n) return 0;\n if(dp[i][minus] != LONG_MIN) return dp[i][minus];\n long long sum=((!minus)?nums[i]:-1*nums[i])+f(i+1, !minus, nums);\n sum = max(sum, nums[i] + f(i+1, 1, nums));\n return dp[i][minus] = sum;\n }\n \n long long maximumTotalCost(vector<int>& nums) {\n n= nums.size();\n fill(&dp[0][0], &dp[0][0]+2*n, LONG_MIN);\n return f(0, 0, nums);\n }\n};\n\n\n\nauto init = []() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();", "memory": "87336" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n#define ll long long \nll dp[100005][3];\nll solve(int indx,int sign,vector<int>&nums){\n if(indx>=nums.size()) return 0;\n if(dp[indx][sign+1] != -1) return dp[indx][sign+1];\n ll take = nums[indx]*sign +solve(indx+1,(sign==1)?(-1):(1),nums);\n ll nottake = nums[indx]+solve(indx+1,-1,nums);\n return dp[indx][sign+1] = max(take,nottake);\n}\n long long maximumTotalCost(vector<int>& nums) {\n memset(dp,-1,sizeof(dp));\n return solve(0,1,nums);\n }\n};", "memory": "87336" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "using ll = long long;\nclass Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n \n vector<int> vec;\n\n vec.push_back(nums[0]);\n\n for(int i=1; i<nums.size(); i++){\n if(nums[i]==0 && vec.back()==0) continue;\n vec.push_back(nums[i]);\n }\n nums = vec;\n \n int n = nums.size();\n vector<ll> dp(n, LLONG_MIN);\n vector<ll> dp2(n, LLONG_MIN);\n dp[n-1] = nums[n-1];\n dp2[n-1] = -nums[n-1];\n\n for(int i = n-2; i>=0; i--){\n\n ll cur_sum = (ll)nums[i];\n\n cur_sum += max(dp2[i+1], dp[i+1]);\n\n dp[i] = max(dp[i], cur_sum);\n dp2[i] = -nums[i]+dp[i+1];\n }\n\n return dp[0];\n }\n};", "memory": "88728" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "using ll = long long;\nclass Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n \n vector<int> vec;\n vec.push_back(nums[0]);\n for(int i=1; i<nums.size(); i++){\n if(nums[i]==0 && vec.back()==0) continue;\n vec.push_back(nums[i]);\n }\n nums = vec;\n \n int n = nums.size();\n vector<ll> dp(n, LLONG_MIN);\n vector<ll> dp2(n, LLONG_MIN);\n dp[n-1] = nums[n-1];\n dp2[n-1] = -nums[n-1];\n\n for(int i = n-2; i>=0; i--){\n\n ll cur_sum = (ll)nums[i];\n\n cur_sum += max(dp2[i+1], dp[i+1]);\n\n dp[i] = max(dp[i], cur_sum);\n dp2[i] = -nums[i]+dp[i+1];\n }\n\n return dp[0];\n }\n};", "memory": "88728" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long vis[3][100004];\n int n;vector<int>arr;\n long long recru(int par,int i){\n if(i==n)return 0;\n if(vis[par][i]!=-1)return vis[par][i];\n //not break\n long long nobreak=INT_MIN,yesbreak=INT_MIN;\n if(par==0)nobreak=arr[i]+recru(!par,i+1);\n else nobreak=recru(!par,i+1)-arr[i];\n if(par==0)yesbreak=arr[i]+recru(0,i+1);\n else yesbreak=recru(0,i+1)-arr[i];\n return vis[par][i]=max(nobreak,yesbreak);\n\n }\n long long maximumTotalCost(vector<int>& nums){\n memset(vis,-1,sizeof(vis));\n arr=nums;\n n=arr.size();\n return recru(0,0);\n }\n};", "memory": "90121" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "#define ll long long\nclass Solution {\npublic:\n vector<int> nums;\n int n;\n ll dp[100001][2];\n \n ll solve(int index,bool pos){\n if(index==n)\n return 0;\n \n if(dp[index][pos]!=-1)\n return dp[index][pos];\n \n if(pos){\n return dp[index][pos]=max(1ll*nums[index]+solve(index+1,0),1ll*nums[index]+solve(index+1,1));\n }\n else{\n return dp[index][pos]=-1ll*nums[index]+solve(index+1,1);\n }\n }\n \n long long maximumTotalCost(vector<int>& nums) {\n this->nums=nums;\n this->n=nums.size();\n memset(dp,-1,sizeof(dp));\n return solve(0,1);\n }\n};", "memory": "90121" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long int help(vector<int>&nums,long long int ind, vector<long long int>&dp)\n {\n if(ind>=nums.size())\n return 0;\n if(dp[ind]!=-1)\n return dp[ind];\n if(ind==nums.size()-1)\n {\n return nums[ind];\n }\n else\n {\n // if(nums[ind]==0)\n // {\n // return dp[ind]=max(help(nums,ind+1,dp),abs(nums[ind+1])+help(nums,ind+2,dp));\n // }\n // else\n // {\n // if(nums[ind]>0)\n // {\n // if(nums[ind+1]>=0)\n return dp[ind]=max(nums[ind]+help(nums,ind+1,dp),nums[ind]-nums[ind+1]+help(nums,ind+2,dp));\n // else\n // {\n // return dp[ind]=max(nums[ind]+help(nums,ind+1,dp),nums[ind]-nums[ind+1]+help(nums,ind+2,dp));\n // }\n // }\n \n // else\n // {\n // if(nums[ind]<0&&nums[ind+1]<=0)\n // {\n // return dp[ind]= max(nums[ind]+help(nums,ind+1,dp),(-nums[ind+1]+nums[ind])+help(nums,ind+2,dp));\n // }\n // else\n // {\n // if(nums[ind]<0&&nums[ind+1]>=0)\n // {\n // return dp[ind]=max(nums[ind]+help(nums,ind+1,dp),(-nums[ind+1]+nums[ind])+help(nums,ind+2,dp));\n // }\n // }\n // } \n // }\n \n }\n return 0;\n }\n long long maximumTotalCost(vector<int>& nums) {\n long long int sum=0;\n long long int n=nums.size();\n vector<long long int>dp(n+1,-1);\n return help(nums,0,dp);\n \n \n }\n};", "memory": "91513" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n int n=nums.size();\n \n vector<long long> dp(n,LLONG_MIN);\n \n dfs(nums,dp,0,n,true);\n \n return dp[0];\n }\n \n long long dfs(vector<int>& nums, vector<long long>& dp, int idx, int& n, bool positive) {\n if(idx>=n) return 0;\n \n if(dp[idx]!=LLONG_MIN && positive) {\n return dp[idx];\n }\n \n if(positive) {\n long long currRes1 = nums[idx] + dfs(nums,dp,idx+1,n,false);\n long long currRes2 = nums[idx] + dfs(nums,dp,idx+1,n,true);\n \n dp[idx]=max(currRes1,currRes2);\n return dp[idx];\n } else {\n return -1*nums[idx] + dfs(nums,dp,idx+1,n,true);\n }\n }\n};", "memory": "91513" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int n;\n long long func(int ind, vector<long long> &dp, vector<int> &nums){\n if(ind>=n)return 0;\n if(ind == n-1)return nums[n-1];\n if(dp[ind]!=-1)return dp[ind];\n dp[ind] = 1ll*nums[ind]+func(ind+1,dp,nums);\n if(ind+1<n){\n dp[ind] = max(dp[ind],1ll*(nums[ind]-nums[ind+1])+func(ind+2,dp,nums));\n }\n return dp[ind];\n }\n long long maximumTotalCost(vector<int>& nums) {\n n = nums.size();vector<long long> dp(n,-1);\n return func(0,dp,nums);\n }\n};", "memory": "92906" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\nlong long dp[100005][2][2];\n\nlong long rec(int i, vector<int>& nums, int start, int sign, int n) {\n if (i == n)\n return 0;\n \n if (dp[i][start][sign] != LLONG_MIN)\n return dp[i][start][sign];\n \n long long ans = LLONG_MIN;\n \n if (start == 0) {\n ans = max(ans, nums[i] + rec(i + 1, nums, 1, sign ^ 1, n));\n } else {\n ans = max(ans, rec(i, nums, 0, 0, n));\n if (sign == 1) {\n ans = max(ans, -nums[i] + rec(i + 1, nums, 1, sign ^ 1, n));\n } else {\n ans = max(ans, nums[i] + rec(i + 1, nums, 1, sign ^ 1, n));\n }\n }\n \n return dp[i][start][sign] = ans;\n}\n\nlong long maximumTotalCost(vector<int>& nums) {\n int n = nums.size();\n \n for (int i = 0; i < 100005; ++i) {\n dp[i][0][0] = dp[i][0][1] = dp[i][1][0] = dp[i][1][1] = LLONG_MIN;\n }\n \n return rec(0, nums, 0, 0, n);\n}\n\n};", "memory": "97083" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n //vector<vector<int>> dp(nums.size(), vector<int>(2,-1));\n vector<long long int> prev(2,0);\n prev[0]=prev[1]=nums[0];\n for(int i=1;i<nums.size();i++){\n vector<long long int> curr(2,0);\n for(int j=0;j<2;j++){\n long long int alag = nums[i]+prev[1];\n long long int dep = -1e9;\n if(j==1){\n dep = -nums[i]+prev[0];\n }else{\n dep = nums[i]+prev[1];\n }\n curr[j]= max(dep,alag);\n }\n prev=curr;\n }\n return max(prev[0],prev[1]);\n }\n};", "memory": "98476" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n long long dp1(int sign, int n, vector<int>& nums, vector<vector<long long>>& dp, int size){\n if(n==size) return 0;\n if(dp[sign][n]!=-1) return dp[sign][n];\n long long ans = INT_MIN;\n if(sign==1){\n ans = max(ans,-nums[n]+dp1(0,n+1,nums,dp,size));\n ans = max(ans,dp1(0,n,nums,dp,size));\n }\n else{\n ans = max(ans,nums[n]+dp1(1,n+1,nums,dp,size));\n }\n return dp[sign][n] = ans;\n }\n long long maximumTotalCost(vector<int>& nums) {\n int n = nums.size();\n vector<vector<long long>> dp(2,vector<long long>(n,-1));\n long long ans = dp1(0,0,nums,dp,n);\n return ans;\n }\n};", "memory": "99868" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n \n int n = nums.size();\n vector<vector<long long>> cache_money(2, vector<long long>(n+1, -1));\n auto dp = [&](auto& dp, int i, bool is_pos) -> long long {\n if(i >= n) {\n return 0;\n }\n \n if(cache_money[is_pos][i] != -1) {\n return cache_money[is_pos][i];\n }\n long long cur = is_pos ? nums[i] : -nums[i];\n // split\n long long split = cur + dp(dp, i+1, true);\n \n // continue growing\n long long grow = cur + dp(dp, i+1, !is_pos);\n \n return cache_money[is_pos][i] = max(split, grow);\n };\n \n return dp(dp, 0, true);\n }\n};", "memory": "99868" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long util(vector<int>& nums, int ind, bool prev, vector<vector<long long>>& dp) {\n\n if (ind >= nums.size()) {\n return 0;\n }\n\n long long nottake = 0;\n long long take = 0;\n\n if (dp[prev][ind] != -1) {\n return dp[prev][ind];\n }\n\n if (prev) {\n take = nums[ind] + util(nums, ind + 1, false, dp);\n nottake = nums[ind] + util(nums, ind + 1, true, dp);\n } else {\n take = -nums[ind] + util(nums, ind + 1, true, dp);\n nottake = nums[ind] + util(nums, ind + 1, false, dp);\n }\n\n dp[prev][ind] = max(take, nottake);\n return dp[prev][ind];\n }\n long long maximumTotalCost(vector<int>& nums) {\n bool prev = true;\n vector<vector<long long>> dp(2, vector<long long>(nums.size(), -1));\n return util(nums, 0, prev, dp);\n }\n};", "memory": "101261" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long util(vector<int>& nums, int ind, bool prev, vector<vector<long long>>& dp) {\n\n if (ind >= nums.size()) {\n return 0;\n }\n\n long long nottake = 0;\n long long take = 0;\n\n if (dp[prev][ind] != -1) {\n return dp[prev][ind];\n }\n\n if (prev) {\n take = nums[ind] + util(nums, ind + 1, false, dp);\n nottake = nums[ind] + util(nums, ind + 1, true, dp);\n } else {\n take = -nums[ind] + util(nums, ind + 1, true, dp);\n }\n\n dp[prev][ind] = max(take, nottake);\n return dp[prev][ind];\n }\n long long maximumTotalCost(vector<int>& nums) {\n bool prev = true;\n vector<vector<long long>> dp(2, vector<long long>(nums.size(), -1));\n return util(nums, 0, prev, dp);\n }\n};", "memory": "101261" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n#define ll long long\n ll dp[1000005][3];\n long long solve(int i,int n,int k,vector<int>&nums){\n if(i>=n){\n return 0;\n }\n if(dp[i][k+1]!=-1) return dp[i][k+1];\n ll pick=nums[i]*k+solve(i+1,n,k*(-1),nums);\n ll notPick=nums[i]+solve(i+1,n,k,nums);\n\n return dp[i][k+1]=max(pick,notPick);\n\n }\n long long maximumTotalCost(vector<int>& nums) {\n \n int n=nums.size();\n memset(dp,-1,sizeof(dp));\n return solve(0,n,1,nums);\n }\n};", "memory": "102653" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n\n long long maximumTotalCost(vector<int>& nums) {\n int n=nums.size();\n vector<vector<long long>>dp(n,vector<long long>(2,INT_MIN));\ndp[0][0]=nums[0];\ndp[0][1]=nums[0];\nfor(int i=1;i<nums.size();i++){\n dp[i][0]=max(nums[i]+dp[i-1][0],nums[i]+dp[i-1][1]);dp[i][1]=-(nums[i])+dp[i-1][0];\n}\nreturn max(dp[n-1][0],dp[n-1][1]);\n }\n};", "memory": "104046" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n #define ll long long int\n long long maximumTotalCost(vector<int>& a) {\n int n = a.size();\n vector<vector<ll>> dp(n+1,vector<ll>(2,0));\n dp[0][0] = a[0];\n dp[0][1] = -1e18;\n for(int i = 1;i<n;i++){\n dp[i][0] = max(dp[i-1][1]+a[i], dp[i-1][0]+a[i]);\n dp[i][1] = dp[i-1][0] - a[i] ;\n }\n return max(dp[n-1][1],dp[n-1][0]);\n }\n};", "memory": "105438" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n \n //each entry can be taken as is or if it was made negative then previous should be positive\n int n=nums.size();\n vector<vector<long long>> dp(n,vector<long long>(2,0));\n\n dp[0][0]=nums[0];\n dp[0][1]=nums[0];\n\n for(int i=1;i<n;i++){\n dp[i][0]=max(dp[i-1][0],dp[i-1][1])+nums[i];\n dp[i][1]=dp[i-1][0]-nums[i];\n }\n\n return max(dp[n-1][0],dp[n-1][1]);\n }\n};", "memory": "106831" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n \n // We want to maximize the subarray\n // 1 6 -3 -4 2 -9 -8\n // 1 7 4 6 \n // 0 -5 10 8\n // positive - > individual\n // negative -> 2nd person \n \n //dp[i][0] = dp[i-1][0] + nums[i], dp[i-1][1] + nums[i];\n //dp[i][1] = dp[i-1][0] - nums[i]\n \n int N = nums.size();\n vector dp(N, vector<long long>(2, 0));\n \n dp[0][0] = (long long)nums[0];\n dp[0][1] = -2000000009;\n \n for(int i=1; i<N; i++){\n dp[i][0] = max(dp[i-1][0] + (long long)nums[i], dp[i-1][1] + (long long)nums[i]);\n dp[i][1] = dp[i-1][0]-(long long)nums[i];\n }\n \n return max(dp[N-1][0], dp[N-1][1]);\n }\n};", "memory": "108223" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n int n=nums.size();\n vector<vector<long long>> dp(n+1, vector<long long>(2, -1e18));\n dp[1][0] = nums[0];\n for(int i=2; i<=n; i++){\n dp[i][0] = nums[i-1] + max(dp[i-1][0], dp[i-1][1]);\n dp[i][1] = dp[i-1][0] - nums[i-1];\n }\n return max(dp[n][0], dp[n][1]);\n }\n};", "memory": "108223" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n vector<vector<long long>> dp(nums.size()+1, vector<long long>(2, 0));\n\n for(int i=nums.size()-1 ; i>=0 ; i--) {\n for(int sign=1 ; sign>=0 ; sign--) {\n long long opt1 = nums[i] + dp[i+1][1];\n long long opt2 = (sign == 0 ? nums[i] : -nums[i]) + dp[i+1][!sign];\n\n dp[i][sign] = max(opt1, opt2);\n }\n }\n\n return dp[0][0];\n }\n};", "memory": "109616" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n int n=nums.size();\n if(n<=0) return 0;\n if(n==1) return nums[0];\n vector<vector<long>>dp(n, vector<long>(2, 0));\n vector<int>curr(2, 0), prev(2, 0);\n dp[0][0]=nums[0];\n dp[0][1]=nums[0];\n dp[1][0]=nums[0]+nums[1];\n dp[1][1]=nums[0]-nums[1];\n\n for(int i=1; i<n; i++) {\n dp[i][0] = max(dp[i-1][0], dp[i-1][1]) + nums[i];\n dp[i][1] = dp[i-1][0] - nums[i];\n }\n\n return max(dp[n-1][0], dp[n-1][1]);\n }\n};\n\n// 1 -2 3 4\n// ", "memory": "111008" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n int n=nums.size();\n if(n<=0) return 0;\n if(n==1) return nums[0];\n vector<vector<long>>dp(n, vector<long>(2, 0));\n vector<long>curr(2, 0), prev(2, 0);\n // dp[0][0]=nums[0];\n // dp[0][1]=nums[0];\n prev[0]=nums[0];\n prev[1]=nums[0];\n\n for(int i=1; i<n; i++) {\n // dp[i][0] = max(dp[i-1][0], dp[i-1][1]) + nums[i];\n // dp[i][1] = dp[i-1][0] - nums[i];\n curr[0] = max(prev[0], prev[1]) + nums[i];\n curr[1] = prev[0] - nums[i];\n prev=curr;\n }\n\n // return max(dp[n-1][0], dp[n-1][1]);\n return max(curr[0], curr[1]);\n }\n};\n\n// 1 -2 3 4\n// ", "memory": "111008" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n#define ll long long\nll f(int i,int c,int n,vector<int>&nums,vector<vector<ll>>&dp){\n if(i>=n)return 0;\n if(dp[i][c]!=-1)return dp[i][c];\n ll int p=0,np=0;\n p=pow(-1,c)*nums[i]+f(i+1,0,n,nums,dp);\n np=pow(-1,c)*nums[i]+f(i+1,1-c,n,nums,dp);\n return dp[i][c]=max(p,np);\n}\n long long maximumTotalCost(vector<int>& nums) {\n int n=nums.size();\n vector<vector<ll>>dp(n+1,vector<ll>(2,0));\n for(int i=n-1;i>=0;i--){\n for(int c=1;c>=0;c--){\n ll p=0,np=0;\n p=pow(-1,c)*nums[i]+dp[i+1][0];\n np=pow(-1,c)*nums[i]+dp[i+1][1-c];\n dp[i][c]=max(p,np);\n }\n }\n return dp[0][0];\n // return f(0,0,n,nums,dp);\n }\n};", "memory": "112401" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#include<bits/stdc++.h>\n#include <climits>\n#undef INT64_MIN\n#define LLONG_MIN -9223372036854775808\nclass Solution {\npublic:\n long long dp[100012][2];\n int n;\n long long r(int b,int i,vector<int>&nums){\n if(i==n)return 0;\n if(dp[i][b]!=LLONG_MIN)return dp[i][b];\n long long s=LLONG_MIN,ts=LLONG_MIN;\n if(b){\n s=1LL*nums[i]+r(0,i+1,nums);\n return dp[i][b]=s;\n }\n s=-(1LL*nums[i])+r(1,i+1,nums);\n ts=1LL*nums[i]+r(0,i+1,nums);\n return dp[i][b]=max(s,ts);\n }\n \n long long maximumTotalCost(vector<int>& nums) {\n n=nums.size();\n vector<vector<long long>> dp(n+2,vector<long long>(2,LLONG_MIN));\n dp[0][0]=nums[0];\n if(n>1)\n dp[1][0]=nums[0]+nums[1];\n if(n>1)\n dp[1][1]=nums[0]-nums[1];\n for(int i=2;i<n;i++){\n for(int j=0;j<2;j++){\n if(j==0)dp[i][j]=max(dp[i-1][0],dp[i-1][1])+1LL*nums[i];\n else{\n dp[i][j]=dp[i-1][j-1]-1LL*nums[i]; \n }\n }\n }\n return max(dp[n-1][0],dp[n-1][1]);\n }\n};", "memory": "113793" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "typedef long long int;\nclass Solution {\npublic:\n const long long inf=1e18+5;\n long long maximumTotalCost(vector<int>& nums) {\n int n=nums.size();\n vector<int>a=nums;\n // dp[i][par] -> start new par or continue last par\n // start new par\n // dp[i][1]=a[i]+max(dp[i-1][par],dp[i-1][par^1]);\n // continue prv par\n // dp[i][par]=dp[i-1][par^1]+((par)?a[i]:a[i-1])\n vector<vector<long long>>dp(n+1,vector<long long>(2,-inf));\n dp[0][0]=0;\n for(int i=1;i<=n;i++){\n for(int j=0;j<2;j++){\n if(j){\n dp[i][j]=a[i-1]+max(dp[i-1][0],dp[i-1][1]);\n }\n dp[i][j]=max(dp[i][j],dp[i-1][j^1]+((j)?a[i-1]:-a[i-1]));\n }\n }\n return max(dp[n][0],dp[n][1]);\n }\n};", "memory": "115186" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<long>> dpvalues;\n long long maximumTotalCost(vector<int>& nums) {\n //inner vector to contain flip or not flip\n dpvalues = vector<vector<long>>(nums.size(),vector<long>(2,-1));\n return flipper(0,false,nums); \n }\n //flip is true if previous is a[i-1] flipped, false if not\n long long flipper(int i,bool flip,vector<int>&nums){\n if(i == nums.size()){\n return 0;\n }\n int index = 0;\n if(flip)index = 1;\n\n if(dpvalues[i][index]!=-1)return dpvalues[i][index];\n\n if(flip){\n dpvalues[i][1] = -nums[i] + flipper(i + 1,false,nums);\n return dpvalues[i][1];\n }\n dpvalues[i][0] = nums[i] + max(flipper(i + 1,true,nums),flipper(i + 1,false,nums));\n return dpvalues[i][0];\n }\n};", "memory": "115186" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n long long helper(int i, int isStart, int sign, vector<int> &nums, vector<vector<vector<long long>>> &dp){\n\n if(i==nums.size()){\n return 0;\n }\n\n if(dp[isStart][sign][i]!=-1e15){\n return dp[isStart][sign][i];\n }\n long long ans = -1e15;\n if(isStart == 0){\n ans = max(ans,nums[i]+helper(i+1,1,1,nums,dp));\n }\n else{\n\n if(sign==0){\n ans = max(ans, nums[i]+helper(i+1,1,1,nums,dp));\n ans = max(ans, helper(i,0,0,nums,dp));\n }\n else{\n ans = max(ans, -nums[i]+helper(i+1,1,0,nums,dp));\n ans = max(ans, helper(i,0,0,nums,dp));\n }\n \n }\n\n return dp[isStart][sign][i] = ans;\n }\n long long maximumTotalCost(vector<int>& nums) {\n \n int n = nums.size();\n vector<vector<vector<long long>>> dp(2, vector<vector<long long>>(2, vector<long long>(n,-1e15)));\n return helper(0,0,0,nums, dp);\n }\n};", "memory": "116578" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\nprivate:\n long long f(int index, int sign, vector<int>& nums, vector<vector<long long>>& dp){\n if(index == nums.size()-1){\n if(nums[index] >= 0) return nums[index];\n else return sign * nums[index];\n }\n\n if(dp[index][sign+1] != -1) return dp[index][sign+1];\n\n long long take = (sign * nums[index]) + f(index+1, sign * -1, nums, dp);\n\n long long notTake = nums[index] + f(index+1, 1, nums, dp);\n\n return dp[index][sign+1] = max(take, notTake);\n\n }\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n // In DP, we dont use prev here and only sign as for new we will put the sign as 1\n // It cant be solved using greedy, vreck prev solutions\n int n = nums.size();\n // vector<vector<long long>> dp(n, vector<long long>(3, -1));\n // return f(0, 1, nums, dp);\n\n // Tabulation\n vector<vector<long long>> dp(n, vector<long long>(3, 0));\n\n dp[n-1][0] = nums[n-1] >= 0 ? nums[n-1] : -nums[n-1];\n dp[n-1][2] = nums[n-1];\n\n for(int index = n-2 ; index >= 0 ; index--){\n for(int sign = -1 ; sign <= 1 ; sign+=2){\n long long take = (sign * nums[index]) + dp[index+1][(sign * -1) + 1];\n\n long long notTake = nums[index] + dp[index+1][1 + 1];\n\n dp[index][sign+1] = max(take, notTake);\n }\n }\n\n return dp[0][2];\n }\n};", "memory": "117971" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long helper(int i,vector<int>& nums,bool flag,vector<vector<long long>>& dp){\n if(i==nums.size()){\n return 0;\n }\n long long ops1,ops2;\n if(dp[i][flag]!=-1){\n return dp[i][flag];\n }\n if(flag==1){\n ops1=nums[i]+helper(i+1,nums,!flag,dp);\n ops2=nums[i]+helper(i+1,nums,flag,dp);\n }else{\n ops1=helper(i+1,nums,!flag,dp)-nums[i];\n ops2=helper(i+1,nums,!flag,dp)-nums[i];\n }\n return dp[i][flag]=max(ops1,ops2);\n }\n long long maximumTotalCost(vector<int>& nums) {\n int n=nums.size();\n vector<vector<long long>>dp(n+1,vector<long long>(2,-1));\n long long ans=helper(0,nums,1,dp);\n return ans;\n }\n};", "memory": "119363" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long f(vector<int> &nums, int i, int l, vector<vector<long long>> &dp){\n if(i==nums.size()) return 0;\n if(dp[i][l] != INT_MIN) return dp[i][l];\n long long np,p;\n if(l){\n p = f(nums,i+1,!l,dp) + nums[i];\n }\n else p = f(nums,i+1,!l,dp) - nums[i];\n np = f(nums,i+1,0,dp) + nums[i];\n return dp[i][l] = max(np, p);\n }\n long long maximumTotalCost(vector<int>& nums) {\n int n = nums.size();\n vector<vector<long long>> dp(n, vector<long long>(2, INT_MIN));\n return f(nums, 0, 1, dp);\n }\n};", "memory": "120756" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long f (int ind, int fl, vector <int> &nums, vector <vector <long long>> &dp){\n if (ind == nums.size()) return 0;\n\n if (dp[ind][fl] != -1) return dp[ind][fl];\n\n long long p = 0, np = 0;\n if (fl){\n p = f (ind + 1, !fl, nums, dp) - nums[ind];\n }\n else{\n p = f (ind + 1, !fl, nums, dp) + nums[ind];\n }\n np = f (ind + 1, 1, nums, dp) + nums[ind];\n\n return dp[ind][fl] = max (p, np);\n }\n long long maximumTotalCost(vector<int>& nums) {\n int n = nums.size();\n vector <vector <long long>> dp (n, vector <long long> (2, -1));\n return f (0, 0, nums, dp);\n }\n};", "memory": "122148" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#define ll long long\nclass Solution {\npublic:\n ll solve(vector<int> &nums, int i, bool neg, vector<vector<ll>> &dp){ \n if(i >= nums.size())\n return 0;\n\n if(dp[i][neg] != -1e9)\n return dp[i][neg];\n\n ll negTake = -1e8, take = -1e8;\n if(neg){\n negTake = (-nums[i]) + solve(nums, i + 1, false, dp);\n }\n take = (nums[i]) + solve(nums, i + 1, true, dp);\n \n return dp[i][neg] = max(take, negTake);\n\n }\n\n public:\n long long maximumTotalCost(vector<int>& nums) {\n int n = nums.size();\n\n vector<vector<ll>> dp(n, vector<ll> (2, -1e9));\n int i = 1; bool neg = true;\n return (ll) nums[0] + solve(nums, i, neg, dp);\n }\n};", "memory": "122148" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "int IO = []{\n ios::sync_with_stdio(false); cin.tie(nullptr); return 0;\n}();\n\nclass Solution {\npublic:\n #define ll long long\n ll find(int flag,int curr,int n,vector<int>&nums,vector<vector<ll> > &dp)\n {\n if(curr>=n)\n return 0;\n \n if(dp[curr][flag]!=-1)\n return dp[curr][flag];\n \n ll temp=LLONG_MIN;\n if(flag==0)\n temp=max(temp,nums[curr]+find(1,curr+1,n,nums,dp));\n else\n temp=max(temp,nums[curr]*-1+find(0,curr+1,n,nums,dp));\n\n temp=max(temp,nums[curr]+find(1,curr+1,n,nums,dp));\n return dp[curr][flag]= temp;\n }\n long long maximumTotalCost(vector<int>& nums) {\n int n=nums.size();\n vector<vector<ll> > dp(n+1,vector<ll> (2,-1));\n return find(0,0,n,nums,dp);\n \n }\n};\n\n/*\n1 -2 3 4\n1 3 6 2\n-1 -3 -6 -2\n*/", "memory": "123541" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#define ll long long\nclass Solution {\npublic:\nint n;vector<int>a;\n vector<vector<ll>>dp;\n ll rec(int l,int s){\n if(l==n)return 0;\n auto &ans=dp[l][s];\n if(ans!=-1e14)return ans;\n ans=max((s?a[l]:-a[l])+rec(l+1,1),(s?a[l]:-a[l])+rec(l+1,!s));//new subarray\n return ans;\n }\n long long maximumTotalCost(vector<int>& nums) {\n a=nums;n=a.size();\n dp.resize(n,vector<ll>(2,-1e14));\n return rec(0,1);\n }\n};", "memory": "123541" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\n long long solve(int i, int sign, int n, vector<int>& nums, vector<vector<long long>> &dp){\n if(i==n) return 0;\n if(dp[i][sign]!=-1) return dp[i][sign];\n long long a = 0, b=0;\n if(sign==0){\n a = nums[i]+solve(i+1,0,n,nums,dp);\n b = -1*nums[i]+solve(i+1,1,n,nums,dp);\n }else{\n a = nums[i]+solve(i+1,0,n,nums,dp);\n return dp[i][sign] = a;\n }\n return dp[i][sign] = max(a,b);\n }\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n int n = nums.size();\n vector<vector<long long>> dp(n,vector<long long>(2,-1));\n long long ans = nums[0]+solve(1,0,n,nums,dp);\n return ans;\n }\n};", "memory": "124933" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long solve(int s,int &n,vector<vector<long long>> &dp,vector<int> &v,int k){\n if(s>=n)return 0;\n if(dp[s][k]!=LONG_LONG_MIN)return dp[s][k];\n long long ans1 = LONG_LONG_MIN,ans2 = LONG_LONG_MIN;\n if(k==0){\n ans1 = v[s]+solve(s+1,n,dp,v,1);\n ans2 = v[s]+solve(s+1,n,dp,v,0);\n }else{\n ans1 = -v[s]+solve(s+1,n,dp,v,0);\n }\n return dp[s][k] = max(ans1,ans2);\n }\n long long maximumTotalCost(vector<int>& v) {\n int n = v.size();\n vector<vector<long long >> dp(n+1,vector<long long>(2,LONG_LONG_MIN));\n return solve(0,n,dp,v,0);\n }\n};", "memory": "124933" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n#define ll long long\nll f(int i,int c,int n,vector<int>&nums,vector<vector<ll>>&dp){\n if(i>=n)return 0;\n if(dp[i][c]!=-1)return dp[i][c];\n ll int p=0,np=0;\n p=pow(-1,c)*nums[i]+f(i+1,0,n,nums,dp);\n np=pow(-1,c)*nums[i]+f(i+1,1-c,n,nums,dp);\n return dp[i][c]=max(p,np);\n}\n long long maximumTotalCost(vector<int>& nums) {\n int n=nums.size();\n vector<vector<ll>>dp(n+1,vector<ll>(2,-1));\n return f(0,0,n,nums,dp);\n }\n};", "memory": "126326" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\n private:\n typedef long long ll;\n vector<vector<ll>> dp;\n ll n;\n ll func(vector<int>&nums,int id,int f){\n if(id>=n) return 0;\n if(dp[id][f]!=-1) return dp[id][f];\n ll take = -1e15,ntake = -1e15;\n if(f==1){\n ntake = (-nums[id])+func(nums,id+1,0);\n }\n take = nums[id]+func(nums,id+1,1);\n return dp[id][f] = max(take,ntake);\n }\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n n = nums.size();\n dp.resize(n+1,vector<ll>(3,-1));\n return nums[0]+func(nums,1,1);\n \n }\n};", "memory": "126326" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\n private:\n typedef long long ll;\n vector<vector<ll>> dp;\n ll n;\n ll func(vector<int>&nums,int id,int f){\n if(id>=n) return 0;\n if(dp[id][f]!=-1) return dp[id][f];\n ll take = -1e15,ntake = -1e15;\n if(f==1){\n ntake = (-nums[id])+func(nums,id+1,0);\n }\n take = nums[id]+func(nums,id+1,1);\n return dp[id][f] = max(take,ntake);\n }\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n n = nums.size();\n dp.resize(n+1,vector<ll>(3,-1));\n return nums[0]+func(nums,1,1);\n \n }\n};", "memory": "127718" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\n private:\n typedef long long ll;\n vector<vector<ll>> dp;\n ll n;\n ll func(vector<int>&nums,int id,int f){\n if(id>=n) return 0;\n if(dp[id][f]!=-1) return dp[id][f];\n ll take = -1e15,ntake = -1e15;\n if(f==1){\n ntake = (-nums[id])+func(nums,id+1,0);\n }\n take = nums[id]+func(nums,id+1,1);\n return dp[id][f] = max(take,ntake);\n }\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n n = nums.size();\n dp.resize(n+1,vector<ll>(3,-1));\n return nums[0]+func(nums,1,1);\n \n }\n};", "memory": "129111" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\n private:\n typedef long long ll;\n vector<vector<ll>> dp;\n ll n;\n ll func(vector<int>&nums,int id,int f){\n if(id>=n) return 0;\n if(dp[id][f]!=-1) return dp[id][f];\n ll take = -1e15,ntake = -1e15;\n if(f==1){\n ntake = (-nums[id])+func(nums,id+1,0);\n }\n take = nums[id]+func(nums,id+1,1);\n return dp[id][f] = max(take,ntake);\n }\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n n = nums.size();\n dp.resize(n+1,vector<ll>(3,-1));\n return nums[0]+func(nums,1,1);\n \n }\n};", "memory": "130503" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n typedef long long ll;\n ll n;\n ll dpf(vector<int>&nums,int i,int f,vector<vector<ll>>& dp){\n if(i>=n) return 0;\n if(dp[i][f]!=-1) return dp[i][f];\n\n ll p=INT_MIN,np=INT_MIN;\n\n if(f==1) np=(-nums[i]+dpf(nums,i+1,0,dp));\n\n p=nums[i]+dpf(nums,i+1,1,dp);\n\n return dp[i][f]=max(p,np);\n }\n long long maximumTotalCost(vector<int>& nums) {\n n=nums.size();\n vector<vector<ll>> dp(n+1,vector<ll>(3,-1));\n\n return nums[0]+dpf(nums,1,1,dp);\n }\n};", "memory": "130503" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint n;\n long long solve(int i,int flag,vector<int>&nums, vector<vector<long long>>&dp){\n if(i==n)return 0;\n if(dp[i][flag+1]!=-1)return dp[i][flag+1];\n long long take=nums[i]*flag+solve(i+1,flag*(-1),nums,dp);\n long long notTake=nums[i]+solve(i+1,-1,nums,dp);\n return dp[i][flag+1]=max(take,notTake);\n }\n long long maximumTotalCost(vector<int>& nums) {\n n=nums.size();\n vector<vector<long long>>dp(n,vector<long long>(3,-1));\n return solve(0,1,nums,dp);\n }\n};", "memory": "131896" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long recur(int i ,int n,vector<int>&nums,bool take,int sign,vector<vector<long long>>&dp){\n if(i==n)return 0;\n if(dp[i][sign+1]!=-1)return dp[i][sign+1];\n long long x=0;\n \n if(take){\n x+=nums[i]*sign+recur(i+1,n,nums,true,sign*-1,dp);\n }\n long long y=nums[i]+recur(i+1,n,nums,true,-1,dp);\n if(x==0)x=LONG_LONG_MIN;\n return dp[i][sign+1]=max(x,y);\n }\n long long maximumTotalCost(vector<int>& nums) {\n int n=nums.size();\n vector<vector<long long>>dp(n+1,vector<long long>(3,-1));\n return recur(0,n,nums,false,1,dp);\n }\n};", "memory": "131896" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\n\n private:\n\n long long solve(int indx , int mul , vector<int>&nums , int n , \n vector<vector<long long>>&dp)\n {\n if(indx >= n)\n return 0;\n\n if(dp[indx][mul + 1] != -1)\n return dp[indx][mul + 1];\n\n long long same = nums[indx]*mul + solve(indx + 1 , mul*-1 , nums , n , dp);\n\n long long newSub = nums[indx] + solve(indx + 1 , -1 , nums , n , dp);\n\n return dp[indx][mul + 1] = max(same , newSub);\n\n }\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n\n int n = nums.size();\n\n vector<vector<long long>>dp(n , vector<long long>(3 , -1));\n\n return solve(0 , 1 , nums , n , dp);\n \n }\n};", "memory": "133288" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long ans(int i,int mul,vector<int>& nums,vector<vector<long long>>&dp){\n int n=nums.size();\n if(i==n)\n return 0;\n long long sum=0;\n if(dp[i][mul+1]!=-1)\n return dp[i][mul+1];\n if(mul==1){\n sum+=nums[i];\n long long temp1=ans(i+1,1,nums,dp),temp2=ans(i+1,-1,nums,dp);\n sum=sum+max(temp1,temp2);\n }\n else{\n sum+=(-1*nums[i]+ans(i+1,1,nums,dp));\n }\n return dp[i][mul+1]=sum;\n }\n long long maximumTotalCost(vector<int>& nums) {\n int n=nums.size();\n vector<vector<long long>>dp(n,vector<long long>(3,-1));\n return ans(0,1,nums,dp);\n }\n};", "memory": "133288" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n unordered_map <int, long long> costs;\n long long helper(int index, vector <int> &nums) {\n if (index == nums.size()) return 0;\n if (index == (nums.size()-1)) return nums[index];\n if (costs.find(index) != costs.end()) {\n return costs[index];\n }\n long long int costvalue = max(nums[index] - nums[index+1] + helper(index+2, nums), nums[index] + helper(index+1, nums));\n costs.insert({ index, costvalue });\n return costvalue;\n }\n long long maximumTotalCost(vector<int>& nums) {\n return helper(0, nums);\n }\n};", "memory": "134681" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n int n=ssize(nums);\n\n vector<vector<long long>>dp(n+1,vector<long long>(2,-1));\n function<long long(long long,long long)> f=[&](long long i,long long pow) -> long long {\n if(i==n) {\n return 0;\n }\n\n if(dp[i][pow==1?1:0]!=-1) return dp[i][pow==1?1:0];\n\n long long cut=nums[i]*pow+f(i+1,1);\n long long notcut=nums[i]*pow+f(i+1,-pow);\n\n return dp[i][pow==1?1:0]=max(cut,notcut);\n };\n\n return f(0,1);\n }\n};", "memory": "136073" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long maximumTotalCost(vector<int>& nums) {\n int n=ssize(nums);\n\n vector<vector<long long>>dp(n+1,vector<long long>(2,-1));\n function<long long(long long,long long)> f=[&](long long i,long long pow) -> long long {\n if(i==n) {\n return 0;\n }\n\n if(dp[i][pow==1?1:0]!=-1) return dp[i][pow==1?1:0];\n\n long long cut=nums[i]*pow+f(i+1,1);\n long long notcut=nums[i]*pow+f(i+1,-pow);\n\n return dp[i][pow==1?1:0]=max(cut,notcut);\n };\n\n return f(0,1);\n }\n};", "memory": "136073" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n unordered_map<long long, long long> dp;\n long long f(int idx, vector<int> &nums){\n if(idx == nums.size()){\n return 0;\n }\n if(dp.count(idx)) return dp[idx];\n\n long long take1 = nums[idx] + f(idx + 1, nums);\n long long take2 = LLONG_MIN;\n if(idx + 1 < nums.size() && nums[idx + 1] < 0){\n take2 = nums[idx] - nums[idx + 1] + f(idx + 2, nums);\n }\n\n return dp[idx] = max(take1, take2);\n }\n\n long long maximumTotalCost(vector<int>& nums) {\n return f(0, nums);\n }\n};", "memory": "137466" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplate<typename T> istream & operator>>(istream & in, vector<T> & lst)\n{ for (auto & e : lst) in >> e; return in; }\n#define endl '\\n'\n#define F first\n#define pb push_back\n#define pf push_front\n#define pob pop_back\n#define pof pop_front\n#define S second\n#define MP make_pair\n// typedef long long ll;\ntypedef long double ld;\ntypedef pair<ll, ll> ii;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<ii> vii;\ntypedef set<ll> si;\ntypedef map<string, ll> msi;\n#define all(v) v.begin(),v.end()\n#define REP(i, a, b) \\\nfor (ll i = ll(a); i <= ll(b); i++)\n#define RREP(i, a, b) \\\nfor (ll i = ll(a); i >= ll(b); i--)\n#define ohyes cout<<\"YES\\n\"\n#define ohno cout<<\"NO\\n\"\n#define getvec(v,a,b) \\\nfor (ll i = a; i <=b; i++) \\\n cin >> v[i];\n#define printvec(v,a,b) {for (ll i = a; i <=b; i++){cout << v[i] << \" \";}cout<<'\\n';}\n#define get2dvec(v,a,b,c,d) \\\nfor (ll i = a; i <=b; i++) \\\n for (ll j = c; j <=d; j++) \\\n cin >> v[i][j];\n#define print2dvec(v,a,b,c,d) for (ll i = a; i <=b; i++){for (ll j = c; j <=d; j++){cout << v[i][j]<<\" \";}cout<<'\\n';}\n#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n// ll mod = 998244353;\n// ll mod = 1e9 + 7;\nll _t,cs;\n// cout<<\"Case \"<<cs-_t<<\": \"<<ans<<'\\n';\n// memset(dp,-1,sizeof(dp));\nclass Solution {\npublic:\n ll n;\n vvi dp,vis;\n vector<int> v;\n ll rec(ll lev,ll pr){\n if(lev>=n)return 0;\n auto &ans=dp[lev][pr];\n if(vis[lev][pr])return ans;\n ans=-1e14;\n if(pr==0){\n ll num=rec(lev+1,1);\n // cout<<lev<<\": \"<<v[lev]<<\" \"<<num<<'\\n';\n // cout<<'\\n';\n ans=max(ans,v[lev]+num);\n }else{\n ll num1=rec(lev+1,1);\n ll num2=rec(lev+1,0);\n // cout<<lev<<\": \"<<v[lev]<<\" \"<<num1<<'\\n';\n // cout<<lev<<\": \"<<-v[lev]<<\" \"<<num2<<'\\n';\n // cout<<'\\n';\n ans=max(ans,v[lev]+num1);\n ans=max(ans,-v[lev]+num2);\n }\n vis[lev][pr]=1;\n return ans;\n }\n long long maximumTotalCost(vector<int>& nums) {\n n=nums.size();\n v=nums;\n dp.clear();\n dp.assign(n+10,vi(2,0));\n \n vis.clear();\n vis.assign(n+10,vi(2,0));\n // return n;\n return rec(0,0);\n }\n};", "memory": "138858" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<vector<long long>>> dp;\n\n long long f(int i, int fresh, int sign, vector<int>& nums){\n if(i == nums.size()) return 0;\n if(dp[i][fresh][sign] != -1e15) return dp[i][fresh][sign];\n long long ans = -1e15;\n if(fresh == 1){\n ans = max(f(i+1, 1, 1, nums)+nums[i], f(i+1, 0, 1, nums)+nums[i]);\n } else {\n if(sign == 1){\n ans = max(f(i+1, 0, !sign, nums)-nums[i], f(i+1, 1, !sign, nums)-nums[i]);\n } else {\n ans = max(f(i+1, 0, !sign, nums)+nums[i], f(i+1, 1, !sign, nums)+nums[i]);\n }\n }\n\n return dp[i][fresh][sign] = ans;\n \n }\n long long maximumTotalCost(vector<int>& nums) {\n int n = nums.size(); \n dp.resize(n, vector<vector<long long>>(2, vector<long long>(2, -1e15)));\n dp[0][1][1] = (long long)nums[0];\n dp[0][1][0] = (long long)nums[0];\n dp[0][0][0] = (long long)nums[0];\n dp[0][0][1] = (long long)nums[0];\n\n for(int i = 1; i< n; i++){\n for(int fresh = 0; fresh < 2; fresh++){\n for(int sign = 0; sign < 2; sign++){\n if(fresh == 1){\n dp[i][fresh][sign] = max(dp[i-1][1][1]+nums[i], dp[i-1][0][1]+nums[i]);\n } else {\n if(sign == 1){\n dp[i][fresh][sign] = max(dp[i-1][0][!sign]-nums[i], dp[i-1][1][!sign]-nums[i]);\n } else {\n dp[i][fresh][sign] = max(dp[i-1][0][!sign]+nums[i], dp[i-1][1][!sign]+nums[i]);\n }\n }\n }\n }\n }\n\n long long ret = max(dp[n-1][0][0], max(dp[n-1][0][1], max(dp[n-1][1][0], dp[n-1][1][1])));\n return ret;\n }\n};", "memory": "140251" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long ff(vector<int> &a,int n,int id,int c,int f,vector<vector<vector<long long>>> &dp){\n if(id>n) return 0;\n if(dp[id][c][f]!=-1) return dp[id][c][f];\n if(c>0){\n if(f){\n return dp[id][c][f]=max(a[id]+ff(a,n,id+1,1,0,dp),a[id]+ff(a,n,id+1,0,1,dp));\n }\n else return dp[id][c][f]=max(-a[id]+ff(a,n,id+1,1,1,dp),-a[id]+ff(a,n,id+1,0,1,dp));\n }\n else{\n if(f){\n return dp[id][c][f]=max(a[id]+ff(a,n,id+1,1,0,dp),a[id]+ff(a,n,id+1,0,1,dp));\n }\n // else return dp[id][c][f]=-a[id]+ff(a,n,id+1,c+1,1,dp);\n }\n return dp[id][c][f]=0;\n }\n long long maximumTotalCost(vector<int>& a) {\n int n=a.size();\n vector<vector<vector<long long>>> dp(n+1,vector<vector<long long>>(2,vector<long long>(2,0)));\n // return ff(a,n-1,0,0,1,dp);\n \n for(int i=n-1;i>=0;i--){\n \n for(int c=0;c<2;c++){\n for(int f=0;f<2;f++){\n if(c==0 && f){\n dp[i][c][f]=max(a[i]+dp[i+1][1][0],a[i]+dp[i+1][0][1]);\n }\n else{\n if(f){\n dp[i][c][f]=max(a[i]+dp[i+1][1][0],a[i]+dp[i+1][0][1]);\n }\n else dp[i][c][f]=max(-a[i]+dp[i+1][1][1],-a[i]+dp[i+1][0][1]);\n }\n }\n }\n }\n return dp[0][0][1];\n }\n};", "memory": "141643" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<vector<long long>>> dp;\n\n long long f(int i, int fresh, int sign, vector<int>& nums){\n if(i == nums.size()) return 0;\n if(dp[i][fresh][sign] != -1e15) return dp[i][fresh][sign];\n long long ans = -1e15;\n if(fresh == 1){\n ans = max(f(i+1, 1, 1, nums)+nums[i], f(i+1, 0, 1, nums)+nums[i]);\n } else {\n if(sign == 1){\n ans = max(f(i+1, 0, !sign, nums)-nums[i], f(i+1, 1, !sign, nums)-nums[i]);\n } else {\n ans = max(f(i+1, 0, !sign, nums)+nums[i], f(i+1, 1, !sign, nums)+nums[i]);\n }\n }\n\n return dp[i][fresh][sign] = ans;\n \n }\n long long maximumTotalCost(vector<int>& nums) {\n int n = nums.size(); \n dp.resize(n, vector<vector<long long>>(2, vector<long long>(2, -1e15)));\n return f(0, 1, 0, nums);\n }\n};", "memory": "143036" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#define ll long long\n\nclass Solution {\npublic:\n\n vector<vector<vector<ll>>> dp;\n\n ll solve(int i, int isStart, int sign, vector<int> &a){\n\n if(i == a.size()) return 0;\n\n if(dp[i][isStart][sign] != -1e15) return dp[i][isStart][sign];\n\n ll ans = -1e15;\n\n if(isStart == 0){\n ans = max(ans, a[i] + solve(i+1, 1, sign^1, a)); //take (fresh start)\n }\n else{\n if(sign == 1){\n //neg\n ans = max(ans, -a[i] + solve(i+1, 1, sign^1, a)); //take\n ans = max(ans, -0 + solve(i, 0, 0, a)); //not-take\n }\n else{\n //pos\n ans = max(ans, a[i] + solve(i+1, 1, sign^1, a)); //take\n ans = max(ans, 0 + solve(i, 0, 0, a)); //not-take\n }\n }\n\n return dp[i][isStart][sign] = ans;\n\n }\n\n long long maximumTotalCost(vector<int> &a) {\n int n = a.size();\n dp = vector<vector<vector<ll>>> (n+1, vector<vector<ll>> (2, vector<ll>(2, -1e15)));\n\n return solve(0, 0, 0, a);\n }\n};", "memory": "144428" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#define ll long long\nclass Solution {\npublic:\n vector<vector<vector<ll>>> dp;\n ll max_sum(int i, int start, int sign, vector<int> &nums){\n if(i==nums.size()){\n return 0 ;\n }\n if(dp[i][start][sign]!= -1e15) return dp[i][start][sign];\n ll ans = -1e15 ;\n if(start==0) { // start \n ans = max(ans, nums[i] + max_sum(i+1,1, 1, nums));\n }\n else{\n if(sign == 0) { // positive\n ans = max(ans, nums[i] + max_sum(i+1, 1, 1, nums));\n ans = max(ans, max_sum(i, 0, 0, nums) );\n }\n else{\n ans = max(ans, -nums[i] + max_sum(i+1, 1, 0, nums));\n ans = max(ans, max_sum(i, 0, 0, nums) );\n }\n }\n return dp[i][start][sign] = ans;\n }\n long long maximumTotalCost(vector<int>& nums) {\n int n = nums.size();\n dp = vector<vector<vector<ll>>>(n , vector<vector<ll>>(2, vector<ll>(2, -1e15)));\n return max_sum(0, 0, 0, nums);\n }\n};", "memory": "145821" }
3,464
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "typedef long long ll;\nclass Solution {\npublic:\nvector<vector<vector<ll>>>dp;\nll solve(int i,int iss,int sign,vector<int>&nums){\n if(i==nums.size())return 0;\n if(dp[i][iss][sign]!=-1e15)return dp[i][iss][sign];\n ll ans=-1e15;\n if(iss==0){\n ans=max(ans,nums[i]+solve(i+1,1,sign^1,nums));\n }\n else{\n if(sign==1){\n ans=max(ans,-nums[i]+solve(i+1,1,sign^1,nums));\n ans=max(ans,solve(i,0,0,nums));\n }\n else{\n ans=max(ans,nums[i]+solve(i+1,1,sign^1,nums));\n ans=max(ans,solve(i,0,0,nums));\n }\n }\n return dp[i][iss][sign]=ans;\n}\n long long maximumTotalCost(vector<int>& nums) {\n int n=nums.size();\n dp=vector<vector<vector<ll>>>(n+1,vector<vector<ll>>(2,vector<ll>(2,-1e15)));\n return solve(0,0,0,nums);\n }\n};", "memory": "147213" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n constexpr int INF = std::numeric_limits<int>::max() / 4;\n const int m = static_cast<int>(grid.size()),\n n = static_cast<int>(grid[0].size());\n int result = m * n;\n auto minArea = [&](int i1, int j1, int i2, int j2)\n {\n int x1 = INF, y1 = INF;\n int x2 = -INF, y2 = -INF;\n for (int i = i1; i <= i2; i++)\n {\n for (int j = j1; j <= j2; j++)\n {\n if (grid[i][j] == 1)\n {\n x1 = std::min(x1, i);\n y1 = std::min(y1, j);\n x2 = std::max(x2, i);\n y2 = std::max(y2, j);\n }\n }\n }\n return ((x1 > x2) || (y1 > y2))?INF:(x2 - x1 + 1) * (y2 - y1 + 1);\n };\n\n for (int i1 = 0; i1 < m - 1; ++i1)\n for (int i2 = i1 + 1; i2 < m - 1; ++i2)\n result = std::min(result, minArea(0, 0, i1, n - 1)\n + minArea(i1 + 1, 0, i2, n - 1)\n + minArea(i2 + 1, 0, m - 1, n - 1));\n for (int j1 = 0; j1 < n - 1; ++j1)\n for (int j2 = j1 + 1; j2 < n - 1; ++j2)\n result = std::min(result, minArea(0, 0, m - 1, j1)\n + minArea(0, j1 + 1, m - 1, j2)\n + minArea(0, j2 + 1, m - 1, n - 1));\n for (int i = 0; i < m - 1; i++)\n for (int j = 0; j < n - 1; j++)\n result = std::min({result,\n minArea(0, 0, i, j)\n + minArea(0, j + 1, i, n - 1)\n + minArea(i + 1, 0, m - 1, n - 1),\n minArea(0, 0, i, n - 1)\n + minArea(i + 1, 0, m - 1, j)\n + minArea(i + 1, j + 1, m - 1, n - 1),\n minArea(0, 0, i, j)\n + minArea(i + 1, 0, m - 1, j)\n + minArea(0, j + 1, m - 1, n - 1),\n minArea(0, 0, m - 1, j)\n + minArea(0, j + 1, i, n - 1)\n + minArea(i + 1, j + 1, m - 1, n - 1)});\n return result;\n }\n};", "memory": "24400" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findArea(int x1,int y1,int x2,int y2,vector<vector<int>>& grid) {\n int left=y2,right=y1;\n int top=x2,bottom=x1;\n bool flag=false;\n for(int i=x1;i<=x2;i++){\n for(int j=y1;j<=y2;j++){\n if(grid[i][j]==1){\n flag=true;\n left=min(left,j);\n right=max(right,j);\n top=min(top,i);\n bottom=max(bottom,i);\n }\n }\n }\n if(!flag)\n return 1e9;\n int res= (right-left+1)*(bottom-top+1);\n return res;\n }\n\n int calculate(int x1,int y1,int x2,int y2,int parts,vector<vector<int>>& grid){\n if(parts==3)\n return findArea(x1,y1,x2,y2,grid);\n int res=1e9;\n for(int i=x1;i<x2;i++){\n res=min(res,findArea(x1,y1,i,y2,grid)+calculate(i+1,y1,x2,y2,parts+1,grid));\n res=min(res,calculate(x1,y1,i,y2,parts+1,grid)+findArea(i+1,y1,x2,y2,grid));\n }\n for(int j=y1;j<y2;j++){\n res=min(res,findArea(x1,y1,x2,j,grid)+calculate(x1,j+1,x2,y2,parts+1,grid));\n res=min(res,calculate(x1,y1,x2,j,parts+1,grid)+findArea(x1,j+1,x2,y2,grid));\n }\n return res;\n }\n\n int minimumSum(vector<vector<int>>& grid) {\n return calculate(0,0,grid.size()-1,grid[0].size()-1,1,grid);\n }\n};", "memory": "24500" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> m;\n int Calc(int x0,int x1,int y0,int y1)\n {\n int up=x1+1,dn=-1,le=y1+1,ri=-1;\n for(int i=x0;i<=x1;++i)\n {\n for(int j=y0;j<=y1;++j)\n {\n if(m[i][j])\n {\n up=min(up,i);\n dn=max(dn,i);\n le=min(le,j);\n ri=max(ri,j);\n }\n }\n }\n if(dn<0) return 0;\n return (dn-up+1)*(ri-le+1);\n }\n int minimumSum(vector<vector<int>>& grid) {\n int row=grid.size(),col=grid[0].size();\n m=move(grid);\n int ans=INT_MAX;\n for(int i1=1;i1<row-1;++i1)\n {\n int a1=Calc(0,i1-1,0,col-1);\n for(int i2=i1+1;i2<row;++i2)\n {\n int a2=Calc(i1,i2-1,0,col-1);\n int a3=Calc(i2,row-1,0,col-1);\n ans=min(ans,a1+a2+a3);\n }\n for(int j=1;j<col;++j)\n {\n int a2=Calc(i1,row-1,0,j-1);\n int a3=Calc(i1,row-1,j,col-1);\n ans=min(ans,a1+a2+a3);\n }\n }\n for(int i2=row-1;i2>0;--i2)\n {\n int a3=Calc(i2,row-1,0,col-1);\n for(int j=1;j<col;++j)\n {\n int a1=Calc(0,i2-1,0,j-1);\n int a2=Calc(0,i2-1,j,col-1);\n ans=min(ans,a1+a2+a3);\n }\n }\n for(int j1=1;j1<col-1;++j1)\n {\n int a1=Calc(0,row-1,0,j1-1);\n for(int j2=j1+1;j2<col;++j2)\n {\n int a2=Calc(0,row-1,j1,j2-1);\n int a3=Calc(0,row-1,j2,col-1);\n ans=min(ans,a1+a2+a3);\n }\n for(int i=1;i<row;++i)\n {\n int a2=Calc(0,i-1,j1,col-1);\n int a3=Calc(i,row-1,j1,col-1);\n ans=min(ans,a1+a2+a3);\n }\n }\n for(int j2=col-1;j2>0;--j2)\n {\n int a3=Calc(0,row-1,j2,col-1);\n for(int i=1;i<row;++i)\n {\n int a1=Calc(0,i-1,0,j2-1);\n int a2=Calc(i,row-1,0,j2-1);\n ans=min(ans,a1+a2+a3);\n }\n }\n return ans;\n }\n};", "memory": "24600" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n long long minimumArea(vector<vector<int>>& grid, int st_i, int en_i, int st_j, int en_j) {\n long long top = -1, bottom = -1, left = -1, right = -1;\n for(int i = st_i; i <= en_i; i++) {\n for(int j = st_j; j <= en_j; j++) {\n if(grid[i][j] == 1) {\n if(top == -1) top = i;\n bottom = i;\n }\n }\n }\n for(int i = st_j; i <= en_j; i++) {\n for(int j = st_i; j <= en_i; j++) {\n if(grid[j][i] == 1) {\n if(left == -1) left = i;\n right = i;\n }\n }\n }\n return (right - left + 1) * (bottom - top + 1);\n }\n \n int minimumSum(vector<vector<int>>& grid) {\n long long i, j, m = grid.size(), n = grid[0].size(), ans = 1e9, one, two, three;\n\n for(i = 0; i < m; i++) {\n one = minimumArea(grid, 0, i, 0, n - 1);\n for(j = 0; j < n; j++) {\n two = minimumArea(grid, i + 1, m - 1, 0, j);\n three = minimumArea(grid, i + 1, m - 1, j + 1, n - 1);\n ans = min(ans, one + two + three);\n }\n }\n \n for(j = 0; j < n; j++) {\n one = minimumArea(grid, 0, m - 1, 0, j);\n for(i = 0; i < m; i++) {\n two = minimumArea(grid, 0, i, j + 1, n - 1);\n three = minimumArea(grid, i + 1, m - 1, j + 1, n - 1);\n ans = min(ans, one + two + three);\n }\n }\n \n for(j = n - 1; j >= 0; j--) {\n one = minimumArea(grid, 0, m - 1, j + 1, n - 1);\n for(i = 0; i < m; i++) {\n two = minimumArea(grid, 0, i, 0, j);\n three = minimumArea(grid, i + 1, m - 1, 0, j);\n ans = min(ans, one + two + three);\n }\n }\n \n for(i = m - 1; i >= 0; i--) {\n one = minimumArea(grid, i + 1, m - 1, 0, n - 1);\n for(j = 0; j < n; j++) {\n two = minimumArea(grid, 0, i, 0, j);\n three = minimumArea(grid, 0, i, j + 1, n - 1);\n ans = min(ans, one + two + three);\n }\n }\n \n for(i = 0; i < m; i++) {\n for(j = i + 1; j < m; j++) {\n one = minimumArea(grid, 0, i, 0, n - 1);\n two = minimumArea(grid, i + 1, j, 0, n - 1);\n three = minimumArea(grid, j + 1, m - 1, 0, n - 1);\n ans = min(ans, one + two + three);\n }\n }\n \n for(i = 0; i < n; i++) {\n for(j = i + 1; j < n; j++) {\n one = minimumArea(grid, 0, m - 1, 0, i);\n two = minimumArea(grid, 0, m - 1, i + 1, j);\n three = minimumArea(grid, 0, m - 1, j + 1, n - 1);\n ans = min(ans, one + two + three);\n }\n }\n \n return ans;\n }\n};\n", "memory": "24600" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n constexpr int INF = std::numeric_limits<int>::max() / 4;\n const int m = static_cast<int>(grid.size()),\n n = static_cast<int>(grid[0].size());\n int result = m * n;\n auto minArea = [&](int i1, int j1, int i2, int j2)\n {\n int x1 = INF, y1 = INF;\n int x2 = -INF, y2 = -INF;\n for (int i = i1; i <= i2; i++)\n {\n for (int j = j1; j <= j2; j++)\n {\n if (grid[i][j] == 1)\n {\n x1 = std::min(x1, i);\n y1 = std::min(y1, j);\n x2 = std::max(x2, i);\n y2 = std::max(y2, j);\n }\n }\n }\n return ((x1 > x2) || (y1 > y2))?INF:(x2 - x1 + 1) * (y2 - y1 + 1);\n };\n\n for (int i1 = 0; i1 < m - 1; ++i1)\n for (int i2 = i1 + 1; i2 < m - 1; ++i2)\n result = std::min(result, minArea(0, 0, i1, n - 1)\n + minArea(i1 + 1, 0, i2, n - 1)\n + minArea(i2 + 1, 0, m - 1, n - 1));\n for (int j1 = 0; j1 < n - 1; ++j1)\n for (int j2 = j1 + 1; j2 < n - 1; ++j2)\n result = std::min(result, minArea(0, 0, m - 1, j1)\n + minArea(0, j1 + 1, m - 1, j2)\n + minArea(0, j2 + 1, m - 1, n - 1));\n for (int i = 0; i < m - 1; i++)\n for (int j = 0; j < n - 1; j++)\n result = std::min({result,\n minArea(0, 0, i, j)\n + minArea(0, j + 1, i, n - 1)\n + minArea(i + 1, 0, m - 1, n - 1),\n minArea(0, 0, i, n - 1)\n + minArea(i + 1, 0, m - 1, j)\n + minArea(i + 1, j + 1, m - 1, n - 1),\n minArea(0, 0, i, j)\n + minArea(i + 1, 0, m - 1, j)\n + minArea(0, j + 1, m - 1, n - 1),\n minArea(0, 0, m - 1, j)\n + minArea(0, j + 1, i, n - 1)\n + minArea(i + 1, j + 1, m - 1, n - 1)});\n return result;\n }\n};", "memory": "24700" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
2
{ "code": "static const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n const int m = grid.size(), n = grid[0].size();\n int tl[m][n], tr[m][n], bl[m][n],\n br[m][n], h[m][m], v[n][n];\n memset(tl, 0, sizeof(tl));\n memset(tr, 0, sizeof(tr));\n memset(bl, 0, sizeof(bl));\n memset(br, 0, sizeof(br));\n memset(h, 0, sizeof(h));\n memset(v, 0, sizeof(v));\n int l, r, t, b;\n int top[n], bottom[n];\n for (int j = 0; j < n; ++j) top[j] = -1;\n for (int i = 0; i < m; ++i) {\n l = -1;\n for (int j = 0; j < n; ++j) {\n if (grid[i][j] == 1) {\n if (top[j] == -1) top[j] = i;\n bottom[j] = i;\n }\n if (top[j] >= 0) {\n if (l == -1) {\n l = j, r = l;\n t = top[j];\n b = bottom[j];\n } else {\n r = j;\n t = min(t, top[j]);\n b = max(b, bottom[j]);\n }\n }\n tl[i][j] = (l == -1) ? 0 : (r - l + 1) * (b - t + 1);\n }\n }\n for (int j = 0; j < n; ++j) top[j] = -1;\n for (int i = 0; i < m; ++i) {\n l = -1;\n for (int j = n - 1; j >= 0; --j) {\n if (grid[i][j] == 1) {\n if (top[j] == -1) top[j] = i;\n bottom[j] = i;\n }\n if(top[j] >= 0) {\n if(l == -1) {\n l = j, r = l;\n t = top[j], b = bottom[j];\n } else {\n l = j;\n t = min(t, top[j]);\n b = max(b, bottom[j]);\n }\n }\n tr[i][j] = (l == -1) ? 0 : (r - l + 1) * (b - t + 1);\n }\n }\n for (int j = 0; j < n; ++j) top[j] = -1;\n for (int i = m - 1; i >= 0; --i) {\n l = -1;\n for (int j = 0; j < n; ++j) {\n if(grid[i][j] == 1) {\n if (top[j] == -1) bottom[j] = i;\n top[j] = i;\n }\n if (top[j] >= 0) {\n if(l == -1) {\n l = j, r = l;\n t = top[j], b = bottom[j];\n } else {\n r = j;\n t = min(t, top[j]);\n b = max(b, bottom[j]);\n }\n }\n bl[i][j] = (l == -1) ? 0 : (r - l + 1) * (b - t + 1);\n }\n }\n for (int j = 0; j < n; ++j) top[j] = -1;\n for (int i = m - 1; i >= 0; --i) {\n l = -1;\n for (int j = n - 1; j >= 0; --j) {\n if (grid[i][j] == 1) {\n if (top[j] == -1) bottom[j] = i;\n top[j] = i;\n }\n if (top[j] >= 0) {\n if(l == -1) {\n l = j, r = l;\n t = top[j], b = bottom[j];\n } else {\n l = j;\n t = min(t, top[j]);\n b = max(b, bottom[j]);\n }\n }\n br[i][j] = (l == -1) ? 0 : (r - l + 1) * (b - t + 1);\n }\n }\n int hends[m][2], vends[n][2];\n for (int j = 0; j < n; ++j) vends[j][0] = -1;\n for(int i = 0; i < m; ++i) {\n hends[i][0] = -1;\n for (int j = 0; j < n; ++j) {\n if (grid[i][j] == 1) {\n if (hends[i][0] == -1) hends[i][0] = j, hends[i][1] = j;\n else hends[i][1] = j;\n if (vends[j][0] == -1) vends[j][0] = i, vends[j][1] = i;\n else vends[j][1] = i;\n }\n }\n }\n for (int i = 0; i < m; ++i) {\n l = -1;\n for(int j = i; j >= 0; --j) {\n if (hends[j][0] >= 0) {\n if (l == -1) {\n l = hends[j][0];\n r = hends[j][1];\n t = j, b = j;\n } else {\n l = min(l, hends[j][0]);\n r = max(r, hends[j][1]);\n t = j;\n }\n }\n h[j][i] = (l == -1) ? 0 : (r - l + 1) * (b - t + 1);\n }\n }\n for (int i = 0; i < n; ++i) {\n l = -1;\n for (int j = i; j >= 0; --j) {\n if (vends[j][0] >= 0) {\n if (l == -1) {\n t = vends[j][0];\n b = vends[j][1];\n l = j, r = j;\n } else {\n t = min(t, vends[j][0]);\n b = max(b, vends[j][1]);\n l = j;\n }\n }\n v[j][i] = (l == -1) ? 0 : (r - l + 1) * (b - t + 1);\n }\n }\n int minarea = m * n;\n for (int i = 0; i < m; ++i) {\n for (int j = i + 1; j < m - 1; ++j) {\n if (h[0][i] + h[i + 1][j] + h[j + 1][m - 1] < minarea)\n minarea = h[0][i] + h[i + 1][j] + h[j + 1][m - 1];\n }\n }\n for (int i = 0; i < n; ++i) {\n for (int j = i + 1; j < n - 1; ++j) {\n if (v[0][i] + v[i + 1][j] + v[j + 1][n - 1] < minarea)\n minarea = v[0][i] + v[i + 1][j] + v[j + 1][n - 1];\n }\n }\n for (int i = 0; i < m - 1; ++i) {\n for (int j = 0; j < n - 1; ++j) {\n minarea = min({\n minarea,\n tl[i][j] + bl[i + 1][j] + v[j + 1][n - 1],\n v[0][j] + tr[i][j + 1] + br[i + 1][j + 1],\n h[i + 1][m - 1] + tl[i][j] + tr[i][j + 1],\n h[0][i] + bl[i + 1][j] + br[i + 1][j + 1]\n });\n }\n }\n return minarea;\n }\n};", "memory": "24800" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
2
{ "code": "#define pii pair<int, int>\n#define F first\n#define S second\n\nclass Solution {\n \n vector<vector<int>> grid;\n \n pair<pii, pii> MinRectangle(pii x, pii y) {\n int lft = x.S, rgt = y.S;\n for (; lft <= y.S; lft ++) {\n int one = 0;\n for (int i = x.F; i <= y.F; i ++) one += grid[i][lft];\n if (one > 0) break;\n }\n for (; rgt >= x.S; rgt --) {\n int one = 0;\n for (int i = x.F; i <= y.F; i ++) one += grid[i][rgt];\n if (one > 0) break;\n }\n \n int top = x.F, dwn = y.F;\n for (; top <= y.F; top ++) {\n int one = 0;\n for (int i = x.S; i <= y.S; i ++) one += grid[top][i];\n if (one > 0) break;\n }\n for (; dwn >= x.F; dwn --) {\n int one = 0;\n for (int i = x.S; i <= y.S; i ++) one += grid[dwn][i];\n if (one > 0) break;\n }\n \n if (lft > rgt) return {{x.F, x.S}, {x.F, x.S}}; // rectangle with area 1\n return {{top, lft}, {dwn, rgt}};\n }\n \n int Area (pair<pii, pii> r) {\n return (r.S.F - r.F.F + 1) * (r.S.S - r.F.S + 1);\n }\n \n int Min2Rectangle (pii x, pii y) {\n int ans = Area({x, y});\n \n for (int col = x.S; col < y.S; col ++) {\n auto lft = MinRectangle ({x.F, x.S}, {y.F, col});\n auto rgt = MinRectangle ({x.F, col+1}, {y.F, y.S});\n \n ans = min (ans, Area(lft) + Area(rgt)); \n }\n for (int row = x.F; row < y.F; row ++) {\n auto lft = MinRectangle ({x.F, x.S}, {row, y.S});\n auto rgt = MinRectangle ({row+1, x.S}, {y.F, y.S});\n \n ans = min (ans, Area(lft) + Area(rgt)); \n }\n return ans;\n }\n \n int Min3Rectangle (pii x, pii y) {\n int ans = Area({x, y});\n \n for (int col = x.S; col < y.S; col ++) {\n auto lft1 = MinRectangle ({x.F, x.S}, {y.F, col});\n auto rgt1 = Min2Rectangle ({x.F, col+1}, {y.F, y.S});\n \n auto lft2 = Min2Rectangle ({x.F, x.S}, {y.F, col});\n auto rgt2 = MinRectangle ({x.F, col+1}, {y.F, y.S});\n \n ans = min (ans, Area(lft1) + rgt1); \n ans = min (ans, lft2 + Area(rgt2)); \n }\n for (int row = x.F; row < y.F; row ++) {\n auto lft1 = MinRectangle ({x.F, x.S}, {row, y.S});\n auto rgt1 = Min2Rectangle ({row+1, x.S}, {y.F, y.S});\n \n auto lft2 = Min2Rectangle ({x.F, x.S}, {row, y.S});\n auto rgt2 = MinRectangle ({row+1, x.S}, {y.F, y.S});\n \n ans = min (ans, Area(lft1) + rgt1); \n ans = min (ans, lft2 + Area(rgt2)); \n }\n return ans;\n }\n \npublic:\n int minimumSum(vector<vector<int>>& _grid) {\n grid = _grid;\n \n int rows = grid.size();\n int cols = grid[0].size();\n \n return Min3Rectangle({0, 0}, {rows-1, cols-1});\n }\n};", "memory": "25000" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
2
{ "code": "#define pii pair<int, int>\n#define F first\n#define S second\n\nclass Solution {\n \n vector<vector<int>> grid;\n \n pair<pii, pii> MinRectangle(pii x, pii y) {\n int lft = x.S, rgt = y.S;\n for (; lft <= y.S; lft ++) {\n int one = 0;\n for (int i = x.F; i <= y.F; i ++) one += grid[i][lft];\n if (one > 0) break;\n }\n for (; rgt >= x.S; rgt --) {\n int one = 0;\n for (int i = x.F; i <= y.F; i ++) one += grid[i][rgt];\n if (one > 0) break;\n }\n \n int top = x.F, dwn = y.F;\n for (; top <= y.F; top ++) {\n int one = 0;\n for (int i = x.S; i <= y.S; i ++) one += grid[top][i];\n if (one > 0) break;\n }\n for (; dwn >= x.F; dwn --) {\n int one = 0;\n for (int i = x.S; i <= y.S; i ++) one += grid[dwn][i];\n if (one > 0) break;\n }\n \n if (lft > rgt) return {{x.F, x.S}, {x.F, x.S}}; // rectangle with area 1\n return {{top, lft}, {dwn, rgt}};\n }\n \n int Area (pair<pii, pii> r) {\n return (r.S.F - r.F.F + 1) * (r.S.S - r.F.S + 1);\n }\n \n int Min2Rectangle (pii x, pii y) {\n int ans = Area({x, y});\n \n for (int col = x.S; col < y.S; col ++) {\n auto lft = MinRectangle ({x.F, x.S}, {y.F, col});\n auto rgt = MinRectangle ({x.F, col+1}, {y.F, y.S});\n \n ans = min (ans, Area(lft) + Area(rgt)); \n }\n for (int row = x.F; row < y.F; row ++) {\n auto lft = MinRectangle ({x.F, x.S}, {row, y.S});\n auto rgt = MinRectangle ({row+1, x.S}, {y.F, y.S});\n \n ans = min (ans, Area(lft) + Area(rgt)); \n }\n return ans;\n }\n \n int Min3Rectangle (pii x, pii y) {\n int ans = Area({x, y});\n \n for (int col = x.S; col < y.S; col ++) {\n auto lft1 = MinRectangle ({x.F, x.S}, {y.F, col});\n auto rgt1 = Min2Rectangle ({x.F, col+1}, {y.F, y.S});\n \n auto lft2 = Min2Rectangle ({x.F, x.S}, {y.F, col});\n auto rgt2 = MinRectangle ({x.F, col+1}, {y.F, y.S});\n \n ans = min (ans, Area(lft1) + rgt1); \n ans = min (ans, lft2 + Area(rgt2)); \n }\n for (int row = x.F; row < y.F; row ++) {\n auto lft1 = MinRectangle ({x.F, x.S}, {row, y.S});\n auto rgt1 = Min2Rectangle ({row+1, x.S}, {y.F, y.S});\n \n auto lft2 = Min2Rectangle ({x.F, x.S}, {row, y.S});\n auto rgt2 = MinRectangle ({row+1, x.S}, {y.F, y.S});\n \n ans = min (ans, Area(lft1) + rgt1); \n ans = min (ans, lft2 + Area(rgt2)); \n }\n return ans;\n }\n \npublic:\n int minimumSum(vector<vector<int>>& _grid) {\n grid = _grid;\n \n int rows = grid.size();\n int cols = grid[0].size();\n \n return Min3Rectangle({0, 0}, {rows-1, cols-1});\n }\n};", "memory": "25100" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
2
{ "code": "#define pii pair<int, int>\n#define F first\n#define S second\n\nclass Solution {\n vector<vector<int>> grid;\n pair<pii, pii> MinRectangle(pii x, pii y) {\n int lft = x.S, rgt = y.S;\n for (; lft <= y.S; lft++) {\n int one = 0;\n for (int i = x.F; i <= y.F; i++)\n one += grid[i][lft];\n if (one > 0)\n break;\n }\n for (; rgt >= x.S; rgt--) {\n int one = 0;\n for (int i = x.F; i <= y.F; i++)\n one += grid[i][rgt];\n if (one > 0)\n break;\n }\n\n int top = x.F, dwn = y.F;\n for (; top <= y.F; top++) {\n int one = 0;\n for (int i = x.S; i <= y.S; i++)\n one += grid[top][i];\n if (one > 0)\n break;\n }\n for (; dwn >= x.F; dwn--) {\n int one = 0;\n for (int i = x.S; i <= y.S; i++)\n one += grid[dwn][i];\n if (one > 0)\n break;\n }\n\n if (lft > rgt)\n return {{x.F, x.S}, {x.F, x.S}}; // rectangle with area 1\n return {{top, lft}, {dwn, rgt}};\n }\n\n int Area(pair<pii, pii> r) {\n return (r.S.F - r.F.F + 1) * (r.S.S - r.F.S + 1);\n }\n\n int Min2Rectangle(pii x, pii y) {\n int ans = Area({x, y});\n\n for (int col = x.S; col < y.S; col++) {\n auto lft = MinRectangle({x.F, x.S}, {y.F, col});\n auto rgt = MinRectangle({x.F, col + 1}, {y.F, y.S});\n\n ans = min(ans, Area(lft) + Area(rgt));\n }\n for (int row = x.F; row < y.F; row++) {\n auto lft = MinRectangle({x.F, x.S}, {row, y.S});\n auto rgt = MinRectangle({row + 1, x.S}, {y.F, y.S});\n\n ans = min(ans, Area(lft) + Area(rgt));\n }\n return ans;\n }\n\n int Min3Rectangle(pii x, pii y) {\n int ans = Area({x, y});\n\n for (int col = x.S; col < y.S; col++) {\n auto lft1 = MinRectangle({x.F, x.S}, {y.F, col});\n auto rgt1 = Min2Rectangle({x.F, col + 1}, {y.F, y.S});\n\n auto lft2 = Min2Rectangle({x.F, x.S}, {y.F, col});\n auto rgt2 = MinRectangle({x.F, col + 1}, {y.F, y.S});\n\n ans = min(ans, Area(lft1) + rgt1);\n ans = min(ans, lft2 + Area(rgt2));\n }\n for (int row = x.F; row < y.F; row++) {\n auto lft1 = MinRectangle({x.F, x.S}, {row, y.S});\n auto rgt1 = Min2Rectangle({row + 1, x.S}, {y.F, y.S});\n\n auto lft2 = Min2Rectangle({x.F, x.S}, {row, y.S});\n auto rgt2 = MinRectangle({row + 1, x.S}, {y.F, y.S});\n\n ans = min(ans, Area(lft1) + rgt1);\n ans = min(ans, lft2 + Area(rgt2));\n }\n return ans;\n }\n\npublic:\n int minimumSum(vector<vector<int>>& _grid) {\n grid = _grid;\n\n int rows = grid.size();\n int cols = grid[0].size();\n\n return Min3Rectangle({0, 0}, {rows - 1, cols - 1});\n }\n};", "memory": "25200" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
2
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define f(i, start, end) for (int i = start; i < end; i++)\n#define fs(i, start, end, step) for (int i = start; i < end; i = i + step)\n#define fr(i, start, end) for (int i = start; i > end; i--)\n#define all(arr) arr.begin(), arr.end()\n#define vi vector<int>\n#define vvi vector<vi>\n#define vb vector<bool>\n#define vvb vector<vb>\n#define vlli vector<lli>\n#define vvlli vector<vlli>\n#define vpii vector<pii>\n#define vpll vector<pll>\n#define pii pair<int, int>\n#define pll pair<lli, lli>\n#define pss pair<string, string>\n#define ceil(a, b) (a + b - 1) / b\n#define max_arr(arr) *max_element(arr.begin(), arr.end())\n#define min_arr(arr) *min_element(arr.begin(), arr.end())\n#define sum_arr(arr) accumulate(arr.begin(), arr.end(), 0LL)\n#define sort_arr(arr) sort(arr.begin(), arr.end())\n#define reverse_arr(arr) reverse(all(arr))\n#define MOD1 (lli)998244353\n#define MOD2 (lli)(1e9 + 7)\n#define fastio \\\n std::ios_base::sync_with_stdio(false); \\\n std::cout.tie(nullptr); \\\n std::cin.tie(nullptr)\ntypedef long long lli;\ntemplate <typename T>\nT getInp()\n{\n T temp;\n cin >> temp;\n return temp;\n}\n\nclass Solution\n{\npublic:\n vvi grid;\n int n, m;\n int split(int i1, int i2, int j1, int j2, int k)\n {\n int imin = INT_MAX, imax = -1, jmin = INT_MAX, jmax = -1;\n f(i, i1, i2 + 1) f(j, j1, j2 + 1) if (grid[i][j] == 1)\n {\n imin = min(imin, i);\n imax = max(imax, i);\n jmin = min(jmin, j);\n jmax = max(jmax, j);\n }\n if (k == 1) return (imax - imin + 1) * (jmax - jmin + 1);\n int res = INT_MAX;\n f(h,imin,imax) f(_k,1,k) \n {\n int s1,s2;\n if ((s1 = split(imin,h,jmin,jmax,_k)) != INT_MAX && (s2 = split(h+1,imax,jmin,jmax,k - _k)) != INT_MAX)\n res = min(res,s1 + s2);\n }\n f(w,jmin,jmax) f(_k,1,k) \n {\n int s1,s2;\n if ((s1 = split(imin,imax,jmin,w,_k)) != INT_MAX && (s2 = split(imin,imax,w+1,jmax,k - _k)) != INT_MAX)\n res = min(res,s1 + s2);\n }\n return res;\n }\n int minimumSum(vector<vector<int>> &grid)\n {\n this->grid = grid;\n n = grid.size();\n m = grid[0].size();\n\n return split(0,n-1,0,m-1,3);\n }\n};", "memory": "25200" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
2
{ "code": "class Solution {\n vector<vector<int>> grid;\n pair<pair<int,int>,pair<int,int>> MinRectangle(pair<int,int> x,pair<int,int> y){\n\n int lft = x.second,rgt = y.second;\n for(; lft<=y.second;lft++){\n int one = 0;\n for(int i=x.first;i<=y.first;i++) one += grid[i][lft];\n if(one > 0) break;\n }\n\n for(;rgt>=x.second;rgt--){\n int one = 0;\n for(int i=x.first;i<=y.first;i++) one += grid[i][rgt];\n if(one > 0) break;\n }\n\n //ABOVE WE HAVE MADE THE LEFT AND RIGHT BOUNDARIED OF THE RECTANGLE\n\n //NOW MAKE THE TOP AND BOTTOM BOUNDARIES OF THE RECTANGLE\n\n int top = x.first,btm = y.first;\n\n for(;top<=y.first;top++){\n int one = 0;\n for(int i=x.second;i<=y.second;i++) one += grid[top][i];\n if(one > 0) break;\n }\n\n for(;btm>=x.first;btm--){\n int one = 0;\n for(int i=x.second;i<=y.second;i++) one += grid[btm][i];\n if(one > 0) break;\n }\n\n if(lft>rgt) return {{x.first,x.second},{x.first,x.second}};\n\n return {{top,lft},{btm,rgt}}; \n }\n\n int Area(pair<pair<int,int>,pair<int,int>> r){\n return (r.second.first-r.first.first+1)*(r.second.second-r.first.second+1);\n }\n\n int Min2Rectangle(pair<int,int> x,pair<int,int> y){\n int ans = Area({x,y});\n //divide it vertically\n for(int col=x.second;col<y.second;col++){\n auto left1 = MinRectangle({x.first,x.second},{y.first,col});\n auto right1 = MinRectangle({x.first,col+1},{y.first,y.second});\n\n ans = min({ans,Area(left1)+Area(right1)});\n }\n\n //divide horizontally\n\n for(int row = x.first;row<y.first;row++){\n auto left1 = MinRectangle({x.first,x.second},{row,y.second});\n auto right1 = MinRectangle({row+1,x.second},{y.first,y.second});\n\n ans = min({ans,Area(left1)+Area(right1)});\n }\n\n return ans;\n\n }\n\n int Min3Rectangle(pair<int,int> x,pair<int,int> y){\n int ans = Area({x, y});\n //divide it vertically\n for(int col=x.second;col<y.second;col++){\n auto left1 = MinRectangle({x.first,x.second},{y.first,col});\n auto right1 = Min2Rectangle({x.first,col+1},{y.first,y.second});\n\n auto left2 = Min2Rectangle({x.first,x.second},{y.first,col});\n auto right2 = MinRectangle({x.first,col+1},{y.first,y.second});\n\n ans = min({ans,Area(left1)+right1,Area(right2)+left2});\n }\n\n //divide horizontally\n\n for(int row = x.first;row<y.first;row++){\n auto left1 = MinRectangle({x.first,x.second},{row,y.second});\n auto right1 = Min2Rectangle({row+1,x.second},{y.first,y.second});\n\n auto left2 = Min2Rectangle({x.first,x.second},{row,y.second});\n auto right2 = MinRectangle({row+1,x.second},{y.first,y.second});\n\n ans = min({ans,Area(left1)+right1,Area(right2)+left2});\n }\n\n return ans;\n\n }\npublic:\n int minimumSum(vector<vector<int>>& _grid) {\n grid = _grid;\n int rows = grid.size();\n int cols = grid[0].size();\n\n return Min3Rectangle({0,0},{rows-1,cols-1});\n \n }\n};", "memory": "25300" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
2
{ "code": "#define pii pair<int, int>\n#define F first\n#define S second\n\nclass Solution {\n \n vector<vector<int>> grid;\n \n pair<pii, pii> MinRectangle(pii x, pii y) {\n int lft = x.S, rgt = y.S;\n for (; lft <= y.S; lft ++) {\n int one = 0;\n for (int i = x.F; i <= y.F; i ++) one += grid[i][lft];\n if (one > 0) break;\n }\n for (; rgt >= x.S; rgt --) {\n int one = 0;\n for (int i = x.F; i <= y.F; i ++) one += grid[i][rgt];\n if (one > 0) break;\n }\n \n int top = x.F, dwn = y.F;\n for (; top <= y.F; top ++) {\n int one = 0;\n for (int i = x.S; i <= y.S; i ++) one += grid[top][i];\n if (one > 0) break;\n }\n for (; dwn >= x.F; dwn --) {\n int one = 0;\n for (int i = x.S; i <= y.S; i ++) one += grid[dwn][i];\n if (one > 0) break;\n }\n \n if (lft > rgt) return {{x.F, x.S}, {x.F, x.S}}; // rectangle with area 1\n return {{top, lft}, {dwn, rgt}};\n }\n \n int Area (pair<pii, pii> r) {\n return (r.S.F - r.F.F + 1) * (r.S.S - r.F.S + 1);\n }\n \n int Min2Rectangle (pii x, pii y) {\n int ans = Area({x, y});\n \n for (int col = x.S; col < y.S; col ++) {\n auto lft = MinRectangle ({x.F, x.S}, {y.F, col});\n auto rgt = MinRectangle ({x.F, col+1}, {y.F, y.S});\n \n ans = min (ans, Area(lft) + Area(rgt)); \n }\n for (int row = x.F; row < y.F; row ++) {\n auto lft = MinRectangle ({x.F, x.S}, {row, y.S});\n auto rgt = MinRectangle ({row+1, x.S}, {y.F, y.S});\n \n ans = min (ans, Area(lft) + Area(rgt)); \n }\n return ans;\n }\n \n int Min3Rectangle (pii x, pii y) {\n int ans = Area({x, y});\n \n for (int col = x.S; col < y.S; col ++) {\n auto lft1 = MinRectangle ({x.F, x.S}, {y.F, col});\n auto rgt1 = Min2Rectangle ({x.F, col+1}, {y.F, y.S});\n \n auto lft2 = Min2Rectangle ({x.F, x.S}, {y.F, col});\n auto rgt2 = MinRectangle ({x.F, col+1}, {y.F, y.S});\n \n ans = min (ans, Area(lft1) + rgt1); \n ans = min (ans, lft2 + Area(rgt2)); \n }\n for (int row = x.F; row < y.F; row ++) {\n auto lft1 = MinRectangle ({x.F, x.S}, {row, y.S});\n auto rgt1 = Min2Rectangle ({row+1, x.S}, {y.F, y.S});\n \n auto lft2 = Min2Rectangle ({x.F, x.S}, {row, y.S});\n auto rgt2 = MinRectangle ({row+1, x.S}, {y.F, y.S});\n \n ans = min (ans, Area(lft1) + rgt1); \n ans = min (ans, lft2 + Area(rgt2)); \n }\n return ans;\n }\n \npublic:\n int minimumSum(vector<vector<int>>& _grid) {\n grid = _grid;\n \n int rows = grid.size();\n int cols = grid[0].size();\n \n return Min3Rectangle({0, 0}, {rows-1, cols-1});\n }\n};", "memory": "25300" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
2
{ "code": "class Solution {\nprivate:\n vector<vector<int>> dp;\n \n int sum1(int i1, int j1, int i2, int j2) {\n int res = dp[i2][j2];\n if (i1 > 0) res -= dp[i1-1][j2];\n if (j1 > 0) res -= dp[i2][j1-1];\n if (i1 > 0 && j1 > 0) res += dp[i1-1][j1-1];\n return res;\n }\n \n int calc(int i1, int j1, int i2, int j2, int split) {\n if (split == 1) {\n if (sum1(i1, j1, i2, j2) == 0) return 0;\n while (i1 < i2 && sum1(i1, j1, i1, j2) == 0) i1++;\n while (i1 < i2 && sum1(i2, j1, i2, j2) == 0) i2--;\n while (j1 < j2 && sum1(i1, j1, i2, j1) == 0) j1++;\n while (j1 < j2 && sum1(i1, j2, i2, j2) == 0) j2--;\n return (i2 - i1 + 1) * (j2 - j1 + 1);\n }\n \n int res = 1000;\n for (int i = i1; i < i2; i++) {\n if (split == 3) {\n res = min(res, calc(i1, j1, i, j2, 1) + calc(i+1, j1, i2, j2, 2));\n res = min(res, calc(i1, j1, i, j2, 2) + calc(i+1, j1, i2, j2, 1));\n } else {\n res = min(res, calc(i1, j1, i, j2, 1) + calc(i+1, j1, i2, j2, 1));\n }\n }\n for (int j = j1; j < j2; j++) {\n if (split == 3) {\n res = min(res, calc(i1, j1, i2, j, 2) + calc(i1, j+1, i2, j2, 1));\n res = min(res, calc(i1, j1, i2, j, 1) + calc(i1, j+1, i2, j2, 2));\n } else {\n res = min(res, calc(i1, j1, i2, j, 1) + calc(i1, j+1, i2, j2, 1));\n }\n }\n return res;\n }\n \npublic:\n int minimumSum(vector<vector<int>>& grid) {\n int m = grid.size(), n = grid[0].size();\n dp = vector<vector<int>>(m, vector<int>(n, 0));\n \n int min_i = m, max_i = 0, min_j = n, max_j = 0;\n \n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == 1) {\n min_i = min(min_i, i);\n max_i = max(max_i, i);\n min_j = min(min_j, j);\n max_j = max(max_j, j);\n }\n int sum = 0;\n if (i > 0) sum += dp[i-1][j];\n if (j > 0) sum += dp[i][j-1];\n if (i > 0 && j > 0) sum -= dp[i-1][j-1];\n dp[i][j] = sum + grid[i][j];\n }\n }\n \n return calc(min_i, min_j, max_i, max_j, 3);\n }\n};", "memory": "25400" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
2
{ "code": "#define LOW_BIT(i) __builtin_ctz(i)\n#define HIGH_BIT(i) (31 - __builtin_clz(i))\n\nint speedup = []{ios::sync_with_stdio(0); cin.tie(0); return 0;}();\n\nclass Solution {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n int rowBits[30], colBits[30]{}, rowArea[30], colArea[30];\n int rowCount = grid.size(), colCount = grid[0].size(), result = rowCount * colCount;\n\n for (int i = 0; i < rowCount; ++i) {\n int bitMask = 0;\n for (int j = 0; j < colCount; ++j) {\n if (grid[i][j]) bitMask |= 1 << j, colBits[j] |= 1 << i;\n }\n rowBits[i] = bitMask;\n }\n\n int top = 0; while (!rowBits[top]) ++top;\n int bottom = rowCount - 1; while (!rowBits[bottom]) --bottom;\n int left = 0; while (!colBits[left]) ++left;\n int right = colCount - 1; while (!colBits[right]) --right;\n\n // Vertical split, first pass\n for (int i = bottom, lBound = colCount, rBound = -1; i >= top; --i) {\n if (int x = rowBits[i]) {\n rBound = max(rBound, HIGH_BIT(x));\n lBound = min(lBound, LOW_BIT(x));\n rowArea[i] = (rBound - lBound + 1) * (bottom - i + 1);\n }\n }\n for (int i = top, lBound = colCount, rBound = -1, areaSum = 0; i <= bottom; ++i) {\n if (int x = rowBits[i]) {\n // Vertical split\n for (int j = i, lTemp = colCount, rTemp = -1, areaTemp = 0; j <= bottom; ++j) {\n if (int x = rowBits[j]) {\n result = min(result, areaSum + areaTemp + rowArea[j]);\n rTemp = max(rTemp, HIGH_BIT(x));\n lTemp = min(lTemp, LOW_BIT(x));\n areaTemp = (rTemp - lTemp + 1) * (j - i + 1);\n }\n }\n // Horizontal split\n for (int splitType : {0, 1}) {\n int currentArea = splitType ? areaSum : rowArea[i];\n int mask = splitType ? -(1 << i) : (1 << i) - 1;\n int rBound = right; while (rBound >= 0 && !(colBits[rBound] & mask)) --rBound;\n if (rBound < 0) continue;\n int lBound = left; while (!(colBits[lBound] & mask)) ++lBound;\n for (int j = rBound, tBound = rowCount, bBound = -1; j >= lBound; --j) {\n if (int x = colBits[j] & mask) {\n bBound = max(bBound, HIGH_BIT(x));\n tBound = min(tBound, LOW_BIT(x));\n colArea[j] = (bBound - tBound + 1) * (rBound - j + 1);\n }\n }\n for (int j = lBound, tBound = rowCount, bBound = -1, areaTemp = 0; j <= rBound; ++j) {\n if (int x = colBits[j] & mask) {\n result = min(result, currentArea + areaTemp + colArea[j]);\n bBound = max(bBound, HIGH_BIT(x));\n tBound = min(tBound, LOW_BIT(x));\n areaTemp = (bBound - tBound + 1) * (j - lBound + 1);\n }\n }\n }\n\n rBound = max(rBound, HIGH_BIT(x));\n lBound = min(lBound, LOW_BIT(x));\n areaSum = (rBound - lBound + 1) * (i - top + 1);\n }\n }\n\n // Horizontal split, first pass\n for (int i = right, tBound = rowCount, bBound = -1; i >= left; --i) {\n if (int x = colBits[i]) {\n bBound = max(bBound, HIGH_BIT(x));\n tBound = min(tBound, LOW_BIT(x));\n rowArea[i] = (bBound - tBound + 1) * (right - i + 1);\n }\n }\n for (int i = left, tBound = rowCount, bBound = -1, areaSum = 0; i <= right; ++i) {\n if (int x = colBits[i]) {\n // Vertical split\n for (int j = i, tTemp = rowCount, bTemp = -1, areaTemp = 0; j <= right; ++j) {\n if (int x = colBits[j]) {\n result = min(result, areaSum + areaTemp + rowArea[j]);\n bTemp = max(bTemp, HIGH_BIT(x));\n tTemp = min(tTemp, LOW_BIT(x));\n areaTemp = (bTemp - tTemp + 1) * (j - i + 1);\n }\n }\n // Horizontal split\n for (int splitType : {0, 1}) {\n int currentArea = splitType ? areaSum : rowArea[i];\n int mask = splitType ? -(1 << i) : (1 << i) - 1;\n int bBound = bottom; while (bBound >= 0 && !(rowBits[bBound] & mask)) --bBound;\n if (bBound < 0) continue;\n int tBound = top; while (!(rowBits[tBound] & mask)) ++tBound;\n for (int j = bBound, lBound = colCount, rBound = -1; j >= tBound; --j) {\n if (int x = rowBits[j] & mask) {\n rBound = max(rBound, HIGH_BIT(x));\n lBound = min(lBound, LOW_BIT(x));\n colArea[j] = (rBound - lBound + 1) * (bBound - j + 1);\n }\n }\n for (int j = tBound, lBound = colCount, rBound = -1, areaTemp = 0; j <= bBound; ++j) {\n if (int x = rowBits[j] & mask) {\n result = min(result, currentArea + areaTemp + colArea[j]);\n rBound = max(rBound, HIGH_BIT(x));\n lBound = min(lBound, LOW_BIT(x));\n areaTemp = (rBound - lBound + 1) * (j - tBound + 1);\n }\n }\n }\n\n bBound = max(bBound, HIGH_BIT(x));\n tBound = min(tBound, LOW_BIT(x));\n areaSum = (bBound - tBound + 1) * (i - left + 1);\n }\n }\n\n return result;\n }\n};\n", "memory": "25500" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n \n \n std::vector<std::vector<int>> rotate(std::vector<std::vector<int>>& grid) \n {\n const int n = grid.size();\n const int m = grid[0].size();\n \n std::vector<std::vector<int>> res(m, std::vector<int>(n));\n \n for(int i = 0; i < n; ++i)\n for(int j = 0; j < m; ++j)\n res[m-1-j][i] = grid[i][j];\n \n return res;\n }\n\n \n int minimumSum(vector<vector<int>>& grid) {\n \n int res = INT_MAX;\n \n int r = solve(grid);\n res = min(res, r);\n grid = rotate(grid);\n\n r = solve(grid);\n res = min(res, r);\n grid = rotate(grid);\n\n r = solve(grid);\n res = min(res, r);\n grid = rotate(grid);\n\n r = solve(grid);\n res = min(res, r);\n //grid = rotate(grid);\n\n \n \n \n \n return res;\n }\n \n int solve(vector<vector<int>>& grid) {\n const int n = grid.size();\n const int m = grid[0].size();\n \n int res = INT_MAX;\n \n for(int i = 1; i < n; ++i)\n {\n for(int j = 1; j < m; ++j)\n {\n int s = 0;\n \n int up = INT_MAX, down = -1;\n int left = INT_MAX, right = -1;\n \n \n for(int x = 0; x < i; ++x)\n {\n for(int y = 0; y < j; ++y)\n {\n if (grid[x][y]) {\n \n up = min(up, x);\n down = max(down, x);\n\n left = min(left, y);\n right = max(right, y);\n }\n }\n }\n \n if (up > down)\n continue;\n \n //cout << down << \" \" << up << \" \" << left << \" \" << right << endl;\n \n s += (down-up+1) * (right-left+1);\n \n \n up = INT_MAX;\n down = -1;\n left = INT_MAX;\n right = -1;\n \n for(int x = i; x < n; ++x)\n {\n for(int y = 0; y < j; ++y)\n {\n if (grid[x][y]) {\n up = min(up, x);\n down = max(down, x);\n\n left = min(left, y);\n right = max(right, y);\n }\n }\n }\n if (up > down)\n continue;\n \n s += (down-up+1)*(right-left+1);\n \n \n up = INT_MAX;\n down = -1;\n left = INT_MAX;\n right = -1;\n \n for(int x = 0; x < n; ++x)\n {\n for(int y = j; y < m; ++y)\n {\n if (grid[x][y]) {\n up = min(up, x);\n down = max(down, x);\n\n left = min(left, y);\n right = max(right, y);\n }\n }\n }\n if (up > down)\n continue;\n \n s += (down-up+1)*(right-left+1);\n \n res = min(res, s);\n }\n }\n \n \n \n \n for(int i = 1; i < n; ++i)\n {\n for(int ii = i+1; ii < n; ++ii)\n {\n int s = 0;\n \n int up = INT_MAX, down = -1;\n int left = INT_MAX, right = -1;\n \n for(int x = 0; x < i; ++x)\n {\n for(int y = 0; y < m; ++y)\n {\n if (grid[x][y]) {\n \n up = min(up, x);\n down = max(down, x);\n\n left = min(left, y);\n right = max(right, y);\n }\n }\n }\n \n if (up > down)\n continue;\n \n //cout << down << \" \" << up << \" \" << left << \" \" << right << endl;\n \n s += (down-up+1) * (right-left+1);\n \n \n up = INT_MAX;\n down = -1;\n left = INT_MAX;\n right = -1;\n \n for(int x = i; x < ii; ++x)\n {\n for(int y = 0; y < m; ++y)\n {\n if (grid[x][y]) {\n up = min(up, x);\n down = max(down, x);\n\n left = min(left, y);\n right = max(right, y);\n }\n }\n }\n if (up > down)\n continue;\n \n s += (down-up+1)*(right-left+1);\n \n \n up = INT_MAX;\n down = -1;\n left = INT_MAX;\n right = -1;\n \n for(int x = ii; x < n; ++x)\n {\n for(int y = 0; y < m; ++y)\n {\n if (grid[x][y]) {\n up = min(up, x);\n down = max(down, x);\n\n left = min(left, y);\n right = max(right, y);\n }\n }\n }\n if (up > down)\n continue;\n \n s += (down-up+1)*(right-left+1);\n \n res = min(res, s);\n }\n }\n\n \n \n \n \n \n \n \n \n \n return res;\n }\n};", "memory": "26500" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
3
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\n/* Macros {{{ */\n/* A lot of this is from some of Benq's submissions\n [https://codeforces.com/profile/Benq]\n Ugly af to the eyes, but with vim fold its barable\n Hopefully c++20 concepts can make all this stuff must cleaner */\n\n/* Basics {{{ */\nusing ll = long long;\nusing ld = long double;\nusing str = string;\n\nusing pi = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pld = pair<ld, ld>;\n#define mp make_pair\n#define fi first\n#define se second\n\n#define arr array\n#define ve vector\nusing vi = vector<int>;\nusing vll = vector<ll>;\nusing vld = vector<ld>;\n\nusing vpi = vector<pi>;\nusing vpll = vector<pll>;\nusing vpld = vector<pld>;\n\nusing vvi = vector<vi>;\nusing vvll = vector<vll>;\nusing vvld = vector<vld>;\n\nusing vvpi = vector<vpi>;\nusing vvpll = vector<vpll>;\nusing vvpld = vector<vpld>;\n\n#define pb push_back\n#define lb lower_bound\n#define ub upper_bound\n#define sz size()\n#define rsz(a) resize(a)\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n\n#define For(i, a, b) for (int i = a; i < b; ++i)\n#define Rof(i, a, b) for (int i = (b)-1; i >= (a); --i)\n#define rep(a) For(_, 0, a)\n#define each(a, x) for (auto &a : x)\n#define reach(a, x) for (auto a = x.rbegin(); a != x.rend(); ++a)\n\ntemplate <typename T, typename U>\ninline void cmin(T &x, U y) {\n if (y < x) x = y;\n}\ntemplate <typename T, typename U>\ninline void cmax(T &x, U y) {\n if (x < y) x = y;\n}\n/*}}}*/\n\n/* IO {{{ */\n\n/* Template Macros {{{ */\n#define tcT template <class T\n#define tcTU tcT, class U\n#define tcTUU tcT, class... U\n/*}}}*/\n\ninline namespace Helpers { /*{{{*/\ntcT, class = void > struct is_iterable : false_type {};\ntcT > struct is_iterable<\n T, void_t<decltype(begin(declval<T>())), decltype(end(declval<T>()))>>\n : true_type {};\ntcT > constexpr bool is_iterable_v = is_iterable<T>::value;\n\ntcT, class = void > struct is_readable : false_type {};\ntcT > struct is_readable<T, typename std::enable_if_t<is_same_v<\n decltype(cin >> declval<T &>()), istream &>>>\n : true_type {};\ntcT > constexpr bool is_readable_v = is_readable<T>::value;\n\ntcT, class = void > struct is_printable : false_type {};\ntcT > struct is_printable<T, typename std::enable_if_t<is_same_v<\n decltype(cout << declval<T>()), ostream &>>>\n : true_type {};\ntcT > constexpr bool is_printable_v = is_printable<T>::value;\n} /* namespace Helpers */\n/*}}}*/\n\ninline namespace Input { /*{{{*/\ntcT > constexpr bool needs_input_v = !is_readable_v<T> && is_iterable_v<T>;\ntcTUU > void re(T &t, U &...u);\ntcTU > void re(pair<T, U> &p); /* pairs */\n\n/* re: read{{{ */\ntcT > typename enable_if<is_readable_v<T>, void>::type re(T &x) {\n cin >> x;\n} /* default */\ntcT > typename enable_if<needs_input_v<T>, void>::type re(\n T &i); // vectors, arrays, etc...\ntcTU > void re(pair<T, U> &p) { re(p.fi, p.se); } // pairs\ntcT > typename enable_if<needs_input_v<T>, void>::type re(T &i) {\n each(x, i) re(x);\n}\ntcTUU > void re(T &t, U &...u) {\n re(t);\n re(u...);\n} /* read multiple}}} */\n\n/* rv: resize and read vectors{{{ */\nvoid rv(size_t) {}\ntcTUU > void rv(size_t N, ve<T> &t, U &...u);\ntemplate <class... U>\nvoid rv(size_t, size_t N2, U &...u);\ntcTUU > void rv(size_t N, ve<T> &t, U &...u) {\n t.rsz(N);\n re(t);\n rv(N, u...);\n}\ntemplate <class... U>\nvoid rv(size_t, size_t N2, U &...u) {\n rv(N2, u...);\n} /*}}}*/\n\n/* dumb shortcuts to read in ints{{{ */\nvoid decrement() {} /* subtract one from each */\ntcTUU > void decrement(T &t, U &...u) {\n --t;\n decrement(u...);\n}\n#define ints(...) \\\n int __VA_ARGS__; \\\n re(__VA_ARGS__);\n#define int1(...) \\\n ints(__VA_ARGS__); \\\n decrement(__VA_ARGS__); /*}}}*/\n} /* namespace Input */\n/*}}}*/\n\ninline namespace ToString { /*{{{*/\ntcT > constexpr bool needs_output_v = !is_printable_v<T> && is_iterable_v<T>;\n\n/* ts: string representation to print */\ntcT > typename enable_if<is_printable_v<T>, str>::type ts(T v) {\n stringstream ss;\n ss << fixed << setprecision(15) << v;\n return ss.str();\n} /* default */\ntcT > str bit_vec(T t) { /* bit vector to string */\n str res = \"{\";\n For(i, 0, t.sz) res += ts(t[i]);\n res += \"}\";\n return res;\n}\nstr ts(ve<bool> v) { return bit_vec(v); }\ntemplate <size_t SZ>\nstr ts(bitset<SZ> b) {\n return bit_vec(b);\n} /* bit vector */\ntcTU > str ts(pair<T, U> p); /* pairs */\ntcT > typename enable_if<needs_output_v<T>, str>::type ts(\n T v); /* vectors, arrays */\ntcTU > str ts(pair<T, U> p) { return \"(\" + ts(p.fi) + \", \" + ts(p.se) + \")\"; }\ntcT > typename enable_if<is_iterable_v<T>, str>::type ts_sep(T v, str sep) {\n /* convert container to string w/ separator sep */\n bool fst = 1;\n str res = \"\";\n for (const auto &x : v) {\n if (!fst) res += sep;\n fst = 0;\n res += ts(x);\n }\n return res;\n}\ntcT > typename enable_if<needs_output_v<T>, str>::type ts(T v) {\n return \"{\" + ts_sep(v, \", \") + \"}\";\n}\n\n/* for nested DS */\ntemplate <int, class T>\ntypename enable_if<!needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {\n return {ts(v)};\n}\ntemplate <int lev, class T>\ntypename enable_if<needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {\n if (lev == 0 || !v.sz) return {ts(v)};\n ve<str> res;\n for (const auto &t : v) {\n if (res.sz) res.back() += \",\";\n ve<str> tmp = ts_lev<lev - 1>(t);\n res.insert(end(res), all(tmp));\n }\n For(i, 0, res.sz) {\n str bef = \" \";\n if (i == 0) bef = \"{\";\n res[i] = bef + res[i];\n }\n res.back() += \"}\";\n return res;\n}\n} /* namespace ToString */\n/*}}}*/\n\ninline namespace Output { /*{{{*/\ntemplate <class T>\nvoid pr_sep(ostream &os, str, const T &t) {\n os << ts(t);\n}\ntemplate <class T, class... U>\nvoid pr_sep(ostream &os, str sep, const T &t, const U &...u) {\n pr_sep(os, sep, t);\n os << sep;\n pr_sep(os, sep, u...);\n}\n/* print w/ no spaces */\ntemplate <class... T>\nvoid pr(const T &...t) {\n pr_sep(cout, \"\", t...);\n}\n/* print w/ spaces, end with newline */\nvoid ps() { cout << \"\\n\"; }\ntemplate <class... T>\nvoid ps(const T &...t) {\n pr_sep(cout, \" \", t...);\n ps();\n}\n/* debug to cerr */\ntemplate <class... T>\nvoid dbg_out(const T &...t) {\n pr_sep(cerr, \" | \", t...);\n cerr << endl;\n}\nvoid loc_info(int line, str names) {\n cerr << \"Line(\" << line << \") -> [\" << names << \"]: \";\n}\ntemplate <int lev, class T>\nvoid dbgl_out(const T &t) {\n cerr << \"\\n\\n\" << ts_sep(ts_lev<lev>(t), \"\\n\") << \"\\n\" << endl;\n}\n} /* namespace Output */\n/*}}}}}}}}}*/\n\n\nclass Solution {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n\n // let vv1[r][c] be the soluiton for the top left rxc with 1 rectangle\n\n int n = grid.size(), m = grid[0].size();\n vvi vv1(n, vi(m)), vv2(n, vi(m)), vv3(n, vi(m)), vv4(n, vi(m));\n\n auto cvv1 = [&](auto r, auto c) {\n int vl = c, vr = 0, vu=r, vd=0;\n\n for(int i=0; i<=r; ++i) for(int j=0; j<=c; ++j) {\n if(grid[i][j] == 1) {\n cmin(vu, i), cmax(vd, i);\n cmin(vl, j), cmax(vr, j);\n }\n }\n\n if(vl > vr || vu > vd) return 1;\n return (vr - vl + 1) * (vd - vu + 1);\n };\n\n auto cvv2 = [&](auto r, auto c) {\n int vl = c, vr = 0, vu=n, vd=r;\n\n for(int i=r; i<n; ++i) for(int j=0; j<=c; ++j) {\n if(grid[i][j] == 1) {\n cmin(vu, i), cmax(vd, i);\n cmin(vl, j), cmax(vr, j);\n }\n }\n\n if(vl > vr || vu > vd) return 1;\n return (vr - vl + 1) * (vd - vu + 1);\n };\n\n auto cvv3 = [&](auto r, auto c) {\n int vl = m, vr = c, vu=r, vd=0;\n\n for(int i=0; i<=r; ++i) for(int j=c; j<m; ++j) {\n if(grid[i][j] == 1) {\n cmin(vu, i), cmax(vd, i);\n cmin(vl, j), cmax(vr, j);\n }\n }\n\n if(vl > vr || vu > vd) return 1;\n return (vr - vl + 1) * (vd - vu + 1);\n };\n\n auto cvv4 = [&](auto r, auto c) {\n int vl = m, vr = c, vu=n, vd=r;\n\n for(int i=r; i<n; ++i) for(int j=c; j<m; ++j) {\n if(grid[i][j] == 1) {\n cmin(vu, i), cmax(vd, i);\n cmin(vl, j), cmax(vr, j);\n }\n }\n\n if(vl > vr || vu > vd) return 1;\n return (vr - vl + 1) * (vd - vu + 1);\n };\n\n for(int r=0; r<n; ++r) for(int c=0; c<m; ++c) {\n vv1[r][c] = cvv1(r, c);\n vv2[r][c] = cvv2(r, c);\n vv3[r][c] = cvv3(r, c);\n vv4[r][c] = cvv4(r, c);\n }\n\n int res = INT_MAX;\n\n for(int r=1; r<n; ++r) for(int c=1; c<m; ++c) {\n int tv;\n\n // cut r then c on bottom\n tv = vv1[r-1][m-1] + vv2[r][c-1] + vv4[r][c];\n cmin(res, tv);\n\n // cut r then c on top\n tv = vv1[r-1][c-1] + vv3[r-1][c] + vv4[r][0];\n cmin(res, tv);\n\n // cut c then r on left\n tv = vv1[r-1][c-1] + vv2[r][c-1] + vv3[n-1][c];\n cmin(res, tv);\n\n // cut c then r on right\n tv = vv1[n-1][c-1] + vv3[r-1][c] + vv4[r][c];\n cmin(res, tv);\n }\n\n for(int r1=1; r1<n; ++r1) for(int r2=r1+1; r2<n; ++r2) {\n int tv = vv1[r1-1][m-1] + vv4[r2][0];\n\n\n int vl = m, vr = 0, vu=n, vd=0;\n for(int i=r1; i<r2; ++i) for(int j=0; j<m; ++j) {\n if(grid[i][j] == 1) {\n cmin(vu, i), cmax(vd, i);\n cmin(vl, j), cmax(vr, j);\n }\n }\n\n if(vl > vr || vu > vd) tv += 1;\n else tv += (vr - vl + 1) * (vd - vu + 1);\n cmin(res, tv);\n\n }\n\n for(int c1=1; c1<m; ++c1) for(int c2=c1+1; c2<m; ++c2) {\n int tv = vv1[n-1][c1-1] + vv4[0][c2];\n\n\n int vl = m, vr = 0, vu=n, vd=0;\n for(int i=0; i<n; ++i) for(int j=c1; j<c2; ++j) {\n if(grid[i][j] == 1) {\n cmin(vu, i), cmax(vd, i);\n cmin(vl, j), cmax(vr, j);\n }\n }\n\n if(vl > vr || vu > vd) tv += 1;\n else tv += (vr - vl + 1) * (vd - vu + 1);\n cmin(res, tv);\n }\n\n return res;\n }\n};\n", "memory": "27600" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
3
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\nstatic const int __ = []() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nstruct TwoDArray {\n size_t row;\n size_t col;\n std::vector<int> data;\n TwoDArray(int r, int c) : row(r), col(c), data(r * c, 0) {}\n int *operator[](size_t r) { return &data[r * col]; }\n};\nclass Solution {\n public:\n int minimumSum(vector<vector<int>> &grid) {\n size_t left = grid[0].size();\n size_t right = 0;\n size_t top = 0;\n size_t bottom = grid.size();\n for (size_t r = 0; r < grid.size(); ++r) {\n for (size_t c = 0; c < grid[0].size(); ++c) {\n if (grid[r][c] == 1) {\n left = std::min(left, c);\n right = std::max(right, c);\n top = std::max(top, r);\n bottom = std::min(bottom, r);\n }\n }\n }\n vector<vector<int>> new_grid(top - bottom + 1,\n vector<int>(right - left + 1, 0));\n for (size_t r = bottom; r <= top; ++r) {\n for (size_t c = left; c <= right; ++c) {\n new_grid[r - bottom][c - left] = grid[r][c];\n }\n }\n return inner(new_grid);\n }\n int inner(vector<vector<int>> &grid) {\n const int m = grid.size(), n = grid[0].size();\n TwoDArray tl{m, n};\n TwoDArray tr{m, n};\n TwoDArray bl{m, n};\n TwoDArray br{m, n};\n TwoDArray h{m, m};\n TwoDArray v{n, n};\n int l, r, t, b;\n\n std::vector<int> top(n, 0), bottom(n, 0);\n for (int j = 0; j < n; ++j) {\n top[j] = -1;\n }\n for (int i = 0; i < m; ++i) {\n l = -1;\n for (int j = 0; j < n; ++j) {\n if (grid[i][j] == 1) {\n if (top[j] == -1) {\n top[j] = i;\n }\n bottom[j] = i;\n }\n if (top[j] >= 0) {\n if (l == -1) {\n l = j, r = l;\n t = top[j];\n b = bottom[j];\n } else {\n r = j;\n t = min(t, top[j]);\n b = max(b, bottom[j]);\n }\n }\n tl[i][j] = (l == -1) ? 0 : (r - l + 1) * (b - t + 1);\n }\n }\n for (int j = 0; j < n; ++j) {\n top[j] = -1;\n }\n for (int i = 0; i < m; ++i) {\n l = -1;\n for (int j = n - 1; j >= 0; --j) {\n if (grid[i][j] == 1) {\n if (top[j] == -1) {\n top[j] = i;\n }\n bottom[j] = i;\n }\n if (top[j] >= 0) {\n if (l == -1) {\n l = j, r = l;\n t = top[j], b = bottom[j];\n } else {\n l = j;\n t = min(t, top[j]);\n b = max(b, bottom[j]);\n }\n }\n tr[i][j] = (l == -1) ? 0 : (r - l + 1) * (b - t + 1);\n }\n }\n for (int j = 0; j < n; ++j) {\n top[j] = -1;\n }\n for (int i = m - 1; i >= 0; --i) {\n l = -1;\n for (int j = 0; j < n; ++j) {\n if (grid[i][j] == 1) {\n if (top[j] == -1) {\n bottom[j] = i;\n }\n top[j] = i;\n }\n if (top[j] >= 0) {\n if (l == -1) {\n l = j, r = l;\n t = top[j], b = bottom[j];\n } else {\n r = j;\n t = min(t, top[j]);\n b = max(b, bottom[j]);\n }\n }\n bl[i][j] = (l == -1) ? 0 : (r - l + 1) * (b - t + 1);\n }\n }\n for (int j = 0; j < n; ++j) {\n top[j] = -1;\n }\n for (int i = m - 1; i >= 0; --i) {\n l = -1;\n for (int j = n - 1; j >= 0; --j) {\n if (grid[i][j] == 1) {\n if (top[j] == -1) {\n bottom[j] = i;\n }\n top[j] = i;\n }\n if (top[j] >= 0) {\n if (l == -1) {\n l = j, r = l;\n t = top[j], b = bottom[j];\n } else {\n l = j;\n t = min(t, top[j]);\n b = max(b, bottom[j]);\n }\n }\n br[i][j] = (l == -1) ? 0 : (r - l + 1) * (b - t + 1);\n }\n }\n TwoDArray hends{m, 2}, vends{n, 2};\n for (int j = 0; j < n; ++j) {\n vends[j][0] = -1;\n }\n for (int i = 0; i < m; ++i) {\n hends[i][0] = -1;\n for (int j = 0; j < n; ++j) {\n if (grid[i][j] == 1) {\n if (hends[i][0] == -1) {\n hends[i][0] = j, hends[i][1] = j;\n } else {\n hends[i][1] = j;\n }\n if (vends[j][0] == -1) {\n vends[j][0] = i, vends[j][1] = i;\n } else {\n vends[j][1] = i;\n }\n }\n }\n }\n for (int i = 0; i < m; ++i) {\n l = -1;\n for (int j = i; j >= 0; --j) {\n if (hends[j][0] >= 0) {\n if (l == -1) {\n l = hends[j][0];\n r = hends[j][1];\n t = j, b = j;\n } else {\n l = min(l, hends[j][0]);\n r = max(r, hends[j][1]);\n t = j;\n }\n }\n h[j][i] = (l == -1) ? 0 : (r - l + 1) * (b - t + 1);\n }\n }\n for (int i = 0; i < n; ++i) {\n l = -1;\n for (int j = i; j >= 0; --j) {\n if (vends[j][0] >= 0) {\n if (l == -1) {\n t = vends[j][0];\n b = vends[j][1];\n l = j, r = j;\n } else {\n t = min(t, vends[j][0]);\n b = max(b, vends[j][1]);\n l = j;\n }\n }\n v[j][i] = (l == -1) ? 0 : (r - l + 1) * (b - t + 1);\n }\n }\n int minarea = m * n;\n for (int i = 0; i < m; ++i) {\n for (int j = i + 1; j < m - 1; ++j) {\n if (h[0][i] + h[i + 1][j] + h[j + 1][m - 1] < minarea) {\n minarea = h[0][i] + h[i + 1][j] + h[j + 1][m - 1];\n }\n }\n }\n for (int i = 0; i < n; ++i) {\n for (int j = i + 1; j < n - 1; ++j) {\n if (v[0][i] + v[i + 1][j] + v[j + 1][n - 1] < minarea) {\n minarea = v[0][i] + v[i + 1][j] + v[j + 1][n - 1];\n }\n }\n }\n for (int i = 0; i < m - 1; ++i) {\n for (int j = 0; j < n - 1; ++j) {\n minarea =\n min({minarea, tl[i][j] + bl[i + 1][j] + v[j + 1][n - 1],\n v[0][j] + tr[i][j + 1] + br[i + 1][j + 1],\n h[i + 1][m - 1] + tl[i][j] + tr[i][j + 1],\n h[0][i] + bl[i + 1][j] + br[i + 1][j + 1]});\n }\n }\n return minarea;\n }\n};", "memory": "28000" }
3,459
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
3
{ "code": "// Time: O(max(n, m)^2)\n// Space: O(max(n, m)^2)\n\n// dp\nclass Solution {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n const auto& cmp = [](int x, int y) {\n return x < y ? -1 : (x > y ? 1 : 0);\n };\n\n const auto& count = [&](int start1, int end1, int start2, int end2) {\n vector<vector<int>> dp(size(grid), vector<int>(size(grid[0])));\n vector<int> up(size(grid[0]), size(grid));\n vector<int> down(size(grid[0]), -1);\n for (int i = start1, d1 = cmp(end1, start1); i != end1; i += d1) {\n int l = size(grid[0]);\n int r = -1;\n int u = size(grid);\n int d = -1;\n for (int j = start2, d2 = cmp(end2, start2); j != end2; j += d2) {\n if (grid[i][j]) {\n up[j] = min(up[j], i);\n down[j] = max(down[j], i);\n }\n u = min(u, up[j]);\n d = max(d, down[j]);\n if (down[j] >= 0) {\n l = min(l, j);\n r = max(r, j);\n }\n dp[i][j] = r >= 0 ? (r - l + 1) * (d - u + 1) : 0;\n }\n }\n return dp;\n };\n \n const auto& count2 = [&](bool is_vertical) {\n const auto& get_n = [&]() {\n return !is_vertical ? size(grid) : size(grid[0]);\n };\n\n const auto& get_m = [&]() {\n return !is_vertical ? size(grid[0]) : size(grid);\n };\n\n const auto& get = [&](int i, int j) {\n return !is_vertical ? grid[i][j] : grid[j][i];\n };\n\n vector<int> left(get_n(), get_m());\n vector<int> right(get_n(), -1);\n for (int i = 0; i < get_n(); ++i) {\n for (int j = 0; j < get_m(); ++j) {\n if (get(i, j) == 0) {\n continue;\n }\n left[i] = min(left[i], j);\n right[i] = max(right[i], j);\n }\n }\n vector<vector<int>> dp(get_n(), vector<int>(get_n()));\n for (int i = 0; i < size(dp); ++i) {\n int l = get_m();\n int r = -1;\n int u = get_n();\n int d = -1;\n for (int j = i; j < size(dp[0]); ++j) {\n if (right[j] != -1) {\n l = min(l, left[j]);\n r = max(r, right[j]);\n u = min(u, j);\n d = max(d, j);\n }\n dp[i][j] = r >= 0 ? (r - l + 1) * (d - u + 1) : 0;\n }\n }\n return dp;\n };\n\n const auto& up_left = count(0, size(grid), 0, size(grid[0]));\n const auto& up_right = count(0, size(grid), size(grid[0]) - 1, -1);\n const auto& down_left = count(size(grid) - 1, -1, 0, size(grid[0]));\n const auto& down_right = count(size(grid) - 1, -1, size(grid[0]) - 1, -1);\n const auto& horizon = count2(false);\n const auto& vertical = count2(true);\n int result = numeric_limits<int>::max();\n for (int i = 0; i + 1 < size(grid); ++i) {\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n result = min({result,\n up_left[i][j] + up_right[i][j + 1] + horizon[i + 1][size(grid) - 1],\n horizon[0][i] + down_left[i + 1][j] + down_right[i + 1][j + 1],\n up_left[i][j] + down_left[i + 1][j] + vertical[j + 1][size(grid[0]) - 1],\n vertical[0][j] + up_right[i][j + 1] + down_right[i + 1][j + 1]\n });\n }\n }\n for (int i = 0; i + 2 < size(grid); ++i) {\n for (int j = i + 1; j + 1 < size(grid); ++j) {\n result = min(result, horizon[0][i] + horizon[i + 1][j] + horizon[j + 1][size(grid) - 1]);\n }\n }\n for (int i = 0; i + 2 < size(grid[0]); ++i) {\n for (int j = i + 1; j + 1 < size(grid[0]); ++j) {\n result = min(result, vertical[0][i] + vertical[i + 1][j] + vertical[j + 1][size(grid[0]) - 1]);\n }\n }\n return result;\n }\n};\n\n// Time: O(n * m * log(max(n, m)) + max(n, m)^2)\n// Space: O(n * m * log(max(n, m)))\n// sparse table\nclass Solution2 {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n vector<SparseTable> st_min_i, st_max_i, st_min_j, st_max_j;\n const auto& minimumArea = [&](int min_i, int max_i, int min_j, int max_j) {\n const int min_r = min(st_min_i[(size(grid) - 1) - min_i].query(min_j, max_j), max_i + 1);\n const int max_r = max(st_max_i[max_i].query(min_j, max_j), min_i - 1);\n const int min_c = min(st_min_j[(size(grid[0]) - 1) - min_j].query(min_i, max_i), max_j + 1);\n const int max_c = max(st_max_j[max_j].query(min_i, max_i), min_j - 1);\n return min_r <= max_i ? (max_r - min_r + 1) * (max_c - min_c + 1) : 0;\n };\n\n vector<int> curr;\n curr.assign(size(grid[0]), size(grid));\n for (int i = size(grid) - 1; i >= 0; --i) {\n for (int j = 0; j < size(grid[0]); ++j) {\n if (grid[i][j]) {\n curr[j] = i;\n }\n }\n st_min_i.emplace_back(SparseTable(curr, [&](int i, int j) { return min(i, j); }));\n }\n curr.assign(size(grid[0]), -1);\n for (int i = 0; i < size(grid); ++i) {\n for (int j = 0; j < size(grid[0]); ++j) {\n if (grid[i][j]) {\n curr[j] = i;\n }\n }\n st_max_i.emplace_back(SparseTable(curr, [&](int i, int j) { return max(i, j); }));\n }\n curr.assign(size(grid), size(grid[0]));\n for (int j = size(grid[0]) - 1; j >= 0; --j) {\n for (int i = 0; i < size(grid); ++i) {\n if (grid[i][j]) {\n curr[i] = j;\n }\n }\n st_min_j.emplace_back(SparseTable(curr, [&](int i, int j) { return min(i, j); }));\n }\n curr.assign(size(grid), -1);\n for (int j = 0; j < size(grid[0]); ++j) {\n for (int i = 0; i < size(grid); ++i) {\n if (grid[i][j]) {\n curr[i] = j;\n }\n }\n st_max_j.emplace_back(SparseTable(curr, [&](int i, int j) { return max(i, j); }));\n }\n int result = numeric_limits<int>::max();\n for (int i = 0; i + 1 < size(grid); ++i) {\n int a = minimumArea(i + 1, size(grid) - 1, 0, size(grid[0]) - 1);\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n const int b = minimumArea(0, i, 0, j);\n const int c = minimumArea(0, i, j + 1, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n for (int i = 0; i + 1 < size(grid); ++i) {\n int a = minimumArea(0, i, 0, size(grid[0]) - 1);\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n const int b = minimumArea(i + 1, size(grid) - 1, 0, j);\n const int c = minimumArea(i + 1, size(grid) - 1, j + 1, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n int a = minimumArea(0, size(grid) - 1, j + 1, size(grid[0]) - 1);\n for (int i = 0; i + 1 < size(grid); ++i) {\n const int b = minimumArea(0, i, 0, j);\n const int c = minimumArea(i + 1, size(grid) - 1, 0, j);\n result = min(result, a + b + c);\n }\n }\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n int a = minimumArea(0, size(grid) - 1, 0, j);\n for (int i = 0; i + 1 < size(grid); ++i) {\n const int b = minimumArea(0, i, j + 1, size(grid[0]) - 1);\n const int c = minimumArea(i + 1, size(grid) - 1, j + 1, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n for (int i = 0; i + 2 < size(grid); ++i) {\n int a = minimumArea(0, i, 0, size(grid[0]) - 1);\n for (int j = i + 1; j + 1 < size(grid); ++j) {\n const int b = minimumArea(i + 1, j, 0, size(grid[0]) - 1);\n const int c = minimumArea(j + 1, size(grid) - 1, 0, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n for (int i = 0; i + 2 < size(grid[0]); ++i) {\n int a = minimumArea(0, size(grid) - 1, 0, i);\n for (int j = i + 1; j + 1 < size(grid[0]); ++j) {\n const int b = minimumArea(0, size(grid) - 1, i + 1, j);\n const int c = minimumArea(0, size(grid) - 1, j + 1, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n return result;\n }\n\nprivate:\n // Reference: https://cp-algorithms.com/data_structures/sparse-table.html\n class SparseTable {\n public:\n SparseTable(const vector<int>& arr, function<int (int, int)> fn)\n : fn(fn) { // Time: O(nlogn) * O(fn) = O(nlogn), Space: O(nlogn)\n const int n = size(arr);\n const int k = __lg(n);\n st.assign(k + 1, vector<int>(n));\n st[0].assign(cbegin(arr), cend(arr));\n for (int i = 1; i <= k; ++i) {\n for (int j = 0; j + (1 << i) <= n; ++j) {\n st[i][j] = fn(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);\n }\n }\n }\n\n int query(int L, int R) const {\n const int i = __lg(R - L + 1);\n return fn(st[i][L], st[i][R - (1 << i) + 1]); // Time: O(fn) = O(1)\n }\n \n private:\n vector<vector<int>> st;\n const function<int (int, int)>& fn;\n };\n};\n\n// Time: O(n * m * log(max(n, m)) + max(n, m)^2)\n// Space: O(n * m * log(max(n, m)))\n// sparse table\nclass Solution3 {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n const auto& rotate = [](const auto& grid) {\n vector<vector<int>> result(size(grid[0]), vector<int>(size(grid)));\n for (int i = 0; i < size(grid); ++i) {\n for (int j = 0; j < size(grid[0]); ++j) {\n result[j][(size(grid) - 1) - i] = grid[i][j];\n }\n }\n return result;\n };\n\n vector<SparseTable> st_min_i, st_max_i, st_min_j, st_max_j;\n const auto& minimumArea = [&](int min_i, int max_i, int min_j, int max_j) {\n const int min_r = min(st_min_i[(size(grid) - 1) - min_i].query(min_j, max_j), max_i + 1);\n const int max_r = max(st_max_i[max_i].query(min_j, max_j), min_i - 1);\n const int min_c = min(st_min_j[(size(grid[0]) - 1) - min_j].query(min_i, max_i), max_j + 1);\n const int max_c = max(st_max_j[max_j].query(min_i, max_i), min_j - 1);\n return min_r <= max_i ? (max_r - min_r + 1) * (max_c - min_c + 1) : 0;\n };\n \n vector<int> curr;\n int result = numeric_limits<int>::max();\n for (int _ = 0; _ < 4; ++_) {\n st_min_i.clear();\n curr.assign(size(grid[0]), size(grid));\n for (int i = size(grid) - 1; i >= 0; --i) {\n for (int j = 0; j < size(grid[0]); ++j) {\n if (grid[i][j]) {\n curr[j] = i;\n }\n }\n st_min_i.emplace_back(SparseTable(curr, [&](int i, int j) { return min(i, j); }));\n }\n st_max_i.clear();\n curr.assign(size(grid[0]), -1);\n for (int i = 0; i < size(grid); ++i) {\n for (int j = 0; j < size(grid[0]); ++j) {\n if (grid[i][j]) {\n curr[j] = i;\n }\n }\n st_max_i.emplace_back(SparseTable(curr, [&](int i, int j) { return max(i, j); }));\n }\n st_min_j.clear();\n curr.assign(size(grid), size(grid[0]));\n for (int j = size(grid[0]) - 1; j >= 0; --j) {\n for (int i = 0; i < size(grid); ++i) {\n if (grid[i][j]) {\n curr[i] = j;\n }\n }\n st_min_j.emplace_back(SparseTable(curr, [&](int i, int j) { return min(i, j); }));\n }\n st_max_j.clear();\n curr.assign(size(grid), -1);\n for (int j = 0; j < size(grid[0]); ++j) {\n for (int i = 0; i < size(grid); ++i) {\n if (grid[i][j]) {\n curr[i] = j;\n }\n }\n st_max_j.emplace_back(SparseTable(curr, [&](int i, int j) { return max(i, j); }));\n }\n for (int i = 0; i + 1 < size(grid); ++i) {\n const int a = minimumArea(0, i, 0, size(grid[0]) - 1);\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n const int b = minimumArea(i + 1, size(grid) - 1, 0, j);\n const int c = minimumArea(i + 1, size(grid) - 1, j + 1, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n for (int i = 0; i + 2 < size(grid); ++i) {\n const int a = minimumArea(0, i, 0, size(grid[0]) - 1);\n for (int j = i + 1; j + 1 < size(grid); ++j) {\n const int b = minimumArea(i + 1, j, 0, size(grid[0]) - 1);\n const int c = minimumArea(j + 1, size(grid) - 1, 0, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n grid = rotate(grid);\n }\n return result;\n }\n\nprivate:\n // Reference: https://cp-algorithms.com/data_structures/sparse-table.html\n class SparseTable {\n public:\n SparseTable(const vector<int>& arr, function<int (int, int)> fn)\n : fn(fn) { // Time: O(nlogn) * O(fn) = O(nlogn), Space: O(nlogn)\n const int n = size(arr);\n const int k = __lg(n);\n st.assign(k + 1, vector<int>(n));\n st[0].assign(cbegin(arr), cend(arr));\n for (int i = 1; i <= k; ++i) {\n for (int j = 0; j + (1 << i) <= n; ++j) {\n st[i][j] = fn(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);\n }\n }\n }\n\n int query(int L, int R) const {\n const int i = __lg(R - L + 1);\n return fn(st[i][L], st[i][R - (1 << i) + 1]); // Time: O(fn) = O(1)\n }\n \n private:\n vector<vector<int>> st;\n const function<int (int, int)>& fn;\n };\n};\n\n// Time: O(max(n, m)^2 * log(max(n, m)))\n// Space: O(1)\n// prefix sum, binary search\nclass Solution4 {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n const auto& binary_search = [](auto left, auto right, const auto& check) {\n while (left <= right) {\n const auto mid = left + (right - left) / 2;\n if (check(mid)) {\n right = mid - 1;\n } else {\n left = mid + 1;\n }\n }\n return left;\n };\n\n const auto& binary_search_right = [](auto left, auto right, const auto& check) {\n while (left <= right) {\n const auto mid = left + (right - left) / 2;\n if (!check(mid)) {\n right = mid - 1;\n } else {\n left = mid + 1;\n }\n }\n return right;\n };\n \n const auto& minimumArea = [&](int min_i, int max_i, int min_j, int max_j) {\n const auto& count = [&](int x1, int y1, int x2, int y2) {\n int cnt = grid[x2][y2];\n if (x1 - 1 >= 0) {\n cnt -= grid[x1 - 1][y2];\n }\n if (y1 - 1 >= 0) {\n cnt -= grid[x2][y1 - 1];\n }\n if (x1 - 1 >= 0 && y1 - 1 >= 0) {\n cnt += grid[x1 - 1][y1 - 1];\n }\n return cnt;\n };\n\n const int min_r = binary_search(min_i, max_i, [&](int i) { return count(min_i, min_j, i, max_j); });\n const int max_r = binary_search_right(min_i, max_i, [&](int i) { return count(i, min_j, max_i, max_j); });\n const int min_c = binary_search(min_j, max_j, [&](int j) { return count(min_i, min_j, max_i, j); });\n const int max_c = binary_search_right(min_j, max_j, [&](int j) { return count(min_i, j, max_i, max_j); });\n return min_r <= max_i ? (max_r - min_r + 1) * (max_c - min_c + 1) : 0;\n };\n\n for (int i = 0; i < size(grid); ++i) {\n for (int j = 0; j < size(grid[0]); ++j) {\n if (i - 1 >= 0) {\n grid[i][j] += grid[i - 1][j];\n }\n if (j - 1 >= 0) {\n grid[i][j] += grid[i][j - 1];\n }\n if (i - 1 >= 0 && j - 1 >= 0) {\n grid[i][j] -= grid[i - 1][j - 1];\n }\n }\n }\n int result = numeric_limits<int>::max();\n for (int i = 0; i + 1 < size(grid); ++i) {\n int a = minimumArea(i + 1, size(grid) - 1, 0, size(grid[0]) - 1);\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n const int b = minimumArea(0, i, 0, j);\n const int c = minimumArea(0, i, j + 1, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n for (int i = 0; i + 1 < size(grid); ++i) {\n int a = minimumArea(0, i, 0, size(grid[0]) - 1);\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n const int b = minimumArea(i + 1, size(grid) - 1, 0, j);\n const int c = minimumArea(i + 1, size(grid) - 1, j + 1, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n int a = minimumArea(0, size(grid) - 1, j + 1, size(grid[0]) - 1);\n for (int i = 0; i + 1 < size(grid); ++i) {\n const int b = minimumArea(0, i, 0, j);\n const int c = minimumArea(i + 1, size(grid) - 1, 0, j);\n result = min(result, a + b + c);\n }\n }\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n int a = minimumArea(0, size(grid) - 1, 0, j);\n for (int i = 0; i + 1 < size(grid); ++i) {\n const int b = minimumArea(0, i, j + 1, size(grid[0]) - 1);\n const int c = minimumArea(i + 1, size(grid) - 1, j + 1, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n for (int i = 0; i + 2 < size(grid); ++i) {\n int a = minimumArea(0, i, 0, size(grid[0]) - 1);\n for (int j = i + 1; j + 1 < size(grid); ++j) {\n const int b = minimumArea(i + 1, j, 0, size(grid[0]) - 1);\n const int c = minimumArea(j + 1, size(grid) - 1, 0, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n for (int i = 0; i + 2 < size(grid[0]); ++i) {\n int a = minimumArea(0, size(grid) - 1, 0, i);\n for (int j = i + 1; j + 1 < size(grid[0]); ++j) {\n const int b = minimumArea(0, size(grid) - 1, i + 1, j);\n const int c = minimumArea(0, size(grid) - 1, j + 1, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n return result;\n }\n};\n\n// Time: O(max(n, m)^2 * log(max(n, m)))\n// Space: O(n * m)\n// prefix sum, binary search\nclass Solution5 {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n const auto& rotate = [](const auto& grid) {\n vector<vector<int>> result(size(grid[0]), vector<int>(size(grid)));\n for (int i = 0; i < size(grid); ++i) {\n for (int j = 0; j < size(grid[0]); ++j) {\n result[j][(size(grid) - 1) - i] = grid[i][j];\n }\n }\n return result;\n };\n \n const auto& binary_search = [](auto left, auto right, const auto& check) {\n while (left <= right) {\n const auto mid = left + (right - left) / 2;\n if (check(mid)) {\n right = mid - 1;\n } else {\n left = mid + 1;\n }\n }\n return left;\n };\n\n const auto& binary_search_right = [](auto left, auto right, const auto& check) {\n while (left <= right) {\n const auto mid = left + (right - left) / 2;\n if (!check(mid)) {\n right = mid - 1;\n } else {\n left = mid + 1;\n }\n }\n return right;\n };\n\n vector<vector<int>> prefix;\n const auto& minimumArea = [&](int min_i, int max_i, int min_j, int max_j) {\n const auto& count = [&](int x1, int y1, int x2, int y2) {\n int cnt = prefix[x2][y2];\n if (x1 - 1 >= 0) {\n cnt -= prefix[x1 - 1][y2];\n }\n if (y1 - 1 >= 0) {\n cnt -= prefix[x2][y1 - 1];\n }\n if (x1 - 1 >= 0 && y1 - 1 >= 0) {\n cnt += prefix[x1 - 1][y1 - 1];\n }\n return cnt;\n };\n\n const int min_r = binary_search(min_i, max_i, [&](int i) { return count(min_i, min_j, i, max_j); });\n const int max_r = binary_search_right(min_i, max_i, [&](int i) { return count(i, min_j, max_i, max_j); });\n const int min_c = binary_search(min_j, max_j, [&](int j) { return count(min_i, min_j, max_i, j); });\n const int max_c = binary_search_right(min_j, max_j, [&](int j) { return count(min_i, j, max_i, max_j); });\n return min_r <= max_i ? (max_r - min_r + 1) * (max_c - min_c + 1) : 0;\n };\n\n int result = numeric_limits<int>::max();\n for (int _ = 0; _ < 4; ++_) {\n prefix.assign(size(grid), vector<int>(size(grid[0])));\n for (int i = 0; i < size(grid); ++i) {\n for (int j = 0; j < size(grid[0]); ++j) {\n prefix[i][j] = grid[i][j];\n if (i - 1 >= 0) {\n prefix[i][j] += prefix[i - 1][j];\n }\n if (j - 1 >= 0) {\n prefix[i][j] += prefix[i][j - 1];\n }\n if (i - 1 >= 0 && j - 1 >= 0) {\n prefix[i][j] -= prefix[i - 1][j - 1];\n }\n }\n }\n for (int i = 0; i + 1 < size(grid); ++i) {\n const int a = minimumArea(0, i, 0, size(grid[0]) - 1);\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n const int b = minimumArea(i + 1, size(grid) - 1, 0, j);\n const int c = minimumArea(i + 1, size(grid) - 1, j + 1, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n for (int i = 0; i + 2 < size(grid); ++i) {\n const int a = minimumArea(0, i, 0, size(grid[0]) - 1);\n for (int j = i + 1; j + 1 < size(grid); ++j) {\n const int b = minimumArea(i + 1, j, 0, size(grid[0]) - 1);\n const int c = minimumArea(j + 1, size(grid) - 1, 0, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n grid = rotate(grid);\n }\n return result;\n }\n};\n\n// Time: O((n^2 + m^2 + 4 * n * m) * n * m) = O(max(n, m)^3 * min(n, m))\n// Space: O(1)\n// brute force\nclass Solution6 {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n const auto& minimumArea = [&](int min_i, int max_i, int min_j, int max_j) {\n int min_r = max_i + 1;\n int max_r = min_i - 1;\n int min_c = max_j + 1;\n int max_c = min_j - 1;\n for (int i = min_i; i <= max_i; ++i) {\n for (int j = min_j; j <= max_j; ++j) {\n if (grid[i][j] == 0) {\n continue;\n }\n min_r = min(min_r, i);\n max_r = max(max_r, i);\n min_c = min(min_c, j);\n max_c = max(max_c, j);\n }\n }\n return min_r <= max_i ? (max_r - min_r + 1) * (max_c - min_c + 1) : 0;\n };\n\n int result = numeric_limits<int>::max();\n for (int i = 0; i + 1 < size(grid); ++i) {\n int a = minimumArea(i + 1, size(grid) - 1, 0, size(grid[0]) - 1);\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n const int b = minimumArea(0, i, 0, j);\n const int c = minimumArea(0, i, j + 1, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n for (int i = 0; i + 1 < size(grid); ++i) {\n int a = minimumArea(0, i, 0, size(grid[0]) - 1);\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n const int b = minimumArea(i + 1, size(grid) - 1, 0, j);\n const int c = minimumArea(i + 1, size(grid) - 1, j + 1, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n int a = minimumArea(0, size(grid) - 1, j + 1, size(grid[0]) - 1);\n for (int i = 0; i + 1 < size(grid); ++i) {\n const int b = minimumArea(0, i, 0, j);\n const int c = minimumArea(i + 1, size(grid) - 1, 0, j);\n result = min(result, a + b + c);\n }\n }\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n int a = minimumArea(0, size(grid) - 1, 0, j);\n for (int i = 0; i + 1 < size(grid); ++i) {\n const int b = minimumArea(0, i, j + 1, size(grid[0]) - 1);\n const int c = minimumArea(i + 1, size(grid) - 1, j + 1, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n for (int i = 0; i + 2 < size(grid); ++i) {\n int a = minimumArea(0, i, 0, size(grid[0]) - 1);\n for (int j = i + 1; j + 1 < size(grid); ++j) {\n const int b = minimumArea(i + 1, j, 0, size(grid[0]) - 1);\n const int c = minimumArea(j + 1, size(grid) - 1, 0, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n for (int i = 0; i + 2 < size(grid[0]); ++i) {\n int a = minimumArea(0, size(grid) - 1, 0, i);\n for (int j = i + 1; j + 1 < size(grid[0]); ++j) {\n const int b = minimumArea(0, size(grid) - 1, i + 1, j);\n const int c = minimumArea(0, size(grid) - 1, j + 1, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n return result;\n }\n};\n\n// Time: O((n^2 + m^2 + 4 * n * m) * n * m) = O(max(n, m)^3 * min(n, m))\n// Space: O(n * m)\n// brute force\nclass Solution7 {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n const auto& rotate = [](const auto& grid) {\n vector<vector<int>> result(size(grid[0]), vector<int>(size(grid)));\n for (int i = 0; i < size(grid); ++i) {\n for (int j = 0; j < size(grid[0]); ++j) {\n result[j][(size(grid) - 1) - i] = grid[i][j];\n }\n }\n return result;\n };\n \n const auto& minimumArea = [&](int min_i, int max_i, int min_j, int max_j) {\n int min_r = max_i + 1;\n int max_r = min_i - 1;\n int min_c = max_j + 1;\n int max_c = min_j - 1;\n for (int i = min_i; i <= max_i; ++i) {\n for (int j = min_j; j <= max_j; ++j) {\n if (grid[i][j] == 0) {\n continue;\n }\n min_r = min(min_r, i);\n max_r = max(max_r, i);\n min_c = min(min_c, j);\n max_c = max(max_c, j);\n }\n }\n return min_r <= max_i ? (max_r - min_r + 1) * (max_c - min_c + 1) : 0;\n };\n\n int result = numeric_limits<int>::max();\n for (int _ = 0; _ < 4; ++_) {\n for (int i = 0; i + 1 < size(grid); ++i) {\n const int a = minimumArea(0, i, 0, size(grid[0]) - 1);\n for (int j = 0; j + 1 < size(grid[0]); ++j) {\n const int b = minimumArea(i + 1, size(grid) - 1, 0, j);\n const int c = minimumArea(i + 1, size(grid) - 1, j + 1, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n for (int i = 0; i + 2 < size(grid); ++i) {\n const int a = minimumArea(0, i, 0, size(grid[0]) - 1);\n for (int j = i + 1; j + 1 < size(grid); ++j) {\n const int b = minimumArea(i + 1, j, 0, size(grid[0]) - 1);\n const int c = minimumArea(j + 1, size(grid) - 1, 0, size(grid[0]) - 1);\n result = min(result, a + b + c);\n }\n }\n grid = rotate(grid);\n }\n return result;\n }\n};", "memory": "29400" }