id
int64 1
3.58k
| problem_description
stringlengths 516
21.8k
| instruction
int64 0
3
| solution_c
dict |
---|---|---|---|
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n int minR(vector<int>& rc, vector<int>& cc, int r0, int c0, int r1, int c1) {\n int a=r0;\n while (a<=r1 and rc[a]==0) ++a;\n if (a>r1) return 0;\n int b=r1;\n while (b>a and rc[b]==0) --b;\n int c=c0;\n while (c<=c1 and cc[c]==0) ++c;\n if (c>c1) return 0;\n int d=c1;\n while (d>c and cc[d]==0) --d;\n return (b-a+1)*(d-c+1);\n }\n int minR2(vector<vector<int>>& grid, vector<int>& rc, vector<int>& cc, int r0, int c0, int r1, int c1) {\n if (r0==r1 and c0==c1) return INT_MAX;\n int res=INT_MAX;\n vector<int> cc1(c1+1), cc2(cc);\n for (int r=r0; r<r1; ++r) {\n for (int c=c0; c<=c1; ++c) {\n cc1[c]+=grid[r][c];\n cc2[c]-=grid[r][c];\n }\n int s1=minR(rc,cc1,r0,c0,r,c1);\n if (s1==0) continue;\n int s2=minR(rc,cc2,r+1,c0,r1,c1);\n if (s2==0) continue;\n res=min(res,s1+s2);\n }\n vector<int> rc1(r1+1), rc2(rc);\n for (int c=c0; c<c1; ++c) {\n for (int r=r0; r<=r1; ++r) {\n rc1[r]+=grid[r][c];\n rc2[r]-=grid[r][c];\n }\n int s1=minR(rc1,cc,r0,c0,r1,c);\n if (s1==0) continue;\n int s2=minR(rc2,cc,r0,c+1,r1,c1);\n if (s2==0) continue;\n res=min(res,s1+s2);\n }\n return res;\n }\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n int rs=int(grid.size()), cs=int(grid[0].size());\n vector<int> rc(rs), cc(cs);\n for (int r=0; r<rs; ++r) {\n for (int c=0; c<cs; ++c) {\n rc[r]+=grid[r][c];\n cc[c]+=grid[r][c];\n }\n } \n int res=INT_MAX;\n vector<int> cc1(cs), cc2(cc);\n for (int r=0; r+1<rs; ++r) {\n for (int c=0; c<cs; ++c) {\n cc1[c]+=grid[r][c];\n cc2[c]-=grid[r][c];\n }\n {\n int s1=minR(rc,cc1,0,0,r,cs-1);\n if (s1>0) {\n int s2=minR2(grid,rc,cc2,r+1,0,rs-1,cs-1);\n if (s2<INT_MAX) res=min(res,s1+s2);\n }\n }\n {\n int s1=minR(rc,cc2,r+1,0,rs-1,cs-1);\n if (s1>0) {\n int s2=minR2(grid,rc,cc1,0,0,r,cs-1);\n if (s2<INT_MAX) res=min(res,s1+s2);\n }\n }\n }\n vector<int> rc1(rs), rc2(rc);\n for (int c=0; c+1<cs; ++c) {\n for (int r=0; r<rs; ++r) {\n rc1[r]+=grid[r][c];\n rc2[r]-=grid[r][c];\n }\n {\n int s1=minR(rc1,cc,0,0,rs-1,c);\n if (s1>0) {\n int s2=minR2(grid,rc2,cc,0,c+1,rs-1,cs-1);\n if (s2<INT_MAX) res=min(res,s1+s2);\n }\n }\n {\n int s1=minR(rc2,cc,0,c+1,rs-1,cs-1);\n if (s1>0) {\n int s2=minR2(grid,rc1,cc,0,0,rs-1,c);\n if (s2<INT_MAX) res=min(res,s1+s2);\n }\n } \n }\n return res;\n }\n};",
"memory": "29800"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[31][31][31][31][3];\n int minimumArea(vector<vector<int>>& grid,int i1,int i2,int j1,int j2) {\n int ix=0,in=1e4,jx=0,jn=1e4;\n for(int i=i1;i<=i2;i++){\n for(int j=j1;j<=j2;j++){\n if(grid[i][j]){\n ix=max(ix,i);\n in=min(in,i);\n jx=max(jx,j);\n jn=min(jn,j);\n }\n }\n }\n int ans=max(0,(ix-in+1)*(jx-jn+1));\n return ans;\n }\n int rec(vector<vector<int>>& grid,int num,int i1,int i2,int j1,int j2){\n if(dp[i1][i2][j1][j2][num-1]!=-1)\n return dp[i1][i2][j1][j2][num-1];\n if(num==1)\n return minimumArea(grid,i1,i2,j1,j2);\n int ans=1e9;\n for(int j=j1;j<j2;j++)\n {\n ans=min(ans,rec(grid,num-1,i1,i2,j1,j)+rec(grid,1,i1,i2,j+1,j2));\n ans=min(ans,rec(grid,1,i1,i2,j1,j)+rec(grid,num-1,i1,i2,j+1,j2));\n }\n for(int i=i1;i<i2;i++)\n {\n ans=min(ans,rec(grid,num-1,i1,i,j1,j2)+rec(grid,1,i+1,i2,j1,j2));\n ans=min(ans,rec(grid,1,i1,i,j1,j2)+rec(grid,num-1,i+1,i2,j1,j2));\n }\n return dp[i1][i2][j1][j2][num-1]=ans;\n }\n int minimumSum(vector<vector<int>>& grid) {\n int n=grid.size(),m=grid[0].size();\n memset(dp,-1,sizeof(dp));\n return rec(grid,3,0,n-1,0,m-1);\n }\n};",
"memory": "36700"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "int dp[30][30][30][30];\nstatic const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\n int n, m;\n const int INF = 1e6;\n\n struct Rectangle {\n int row_start, row_end, col_start, col_end;\n inline int area() {\n if (row_start == INT_MAX) return 0;\n return (row_end - row_start + 1) * (col_end - col_start + 1);\n }\n inline void show() {\n cout << \"Rectangle: \" << row_start << \", \" << row_end << \", \" << col_start << \", \" << col_end << endl;\n }\n };\n\n inline bool invalid(const Rectangle* r) {\n return r->row_start == n || r->col_start == m;\n }\n int minAreaForSingleRectangle(vector<vector<int>>& grid, Rectangle* r) {\n if (invalid(r)) return INF;\n if (dp[r->row_start][r->row_end][r->col_start][r->col_end] != -1) {\n return dp[r->row_start][r->row_end][r->col_start][r->col_end];\n }\n\n Rectangle rt = Rectangle{INT_MAX, INT_MIN, INT_MAX, INT_MIN};\n for (int i = r->row_start ; i <= r->row_end ; i++) {\n for (int j = r->col_start ; j <= r->col_end ; j++) {\n if (grid[i][j] == 0) continue;\n rt.row_start = min(rt.row_start, i);\n rt.row_end = max(rt.row_end, i);\n rt.col_start = min(rt.col_start,j);\n rt.col_end = max(rt.col_end, j);\n }\n }\n // int area = rt.area();\n // dp[r->row_start][r->row_end][r->col_start][r->col_end] = area;\n // delete r;\n // return area;\n\n return dp[r->row_start][r->row_end][r->col_start][r->col_end] = rt.area();\n }\n\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n n = grid.size(), m = grid[0].size();\n memset(dp, -1, sizeof(dp));\n int result = n * m;\n for (int i = 1 ; i < n ; i++) {\n int area = minAreaForSingleRectangle(grid, new Rectangle{0, i-1, 0, m - 1}), other = INF;\n for (int j = 1 ; j < m ; j++) {\n other = min(\n other,\n minAreaForSingleRectangle(grid, new Rectangle{i, n-1, 0, j-1}) + minAreaForSingleRectangle(grid, new Rectangle{i, n-1, j, m-1})\n );\n }\n result = min(result, area + other);\n }\n\n for (int i = 1 ; i < n ; i++) {\n int area = minAreaForSingleRectangle(grid, new Rectangle{i, n-1, 0, m - 1}), other = INF;\n for (int j = 1 ; j < m ; j++) {\n other = min(\n other,\n minAreaForSingleRectangle(grid, new Rectangle{0, i-1, 0, j-1}) + minAreaForSingleRectangle(grid, new Rectangle{0, i-1, j, m-1})\n );\n }\n result = min(result, area + other);\n }\n\n for (int j = 1 ; j < m ; j++) {\n int area = minAreaForSingleRectangle(grid, new Rectangle{0, n-1, 0, j-1}), other = INF;\n for (int i = 1 ; i < n ; i++) {\n other = min(\n other,\n minAreaForSingleRectangle(grid, new Rectangle{0, i-1, j, m-1}) + minAreaForSingleRectangle(grid, new Rectangle{i, n-1, j, m-1})\n );\n }\n result = min(result, area + other);\n }\n\n for (int j = 1 ; j < m ; j++) {\n int area = minAreaForSingleRectangle(grid, new Rectangle{0, n-1, j, m-1}), other = INF;\n for (int i = 1 ; i < n ; i++) {\n other = min(\n other,\n minAreaForSingleRectangle(grid, new Rectangle{0, i-1, 0, j-1}) + minAreaForSingleRectangle(grid, new Rectangle{i, n-1, 0, j-1})\n );\n }\n result = min(result, area + other);\n }\n\n for (int i = 1 ; i < n ; i++) {\n for (int i2 = i + 1 ; i2 < n ; i2++) {\n result = min(\n result,\n minAreaForSingleRectangle(grid, new Rectangle{0, i-1, 0, m - 1})\n + minAreaForSingleRectangle(grid, new Rectangle{i, i2-1, 0, m - 1}) \n + minAreaForSingleRectangle(grid, new Rectangle{i2, n-1, 0, m - 1})\n );\n }\n }\n\n for (int j = 1 ; j < m ; j++) {\n for (int j2 = j + 1 ; j2 < m ; j2++) {\n result = min(\n result,\n minAreaForSingleRectangle(grid, new Rectangle{0, n-1, 0, j - 1})\n + minAreaForSingleRectangle(grid, new Rectangle{0, n-1, j, j2 - 1}) \n + minAreaForSingleRectangle(grid, new Rectangle{0, n-1, j2, m - 1})\n );\n }\n }\n\n return result;\n }\n};",
"memory": "38800"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "int dp[30][30][30][30];\n\nclass Solution {\n int n, m;\n const int INF = 1e6;\n\n struct Rectangle {\n int row_start, row_end, col_start, col_end;\n inline int area() {\n if (row_start == INT_MAX) return 0;\n return (row_end - row_start + 1) * (col_end - col_start + 1);\n }\n inline void show() {\n cout << \"Rectangle: \" << row_start << \", \" << row_end << \", \" << col_start << \", \" << col_end << endl;\n }\n };\n\n inline bool invalid(const Rectangle* r) {\n return r->row_start == n || r->col_start == m;\n }\n int minAreaForSingleRectangle(vector<vector<int>>& grid, Rectangle* r) {\n if (invalid(r)) return INF;\n if (dp[r->row_start][r->row_end][r->col_start][r->col_end] != -1) {\n return dp[r->row_start][r->row_end][r->col_start][r->col_end];\n }\n\n Rectangle rt = Rectangle{INT_MAX, INT_MIN, INT_MAX, INT_MIN};\n for (int i = r->row_start ; i <= r->row_end ; i++) {\n for (int j = r->col_start ; j <= r->col_end ; j++) {\n if (grid[i][j] == 0) continue;\n rt.row_start = min(rt.row_start, i);\n rt.row_end = max(rt.row_end, i);\n rt.col_start = min(rt.col_start,j);\n rt.col_end = max(rt.col_end, j);\n }\n }\n // int area = rt.area();\n // dp[r->row_start][r->row_end][r->col_start][r->col_end] = area;\n // delete r;\n // return area;\n\n return dp[r->row_start][r->row_end][r->col_start][r->col_end] = rt.area();\n }\n\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n n = grid.size(), m = grid[0].size();\n memset(dp, -1, sizeof(dp));\n int result = n * m;\n for (int i = 1 ; i < n ; i++) {\n int area = minAreaForSingleRectangle(grid, new Rectangle{0, i-1, 0, m - 1}), other = INF;\n for (int j = 1 ; j < m ; j++) {\n other = min(\n other,\n minAreaForSingleRectangle(grid, new Rectangle{i, n-1, 0, j-1}) + minAreaForSingleRectangle(grid, new Rectangle{i, n-1, j, m-1})\n );\n }\n result = min(result, area + other);\n }\n\n for (int i = 1 ; i < n ; i++) {\n int area = minAreaForSingleRectangle(grid, new Rectangle{i, n-1, 0, m - 1}), other = INF;\n for (int j = 1 ; j < m ; j++) {\n other = min(\n other,\n minAreaForSingleRectangle(grid, new Rectangle{0, i-1, 0, j-1}) + minAreaForSingleRectangle(grid, new Rectangle{0, i-1, j, m-1})\n );\n }\n result = min(result, area + other);\n }\n\n for (int j = 1 ; j < m ; j++) {\n int area = minAreaForSingleRectangle(grid, new Rectangle{0, n-1, 0, j-1}), other = INF;\n for (int i = 1 ; i < n ; i++) {\n other = min(\n other,\n minAreaForSingleRectangle(grid, new Rectangle{0, i-1, j, m-1}) + minAreaForSingleRectangle(grid, new Rectangle{i, n-1, j, m-1})\n );\n }\n result = min(result, area + other);\n }\n\n for (int j = 1 ; j < m ; j++) {\n int area = minAreaForSingleRectangle(grid, new Rectangle{0, n-1, j, m-1}), other = INF;\n for (int i = 1 ; i < n ; i++) {\n other = min(\n other,\n minAreaForSingleRectangle(grid, new Rectangle{0, i-1, 0, j-1}) + minAreaForSingleRectangle(grid, new Rectangle{i, n-1, 0, j-1})\n );\n }\n result = min(result, area + other);\n }\n\n for (int i = 1 ; i < n ; i++) {\n for (int i2 = i + 1 ; i2 < n ; i2++) {\n result = min(\n result,\n minAreaForSingleRectangle(grid, new Rectangle{0, i-1, 0, m - 1})\n + minAreaForSingleRectangle(grid, new Rectangle{i, i2-1, 0, m - 1}) \n + minAreaForSingleRectangle(grid, new Rectangle{i2, n-1, 0, m - 1})\n );\n }\n }\n\n for (int j = 1 ; j < m ; j++) {\n for (int j2 = j + 1 ; j2 < m ; j2++) {\n result = min(\n result,\n minAreaForSingleRectangle(grid, new Rectangle{0, n-1, 0, j - 1})\n + minAreaForSingleRectangle(grid, new Rectangle{0, n-1, j, j2 - 1}) \n + minAreaForSingleRectangle(grid, new Rectangle{0, n-1, j2, m - 1})\n );\n }\n }\n\n return result;\n }\n};",
"memory": "38900"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "int dp[30][30][30][30];\n\nclass Solution {\n int n, m;\n const int INF = 1e6;\n\n struct Rectangle {\n int row_start, row_end, col_start, col_end;\n inline int area() {\n if (row_start == INT_MAX) return 0;\n return (row_end - row_start + 1) * (col_end - col_start + 1);\n }\n inline void show() {\n cout << \"Rectangle: \" << row_start << \", \" << row_end << \", \" << col_start << \", \" << col_end << endl;\n }\n };\n\n inline bool invalid(const Rectangle* r) {\n return r->row_start == n || r->col_start == m;\n }\n int minAreaForSingleRectangle(vector<vector<int>>& grid, Rectangle* r) {\n if (invalid(r)) return INF;\n if (dp[r->row_start][r->row_end][r->col_start][r->col_end] != -1) {\n return dp[r->row_start][r->row_end][r->col_start][r->col_end];\n }\n\n Rectangle rt = Rectangle{INT_MAX, INT_MIN, INT_MAX, INT_MIN};\n for (int i = r->row_start ; i <= r->row_end ; i++) {\n for (int j = r->col_start ; j <= r->col_end ; j++) {\n if (grid[i][j] == 0) continue;\n rt.row_start = min(rt.row_start, i);\n rt.row_end = max(rt.row_end, i);\n rt.col_start = min(rt.col_start,j);\n rt.col_end = max(rt.col_end, j);\n }\n }\n int area = rt.area();\n dp[r->row_start][r->row_end][r->col_start][r->col_end] = area;\n delete r;\n return area;\n }\n\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n n = grid.size(), m = grid[0].size();\n memset(dp, -1, sizeof(dp));\n int result = n * m;\n for (int i = 1 ; i < n ; i++) {\n int area = minAreaForSingleRectangle(grid, new Rectangle{0, i-1, 0, m - 1}), other = INF;\n for (int j = 1 ; j < m ; j++) {\n other = min(\n other,\n minAreaForSingleRectangle(grid, new Rectangle{i, n-1, 0, j-1}) + minAreaForSingleRectangle(grid, new Rectangle{i, n-1, j, m-1})\n );\n }\n result = min(result, area + other);\n }\n\n for (int i = 1 ; i < n ; i++) {\n int area = minAreaForSingleRectangle(grid, new Rectangle{i, n-1, 0, m - 1}), other = INF;\n for (int j = 1 ; j < m ; j++) {\n other = min(\n other,\n minAreaForSingleRectangle(grid, new Rectangle{0, i-1, 0, j-1}) + minAreaForSingleRectangle(grid, new Rectangle{0, i-1, j, m-1})\n );\n }\n result = min(result, area + other);\n }\n\n for (int j = 1 ; j < m ; j++) {\n int area = minAreaForSingleRectangle(grid, new Rectangle{0, n-1, 0, j-1}), other = INF;\n for (int i = 1 ; i < n ; i++) {\n other = min(\n other,\n minAreaForSingleRectangle(grid, new Rectangle{0, i-1, j, m-1}) + minAreaForSingleRectangle(grid, new Rectangle{i, n-1, j, m-1})\n );\n }\n result = min(result, area + other);\n }\n\n for (int j = 1 ; j < m ; j++) {\n int area = minAreaForSingleRectangle(grid, new Rectangle{0, n-1, j, m-1}), other = INF;\n for (int i = 1 ; i < n ; i++) {\n other = min(\n other,\n minAreaForSingleRectangle(grid, new Rectangle{0, i-1, 0, j-1}) + minAreaForSingleRectangle(grid, new Rectangle{i, n-1, 0, j-1})\n );\n }\n result = min(result, area + other);\n }\n\n for (int i = 1 ; i < n ; i++) {\n for (int i2 = i + 1 ; i2 < n ; i2++) {\n result = min(\n result,\n minAreaForSingleRectangle(grid, new Rectangle{0, i-1, 0, m - 1})\n + minAreaForSingleRectangle(grid, new Rectangle{i, i2-1, 0, m - 1}) \n + minAreaForSingleRectangle(grid, new Rectangle{i2, n-1, 0, m - 1})\n );\n }\n }\n\n for (int j = 1 ; j < m ; j++) {\n for (int j2 = j + 1 ; j2 < m ; j2++) {\n result = min(\n result,\n minAreaForSingleRectangle(grid, new Rectangle{0, n-1, 0, j - 1})\n + minAreaForSingleRectangle(grid, new Rectangle{0, n-1, j, j2 - 1}) \n + minAreaForSingleRectangle(grid, new Rectangle{0, n-1, j2, m - 1})\n );\n }\n }\n\n return result;\n }\n};",
"memory": "40200"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n auto sol = [](vector<vector<int>> &grid) {\n int n = grid.size(), m = grid[0].size();\n vector<vector<int>> topLeft(n, vector<int>(m));\n vector<vector<int>> bottomLeft(n, vector<int>(m));\n vector<vector<int>> topRight(n, vector<int>(m));\n vector<int> rl(m, n), rr(m, 0), cl(m, m), cr(m, 0);\n for (int i = 0; i < n; ++i) {\n int rowL = n, rowR = 0, colL = m, colR = 0;\n for (int j = 0; j < m; ++j) {\n if (grid[i][j] == 1) {\n rl[j] = min(rl[j], i);\n rr[j] = max(rr[j], i);\n cl[j] = min(cl[j], j);\n cr[j] = max(cr[j], j);\n }\n rowL = min(rowL, rl[j]);\n rowR = max(rowR, rr[j]);\n colL = min(colL, cl[j]);\n colR = max(colR, cr[j]);\n topLeft[i][j] = max(0, (rowR - rowL + 1) * (colR - colL + 1));\n }\n }\n for (auto &x: rl) x = n;\n for (auto &x: rr) x = 0;\n for (auto &x: cl) x = m;\n for (auto &x: cr) x = 0;\n for (int i = n-1; i >= 0; --i) {\n int rowL = n, rowR = 0, colL = m, colR = 0;\n for (int j = 0; j < m; ++j) {\n if (grid[i][j] == 1) {\n rl[j] = min(rl[j], i);\n rr[j] = max(rr[j], i);\n cl[j] = min(cl[j], j);\n cr[j] = max(cr[j], j);\n }\n rowL = min(rowL, rl[j]);\n rowR = max(rowR, rr[j]);\n colL = min(colL, cl[j]);\n colR = max(colR, cr[j]);\n bottomLeft[i][j] = max(0, (rowR - rowL + 1) * (colR - colL + 1));\n }\n }\n for (auto &x: rl) x = n;\n for (auto &x: rr) x = 0;\n for (auto &x: cl) x = m;\n for (auto &x: cr) x = 0;\n for (int i = 0; i < n; ++i) {\n int rowL = n, rowR = 0, colL = m, colR = 0;\n for (int j = m-1; j >= 0; --j) {\n if (grid[i][j] == 1) {\n rl[j] = min(rl[j], i);\n rr[j] = max(rr[j], i);\n cl[j] = min(cl[j], j);\n cr[j] = max(cr[j], j);\n }\n rowL = min(rowL, rl[j]);\n rowR = max(rowR, rr[j]);\n colL = min(colL, cl[j]);\n colR = max(colR, cr[j]);\n topRight[i][j] = max(0, (rowR - rowL + 1) * (colR - colL + 1));\n }\n }\n int ans = n * m;\n for (int i = 0; i < n - 1; ++i) {\n for (int j = 0; j < m - 1; ++j) {\n ans = min(ans, topLeft[i][j] + bottomLeft[i + 1][j] + topRight[n - 1][j + 1]);\n // cout << ans << ' ' << i << ' ' << j << ' ' << topLeft[i][j] << ' ' << bottomLeft[i + 1][j] << ' ' << topRight[n - 1][j + 1] << endl;\n }\n }\n for (int i = 0; i < m - 2; ++i) {\n for (int j = i + 2; j < m; ++j) {\n int rowL = n, rowR = 0, colL = m, colR = 0;\n for (int k = 0; k < n; ++k)\n for (int l = i + 1; l < j; ++l)\n if (grid[k][l] == 1) {\n rowL = min(rowL, k);\n rowR = max(rowR, k);\n colL = min(colL, l);\n colR = max(colR, l);\n }\n ans = min(ans, topLeft[n-1][i] + topRight[n-1][j] + max(0, (rowR - rowL + 1) * (colR - colL + 1)));\n // cout << ans << ' ' << i << ' ' << j << ' ' << topLeft[n-1][i] << ' ' << topRight[n-1][j] << ' ' << max(0, (rowR - rowL + 1) * (colR - colL + 1)) << endl;\n }\n }\n return ans;\n };\n \n auto sol1 = [&sol](vector<vector<int>> &grid) {\n int ans = sol(grid);\n // cout << ans << endl;\n reverse(grid.begin(), grid.end());\n ans = min(ans, sol(grid));\n // cout << ans << endl;\n for (auto &v: grid) reverse(v.begin(), v.end());\n ans = min(ans, sol(grid));\n // cout << ans << endl;\n reverse(grid.begin(), grid.end());\n ans = min(ans, sol(grid));\n // cout << ans << endl;\n return ans;\n };\n \n int ans = sol1(grid);\n int n = grid.size(), m = grid[0].size();\n vector<vector<int>> diaGrid(m, vector<int>(n));\n for (int i = 0; i < n; ++i)\n for (int j = 0; j < m; ++j)\n diaGrid[j][i] = grid[i][j];\n ans = min(ans, sol1(diaGrid));\n return ans;\n }\n};",
"memory": "41200"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int cost(vector<int> coords, vector<vector<int>> &grid) {\n int stx = 35, sty = 35, enx = -1, eny = -1;\n for (int x = coords[0]; x <= coords[2]; x++) {\n for (int y = coords[1]; y <= coords[3]; y++) {\n if (grid[x][y] == 1) {\n stx = min(stx, x);\n enx = max(enx, x);\n sty = min(sty, y);\n eny = max(eny, y);\n }\n }\n }\n if (enx == -1) {\n return 1e3;\n }\n return abs(enx - stx + 1) * abs(eny - sty + 1);\n }\n int cutter(bool proceed, vector<int> coords, vector<vector<int>> &grid) {\n int stx = coords[0], sty = coords[1];\n int enx = coords[2], eny = coords[3];\n int res = 1e3;\n for (int x = stx; x < enx; x++) {\n if (proceed) {\n res = min(res, cost({stx, sty, x, eny}, grid) + cutter(false, {x + 1, sty, enx, eny}, grid));\n res = min(res, cutter(false, {stx, sty, x, eny}, grid) + cost({x + 1, sty, enx, eny}, grid));\n } else {\n res = min(res, cost({stx, sty, x, eny}, grid) + cost({x + 1, sty, enx, eny}, grid));\n } \n }\n for (int y = sty; y < eny; y++) {\n if (proceed) {\n res = min(res, cost({stx, sty, enx, y}, grid) + cutter(false, {stx, y + 1, enx, eny}, grid));\n res = min(res, cutter(false, {stx, sty, enx, y}, grid) + cost({stx, y + 1, enx, eny}, grid));\n } else {\n res = min(res, cost({stx, sty, enx, y}, grid) + cost({stx, y + 1, enx, eny}, grid));\n } \n }\n return res;\n }\n int minimumSum(vector<vector<int>>& grid) {\n int n = grid.size() - 1, m = grid[0].size() - 1;\n return cutter(true, {0, 0, n, m}, grid);\n }\n};",
"memory": "41500"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int cost(vector<int> coords, vector<vector<int>> &grid) {\n int stx = 35, sty = 35, enx = -1, eny = -1;\n for (int x = coords[0]; x <= coords[2]; x++) {\n for (int y = coords[1]; y <= coords[3]; y++) {\n if (grid[x][y] == 1) {\n stx = min(stx, x);\n enx = max(enx, x);\n sty = min(sty, y);\n eny = max(eny, y);\n }\n }\n }\n if (enx == -1) {\n return 1e5;\n }\n return abs(enx - stx + 1) * abs(eny - sty + 1);\n }\n int cutter(int need, vector<int> coords, vector<vector<int>> &grid) {\n int stx = coords[0], sty = coords[1];\n int enx = coords[2], eny = coords[3];\n int res = 1e5;\n for (int x = stx; x < enx; x++) {\n if (need == 3) {\n res = min(res, cost({stx, sty, x, eny}, grid) + cutter(need - 1, {x + 1, sty, enx, eny}, grid));\n res = min(res, cutter(need - 1, {stx, sty, x, eny}, grid) + cost({x + 1, sty, enx, eny}, grid));\n } else {\n res = min(res, cost({stx, sty, x, eny}, grid) + cost({x + 1, sty, enx, eny}, grid));\n } \n }\n for (int y = sty; y < eny; y++) {\n if (need == 3) {\n res = min(res, cost({stx, sty, enx, y}, grid) + cutter(need - 1, {stx, y + 1, enx, eny}, grid));\n res = min(res, cutter(need - 1, {stx, sty, enx, y}, grid) + cost({stx, y + 1, enx, eny}, grid));\n } else {\n res = min(res, cost({stx, sty, enx, y}, grid) + cost({stx, y + 1, enx, eny}, grid));\n } \n }\n return res;\n }\n int minimumSum(vector<vector<int>>& grid) {\n int n = grid.size() - 1, m = grid[0].size() - 1;\n return cutter(3, {0, 0, n, m}, grid);\n }\n};",
"memory": "41700"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int cost(vector<int> coords, vector<vector<int>> &grid) {\n int stx = 35, sty = 35, enx = -1, eny = -1;\n for (int x = coords[0]; x <= coords[2]; x++) {\n for (int y = coords[1]; y <= coords[3]; y++) {\n if (grid[x][y] == 1) {\n stx = min(stx, x);\n enx = max(enx, x);\n sty = min(sty, y);\n eny = max(eny, y);\n }\n }\n }\n if (enx == -1) {\n return 1e5;\n }\n return abs(enx - stx + 1) * abs(eny - sty + 1);\n }\n int cutter(bool proceed, vector<int> coords, vector<vector<int>> &grid) {\n int stx = coords[0], sty = coords[1];\n int enx = coords[2], eny = coords[3];\n int res = 1e5;\n for (int x = stx; x < enx; x++) {\n if (proceed) {\n res = min(res, cost({stx, sty, x, eny}, grid) + cutter(false, {x + 1, sty, enx, eny}, grid));\n res = min(res, cutter(false, {stx, sty, x, eny}, grid) + cost({x + 1, sty, enx, eny}, grid));\n } else {\n res = min(res, cost({stx, sty, x, eny}, grid) + cost({x + 1, sty, enx, eny}, grid));\n } \n }\n for (int y = sty; y < eny; y++) {\n if (proceed) {\n res = min(res, cost({stx, sty, enx, y}, grid) + cutter(false, {stx, y + 1, enx, eny}, grid));\n res = min(res, cutter(false, {stx, sty, enx, y}, grid) + cost({stx, y + 1, enx, eny}, grid));\n } else {\n res = min(res, cost({stx, sty, enx, y}, grid) + cost({stx, y + 1, enx, eny}, grid));\n } \n }\n return res;\n }\n int minimumSum(vector<vector<int>>& grid) {\n int n = grid.size() - 1, m = grid[0].size() - 1;\n return cutter(true, {0, 0, n, m}, grid);\n }\n};",
"memory": "41700"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int cost(vector<int> coords, vector<vector<int>> &grid) {\n int stx = 35, sty = 35, enx = -1, eny = -1;\n for (int x = coords[0]; x <= coords[2]; x++) {\n for (int y = coords[1]; y <= coords[3]; y++) {\n if (grid[x][y] == 1) {\n stx = min(stx, x);\n enx = max(enx, x);\n sty = min(sty, y);\n eny = max(eny, y);\n }\n }\n }\n if (enx == -1) {\n return 1e5;\n }\n return abs(enx - stx + 1) * abs(eny - sty + 1);\n }\n int cutter(int need, vector<int> coords, vector<vector<int>> &grid) {\n cout << need << '\\n';\n for (auto x: coords) {\n cout << x << \" \";\n }\n cout << '\\n';\n int stx = coords[0], sty = coords[1];\n int enx = coords[2], eny = coords[3];\n int res = 1e5;\n for (int x = stx; x < enx; x++) {\n if (need == 3) {\n cout << stx << \" \" << sty << \" \" << x << \" \" << eny << '\\n';\n cout << \"cost :\" << cost({stx, sty, x, eny}, grid) << '\\n';\n res = min(res, cost({stx, sty, x, eny}, grid) + cutter(need - 1, {x + 1, sty, enx, eny}, grid));\n res = min(res, cutter(need - 1, {stx, sty, x, eny}, grid) + cost({x + 1, sty, enx, eny}, grid));\n } else {\n res = min(res, cost({stx, sty, x, eny}, grid) + cost({x + 1, sty, enx, eny}, grid));\n } \n }\n for (int y = sty; y < eny; y++) {\n if (need == 3) {\n cout << stx << \" \" << sty << \" \" << enx << \" \" << y << '\\n';\n cout << \"cost :\" << cost({stx, sty, enx, y}, grid) << '\\n';\n res = min(res, cost({stx, sty, enx, y}, grid) + cutter(need - 1, {stx, y + 1, enx, eny}, grid));\n res = min(res, cutter(need - 1, {stx, sty, enx, y}, grid) + cost({stx, y + 1, enx, eny}, grid));\n } else {\n // cout << \"broke: \" << stx << \" \" << sty << \" \" << enx << \" \" << y << \" : \";\n // cout << \"broke: \" << stx << \" \" << y + 1 << \" \" << enx << \" \" << eny << '\\n';\n // cout << cost({stx, sty, enx, y}, grid) << \" \" << cost({stx, y + 1, enx, eny}, grid) << '\\n';\n res = min(res, cost({stx, sty, enx, y}, grid) + cost({stx, y + 1, enx, eny}, grid));\n } \n }\n cout << \"res \" << res << '\\n';\n return res;\n }\n int minimumSum(vector<vector<int>>& grid) {\n int n = grid.size() - 1, m = grid[0].size() - 1;\n // cout << \"cost \" << cost({1, 2, 1, 3}, grid) << '\\n';\n return cutter(3, {0, 0, n, m}, grid);\n }\n};",
"memory": "41800"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[31][31][31][31][5];\n int find(vector<vector<int>>&grid, int sr,int sc,int er, int ec){\n int a=0;\n int b=INT_MAX;\n int c=INT_MAX;\n int d=0;\n int flag = 1;\n for(int i=sr;i<=er;i++){\n for(int j=sc;j<=ec;j++){\n if(grid[i][j]==1){\n flag = 0;\n a = max(a,i);\n b = min(b,j);\n c = min(c,i);\n d = max(d,j);\n }\n }\n }\n if(flag)return 1e9;\n return (a-c+1)*(d-b+1);\n }\n int solve(vector<vector<int>> &grid,int sr, int er, int sc, int ec, int part){\n if(part==0){\n return find(grid, sr,sc,er,ec);\n }\n if(dp[sr][er][sc][ec][part]!=-1)return dp[sr][er][sc][ec][part];\n int ans = 1e9;\n for(int i=sr;i<er;i++){\n ans = min({ans,solve(grid,sr,i,sc,ec,0)+solve(grid,i+1,er,sc,ec,part-1),solve(grid,sr,i,sc,ec,part-1)+solve(grid,i+1,er,sc,ec,0)});\n }\n\n for(int j=sc;j<ec;j++){\n ans = min({ans,solve(grid,sr,er,sc,j,0)+solve(grid,sr,er,j+1,ec,part-1),solve(grid,sr,er,sc,j,part-1)+solve(grid,sr,er,j+1,ec,0)});\n }\n return dp[sr][er][sc][ec][part] = ans;\n }\n int minimumSum(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n memset(dp,-1,sizeof(dp));\n return solve(grid,0,n-1,0,m-1,2);\n }\n};",
"memory": "44700"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[31][31][31][31][5];\n int find(vector<vector<int>>&grid, int sr,int sc,int er, int ec){\n int a=0;\n int b=INT_MAX;\n int c=INT_MAX;\n int d=0;\n int flag = 1;\n for(int i=sr;i<=er;i++){\n for(int j=sc;j<=ec;j++){\n if(grid[i][j]==1){\n flag = 0;\n a = max(a,i);\n b = min(b,j);\n c = min(c,i);\n d = max(d,j);\n }\n }\n }\n if(flag)return 1e9;\n return (a-c+1)*(d-b+1);\n }\n int solve(vector<vector<int>> &grid,int sr, int er, int sc, int ec, int part){\n if(part==0){\n return find(grid, sr,sc,er,ec);\n }\n if(dp[sr][er][sc][ec][part]!=-1)return dp[sr][er][sc][ec][part];\n int ans = 1e9;\n for(int i=sr;i<er;i++){\n ans = min({ans,solve(grid,sr,i,sc,ec,0)+solve(grid,i+1,er,sc,ec,part-1),solve(grid,sr,i,sc,ec,part-1)+solve(grid,i+1,er,sc,ec,0)});\n }\n\n for(int j=sc;j<ec;j++){\n ans = min({ans,solve(grid,sr,er,sc,j,0)+solve(grid,sr,er,j+1,ec,part-1),solve(grid,sr,er,sc,j,part-1)+solve(grid,sr,er,j+1,ec,0)});\n }\n return dp[sr][er][sc][ec][part] = ans;\n }\n int minimumSum(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n memset(dp,-1,sizeof(dp));\n return solve(grid,0,n-1,0,m-1,2);\n }\n};",
"memory": "44900"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int get_one_rectangle_with_ones(vector<vector<int>> &grid,int r,int c,int m,int n){\n int sr=INT_MAX,sc=INT_MAX;\n int er=INT_MIN,ec=INT_MIN;\n\n for(int i=r;i<=m;i++){// why here i<=m\n for(int j=c;j<=n;j++){\n if(grid[i][j]==1){\n sr=min(sr,i);\n sc=min(sc,j);\n er=max(er,i);\n ec=max(ec,j);\n }\n }\n }\n if(sr==INT_MAX) return 0;\n return (er-sr+1) * (ec-sc+1);\n }\n int get_rectangle(vector<vector<int>> &grid,int r,int c,int m,int n,int k,unordered_map<string,int>& dp){\n //commas are very nessarrory, without commas : \n // For state (1, 11, 1, 11, 2), the string key would be \"11111112\".\n // For state (11, 1, 1, 1, 2), the string key would also be \"11111112\".\n string str=to_string(r) + \",\" + to_string(c) + \",\" + to_string(m) + \",\" + to_string(n) + \",\" + to_string(k);\n if(dp.count(str)) return dp[str];\n\n int ans=INT_MAX;\n if(k==1){\n ans=get_one_rectangle_with_ones(grid,r,c,m,n);\n }\n else if(k==2){\n //choose 2 diff sunmatrix\n for(int i=r;i<m;i++){//why here i<=m is not \n ans = min(ans, get_rectangle(grid,r,c,i,n,1,dp) + get_rectangle(grid,i+1,c,m,n,1,dp) ) ;\n }\n for(int j=c;j<n;j++){\n ans = min(ans, get_rectangle(grid,r,c,m,j,1,dp) + get_rectangle(grid,r,j+1,m,n,1,dp) ) ;\n }\n }\n else if(k==3){\n for(int i=r;i<m;i++){\n ans=min(ans, get_rectangle(grid,r,c,i,n,1,dp) + get_rectangle(grid,i+1,c,m,n,2,dp) );\n ans=min(ans, get_rectangle(grid,r,c,i,n,2,dp) + get_rectangle(grid,i+1,c,m,n,1,dp) );\n }\n for(int j=c;j<n;j++){\n ans=min(ans, get_rectangle(grid,r,c,m,j,1,dp) + get_rectangle(grid,r,j+1,m,n,2,dp) );\n ans=min(ans, get_rectangle(grid,r,c,m,j,2,dp) + get_rectangle(grid,r,j+1,m,n,1,dp) );\n }\n }\n\n dp[str]=ans;\n return ans;\n }\n int minimumSum(vector<vector<int>>& grid) {\n int m=grid.size();\n int n=grid[0].size();\n unordered_map<string,int> dp;\n return get_rectangle(grid,0,0,m-1,n-1,3,dp);// here m-1,n-1\n }\n};",
"memory": "50400"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n\n unordered_map<string, int> memo;\n\n function<int(int, int, int, int, int)> getOne = [&](int i1, int j1, int i2, int j2, int k) {\n int minx = INT_MAX;\n int maxx = INT_MIN;\n int miny = INT_MAX;\n int maxy = INT_MIN;\n\n for (int i = i1; i <= i2; ++i) {\n for (int j = j1; j <= j2; ++j) {\n if (grid[i][j] == 1) {\n minx = min(minx, i);\n maxx = max(maxx, i);\n miny = min(miny, j);\n maxy = max(maxy, j);\n }\n }\n }\n\n if (minx == INT_MAX) {\n return 0;\n }\n\n return (maxx - minx + 1) * (maxy - miny + 1);\n };\n\n function<int(int, int, int, int, int)> getNext = [&](int i1, int j1, int i2, int j2, int k) {\n string key = to_string(i1) + \",\" + to_string(j1) + \",\" + to_string(i2) + \",\" + to_string(j2) + \",\" + to_string(k);\n if (memo.find(key) != memo.end()) {\n return memo[key];\n }\n\n int output = INT_MAX;\n\n if (k == 1) {\n output = getOne(i1, j1, i2, j2, k);\n } else if (k == 2) {\n for (int i = i1; i < i2; ++i) {\n output = min(output, getNext(i1, j1, i, j2, 1) + getNext(i + 1, j1, i2, j2, 1));\n }\n for (int j = j1; j < j2; ++j) {\n output = min(output, getNext(i1, j1, i2, j, 1) + getNext(i1, j + 1, i2, j2, 1));\n }\n } else if (k == 3) {\n for (int i = i1; i < i2; ++i) {\n output = min(output, getNext(i1, j1, i, j2, 1) + getNext(i + 1, j1, i2, j2, 2));\n output = min(output, getNext(i1, j1, i, j2, 2) + getNext(i + 1, j1, i2, j2, 1));\n }\n for (int j = j1; j < j2; ++j) {\n output = min(output, getNext(i1, j1, i2, j, 1) + getNext(i1, j + 1, i2, j2, 2));\n output = min(output, getNext(i1, j1, i2, j, 2) + getNext(i1, j + 1, i2, j2, 1));\n }\n }\n\n memo[key] = output;\n return output;\n };\n\n int ans = getNext(0, 0, m - 1, n - 1, 3);\n return ans;\n }\n};",
"memory": "50600"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n\n unordered_map<string, int> memo;\n\n function<int(int, int, int, int, int)> getOne = [&](int i1, int j1, int i2, int j2, int k) {\n int minx = INT_MAX;\n int maxx = INT_MIN;\n int miny = INT_MAX;\n int maxy = INT_MIN;\n\n for (int i = i1; i <= i2; ++i) {\n for (int j = j1; j <= j2; ++j) {\n if (grid[i][j] == 1) {\n minx = min(minx, i);\n maxx = max(maxx, i);\n miny = min(miny, j);\n maxy = max(maxy, j);\n }\n }\n }\n\n if (minx == INT_MAX) {\n return 0;\n }\n\n return (maxx - minx + 1) * (maxy - miny + 1);\n };\n\n function<int(int, int, int, int, int)> getNext = [&](int i1, int j1, int i2, int j2, int k) {\n string key = to_string(i1) + \",\" + to_string(j1) + \",\" + to_string(i2) + \",\" + to_string(j2) + \",\" + to_string(k);\n if (memo.find(key) != memo.end()) {\n return memo[key];\n }\n\n int output = INT_MAX;\n\n if (k == 1) {\n output = getOne(i1, j1, i2, j2, k);\n } else if (k == 2) {\n for (int i = i1; i < i2; ++i) {\n output = min(output, getNext(i1, j1, i, j2, 1) + getNext(i + 1, j1, i2, j2, 1));\n }\n for (int j = j1; j < j2; ++j) {\n output = min(output, getNext(i1, j1, i2, j, 1) + getNext(i1, j + 1, i2, j2, 1));\n }\n } else if (k == 3) {\n for (int i = i1; i < i2; ++i) {\n output = min(output, getNext(i1, j1, i, j2, 1) + getNext(i + 1, j1, i2, j2, 2));\n output = min(output, getNext(i1, j1, i, j2, 2) + getNext(i + 1, j1, i2, j2, 1));\n }\n for (int j = j1; j < j2; ++j) {\n output = min(output, getNext(i1, j1, i2, j, 1) + getNext(i1, j + 1, i2, j2, 2));\n output = min(output, getNext(i1, j1, i2, j, 2) + getNext(i1, j + 1, i2, j2, 1));\n }\n }\n\n memo[key] = output;\n return output;\n };\n\n int ans = getNext(0, 0, m - 1, n - 1, 3);\n return ans;\n }\n};",
"memory": "50700"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[35][35][35][35][4];\n int n,m;\n int onesum(int x1,int y1,int x2,int y2,vector<vector<int>> &grid){\n int min1 = 1e9,min2 = 1e9,max1 = 0,max2 = 0;\n for(int i = x1; i <= x2; i++){\n for(int j = y1; j <= y2; j++){\n if(grid[i][j]){\n min1 = min(min1,i);min2 = min(min2,j);\n max1 = max(max1,i);max2 = max(max2,j);\n }\n }\n }\n if(min1 == 1e9){return 0;}\n int ans = (max1-min1+1)*(max2-min2+1);\n return ans;\n } \n int rec(int x1,int y1,int x2,int y2,int k,vector<vector<int>> &grid){\n if(k == 0){\n return 0;\n }\n if(k == 1){\n int minn = 1e9;\n for(int i = x1; i < x2; i++){\n minn = min(minn,onesum(x1,y1,i,y2,grid)+onesum(i+1,y1,x2,y2,grid));\n }\n for(int j = y1; j < y2; j++){\n minn = min(minn,onesum(x1,y1,x2,j,grid)+onesum(x1,j+1,x2,y2,grid));\n }\n return dp[x1][y1][x2][y2][k] = minn;\n }\n else{\n int minn = 1e9;\n for(int i = x1; i < x2; i++){\n minn = min(minn,rec(x1,y1,i,y2,1,grid)+onesum(i+1,y1,x2,y2,grid));\n minn = min(minn,onesum(x1,y1,i,y2,grid)+rec(i+1,y1,x2,y2,1,grid));\n }\n for(int j = y1; j < y2; j++){\n minn = min(minn,rec(x1,y1,x2,j,1,grid)+onesum(x1,j+1,x2,y2,grid));\n minn = min(minn,onesum(x1,y1,x2,j,grid)+rec(x1,j+1,x2,y2,1,grid));\n }\n return dp[x1][y1][x2][y2][k] = minn;\n }\n }\n int minimumSum(vector<vector<int>>& grid) {\n n = grid.size();\n m = grid[0].size();\n for(int i = 0; i <= n; i++){\n for(int j = 0; j <= m; j++){\n for(int k = 0; k <= n; k++){\n for(int l = 0; l <= m; l++){\n for(int p = 0; p <= 3; p++){\n dp[i][j][k][l][p] = -1;\n }\n }\n }\n }\n }\n return rec(0,0,n-1,m-1,2,grid);\n }\n};",
"memory": "51100"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n unordered_map<string, int> memo;\n vector<vector<int>> grid;\n\n int getOne(int i1, int j1, int i2, int j2) {\n int minx = INT_MAX;\n int maxx = INT_MIN;\n int miny = INT_MAX;\n int maxy = INT_MIN;\n\n for (int i = i1; i <= i2; ++i) {\n for (int j = j1; j <= j2; ++j) {\n if (grid[i][j] == 1) {\n minx = min(minx, i);\n maxx = max(maxx, i);\n miny = min(miny, j);\n maxy = max(maxy, j);\n }\n }\n }\n\n if (minx == INT_MAX) {\n return 0;\n }\n\n return (maxx - minx + 1) * (maxy - miny + 1);\n }\n\n int getNext(int i1, int j1, int i2, int j2, int k) {\n string key = to_string(i1) + \",\" + to_string(j1) + \",\" + to_string(i2) + \",\" + to_string(j2) + \",\" + to_string(k);\n if (memo.find(key) != memo.end()) {\n return memo[key];\n }\n\n int output = INT_MAX;\n\n if (k == 1) {\n output = getOne(i1, j1, i2, j2);\n } else if (k == 2) {\n for (int i = i1; i < i2; ++i) {\n output = min(output, getNext(i1, j1, i, j2, 1) + getNext(i + 1, j1, i2, j2, 1));\n }\n for (int j = j1; j < j2; ++j) {\n output = min(output, getNext(i1, j1, i2, j, 1) + getNext(i1, j + 1, i2, j2, 1));\n }\n } else if (k == 3) {\n for (int i = i1; i < i2; ++i) {\n output = min(output, getNext(i1, j1, i, j2, 1) + getNext(i + 1, j1, i2, j2, 2));\n output = min(output, getNext(i1, j1, i, j2, 2) + getNext(i + 1, j1, i2, j2, 1));\n }\n for (int j = j1; j < j2; ++j) {\n output = min(output, getNext(i1, j1, i2, j, 1) + getNext(i1, j + 1, i2, j2, 2));\n output = min(output, getNext(i1, j1, i2, j, 2) + getNext(i1, j + 1, i2, j2, 1));\n }\n }\n\n memo[key] = output;\n return output;\n }\n\n int minimumSum(vector<vector<int>>& grid) {\n this->grid = grid;\n int m = grid.size();\n int n = grid[0].size();\n int ans = getNext(0, 0, m - 1, n - 1, 3);\n return ans;\n }\n};\n",
"memory": "51200"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n using rc = array<int,2>;\n using rect = array<rc, 2>; // min and max corners\n \n auto cover = [&grid](rect const& rec) {\n int lc = INT_MAX, uc = INT_MIN;\n int lr = INT_MAX, ur = INT_MIN;\n auto const& [r, c] = rec[0];\n auto const& [R, C] = rec[1];\n for(int i = r; i < R; ++i) {\n for(int j = c; j < C; ++j) {\n if(grid[i][j]) {\n lc = min(lc, j); lr = min(lr, i);\n uc = max(uc, j); ur = max(ur, i);\n }\n }\n }\n return lc != INT_MAX ? (uc - lc + 1) * (ur - lr + 1) : 0; \n };\n\n function<void(vector<rect>const&, const int, const int, const int, const int, const int)> splitlast;\n int area = INT_MAX;\n splitlast = [&area, &cover, &splitlast](vector<rect>const& rects, const int r, const int R, const int c, const int C, const int i) {\n if(rects.size() == 3) {\n area = min(area, cover(rects[0]) + cover(rects[1]) + cover(rects[2]));\n return;\n }\n vector<rect> rects2(rects);\n bool next = false;\n if(i < R) { // split at row i\n rects2.back() = rect({{r,c}, {i,C}});\n rects2.emplace_back(rect({{i,c}, {R,C}}));\n splitlast(rects2, i, R, c, C, i+1); \n if(rects2.size() == 2) {\n rects2.front().swap(rects2.back());\n splitlast(rects2, r, i, c, C, r+1);\n }\n next = true;\n }\n else if(int j = i - R + 1; j < C) { // split at column j\n rects2.back() = rect({{r,c}, {R,j}});\n rects2.emplace_back(rect({{r,j}, {R,C}}));\n splitlast(rects2, r, R, j, C, r+1);\n if(rects2.size() == 2) {\n rects2.front().swap(rects2.back());\n splitlast(rects2, r, R, c, j, r+1);\n }\n next = true;\n }\n if(next)\n splitlast(rects, r, R, c, C, i+1);\n };\n\n int rows = grid.size(), cols = grid[0].size();\n splitlast(vector<rect>(1, rect({{0, 0}, {rows, cols}}) ), 0, rows, 0, cols, 1);\n return area;\n }\n};",
"memory": "62600"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n \n function<vector<int>(vector<vector<int>>&)> fn = [&](vector<vector<int>>& grid) {\n vector<int> ans; \n int imin = 30, jmin = 30, imax = 0, jmax = 0; \n for (int i = 0; i < grid.size()-1; ++i) {\n for (int j = 0; j < grid[i].size(); ++j) \n if (grid[i][j]) {\n imin = min(imin, i); \n jmin = min(jmin, j); \n imax = max(imax, i); \n jmax = max(jmax, j); \n }\n int val = 0; \n if (imin <= imax && jmin <= jmax) \n val = (imax-imin+1) * (jmax-jmin+1); \n ans.push_back(val); \n } \n return ans; \n };\n \n auto rotate = [&](vector<vector<int>>& grid) {\n int m = grid.size(), n = grid[0].size();\n vector<vector<int>> ans(n, vector<int>(m)); \n reverse(grid.begin(), grid.end()); \n for (int i = 0; i < m; ++i)\n for (int j = 0; j < n; ++j)\n ans[j][i] = grid[i][j]; \n return ans; \n };\n \n int ans = INT_MAX; \n for (int k = 4; k--; ) {\n vector<int> half = fn(grid); \n for (int i = 0; i < half.size(); ++i) {\n if (half[i]) {\n vector<vector<int>> sub(grid.begin()+i+1, grid.end()); \n for (int sz = 2; sz--; ) {\n vector<int> top = fn(sub); \n reverse(sub.begin(), sub.end()); \n vector<int> bottom = fn(sub); \n for (int j = 0, n = top.size(); j < n; ++j) \n if (top[j] && bottom[n-1-j]) \n ans = min(ans, half[i] + top[j] + bottom[n-1-j]); \n sub = rotate(sub); \n }\n }\n }\n grid = rotate(grid); \n }\n return ans; \n }\n};",
"memory": "68500"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n \n function<vector<int>(vector<vector<int>>&)> fn = [&](vector<vector<int>>& grid) {\n vector<int> ans; \n int imin = 50, jmin = 50, imax = -1, jmax = -1; \n for (int i = 0; i < grid.size()-1; ++i) {\n for (int j = 0; j < grid[i].size(); ++j) \n if (grid[i][j]) {\n imin = min(imin, i); \n jmin = min(jmin, j); \n imax = max(imax, i); \n jmax = max(jmax, j); \n }\n int val = 0; \n if (imin <= imax && jmin <= jmax) \n val = (imax-imin+1) * (jmax-jmin+1); \n ans.push_back(val); \n } \n return ans; \n };\n \n auto rotate = [&](vector<vector<int>>& grid) {\n int m = grid.size(), n = grid[0].size();\n vector<vector<int>> ans(n, vector<int>(m)); \n reverse(grid.begin(), grid.end()); \n for (int i = 0; i < m; ++i)\n for (int j = 0; j < n; ++j)\n ans[j][i] = grid[i][j]; \n return ans; \n };\n \n int ans = INT_MAX; \n for (int k = 4; k--; ) {\n vector<int> half = fn(grid); \n for (int i = 0; i < half.size(); ++i) {\n if (half[i]) {\n vector<vector<int>> sub(grid.begin()+i+1, grid.end()); \n for (int sz = 2; sz--; ) {\n vector<int> top = fn(sub); \n reverse(sub.begin(), sub.end()); \n vector<int> bottom = fn(sub); \n for (int j = 0, n = top.size(); j < n; ++j) \n if (top[j] && bottom[n-1-j]) \n ans = min(ans, half[i] + top[j] + bottom[n-1-j]); \n sub = rotate(sub); \n }\n }\n }\n grid = rotate(grid); \n }\n return ans; \n }\n};",
"memory": "68600"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n \n function<vector<int>(vector<vector<int>>&)> fn = [&](vector<vector<int>>& grid) {\n vector<int> ans; \n int imin = 30, jmin = 30, imax = 0, jmax = 0; \n for (int i = 0; i < grid.size()-1; ++i) {\n for (int j = 0; j < grid[i].size(); ++j) \n if (grid[i][j]) {\n imin = min(imin, i); \n jmin = min(jmin, j); \n imax = max(imax, i); \n jmax = max(jmax, j); \n }\n int val = 0; \n if (imin <= imax && jmin <= jmax) \n val = (imax-imin+1) * (jmax-jmin+1); \n ans.push_back(val); \n } \n return ans; \n };\n \n auto rotate = [&](vector<vector<int>>& grid) {\n int m = grid.size(), n = grid[0].size();\n vector<vector<int>> ans(n, vector<int>(m)); \n reverse(grid.begin(), grid.end()); \n for (int i = 0; i < m; ++i)\n for (int j = 0; j < n; ++j)\n ans[j][i] = grid[i][j]; \n return ans; \n };\n \n int ans = INT_MAX; \n for (int k = 4; k--; ) {\n vector<int> half = fn(grid); \n for (int i = 0; i < half.size(); ++i) {\n if (half[i]) {\n vector<vector<int>> sub(grid.begin()+i+1, grid.end()); \n for (int sz = 2; sz--; ) {\n vector<int> top = fn(sub); \n reverse(sub.begin(), sub.end()); \n vector<int> bottom = fn(sub); \n for (int j = 0, n = top.size(); j < n; ++j) \n if (top[j] && bottom[n-1-j]) \n ans = min(ans, half[i] + top[j] + bottom[n-1-j]); \n sub = rotate(sub); \n }\n }\n }\n grid = rotate(grid); \n }\n return ans; \n }\n};",
"memory": "68600"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n int area(vector<pair<int, int> >& grid) {\n int upmin = INT_MAX, downmax = 0;\n int leftmin = INT_MAX, rightmax = 0;\n\n for(auto &[i, j]: grid){\n upmin = min(upmin, i);\n downmax = max(downmax, i);\n leftmin = min(leftmin, j);\n rightmax = max(rightmax, j);\n }\n\n return (downmax-upmin+1)*(rightmax-leftmin+1);\n }\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size();\n\n vector<vector<int> > v;\n for(int i = 0; i < n; i++){\n for(int j = 0; j < m; j++){\n if(grid[i][j] == 0) continue;\n v.push_back({i, j});\n }\n }\n int ans = INT_MAX;\n\n // column cut\n for(int i = 0; i < 30; i++){\n vector<pair<int, int> > v1, v2;\n for(auto it: v){\n if(it[1] <= i) v1.push_back({it[0], it[1]});\n else v2.push_back({it[0], it[1]});\n }\n if(v1.size() == 0 || v2.size() == 0) continue;\n \n\n vector<pair<int, int> > v3, v4;\n\n // column cut for inner 1\n for(int j = 0; j < 30; j++){\n for(auto it: v1){\n if(it.first <= j) v3.push_back({it.first, it.second});\n else v4.push_back({it.first, it.second});\n }\n if(v3.size() && v4.size()){\n int cnt = area(v2)+area(v3)+area(v4);\n ans = min(ans, cnt);\n }\n v3.clear();\n v4.clear();\n }\n\n //row cut for inner 1\n for(int j = 0; j < 30; j++){\n for(auto it: v1){\n if(it.second <= j) v3.push_back({it.first, it.second});\n else v4.push_back({it.first, it.second});\n }\n if(v3.size() && v4.size()){\n int cnt = area(v2)+area(v3)+area(v4);\n ans = min(ans, cnt);\n }\n v3.clear();\n v4.clear();\n }\n\n // column cut for inner 2\n for(int j = 0; j < 30; j++){\n for(auto it: v2){\n if(it.first <= j) v3.push_back({it.first, it.second});\n else v4.push_back({it.first, it.second});\n }\n if(v3.size() && v4.size()){\n int cnt = area(v1)+area(v3)+area(v4);\n ans = min(ans, cnt);\n }\n v3.clear();\n v4.clear();\n }\n\n //row cut for inner 2\n for(int j = 0; j < 30; j++){\n for(auto it: v2){\n if(it.second <= j) v3.push_back({it.first, it.second});\n else v4.push_back({it.first, it.second});\n }\n if(v3.size() && v4.size()){\n int cnt = area(v1)+area(v3)+area(v4);\n ans = min(ans, cnt);\n }\n v3.clear();\n v4.clear();\n }\n }\n\n // row cut\n for(int i = 0; i < 30; i++){\n vector<pair<int, int> > v1, v2;\n for(auto it: v){\n if(it[0] <= i) v1.push_back({it[0], it[1]});\n else v2.push_back({it[0], it[1]});\n }\n if(v1.size() == 0 || v2.size() == 0) continue;\n\n vector<pair<int, int> > v3, v4;\n\n // column cut for inner 1\n for(int j = 0; j < 30; j++){\n for(auto it: v1){\n if(it.first <= j) v3.push_back({it.first, it.second});\n else v4.push_back({it.first, it.second});\n }\n if(v3.size() && v4.size()){\n int cnt = area(v2)+area(v3)+area(v4);\n ans = min(ans, cnt);\n }\n v3.clear();\n v4.clear();\n }\n\n //row cut for inner 1\n for(int j = 0; j < 30; j++){\n for(auto it: v1){\n if(it.second <= j) v3.push_back({it.first, it.second});\n else v4.push_back({it.first, it.second});\n }\n if(v3.size() && v4.size()){\n int cnt = area(v2)+area(v3)+area(v4);\n ans = min(ans, cnt);\n }\n v3.clear();\n v4.clear();\n }\n\n // column cut for inner 2\n for(int j = 0; j < 30; j++){\n for(auto it: v2){\n if(it.first <= j) v3.push_back({it.first, it.second});\n else v4.push_back({it.first, it.second});\n }\n if(v3.size() && v4.size()){\n int cnt = area(v1)+area(v3)+area(v4);\n ans = min(ans, cnt);\n }\n v3.clear();\n v4.clear();\n }\n\n //row cut for inner 2\n for(int j = 0; j < 30; j++){\n for(auto it: v2){\n if(it.second <= j) v3.push_back({it.first, it.second});\n else v4.push_back({it.first, it.second});\n }\n if(v3.size() && v4.size()){\n int cnt = area(v1)+area(v3)+area(v4);\n ans = min(ans, cnt);\n }\n v3.clear();\n v4.clear();\n }\n }\n\n return ans;\n }\n};",
"memory": "78500"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n int minR(vector<vector<int>>& grid, int r0, int c0, int r1, int c1) {\n vector<int> rc(r1+1), cc(c1+1);\n for (int r=r0; r<=r1; ++r) {\n for (int c=c0; c<=c1; ++c) {\n rc[r]+=grid[r][c];\n cc[c]+=grid[r][c];\n }\n }\n int a=r0;\n while (a<=r1 and rc[a]==0) ++a;\n if (a>r1) return 0;\n int b=r1;\n while (b>a and rc[b]==0) --b;\n int c=c0;\n while (c<=c1 and cc[c]==0) ++c;\n if (c>c1) return 0;\n int d=c1;\n while (d>c and cc[d]==0) --d;\n return (b-a+1)*(d-c+1);\n }\n int minR2(vector<vector<int>>& grid, int r0, int c0, int r1, int c1) {\n if (r0==r1 and c0==c1) return INT_MAX;\n int res=INT_MAX;\n for (int r=r0; r<r1; ++r) {\n int s1=minR(grid,r0,c0,r,c1);\n if (s1==0) continue;\n int s2=minR(grid,r+1,c0,r1,c1);\n if (s2==0) continue;\n res=min(res,s1+s2);\n }\n for (int c=c0; c<c1; ++c) {\n int s1=minR(grid,r0,c0,r1,c);\n if (s1==0) continue;\n int s2=minR(grid,r0,c+1,r1,c1);\n if (s2==0) continue;\n res=min(res,s1+s2);\n }\n return res;\n }\npublic:\n int minimumSum(vector<vector<int>>& grid) {\n int rs=int(grid.size()), cs=int(grid[0].size());\n int res=INT_MAX;\n for (int r=0; r+1<rs; ++r) {\n int s1=minR(grid,0,0,r,cs-1);\n if (s1==0) continue;\n int s2=minR2(grid,r+1,0,rs-1,cs-1);\n if (s2==INT_MAX) continue;\n res=min(res,s1+s2);\n }\n for (int r=0; r+1<rs; ++r) {\n int s1=minR(grid,r+1,0,rs-1,cs-1);\n if (s1==0) continue;\n int s2=minR2(grid,0,0,r,cs-1);\n if (s2==INT_MAX) continue;\n res=min(res,s1+s2);\n }\n for (int c=0; c+1<cs; ++c) {\n int s1=minR(grid,0,0,rs-1,c);\n if (s1==0) continue;\n int s2=minR2(grid,0,c+1,rs-1,cs-1);\n if (s2==INT_MAX) continue;\n res=min(res,s1+s2);\n }\n for (int c=0; c+1<cs; ++c) {\n int s1=minR(grid,0,c+1,rs-1,cs-1);\n if (s1==0) continue;\n int s2=minR2(grid,0,0,rs-1,c);\n if (s2==INT_MAX) continue;\n res=min(res,s1+s2);\n } \n return res;\n }\n};",
"memory": "83200"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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'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\nclass Solution {\n public:\n int minimumSum(vector<vector<int>> &grid) {\n\n auto table = prepare_table(grid);\n std::function<int(size_t, size_t, size_t, size_t, int)> getOne =\n [&](size_t r1, size_t c1, size_t r2, size_t c2, int k) {\n if (r1 >= r2 || c1 >= c2) {\n return 0;\n }\n if (k == 1) {\n // std::cout<<\"for \"<<r1<<\" \"<<c1<<\" \"<<r2<<\" \"<<c2<<\" \"<<k<<\" min is \"<<table[r1][c1][r2][c2]<<std::endl;\n return table[r1][c1][r2][c2];\n }\n int min = table[r1][c1][r2][c2];\n for (auto one = 1; one < k; ++one) {\n auto two = k - one;\n if (one > two) {\n break;\n }\n for (auto r = r1 + 1; r < r2; ++r) {\n auto left = getOne(r1, c1, r, c2, one);\n auto right = getOne(r, c1, r2, c2, two);\n min = std::min(min, left + right);\n }\n for (auto c = c1 + 1; c < c2; ++c) {\n\n auto left = getOne(r1, c1, r2, c, one);\n auto right = getOne(r1, c, r2, c2, two);\n min = std::min(min, left + right);\n }\n if (one != two) {\n\n for (auto r = r1 + 1; r < r2; ++r) {\n auto left = getOne(r1, c1, r, c2, two);\n auto right = getOne(r, c1, r2, c2, one);\n min = std::min(min, left + right);\n }\n for (auto c = c1 + 1; c < c2; ++c) {\n\n auto left = getOne(r1, c1, r2, c, two);\n auto right = getOne(r1, c, r2, c2, one);\n min = std::min(min, left + right);\n }\n }\n }\n // std::cout<<\"for \"<<r1<<\" \"<<c1<<\" \"<<r2<<\" \"<<c2<<\" \"<<k<<\" min is \"<<min<<std::endl;\n return min;\n };\n return getOne(0, 0, grid.size(), grid[0].size(), 3);\n }\n\n vector<vector<vector<vector<int>>>>\n prepare_table(const vector<vector<int>> &grid) {\n size_t row = grid.size();\n size_t col = grid[0].size();\n vector<int> tmp(col + 1, std::numeric_limits<int>::max());\n vector<vector<int>> tmp2(row + 1, tmp);\n vector<vector<vector<int>>> tmp3(col, tmp2);\n vector<vector<vector<vector<int>>>> table(row, tmp3);\n for (size_t r1 = 0; r1 < row; ++r1) {\n for (size_t r2 = r1 + 1; r2 <= row; ++r2) {\n for (size_t c1 = 0; c1 < col; ++c1) {\n for (size_t c2 = c1 + 1; c2 <= col; ++c2) {\n table[r1][c1][r2][c2] = calculate(grid, r1, c1, r2, c2);\n }\n }\n }\n }\n return table;\n }\n\n int calculate(const vector<vector<int>> &grid, size_t r1, size_t c1,\n size_t r2, size_t c2) {\n size_t left = c2, right = c1;\n size_t up = r1, down = r2;\n bool find = false;\n for (size_t r = r1; r < r2; ++r) {\n for (size_t c = c1; c < c2; ++c) {\n if (grid[r][c] == 1) {\n left = std::min(left, c);\n right = std::max(right, c);\n up = std::max(up, r);\n down = std::min(down, r);\n find = true;\n }\n }\n }\n if (!find) {\n return 0;\n }\n return (up - down + 1) * (right - left + 1);\n }\n};",
"memory": "131800"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution {\n public:\n int minimumSum(vector<vector<int>> &grid) {\n\n auto table = prepare_table(grid);\n std::function<int(size_t, size_t, size_t, size_t, int)> getOne =\n [&](size_t r1, size_t c1, size_t r2, size_t c2, int k) {\n if (r1 >= r2 || c1 >= c2) {\n return 0;\n }\n if (k == 1) {\n // std::cout<<\"for \"<<r1<<\" \"<<c1<<\" \"<<r2<<\" \"<<c2<<\" \"<<k<<\" min is \"<<table[r1][c1][r2][c2]<<std::endl;\n return table[r1][c1][r2][c2];\n }\n int min = table[r1][c1][r2][c2];\n for (auto one = 1; one < k; ++one) {\n auto two = k - one;\n if (one > two) {\n break;\n }\n for (auto r = r1 + 1; r < r2; ++r) {\n auto left = getOne(r1, c1, r, c2, one);\n auto right = getOne(r, c1, r2, c2, two);\n min = std::min(min, left + right);\n }\n for (auto c = c1 + 1; c < c2; ++c) {\n\n auto left = getOne(r1, c1, r2, c, one);\n auto right = getOne(r1, c, r2, c2, two);\n min = std::min(min, left + right);\n }\n if (one != two) {\n\n for (auto r = r1 + 1; r < r2; ++r) {\n auto left = getOne(r1, c1, r, c2, two);\n auto right = getOne(r, c1, r2, c2, one);\n min = std::min(min, left + right);\n }\n for (auto c = c1 + 1; c < c2; ++c) {\n\n auto left = getOne(r1, c1, r2, c, two);\n auto right = getOne(r1, c, r2, c2, one);\n min = std::min(min, left + right);\n }\n }\n }\n // std::cout<<\"for \"<<r1<<\" \"<<c1<<\" \"<<r2<<\" \"<<c2<<\" \"<<k<<\" min is \"<<min<<std::endl;\n return min;\n };\n return getOne(0, 0, grid.size(), grid[0].size(), 3);\n }\n\n vector<vector<vector<vector<int>>>>\n prepare_table(const vector<vector<int>> &grid) {\n size_t row = grid.size();\n size_t col = grid[0].size();\n vector<int> tmp(col + 1, std::numeric_limits<int>::max());\n vector<vector<int>> tmp2(row + 1, tmp);\n vector<vector<vector<int>>> tmp3(col, tmp2);\n vector<vector<vector<vector<int>>>> table(row, tmp3);\n for (size_t r1 = 0; r1 < row; ++r1) {\n for (size_t r2 = r1 + 1; r2 <= row; ++r2) {\n for (size_t c1 = 0; c1 < col; ++c1) {\n for (size_t c2 = c1 + 1; c2 <= col; ++c2) {\n table[r1][c1][r2][c2] = calculate(grid, r1, c1, r2, c2);\n }\n }\n }\n }\n return table;\n }\n\n int calculate(const vector<vector<int>> &grid, size_t r1, size_t c1,\n size_t r2, size_t c2) {\n size_t left = c2, right = c1;\n size_t up = r1, down = r2;\n bool find = false;\n for (size_t r = r1; r < r2; ++r) {\n for (size_t c = c1; c < c2; ++c) {\n if (grid[r][c] == 1) {\n left = std::min(left, c);\n right = std::max(right, c);\n up = std::max(up, r);\n down = std::min(down, r);\n find = true;\n }\n }\n }\n if (!find) {\n return 0;\n }\n return (up - down + 1) * (right - left + 1);\n }\n};\n",
"memory": "132000"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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'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\nclass Solution {\n public:\n int minimumSum(vector<vector<int>> &grid) {\n\n auto table = prepare_table(grid);\n std::function<int(size_t, size_t, size_t, size_t, int)> getOne =\n [&](size_t r1, size_t c1, size_t r2, size_t c2, int k) {\n if (r1 >= r2 || c1 >= c2) {\n return 0;\n }\n if (k == 1) {\n // std::cout<<\"for \"<<r1<<\" \"<<c1<<\" \"<<r2<<\" \"<<c2<<\" \"<<k<<\" min is \"<<table[r1][c1][r2][c2]<<std::endl;\n return table[r1][c1][r2][c2];\n }\n int min = table[r1][c1][r2][c2];\n for (auto one = 1; one < k; ++one) {\n auto two = k - one;\n if (one > two) {\n break;\n }\n for (auto r = r1 + 1; r < r2; ++r) {\n auto left = getOne(r1, c1, r, c2, one);\n auto right = getOne(r, c1, r2, c2, two);\n min = std::min(min, left + right);\n }\n for (auto c = c1 + 1; c < c2; ++c) {\n\n auto left = getOne(r1, c1, r2, c, one);\n auto right = getOne(r1, c, r2, c2, two);\n min = std::min(min, left + right);\n }\n if (one != two) {\n\n for (auto r = r1 + 1; r < r2; ++r) {\n auto left = getOne(r1, c1, r, c2, two);\n auto right = getOne(r, c1, r2, c2, one);\n min = std::min(min, left + right);\n }\n for (auto c = c1 + 1; c < c2; ++c) {\n\n auto left = getOne(r1, c1, r2, c, two);\n auto right = getOne(r1, c, r2, c2, one);\n min = std::min(min, left + right);\n }\n }\n }\n // std::cout<<\"for \"<<r1<<\" \"<<c1<<\" \"<<r2<<\" \"<<c2<<\" \"<<k<<\" min is \"<<min<<std::endl;\n return min;\n };\n return getOne(0, 0, grid.size(), grid[0].size(), 3);\n }\n\n vector<vector<vector<vector<int>>>>\n prepare_table(const vector<vector<int>> &grid) {\n size_t row = grid.size();\n size_t col = grid[0].size();\n vector<int> tmp(col + 1, std::numeric_limits<int>::max());\n vector<vector<int>> tmp2(row + 1, tmp);\n vector<vector<vector<int>>> tmp3(col, tmp2);\n vector<vector<vector<vector<int>>>> table(row, tmp3);\n for (size_t r1 = 0; r1 < row; ++r1) {\n for (size_t r2 = r1 + 1; r2 <= row; ++r2) {\n for (size_t c1 = 0; c1 < col; ++c1) {\n for (size_t c2 = c1 + 1; c2 <= col; ++c2) {\n table[r1][c1][r2][c2] = calculate(grid, r1, c1, r2, c2);\n }\n }\n }\n }\n return table;\n }\n\n int calculate(const vector<vector<int>> &grid, size_t r1, size_t c1,\n size_t r2, size_t c2) {\n size_t left = c2, right = c1;\n size_t up = r1, down = r2;\n bool find = false;\n for (size_t r = r1; r < r2; ++r) {\n for (size_t c = c1; c < c2; ++c) {\n if (grid[r][c] == 1) {\n left = std::min(left, c);\n right = std::max(right, c);\n up = std::max(up, r);\n down = std::min(down, r);\n find = true;\n }\n }\n }\n if (!find) {\n return 0;\n }\n return (up - down + 1) * (right - left + 1);\n }\n};\n",
"memory": "132000"
} |
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'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> </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's at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li>
<li>The 1'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'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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 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's in <code>grid</code>.</li>
</ul>
| 3 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Table {\n Table(const Table &) = default;\n Table &operator=(const Table &) = default;\n Table(Table &&) = default;\n Table &operator=(Table &&) = default;\n std::vector<int> m;\n size_t col;\n size_t row;\n Table(size_t r, size_t c) : col(c), row(r) {\n m.resize(r * (r + 1) * c * (c + 1), std::numeric_limits<int>::max());\n }\n int &get(size_t r1, size_t c1, size_t r2, size_t c2) {\n auto idx = r1 * col * (col + 1) * (row + 1) +\n c1 * (col + 1) * (row + 1) + r2 * (col + 1) + c2;\n\n return m[idx];\n }\n};\nclass Solution {\n public:\n auto emptyTable(const vector<vector<int>> &grid) {\n Table table(grid.size(), grid[0].size());\n return table;\n }\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, 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 }\n int inner(vector<vector<int>> &grid) {\n\n std::vector<Table> tables;\n tables.resize(4, emptyTable(grid));\n tables[1] = prepare_table(grid);\n std::function<int(size_t, size_t, size_t, size_t, int)> getOne =\n [&](size_t r1, size_t c1, size_t r2, size_t c2, int k) {\n if (r1 >= r2 || c1 >= c2) {\n return 0;\n }\n auto min = tables[k].get(r1, c1, r2, c2);\n if (min != std::numeric_limits<int>::max()) {\n return min;\n }\n min = tables[1].get(r1, c1, r2, c2);\n for (auto one = 1; one < k; ++one) {\n auto two = k - one;\n if (one > two) {\n break;\n }\n for (auto r = r1 + 1; r < r2; ++r) {\n auto left = getOne(r1, c1, r, c2, one);\n auto right = getOne(r, c1, r2, c2, two);\n min = std::min(min, left + right);\n }\n for (auto c = c1 + 1; c < c2; ++c) {\n\n auto left = getOne(r1, c1, r2, c, one);\n auto right = getOne(r1, c, r2, c2, two);\n if (right == std::numeric_limits<int>::max()) {\n std::cout<<r1<<\", \"<<c<<\", \"<<r2<<\", \"<<c2<<\", \"<<two<<std::endl;\n }\n min = std::min(min, left + right);\n }\n if (one != two) {\n\n for (auto r = r1 + 1; r < r2; ++r) {\n auto left = getOne(r1, c1, r, c2, two);\n auto right = getOne(r, c1, r2, c2, one);\n min = std::min(min, left + right);\n }\n for (auto c = c1 + 1; c < c2; ++c) {\n\n auto left = getOne(r1, c1, r2, c, two);\n auto right = getOne(r1, c, r2, c2, one);\n min = std::min(min, left + right);\n }\n }\n }\n tables[k].get(r1, c1, r2, c2) = min;\n // std::cout<<\"for \"<<r1<<\" \"<<c1<<\" \"<<r2<<\" \"<<c2<<\" \"<<k<<\"\n // min is \"<<min<<std::endl;\n return min;\n };\n return getOne(0, 0, grid.size(), grid[0].size(), 3);\n }\n\n Table prepare_table(const vector<vector<int>> &grid) {\n auto table = emptyTable(grid);\n size_t row = grid.size();\n size_t col = grid[0].size();\n for (size_t r1 = 0; r1 < row; ++r1) {\n for (size_t r2 = r1 + 1; r2 <= row; ++r2) {\n for (size_t c1 = 0; c1 < col; ++c1) {\n for (size_t c2 = c1 + 1; c2 <= col; ++c2) {\n table.get(r1, c1, r2, c2) =\n calculate(grid, r1, c1, r2, c2);\n }\n }\n }\n }\n return table;\n }\n\n int calculate(const vector<vector<int>> &grid, size_t r1, size_t c1,\n size_t r2, size_t c2) {\n size_t left = c2, right = c1;\n size_t up = r1, down = r2;\n bool find = false;\n for (size_t r = r1; r < r2; ++r) {\n for (size_t c = c1; c < c2; ++c) {\n if (grid[r][c] == 1) {\n left = std::min(left, c);\n right = std::max(right, c);\n up = std::max(up, r);\n down = std::min(down, r);\n find = true;\n }\n }\n }\n if (!find) {\n return 0;\n }\n return (up - down + 1) * (right - left + 1);\n }\n};\n",
"memory": "259200"
} |
3,469 | <p>You are given two integers <code>red</code> and <code>blue</code> representing the count of red and blue colored balls. You have to arrange these balls to form a triangle such that the 1<sup>st</sup> row will have 1 ball, the 2<sup>nd</sup> row will have 2 balls, the 3<sup>rd</sup> row will have 3 balls, and so on.</p>
<p>All the balls in a particular row should be the <strong>same</strong> color, and adjacent rows should have <strong>different</strong> colors.</p>
<p>Return the <strong>maximum</strong><em> height of the triangle</em> that can be achieved.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 4</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/brb.png" style="width: 300px; height: 240px; padding: 10px;" /></p>
<p>The only possible arrangement is shown above.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 1, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 10, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= red, blue <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxHeightOfTriangle(int red, int blue) {\n int max = 0;\n int current = 1;\n int currBlue = blue;\n int currRed = red;\n bool onBlue = true;\n while(currBlue >= 0 && currRed >= 0){\n if(onBlue){\n if(currBlue - current >= 0){\n currBlue -= current;\n if(current > max){\n max = current;\n }\n current++;\n onBlue = !onBlue;\n }\n else{\n break;\n }\n }\n else{\n if(currRed - current >= 0){\n currRed -= current;\n if(current > max){\n max = current;\n }\n current++;\n onBlue = !onBlue;\n }\n else{\n break;\n }\n }\n }\n std::cout << currBlue << std::endl;\n std::cout << currRed << std::endl;\n current = 1;\n currBlue = blue;\n currRed = red;\n onBlue = false;\n while(currBlue >= 0 && currRed >= 0){\n if(onBlue){\n if(currBlue - current >= 0){\n currBlue -= current;\n if(current > max){\n max = current;\n }\n current++;\n onBlue = !onBlue;\n }\n else{\n break;\n }\n }\n else{\n if(currRed - current >= 0){\n currRed -= current;\n if(current > max){\n max = current;\n }\n current++;\n onBlue = !onBlue;\n }\n else{\n break;\n }\n }\n }\n return max;\n }\n};",
"memory": "8000"
} |
3,469 | <p>You are given two integers <code>red</code> and <code>blue</code> representing the count of red and blue colored balls. You have to arrange these balls to form a triangle such that the 1<sup>st</sup> row will have 1 ball, the 2<sup>nd</sup> row will have 2 balls, the 3<sup>rd</sup> row will have 3 balls, and so on.</p>
<p>All the balls in a particular row should be the <strong>same</strong> color, and adjacent rows should have <strong>different</strong> colors.</p>
<p>Return the <strong>maximum</strong><em> height of the triangle</em> that can be achieved.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 4</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/brb.png" style="width: 300px; height: 240px; padding: 10px;" /></p>
<p>The only possible arrangement is shown above.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 1, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 10, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= red, blue <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxHeightOfTriangle(int r, int b) {\n int red = r, blue = b;\n int ans = 0, balls = 1, height = 0;\n\n while (red + blue >= balls) {\n if (balls % 2 == 0)\n if (blue >= balls) blue -= balls;\n else break;\n else\n if (red >= balls) red -= balls;\n else break;\n balls++, height++;\n }\n\n red = r, blue = b;\n ans = max(ans, height), balls = 1, height = 0;\n\n while (red + blue >= balls) {\n if (balls % 2 == 0)\n if (red >= balls) red -= balls;\n else break;\n else\n if (blue >= balls) blue -= balls;\n else break;\n balls++, height++;\n }\n\n return max(ans, height);\n }\n};\n",
"memory": "8100"
} |
3,469 | <p>You are given two integers <code>red</code> and <code>blue</code> representing the count of red and blue colored balls. You have to arrange these balls to form a triangle such that the 1<sup>st</sup> row will have 1 ball, the 2<sup>nd</sup> row will have 2 balls, the 3<sup>rd</sup> row will have 3 balls, and so on.</p>
<p>All the balls in a particular row should be the <strong>same</strong> color, and adjacent rows should have <strong>different</strong> colors.</p>
<p>Return the <strong>maximum</strong><em> height of the triangle</em> that can be achieved.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 4</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/brb.png" style="width: 300px; height: 240px; padding: 10px;" /></p>
<p>The only possible arrangement is shown above.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 1, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 10, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= red, blue <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxHeightOfTriangle(int red, int blue) {\n return max(pattern(red, blue), pattern(blue, red));\n }\n int pattern(int red, int blue){\n int balls = 1, height = 0;\n while(true){\n red -= balls;\n if(red < 0)\n break;\n balls++;\n height++;\n blue -= balls;\n if(blue < 0)\n break;\n balls++;\n height++;\n }\n return height;\n }\n};",
"memory": "8100"
} |
3,469 | <p>You are given two integers <code>red</code> and <code>blue</code> representing the count of red and blue colored balls. You have to arrange these balls to form a triangle such that the 1<sup>st</sup> row will have 1 ball, the 2<sup>nd</sup> row will have 2 balls, the 3<sup>rd</sup> row will have 3 balls, and so on.</p>
<p>All the balls in a particular row should be the <strong>same</strong> color, and adjacent rows should have <strong>different</strong> colors.</p>
<p>Return the <strong>maximum</strong><em> height of the triangle</em> that can be achieved.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 4</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/brb.png" style="width: 300px; height: 240px; padding: 10px;" /></p>
<p>The only possible arrangement is shown above.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 1, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 10, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= red, blue <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int calculate_height(int a , int b){\n int height = 0;\n bool flag = true;\n int i = 1;\n while(flag){\n if(i%2 != 0 && a-i>=0){\n //odd level 1 3 5 7 ...\n height++;\n a = a - i;\n }\n else if(i%2 == 0 && b-i>=0){\n height++;\n b=b-i;\n }\n else if(a-i<0){\n flag = false;\n }\n else if(b-i < 0){\n flag = false;\n }\n i++;\n }\n return height;\n }\n int maxHeightOfTriangle(int red, int blue) {\n int h1 = calculate_height(red,blue);\n int h2 = calculate_height(blue,red);\n\n return max(h1,h2);\n }\n};",
"memory": "8200"
} |
3,469 | <p>You are given two integers <code>red</code> and <code>blue</code> representing the count of red and blue colored balls. You have to arrange these balls to form a triangle such that the 1<sup>st</sup> row will have 1 ball, the 2<sup>nd</sup> row will have 2 balls, the 3<sup>rd</sup> row will have 3 balls, and so on.</p>
<p>All the balls in a particular row should be the <strong>same</strong> color, and adjacent rows should have <strong>different</strong> colors.</p>
<p>Return the <strong>maximum</strong><em> height of the triangle</em> that can be achieved.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 4</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/brb.png" style="width: 300px; height: 240px; padding: 10px;" /></p>
<p>The only possible arrangement is shown above.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 1, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 10, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= red, blue <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int count(int red ,int blue,bool isred){\n int i =1 ;\n int cnt = 0;\n while(red>=0 && blue>=0){\n if(isred){\n if(red >=i){\n cnt++;\n red-=i;\n i++;\n isred = false;\n }else{\n break;\n }\n }else{\n if(blue >= i){\n cnt++;\n blue-=i;\n i++;\n isred = true;\n }else{\n break;\n }\n\n }\n\n }\n return cnt;\n\n }\n int maxHeightOfTriangle(int red, int blue) {\n \n int a = count(red,blue,true);\n int b = count(red,blue,false);\n\n\n return max(a,b);\n }\n};",
"memory": "8200"
} |
3,469 | <p>You are given two integers <code>red</code> and <code>blue</code> representing the count of red and blue colored balls. You have to arrange these balls to form a triangle such that the 1<sup>st</sup> row will have 1 ball, the 2<sup>nd</sup> row will have 2 balls, the 3<sup>rd</sup> row will have 3 balls, and so on.</p>
<p>All the balls in a particular row should be the <strong>same</strong> color, and adjacent rows should have <strong>different</strong> colors.</p>
<p>Return the <strong>maximum</strong><em> height of the triangle</em> that can be achieved.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 4</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/brb.png" style="width: 300px; height: 240px; padding: 10px;" /></p>
<p>The only possible arrangement is shown above.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 1, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 10, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= red, blue <= 100</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maxHeightOfTriangle(int red, int blue) {\n return max(solve(red, blue), solve(blue, red));\n }\n int solve(int a, int b) {\n int lvl = 0, i = 1, j = 2;\n while(a >= 0 && b >= 0) {\n if(a - i >= 0) {\n a -= i;\n i += 2;\n lvl++;\n } else break;\n if(b - j >= 0) {\n b -= j;\n j += 2;\n lvl++;\n } else break;\n }\n return lvl;\n }\n};",
"memory": "8300"
} |
3,469 | <p>You are given two integers <code>red</code> and <code>blue</code> representing the count of red and blue colored balls. You have to arrange these balls to form a triangle such that the 1<sup>st</sup> row will have 1 ball, the 2<sup>nd</sup> row will have 2 balls, the 3<sup>rd</sup> row will have 3 balls, and so on.</p>
<p>All the balls in a particular row should be the <strong>same</strong> color, and adjacent rows should have <strong>different</strong> colors.</p>
<p>Return the <strong>maximum</strong><em> height of the triangle</em> that can be achieved.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 4</span></p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/brb.png" style="width: 300px; height: 240px; padding: 10px;" /></p>
<p>The only possible arrangement is shown above.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 1, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 10, blue = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/16/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= red, blue <= 100</code></li>
</ul>
| 1 | {
"code": "class Solution {\n int check(int first,int second)\n {\n int parity = 1;\n int total = 0;\n while(first >= 0 && second >= 0)\n {\n if(parity % 2 == 1)\n {\n first = first - parity;\n }\n else\n {\n second = second - parity;\n }\n if(first < 0 || second < 0)\n {\n return total;\n }\n total++;\n parity++;\n }\n return total;\n }\npublic:\n int maxHeightOfTriangle(int red, int blue) {\n int ans = max(check(red,blue), check(blue,red));\n return ans;\n }\n};",
"memory": "8300"
} |
3,426 | <p>You are given a string <code>s</code>. Simulate events at each second <code>i</code>:</p>
<ul>
<li>If <code>s[i] == 'E'</code>, a person enters the waiting room and takes one of the chairs in it.</li>
<li>If <code>s[i] == 'L'</code>, a person leaves the waiting room, freeing up a chair.</li>
</ul>
<p>Return the <strong>minimum </strong>number of chairs needed so that a chair is available for every person who enters the waiting room given that it is initially <strong>empty</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "EEEEEEE"</span></p>
<p><strong>Output:</strong> <span class="example-io">7</span></p>
<p><strong>Explanation:</strong></p>
<p>After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELELEEL"</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 2 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>0</td>
</tr>
<tr>
<td>6</td>
<td>Leave</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELEELEELLL"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 3 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>6</td>
<td>Enter</td>
<td>3</td>
<td>0</td>
</tr>
<tr>
<td>7</td>
<td>Leave</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>8</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>9</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
</tbody>
</table>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 50</code></li>
<li><code>s</code> consists only of the letters <code>'E'</code> and <code>'L'</code>.</li>
<li><code>s</code> represents a valid sequence of entries and exits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumChairs(const std::string& s) {\n int count = 0;\n int _max = 0;\n for (auto ch : s) {\n if (ch == 'E') {\n ++count;\n _max = std::max(_max, count);\n } else {\n --count;\n }\n }\n return _max;\n }\n};",
"memory": "8300"
} |
3,426 | <p>You are given a string <code>s</code>. Simulate events at each second <code>i</code>:</p>
<ul>
<li>If <code>s[i] == 'E'</code>, a person enters the waiting room and takes one of the chairs in it.</li>
<li>If <code>s[i] == 'L'</code>, a person leaves the waiting room, freeing up a chair.</li>
</ul>
<p>Return the <strong>minimum </strong>number of chairs needed so that a chair is available for every person who enters the waiting room given that it is initially <strong>empty</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "EEEEEEE"</span></p>
<p><strong>Output:</strong> <span class="example-io">7</span></p>
<p><strong>Explanation:</strong></p>
<p>After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELELEEL"</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 2 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>0</td>
</tr>
<tr>
<td>6</td>
<td>Leave</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELEELEELLL"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 3 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>6</td>
<td>Enter</td>
<td>3</td>
<td>0</td>
</tr>
<tr>
<td>7</td>
<td>Leave</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>8</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>9</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
</tbody>
</table>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 50</code></li>
<li><code>s</code> consists only of the letters <code>'E'</code> and <code>'L'</code>.</li>
<li><code>s</code> represents a valid sequence of entries and exits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumChairs(string s) {\n int n=s.size(),cnt=0,mini=INT_MIN;\n int filled=0,empty=0;\n for(int i=0; i<n; i++){\n if(s[i]=='E' && empty==0){\n cnt++;\n filled++;\n } \n else if(s[i]=='E' && empty>0){\n filled++;\n empty--;\n }\n else if(s[i]=='L' && filled>0) {\n filled--; \n empty++;\n }\n }\n return cnt;\n }\n};",
"memory": "8400"
} |
3,426 | <p>You are given a string <code>s</code>. Simulate events at each second <code>i</code>:</p>
<ul>
<li>If <code>s[i] == 'E'</code>, a person enters the waiting room and takes one of the chairs in it.</li>
<li>If <code>s[i] == 'L'</code>, a person leaves the waiting room, freeing up a chair.</li>
</ul>
<p>Return the <strong>minimum </strong>number of chairs needed so that a chair is available for every person who enters the waiting room given that it is initially <strong>empty</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "EEEEEEE"</span></p>
<p><strong>Output:</strong> <span class="example-io">7</span></p>
<p><strong>Explanation:</strong></p>
<p>After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELELEEL"</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 2 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>0</td>
</tr>
<tr>
<td>6</td>
<td>Leave</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELEELEELLL"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 3 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>6</td>
<td>Enter</td>
<td>3</td>
<td>0</td>
</tr>
<tr>
<td>7</td>
<td>Leave</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>8</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>9</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
</tbody>
</table>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 50</code></li>
<li><code>s</code> consists only of the letters <code>'E'</code> and <code>'L'</code>.</li>
<li><code>s</code> represents a valid sequence of entries and exits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumChairs(string s) {\n int cnt = 0;\n int res = 0;\n for(auto c:s)\n {\n if(c=='E')\n {\n cnt++;\n res = max(cnt,res);\n }\n else cnt--;\n }\n return res;\n }\n};",
"memory": "8500"
} |
3,426 | <p>You are given a string <code>s</code>. Simulate events at each second <code>i</code>:</p>
<ul>
<li>If <code>s[i] == 'E'</code>, a person enters the waiting room and takes one of the chairs in it.</li>
<li>If <code>s[i] == 'L'</code>, a person leaves the waiting room, freeing up a chair.</li>
</ul>
<p>Return the <strong>minimum </strong>number of chairs needed so that a chair is available for every person who enters the waiting room given that it is initially <strong>empty</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "EEEEEEE"</span></p>
<p><strong>Output:</strong> <span class="example-io">7</span></p>
<p><strong>Explanation:</strong></p>
<p>After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELELEEL"</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 2 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>0</td>
</tr>
<tr>
<td>6</td>
<td>Leave</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELEELEELLL"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 3 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>6</td>
<td>Enter</td>
<td>3</td>
<td>0</td>
</tr>
<tr>
<td>7</td>
<td>Leave</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>8</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>9</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
</tbody>
</table>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 50</code></li>
<li><code>s</code> consists only of the letters <code>'E'</code> and <code>'L'</code>.</li>
<li><code>s</code> represents a valid sequence of entries and exits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumChairs(string s) {\n int n=s.size();\n int count=0;\n int m=0;\n for(int i=0;i<n;i++){\n if(s[i]=='E'){\n count++;\n }\n if(s[i]=='L'){\n count--;\n }\n if(count>m){\n m=count;\n }\n }\n return m;\n \n }\n};",
"memory": "8500"
} |
3,426 | <p>You are given a string <code>s</code>. Simulate events at each second <code>i</code>:</p>
<ul>
<li>If <code>s[i] == 'E'</code>, a person enters the waiting room and takes one of the chairs in it.</li>
<li>If <code>s[i] == 'L'</code>, a person leaves the waiting room, freeing up a chair.</li>
</ul>
<p>Return the <strong>minimum </strong>number of chairs needed so that a chair is available for every person who enters the waiting room given that it is initially <strong>empty</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "EEEEEEE"</span></p>
<p><strong>Output:</strong> <span class="example-io">7</span></p>
<p><strong>Explanation:</strong></p>
<p>After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELELEEL"</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 2 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>0</td>
</tr>
<tr>
<td>6</td>
<td>Leave</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELEELEELLL"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 3 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>6</td>
<td>Enter</td>
<td>3</td>
<td>0</td>
</tr>
<tr>
<td>7</td>
<td>Leave</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>8</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>9</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
</tbody>
</table>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 50</code></li>
<li><code>s</code> consists only of the letters <code>'E'</code> and <code>'L'</code>.</li>
<li><code>s</code> represents a valid sequence of entries and exits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumChairs(string s) {\n int cnt=0, ans=0;\n for(auto it: s){\n if(it=='E') cnt++;\n else if(it=='L') cnt--;\n ans=max(cnt, ans);\n }\n return ans;\n }\n};",
"memory": "8600"
} |
3,426 | <p>You are given a string <code>s</code>. Simulate events at each second <code>i</code>:</p>
<ul>
<li>If <code>s[i] == 'E'</code>, a person enters the waiting room and takes one of the chairs in it.</li>
<li>If <code>s[i] == 'L'</code>, a person leaves the waiting room, freeing up a chair.</li>
</ul>
<p>Return the <strong>minimum </strong>number of chairs needed so that a chair is available for every person who enters the waiting room given that it is initially <strong>empty</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "EEEEEEE"</span></p>
<p><strong>Output:</strong> <span class="example-io">7</span></p>
<p><strong>Explanation:</strong></p>
<p>After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELELEEL"</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 2 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>0</td>
</tr>
<tr>
<td>6</td>
<td>Leave</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELEELEELLL"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 3 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>6</td>
<td>Enter</td>
<td>3</td>
<td>0</td>
</tr>
<tr>
<td>7</td>
<td>Leave</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>8</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>9</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
</tbody>
</table>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 50</code></li>
<li><code>s</code> consists only of the letters <code>'E'</code> and <code>'L'</code>.</li>
<li><code>s</code> represents a valid sequence of entries and exits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumChairs(string s) {\n int n=s.length();\n int l=0;int r=0;\n int mx=0;\n for(int i=0;i<n;i++){\n if(s[i]=='E') {l++;\n mx=max(mx,l);}\n else l--;\n }\n return mx;\n }\n};",
"memory": "8600"
} |
3,426 | <p>You are given a string <code>s</code>. Simulate events at each second <code>i</code>:</p>
<ul>
<li>If <code>s[i] == 'E'</code>, a person enters the waiting room and takes one of the chairs in it.</li>
<li>If <code>s[i] == 'L'</code>, a person leaves the waiting room, freeing up a chair.</li>
</ul>
<p>Return the <strong>minimum </strong>number of chairs needed so that a chair is available for every person who enters the waiting room given that it is initially <strong>empty</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "EEEEEEE"</span></p>
<p><strong>Output:</strong> <span class="example-io">7</span></p>
<p><strong>Explanation:</strong></p>
<p>After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELELEEL"</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 2 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>0</td>
</tr>
<tr>
<td>6</td>
<td>Leave</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELEELEELLL"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 3 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>6</td>
<td>Enter</td>
<td>3</td>
<td>0</td>
</tr>
<tr>
<td>7</td>
<td>Leave</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>8</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>9</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
</tbody>
</table>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 50</code></li>
<li><code>s</code> consists only of the letters <code>'E'</code> and <code>'L'</code>.</li>
<li><code>s</code> represents a valid sequence of entries and exits.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minimumChairs(string s) {\n int n=s.size();\n int count=0;\n int m=0;\n for(int i=0;i<n;i++){\n if(s[i]=='E'){\n count++;\n }\n if(s[i]=='L'){\n count--;\n }\n if(count>m){\n m=count;\n }\n }\n return m;\n \n }\n};",
"memory": "8700"
} |
3,426 | <p>You are given a string <code>s</code>. Simulate events at each second <code>i</code>:</p>
<ul>
<li>If <code>s[i] == 'E'</code>, a person enters the waiting room and takes one of the chairs in it.</li>
<li>If <code>s[i] == 'L'</code>, a person leaves the waiting room, freeing up a chair.</li>
</ul>
<p>Return the <strong>minimum </strong>number of chairs needed so that a chair is available for every person who enters the waiting room given that it is initially <strong>empty</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "EEEEEEE"</span></p>
<p><strong>Output:</strong> <span class="example-io">7</span></p>
<p><strong>Explanation:</strong></p>
<p>After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELELEEL"</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 2 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>0</td>
</tr>
<tr>
<td>6</td>
<td>Leave</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "ELEELEELLL"</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>Let's consider that there are 3 chairs in the waiting room. The table below shows the state of the waiting room at each second.</p>
</div>
<table>
<tbody>
<tr>
<th>Second</th>
<th>Event</th>
<th>People in the Waiting Room</th>
<th>Available Chairs</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>6</td>
<td>Enter</td>
<td>3</td>
<td>0</td>
</tr>
<tr>
<td>7</td>
<td>Leave</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>8</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>9</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
</tbody>
</table>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 50</code></li>
<li><code>s</code> consists only of the letters <code>'E'</code> and <code>'L'</code>.</li>
<li><code>s</code> represents a valid sequence of entries and exits.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minimumChairs(string s) {\n int n=s.size();\n int count=0;\n int m=0;\n for(int i=0;i<n;i++){\n if(s[i]=='E'){\n count++;\n }\n else if(s[i]=='L'){\n count--;\n }\n if(count>m){\n m=count;\n }\n }\n return m;\n \n }\n};",
"memory": "8700"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></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 int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end(), [](auto& a, auto& b) {\n return a[0] != b[0]? a[0] < b[0] : a[1] > b[1];\n });\n\n const int n = meetings.size();\n int last = meetings[0][1];\n int start = meetings[0][0]-1;\n for (int i = 0;i < n;i++) {\n if (meetings[i][0] <= last) {\n last = max(last, meetings[i][1]);\n } else {\n days -= last-start;\n\n start = meetings[i][0]-1;\n last = meetings[i][1];\n }\n }\n\n days -= last-start;\n\n return days;\n }\n};",
"memory": "125300"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></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 int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end(), [](auto& a, auto& b) {\n return a[0] != b[0]? a[0] < b[0] : a[1] > b[1];\n });\n\n const int n = meetings.size();\n int last = meetings[0][1];\n int start = meetings[0][0];\n for (int i = 0;i < n;i++) {\n if (meetings[i][0] <= last) {\n last = max(last, meetings[i][1]);\n } else {\n days -= last-start+1;\n\n start = meetings[i][0];\n last = meetings[i][1];\n }\n }\n\n days -= last-start+1;\n\n return days;\n }\n};",
"memory": "125400"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 0 | {
"code": "static const auto boost = []() {\n std::cin.tie(0);\n std::cout.tie(0);\n std::ios::sync_with_stdio(false);\n return 0;\n}();\n\nclass Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n\n std::sort(meetings.begin(),meetings.end());\n int max_end = meetings[0][1] , count = meetings[0][0] - 1;\n for(auto&& vec : meetings) {\n auto&& [start, end] = std::make_tuple(vec[0],vec[1]);\n if(start > max_end) {\n count += start - max_end-1;\n } \n max_end = std::max(end, max_end);\n }\n count += days - max_end;\n return count;\n }\n\n};",
"memory": "125500"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 0 | {
"code": "static const auto boost = []() {\n std::cin.tie(0);\n std::cout.tie(0);\n std::ios::sync_with_stdio(false);\n return 0;\n}();\n\nclass Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n\n std::sort(meetings.begin(),meetings.end());\n int max_end = meetings[0][1] , count = meetings[0][0] - 1;\n for(auto&& vec : meetings) {\n if(vec[0] > max_end) {\n count += vec[0] - max_end-1;\n } \n max_end = std::max(vec[1], max_end);\n }\n count += days - max_end;\n return count;\n }\n\n};",
"memory": "125500"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 0 | {
"code": "static const auto boost = []() {\n std::cin.tie(0);\n std::cout.tie(0);\n std::ios::sync_with_stdio(false);\n return 0;\n}();\n\nclass Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n\n std::sort(meetings.begin(),meetings.end());\n int max_end = meetings[0][1] , count = meetings[0][0] - 1;\n for(int i = 1; i != std::ssize(meetings); ++i) {\n if(meetings[i][0] > max_end) {\n count += meetings[i][0] - max_end-1;\n } \n max_end = std::max(meetings[i][1], max_end);\n }\n count += days - max_end;\n return count;\n }\n\n};",
"memory": "125600"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& m) {\n ios::sync_with_stdio(false);\n sort(m.begin(),m.end());\n\n int prev = m[0][1];\n int ans = m[0][0]-1;\n\n for(int i = 1; i < m.size(); i++){\n if((prev < m[i][0])){\n ans += m[i][0]-prev-1;\n prev = max(prev,m[i][1]);\n }else{\n prev = max(prev,m[i][1]);\n }\n }\n if(days > prev) ans += (days-prev);\n return ans;\n }\n};",
"memory": "125700"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n int work = meetings[0][1]-meetings[0][0]+1;\n int max = meetings[0][1];\n for(int i = 1; i < meetings.size(); i++){\n if(meetings[i][1] <= max) continue;\n else if(meetings[i][1] > max){\n if(meetings[i][0] > max){\n work += meetings[i][1]-meetings[i][0]+1;\n }\n else{\n work += meetings[i][1]-max;\n }\n max = meetings[i][1];\n }\n } \n return days-work;\n }\n};",
"memory": "125800"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n if(meetings.empty()) {\n return days;\n }\n\n // Sort meetings by their start day\n sort(meetings.begin(), meetings.end());\n \n int ans = 0;\n int current_start = meetings[0][0];\n int current_end = meetings[0][1];\n \n for (int i = 1; i < meetings.size(); ++i) {\n if (meetings[i][0] > current_end) {\n // No overlap, count the current block\n ans += current_end - current_start + 1;\n // Move to the next block\n current_start = meetings[i][0];\n current_end = meetings[i][1];\n } else {\n // Overlapping meetings, merge intervals\n current_end = max(current_end, meetings[i][1]);\n }\n }\n \n // Count the last block\n ans += current_end - current_start + 1;\n \n return days - ans;\n}\n};",
"memory": "125900"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countDays(int days, std::vector<std::vector<int>>& meetings) {\n\n sort(meetings.begin() , meetings.end());\n\n int strt = meetings[0][0];\n int end = meetings[0][1];\n\n for(int i=1 ; i<meetings.size() ; i++){\n if(meetings[i][0] <= end){\n end = std::max(end,meetings[i][1]);\n }\n else{\n days -= end-strt+1;\n strt = meetings[i][0];\n end = meetings[i][1];\n }\n }\n days -= end-strt+1;\n\n return days;\n }\n};",
"memory": "126000"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n \n // Just the extension of merge intervals problem\n\n if(meetings.size() == 1){\n int ans = 0; \n ans = ans + (meetings[0][1]-meetings[0][0]+1);\n return days-ans; \n }\n\n sort(meetings.begin(),meetings.end());\n\n vector<vector<int>>intervals;\n intervals.push_back(meetings[0]);\n\n for(int i=1;i<meetings.size();i++){\n \n if(intervals.back()[1] >= meetings[i][0]){\n intervals.back()[1] = max(intervals.back()[1],meetings[i][1]);\n }\n\n else{\n intervals.push_back(meetings[i]);\n }\n }\n\n int cnt_days = 0; \n\n for(int i=0;i<intervals.size();i++){\n cnt_days = cnt_days + (intervals[i][1]-intervals[i][0]+1);\n }\n\n return days-cnt_days; \n } \n};",
"memory": "126100"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 0 | {
"code": "class Solution {\n//TC:O(nlogn)\n//SC:O(nlogn)\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n int ans = 0, last = 0;\n\n // Sort meetings by their start day to process them in chronological order\n sort(meetings.begin(), meetings.end());\n\n // Process each meeting\n for (auto& meeting : meetings) {\n int start = meeting[0];\n int end = meeting[1];\n\n // If there is a gap between the last processed day and the start of the current meeting\n if (start > last + 1) {\n ans += (start - last - 1); // Days between last and start are free\n }\n\n // Update the last processed day\n last = max(last, end);\n }\n\n // After processing all meetings, check if there are available days after the last meeting\n if (days > last) {\n ans += (days - last); // Remaining days from last meeting to the end are free\n }\n\n return ans;\n }\n};\n",
"memory": "126100"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n int count = 0;\n int last=0;\n for (auto& meeting : meetings) {\n if (meeting[0]>last) {\n count += meeting[0] - last-1;\n }\n last=max(last,meeting[1]);\n }\n count += days - last;\n return count;\n}\n};",
"memory": "126200"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n // for(auto it : meetings){\n // cout<<it[0]<<\" \"<<it[1]<<endl;\n // }\n\n int ans= 0;\n\n int left=meetings[0][0];\n int right=meetings[0][1];\n\n if(left>1) ans = left-1;\n // cout<<right<<endl;\n for(int i = 1; i<meetings.size(); i++){\n int currleft = meetings[i][0];\n int currright = meetings[i][1];\n // cout<<currright<<endl;\n \n if(right<currleft){\n ans += currleft-right-1;\n left = currleft;\n right = currright;\n }\n else{\n right = max(right,currright);\n }\n // cout<<ans<<endl;\n }\n\n if(right<days) ans+= days - right;\n return ans;\n }\n};",
"memory": "126200"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>>merge(vector<vector<int>>&nums){\n vector<vector<int>>result;\n result.push_back(nums.front());\n for(int i=1;i<nums.size();i++)\n {\n if(result.back()[1]<nums[i][0]){\n result.push_back(nums[i]);\n }\n else{\n result.back()[0]=min(result.back()[0],nums[i][0]);\n result.back()[1]=max(result.back()[1],nums[i][1]);\n }\n }\n return result;\n }\n int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin(),meetings.end());\n auto intervals=merge(meetings);\n int busyDay=0;\n for(auto it:intervals){\n busyDay+=(it[1]-it[0]+1);\n }\n return days-busyDay;\n }\n};",
"memory": "126300"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n vector<vector<int>> mergeIntervals(vector<vector<int>>& meetings){\n vector<vector<int>> ans;\n ans.push_back(meetings[0]);\n for(int i=1;i<meetings.size();i++){\n int last = ans.back()[1];\n if(last >= meetings[i][0]){\n if(last < meetings[i][1]){\n ans.back()[1] = meetings[i][1];\n }\n continue;\n }\n ans.push_back(meetings[i]);\n }\n return ans;\n }\n\n int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n\n vector<vector<int>> mIter = mergeIntervals(meetings);\n \n for(auto it: mIter){\n days -= it[1]-it[0]+1;\n }\n \n return days;\n }\n};",
"memory": "126400"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n vector<vector<int>> mergeIntervals(vector<vector<int>>& a) {\n sort(a.begin(),a.end());\n \n vector<vector<int>> ans;\n \n int n=a.size();\n \n int s=a[0][0], e=a[0][1];\n for(int i=1;i<n;i++){\n int si=a[i][0], ei=a[i][1];\n \n if(si>e){\n ans.push_back({s,e});\n s=si;\n }\n e=max(e,ei);\n }\n ans.push_back({s,e});\n \n return ans;\n }\n \n int countDays(int days, vector<vector<int>>& meetings) {\n vector<vector<int>> merged_meetings = mergeIntervals(meetings);\n \n int busy_days = 0;\n for(vector<int> d : merged_meetings){\n int start = d[0], end = d[1];\n busy_days += (end - start +1);\n } \n \n return days - busy_days;\n }\n};",
"memory": "126400"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n \n vector<vector<int>> listM;\n\n auto curr=meetings[0];\n\n for(int i=1;i<meetings.size();i++){\n if(curr[1]>=meetings[i][0]){\n curr[1]=max(curr[1], meetings[i][1]);\n }else{\n listM.push_back(curr);\n curr=meetings[i];\n }\n }\n \n if(listM.size()==0 or listM.back()!=curr){\n listM.push_back(curr);\n }\n int n=listM.size();\n int res=0;\n\n res+=listM[0][0]-1;\n\n for(int i=1;i<n;i++){\n res+=listM[i][0]-listM[i-1][1]-1;\n }\n\n // for(int i=0;i<n;i++){\n // cout<<listM[i][0]<<\" \"<<listM[i][1]<<\"\\n\";\n // }\n\n if(listM[n-1][1]<days){\n res+=(days-listM[n-1][1]);\n }\n\n return res;\n \n }\n};",
"memory": "126500"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& m) {\n if(m.size()==0 && days!=0)\n {\n return days;\n }\n if(m.size()==1)\n {\n int c = (m[0][1] - m[0][0]) + 1;\n return days-c;\n }\n vector<vector<int>> arr;\n sort(m.begin(),m.end());\n\n int s = m[0][0];\n int e = m[0][1];\n\n for(int i=1; i<m.size(); i++)\n {\n if(m[i][0] <= e && m[i][1] > e)\n {\n e = m[i][1];\n }\n else\n {\n if(m[i][0] > e)\n {\n arr.push_back({s,e});\n s=m[i][0];\n e=m[i][1];\n }\n }\n }\n arr.push_back({s,e});\n int c = 0;\n for(int i=0; i<arr.size(); i++)\n {\n c+=(arr[i][1]-arr[i][0])+1;\n }\n\n return days-c;\n\n }\n};",
"memory": "126600"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\n /*\n normal merging inerval problem\n after doing merge interval, scan the merged days to find gaps \n between the separate intervals.\n add those gaps as those gaps are the days employee would be free.\n\n also make sure to consider edge cases\n 1. for example, the total number of days is 10, and the second value\n of the last interval comes out to be 8, that is the last meeting ended \n on 8th day.\n so while calculting the gaps we will miss this.\n hence after calculating the gaps make sure to cover this.\n days - merged[last_index][1]\n 2. for example our meetings start from day3, that is the start of the\n very first meeting is 3rd day, we also have to consider the gap or free\n time before everything started, this also wont be calculated while checking\n the gaps.\n merged[0][0]-1\n\n tc: most time is consumed in sorting, lets assume there are n meetings\n each meeting has start and end element\n so 2nlog(2n)\n then the other ooperations are linear\n hence overall tc of nlog(n)\n */\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n int n = meetings.size();\n sort(meetings.begin(),meetings.end());\n vector<vector<int>> merged;\n\n int first = meetings[0][0];\n int second = meetings[0][1];\n\n for(int i=1;i<n;i++)\n {\n if(second<meetings[i][0])\n {\n merged.push_back({first,second});\n first = meetings[i][0];\n second = meetings[i][1];\n }\n else\n {\n second = max(second,meetings[i][1]);\n }\n }\n\n merged.push_back({first,second});\n\n int ans=0;\n\n for(int i=0;i<merged.size()-1;i++)\n {\n cout<<\"1\";\n ans+=merged[i+1][0]-merged[i][1]-1;\n }\n\n ans +=days-merged[merged.size()-1][1];\n ans +=merged[0][0]-1;\n\n return ans;\n \n }\n};",
"memory": "126700"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin(),meetings.end());\n stack<pair<int,int>> mp;\n int ans=0;\n mp.push({meetings[0][0],meetings[0][1]});\n if(meetings[0][0])ans+=(meetings[0][0]-1);\n for(int i=1;i<meetings.size();i++){\n auto v=mp.top();\n if(v.second>=meetings[i][1]){\n continue;\n }else if(v.second>=meetings[i][0]){\n v.second=meetings[i][1];\n mp.pop();\n mp.push(v);\n }else{\n ans+=(meetings[i][0]-v.second-1);\n mp.push({meetings[i][0],meetings[i][1]});\n }\n // cout<<meetings[i][0]<<\" \";\n }\n auto v=mp.top();\n if(days>v.second)ans+=(days-v.second);\n return ans;\n }\n};",
"memory": "126800"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n stack<pair<int,int>>st;\n for (int i=0; i<meetings.size(); i++) {\n if (st.empty()) {\n st.push({meetings[i][0], meetings[i][1]});\n }\n else if (st.empty()==0 && st.top().second>=meetings[i][0]) {\n pair<int, int>curr = st.top();\n st.pop();\n curr.second = max(meetings[i][1], curr.second);\n st.push(curr);\n }\n else {\n st.push({meetings[i][0], meetings[i][1]});\n }\n }\n int ans = 0;\n while (st.empty()==0) {\n ans += st.top().second - st.top().first + 1;\n st.pop();\n }\n return days - ans;\n }\n};",
"memory": "126900"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin(),meetings.end());\n int ans = 0 ;\n stack<int> st ;\n st.push(0);\n int n = meetings.size();\n for(int i = 0 ;i<n;i++){\n if(meetings[i][0]>st.top()){\n ans += meetings[i][0]-st.top()-1;\n }\n int num = max(max(st.top(),meetings[i][0]),meetings[i][1]);\n st.pop();\n st.push(num);\n }\n ans += days-st.top();\n return ans ;\n }\n};",
"memory": "127000"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n\n\n stack<pair<int,int>> s;\n\n sort(meetings.begin(),meetings.end());\n\n int n = meetings.size();\n\n for(int i=0;i<n;i++){\n\n int start_i = meetings[i][0];\n int end_i = meetings[i][1];\n\n while(!s.empty() && start_i <= s.top().second){\n\n end_i = max(end_i,s.top().second);\n start_i = min(start_i,s.top().first);\n s.pop();\n }\n\n s.push({start_i,end_i});\n\n\n }\n\n vector<pair<int,int>> ans;\n\n while(!s.empty()){\n ans.push_back(s.top());\n s.pop();\n }\n\n reverse(ans.begin(),ans.end());\n\n int sum = 0;\n\n for(int i =1;i<ans.size();i++){\n sum += (ans[i].first - ans[i-1].second-1);\n }\n\n // cout<<sum<<endl;\n\n int siz = ans.size();\n\n return sum + (ans[0].first -1) + (days - ans[siz-1].second);\n \n }\n};",
"memory": "127100"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int day, vector<vector<int>>& meetings) {\n sort(meetings.begin(),meetings.end());\n vector<int>days;\n int cnt=0;\n // for(int i=0;i<meetings.size();i++){\n // cout<<meetings[i][0]<<\"-\"<<meetings[i][1]<<\" \";\n // }\n // cout<<endl;\n days.push_back(meetings[0][0]);\n days.push_back(meetings[0][1]);\n\n for(int i=1;i<meetings.size();i++){\n if(days.back()<meetings[i][0]){\n cnt+= (meetings[i][0]-days.back()-1);\n days.push_back(meetings[i][0]);\n }\n if(days.back()<meetings[i][1]){\n days.push_back(meetings[i][1]);\n }\n }\n // for(auto ele:days){\n // cout<<ele<<\" \";\n // }\n // cout<<endl;\n // cout<<cnt<<endl;\n cnt+= (days[0]-1);\n cnt+= (day-days[days.size()-1]);\n return cnt;\n }\n};",
"memory": "127400"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"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 int countDays(int days, vector<vector<int>>& meetings) {\n const int n = meetings.size();\n vector<int> inds(n);\n\n iota(inds.begin(), inds.end(), 0);\n\n sort(inds.begin(), inds.end(), [&](int a, int b) {\n if (meetings[a][0] != meetings[b][0]) {\n return meetings[a][0] < meetings[b][0];\n }\n \n return meetings[a][1] > meetings[b][1];\n });\n\n int last = meetings[inds[0]][1];\n int start = meetings[inds[0]][0];\n for (int i = 0;i < n;i++) {\n if (meetings[inds[i]][0] <= last) {\n last = max(last, meetings[inds[i]][1]);\n } else {\n days -= last-start+1;\n\n start = meetings[inds[i]][0];\n last = meetings[inds[i]][1];\n }\n }\n\n days -= last-start+1;\n\n return days;\n }\n};",
"memory": "127500"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end(), [](const vector<int>& lhs, const vector<int>& rhs) {\n if (lhs[0] != rhs[0])\n return lhs[0] < rhs[0];\n return lhs[1] < rhs[1];\n });\n vector<vector<int>> merged;\n merged.reserve(meetings.size());\n merged.push_back(meetings[0]);\n for (int i = 1; i < meetings.size(); ++i)\n {\n // l1, r1\n // l2, r2\n // l1 <= l2\n // if l2 > r1, push_back, else merge\n if (meetings[i][0] > merged.back()[1] + 1)\n {\n merged.push_back(meetings[i]);\n }\n else\n {\n merged.back()[0] = min(merged.back()[0], meetings[i][0]);\n merged.back()[1] = max(merged.back()[1], meetings[i][1]);\n } \n }\n int total = 0;\n if (!merged.empty() && merged[0][0] > 1)\n total+= (merged[0][0] - 1);\n if (!merged.empty() && merged.back()[1] < days)\n total+= (days - merged.back()[1]);\n //cout << \"total1 = \" << total << endl;\n for (int i = 1; i < merged.size(); ++i)\n {\n total += (merged[i][0] - merged[i-1][1] - 1);\n }\n return total;\n }\n};",
"memory": "127800"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n int n = meetings.size();\n vector<pair<int,int>> v(n);\n for(int i = 0;i < n;i++){\n int a = meetings[i][0];\n int b = meetings[i][1];\n v[i] = {a,b};\n }\n sort(v.begin(),v.end());\n int count = 0;\n int end = v[0].second;\n count += v[0].first - 1;\n for(int i = 1;i < n;i++){\n if(v[i].first > end){\n count += v[i].first - end - 1;\n end = v[i].second;\n }\n else end = max(end,v[i].second);\n }\n count += days - end;\n return count;\n }\n};",
"memory": "129699"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& m) {\n sort(m.begin(), m.end());\n int n = m.size();\n\n vector<vector<int>> ans;\n\n ans.push_back(m[0]);\n\n for(int i=1; i<n;i++){\n if(ans.back()[1] >= m[i][0]){\n if(ans.back()[1] < m[i][1]){\n int low = ans.back()[0];\n int h = m[i][1];\n ans.pop_back();\n ans.push_back({low,h});\n }\n }else if(ans.back()[0] == m[i][0]){\n int low = m[i][0];\n int h = max(ans.back()[1], m[i][1]);\n ans.pop_back();\n ans.push_back({low,h});\n }else{\n ans.push_back(m[i]);\n }\n }\n\n for(auto&it:ans){\n cout << it[0] << \" \" << it[1] << endl;\n days-= (it[1] - it[0] +1);\n }\n\n return (days < 0 ? 0 : days);\n\n }\n};",
"memory": "131000"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n vector<vector<int>> copy;\n copy.push_back(meetings[0]);\n for (int i = 1; i < meetings.size(); i++) {\n int u = copy.back()[0];\n int v = copy.back()[1];\n if (meetings[i][0] >= u and meetings[i][1] <= v) {\n continue;\n\n } else if (meetings[i][0] <= v) {\n copy.pop_back();\n copy.push_back({u, meetings[i][1]});\n } else {\n copy.push_back({meetings[i][0], meetings[i][1]});\n }\n }\n int ans = days;\n for (auto i : copy) {\n cout << i[0] << \" \" << i[1] << endl;\n ans -= (i[1] - i[0] + 1);\n }\n return ans;\n }\n};",
"memory": "131200"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Interval\n{\npublic:\n int start, end;\n Interval(int start, int end) : start(start), end(end) {}\n void merge(Interval &other)\n {\n this->start = min(this->start, other.start);\n this->end = max(this->end, other.end);\n }\n\n bool can_merge(Interval &other)\n {\n return other.start >= this->start && other.start <= this->end;\n }\n};\nstd::ostream &operator<<(std::ostream &os, const Interval &interval)\n{\n os << \"Interval(\" << interval.start << \", \" << interval.end << \")\";\n return os;\n}\nclass Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n std::sort(meetings.begin(), meetings.end());\n deque<Interval> intervals;\n vector<Interval> merged_intervals;\n for (const auto &x : meetings)\n intervals.emplace_back(x[0], x[1]);\n vector<vector<int>> sol;\n auto curr = intervals.front();\n intervals.pop_front();\n\n while (!intervals.empty())\n {\n auto next = intervals.front();\n if (curr.can_merge(next))\n {\n curr.merge(next);\n intervals.pop_front();\n }\n else\n {\n merged_intervals.push_back(curr);\n curr = intervals.front();\n intervals.pop_front();\n }\n }\n merged_intervals.push_back(curr);\n auto prev = 0;\n auto total = 0;\n for (const auto& interval : merged_intervals) {\n total += interval.start - prev - 1;\n prev = interval.end;\n }\n total += days - prev;\n return total;\n }\n};",
"memory": "131400"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n std::vector<Event> events;\n events.reserve(2*meetings.size());\n for(auto&& pair: meetings) {\n events.emplace_back(pair[0], Event::Start);\n events.emplace_back(pair[1], Event::End);\n }\n std::sort(events.begin(),events.end(), [](auto&& lhs, auto&& rhs) {\n return lhs.day == rhs.day ? lhs.type < rhs.type: lhs.day < rhs.day;\n });\n int meetings_count =0 , count =0, prev_day = 0;\n for(auto&& [day, type] : events) {\n switch (type) {\n case Event::Start: if(meetings_count++ == 0) {\n count += day - prev_day -1;\n } break;\n case Event::End: --meetings_count; break;\n }\n prev_day = day;\n }\n if(meetings_count == 0) {\n count += days - prev_day;\n }\n return count;\n }\nprivate:\n\n struct Event {\n int day;\n enum Type {\n Start, End\n } type;\n };\n};",
"memory": "132700"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "static auto fastio = []() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n};\n\nclass Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n fastio();\n int n = meetings.size();\n vector<pair<int, int>> vp;\n for (int i = 0; i < n; i++) {\n vp.push_back({meetings[i][0], meetings[i][1]});\n }\n sort(vp.begin(), vp.end());\n // for (auto& ii : vp) {\n // cout << ii.first << \" \" << ii.second << endl;\n // }\n int sum = vp[0].first - 1;\n int LastDayMeeting = vp[0].second;\n for (int i = 1; i < n; i++) {\n if (vp[i].first > LastDayMeeting) {\n sum += (vp[i].first - LastDayMeeting) - 1;\n }\n LastDayMeeting = max(LastDayMeeting, vp[i].second);\n }\n sum += days - LastDayMeeting;\n return sum;\n }\n};",
"memory": "133600"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nstatic bool cmp(const pair<int,int>&a,const pair<int,int>&b){\n return a.second<b.second;\n}\n int countDays(int days, vector<vector<int>>& m) {\n vector<pair<int,int>>vp;\n for(int i=0;i<m.size();i++){\n int a=m[i][0];\n int b=m[i][1];\n vp.push_back({a,b});\n\n }\n sort(vp.begin(),vp.end());\n int ans=0;\n int l=1;\n for(auto it:vp){\n cout<<it.first<<\" \"<<it.second<<endl;\n }\n if(vp[0].first>l){\n ans+=vp[0].first-l;\n }\n l=vp[0].second;\n for(int i=1;i<vp.size();i++){\n if(vp[i].first>l){\n ans+=vp[i].first-l-1;\n \n }\n if(vp[i].second>l){\n l=vp[i].second;\n }\n \n \n }\n if(l<days){\n ans+=days-l;\n }\n return ans; \n }\n};",
"memory": "134100"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n typedef pair<int,int>PII;\n\n int countDays(int days, vector<vector<int>>& meetings) {\n vector<PII>vec;\n for(int i=0; i<meetings.size(); i++)\n vec.push_back({meetings[i][0], meetings[i][1]});\n sort(vec.begin(), vec.end());\n int k=vec[0].second, sum=vec[0].first-1;\n for(int i=1; i<vec.size(); i++){\n if(vec[i].first>k)\n sum+=(vec[i].first-k-1);\n k=max(k,vec[i].second);\n }\n sum+=days-k;\n return sum;\n }\n};",
"memory": "134200"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n int ct= 0;\n \n vector<pair<int,int>> arr;\n\n arr.push_back({0 , 0});\n int n = meetings.size();\n for(int i=0 ; i<n ; i++){\n arr.push_back({meetings[i][0] , meetings[i][1]});\n }\n arr.push_back({days+1,days+1});\n sort(arr.begin() , arr.end());\n int prevMax = 0;\n\n\n for(int i=1 ; i<n+2 ; i++){\n if(arr[i].first>prevMax){\n ct += (arr[i].first - (prevMax+1));\n }\n prevMax = max(prevMax , arr[i].second);\n }\n\n // for(int i=0 ; i<n+2 ; i++){\n // cout << arr[i].first << \" \" << arr[i].second << \",\";\n // }\n\n // [0, 0], [1, 3] , [2,4] , [6, 6]\n return ct;\n }\n};",
"memory": "134300"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n \n vector<pair<int, int>> m;\n for(int i = 0; i < meetings.size(); i++) {\n m.push_back({meetings[i][0], meetings[i][1]});\n }\n \n sort(m.begin(), m.end());\n\n vector<pair<int, int>> n;\n n.push_back(m[0]);\n\n for(int i = 1; i < m.size(); i++) {\n pair<int, int> lastcur = n.back();\n n.pop_back();\n\n if (lastcur.second >= m[i].first) {\n lastcur.second = max(lastcur.second, m[i].second);\n n.push_back(lastcur);\n } else {\n n.push_back(lastcur);\n n.push_back(m[i]);\n }\n }\n\n int ans = days;\n\n for(int i = 0; i < n.size(); i++) {\n ans -= n[i].second - n[i].first + 1;\n }\n\n return ans;\n }\n};",
"memory": "134500"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n\n vector <int> meetingStart;\n vector <int> meetingEnd;\n\n for(int i =0 ; i < meetings.size() ;i++)\n {\n meetingStart.push_back(meetings[i][0]);\n meetingEnd.push_back(meetings[i][1]);\n }\n sort(meetingStart.begin(), meetingStart.end());\n sort(meetingEnd.begin(), meetingEnd.end());\n\n int res = 0;\n int curr = 0;\n int currPos = 1;\n\n int start = 0;\n int end = 0;\n\n while(start < meetingStart.size() && end < meetingEnd.size() )\n {\n if(meetingStart[start] < meetingEnd[end])\n {\n if(curr == 0)\n {\n if(currPos < meetingStart[start])\n res += (meetingStart[start]-currPos);\n }\n curr++;\n start++;\n // currPos = meetingStart[start];\n\n }\n else if(meetingStart[start] > meetingEnd[end])\n {\n curr--;\n if(curr == 0)\n {\n currPos = meetingEnd[end]+1;\n }\n end++;\n \n\n }\n else\n {\n if(curr == 0)\n {\n if(currPos < meetingStart[start])\n res += (meetingStart[start]-currPos);\n\n currPos = meetingEnd[end]+1;\n\n }\n start++;\n end++;\n\n }\n }\n\n while(end < meetingEnd.size() )\n {\n curr--;\n if(curr == 0)\n {\n currPos = meetingEnd[end]+1;\n }\n end++;\n\n }\n days++;\n cout << currPos << \" \" << days;\n if(curr == 0 && currPos < days)\n res += (days-currPos);\n\n \n return res;\n \n }\n};",
"memory": "134600"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n\n vector <int> meetingStart;\n vector <int> meetingEnd;\n\n for(int i =0 ; i < meetings.size() ;i++)\n {\n meetingStart.push_back(meetings[i][0]);\n meetingEnd.push_back(meetings[i][1]);\n }\n sort(meetingStart.begin(), meetingStart.end());\n sort(meetingEnd.begin(), meetingEnd.end());\n\n int res = 0;\n int currMeet = 0;\n int lastPos = 1;\n\n int start = 0;\n int end = 0;\n\n while(start < meetingStart.size() && end < meetingEnd.size() )\n {\n if(meetingStart[start] < meetingEnd[end])\n {\n // Check that currMeet is zero\n if(currMeet == 0)\n {\n // Add diff. of lastPos and starting of new meeting\n if(lastPos < meetingStart[start])\n res += (meetingStart[start]-lastPos);\n }\n\n currMeet++;\n start++;\n \n\n }\n else if(meetingStart[start] > meetingEnd[end])\n {\n currMeet--;\n // if currMeet is zero then update the lastPos to end of meeting\n if(currMeet == 0)\n {\n lastPos = meetingEnd[end]+1;\n }\n end++;\n \n\n }\n else\n {\n // Check that currMeet is zero\n if(currMeet == 0)\n {\n // Add diff. of lastPos and starting of new meeting\n if(lastPos < meetingStart[start])\n res += (meetingStart[start]-lastPos);\n\n lastPos = meetingEnd[end]+1;\n\n }\n start++;\n end++;\n\n }\n }\n \n // Check the remaining days are without Meeting\n if( meetingEnd[meetingEnd.size()-1] < days)\n res += (days-meetingEnd[meetingEnd.size()-1]);\n\n \n return res;\n \n }\n};",
"memory": "134600"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int d, vector<vector<int>>& m) {\n /*vector<int> arr(d+2, 0);\n //arr[0]=0;\n for (int i=0; i!=m.size(); i++)\n {\n arr[m[i][0]]++;\n arr[m[i][1]+1]--;\n }\n int ans=0;\n //if (arr[0]>=1) ans++;\n for (int i=1; i<arr.size()-1; i++)\n {\n arr[i]+=arr[i-1];\n if (arr[i]==0) ans++;\n }*/\n vector<int> b, e;\n for (int i=0; i!=m.size(); i++)\n {\n b.push_back(m[i][0]);\n e.push_back(m[i][1]);\n }\n sort(b.begin(), b.end());\n sort(e.begin(), e.end());\n int ans=b[0]-1;\n int l=0, r=0;\n while (r<e.size())\n {\n while (l+1<b.size()&&b[l+1]<=e[r]) l++;\n if (l+1==b.size()) {ans+=(d-e[e.size()-1]); break;}\n else\n {\n //if (l+1<b.size()&&b[l+1]<=e[r]) continue;\n if (l==r)\n {\n ans+=(b[l+1]-e[r]-1);\n l++;\n }\n r++;\n }\n }\n return ans;\n }\n};",
"memory": "134700"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n map<int, int> mp;\n mp[0] = 0;\n\n sort(meetings.begin(), meetings.end());\n\n int res = 0;\n for(const auto& meet : meetings) {\n int start = meet[0], end = meet[1];\n if(start > mp.rbegin()->first) {\n int diff = start - mp.rbegin()->first - 1;\n mp[end] = mp.rbegin()->second + diff;\n } else {\n if(end > mp.rbegin()->first) {\n mp[end] = mp.rbegin()->second;\n }\n }\n }\n\n int diff = days - mp.rbegin()->first;\n if(diff == 0) return mp.rbegin()->second;\n\n return (diff + mp.rbegin()->second);\n }\n};",
"memory": "134800"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin() , meetings.end());\n int n = meetings.size();\n vector<vector<int>> ans;\n for(int i=0;i<meetings.size();i++)\n {\n if(!ans.empty() && ans.back()[1] > meetings[i][0])\n {\n if(ans.back()[1] < meetings[i][1])\n ans.back()[1] = meetings[i][1];\n }\n\n else\n ans.push_back({meetings[i][0] , meetings[i][1]});\n }\n\n for(int i=0;i<ans.size();i++)\n cout<<ans[i][0]<<\" \"<<ans[i][1]<<endl;\n\n n = ans.size();\n int count = (days - ans[n-1][1] >= 1) ? days - ans[n-1][1] : 0;\n count += (ans[0][0] - 1 >= 1) ? ans[0][0] - 1 : 0;\n for(int i=0;i<ans.size()-1;i++)\n {\n int x = ans[i][1];\n int y = ans[i+1][0];\n if(y - x > 1)\n count += y - x - 1;\n\n }\n\n return count;\n }\n};",
"memory": "137100"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin() , meetings.end());\n int n = meetings.size();\n vector<vector<int>> ans;\n for(int i=0;i<meetings.size();i++)\n {\n if(!ans.empty() && ans.back()[1] > meetings[i][0])\n {\n if(ans.back()[1] < meetings[i][1])\n ans.back()[1] = meetings[i][1];\n }\n\n else\n ans.push_back({meetings[i][0] , meetings[i][1]});\n }\n\n n = ans.size();\n int count = (days - ans[n-1][1] >= 1) ? days - ans[n-1][1] : 0;\n count += (ans[0][0] - 1 >= 1) ? ans[0][0] - 1 : 0;\n for(int i=0;i<ans.size()-1;i++)\n {\n int x = ans[i][1];\n int y = ans[i+1][0];\n if(y - x > 1)\n count += y - x - 1;\n\n }\n\n return count;\n }\n};",
"memory": "137200"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n map<int,int>m ;\n for(auto &it:meetings){\n int from = it[0] ;\n int to = it[1] ;\n if(m.find(from)==m.end())m[from]=to ;\n else{\n m[from]=max(m[from],to) ;\n }\n }\n int maxi = -1 ;\n int count = 0 ;\n for(auto &it:m){\n int from = it.first ;\n int to = it.second ;\n if(maxi>=to)continue ;\n if(maxi>=from)from=maxi+1 ;\n count+=(to-from+1) ;\n maxi = max(maxi,to) ;\n }\n return days-count ;\n }\n};",
"memory": "138900"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n std::vector<std::pair<int, int>> events;\n \n // Add start and end events for each meeting\n for (const auto& meeting : meetings) {\n events.emplace_back(meeting[0], 1); // Start of meeting\n events.emplace_back(meeting[1] + 1, -1); // End of meeting (exclusive)\n }\n \n // Add events for the start and end of the total period\n events.emplace_back(1, 0);\n events.emplace_back(days + 1, 0);\n \n // Sort events by day\n std::sort(events.begin(), events.end());\n \n int active_meetings = 0;\n int available_days = 0;\n int prev_day = 1;\n \n for (const auto& [day, event_type] : events) {\n if (active_meetings == 0) {\n available_days += day - prev_day;\n }\n \n active_meetings += event_type;\n prev_day = day;\n }\n \n return available_days;\n }\n};",
"memory": "141700"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n std::vector<Event> events;\n\n for(auto&& pair: meetings) {\n events.emplace_back(pair[0], Event::Start);\n events.emplace_back(pair[1], Event::End);\n }\n std::sort(events.begin(),events.end(), [](auto&& lhs, auto&& rhs) {\n return lhs.day == rhs.day ? lhs.type < rhs.type: lhs.day < rhs.day;\n });\n int meetings_count =0 , count =0, prev_day = 0;\n for(auto&& [day, type] : events) {\n switch (type) {\n case Event::Start: if(meetings_count++ == 0) {\n count += day - prev_day -1;\n } break;\n case Event::End: --meetings_count; break;\n }\n prev_day = day;\n }\n if(meetings_count == 0) {\n count += days - prev_day;\n }\n return count;\n }\nprivate:\n\n struct Event {\n int day;\n enum Type {\n Start, End\n } type;\n };\n};",
"memory": "142000"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n std::vector<Event> events;\n\n for(auto&& pair: meetings) {\n events.emplace_back(pair[0], Event::Start);\n events.emplace_back(pair[1], Event::End);\n }\n std::sort(events.begin(),events.end(), [](auto&& lhs, auto&& rhs) {\n return lhs.day == rhs.day ? lhs.type < rhs.type: lhs.day < rhs.day;\n });\n int meetings_count =0 , count =0, prev_day = 0;\n for(auto&& [day, type] : events) {\n std::cout << day << \" \" << type << \"\\n\";\n switch (type) {\n case Event::Start: {\n if(meetings_count++ == 0) {\n count += day - prev_day -1;\n std::cout << \"+\" << day - prev_day -1 << \"\\n\";\n }\n } break;\n case Event::End: --meetings_count; break;\n }\n prev_day = day;\n }\n if(meetings_count == 0) {\n count += days - prev_day;\n }\n return count;\n }\nprivate:\n\n struct Event {\n int day;\n enum Type {\n Start, End\n } type;\n };\n};",
"memory": "142100"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n vector<pair<int,int>>events;\n for (auto& meet : meetings) {\n events.push_back({meet[0], 0});\n events.push_back({meet[1], 1});\n }\n sort(events.begin(), events.end());\n int result = 0;\n int lastBusyDay = 0;\n int curActive = 0;\n for (auto&[day, type] : events) {\n if (type == 1) {\n curActive--;\n } else {\n if (curActive == 0) {\n result += max(day - lastBusyDay - 1, 0);\n }\n curActive++;\n }\n lastBusyDay = day;\n }\n result += (days - lastBusyDay);\n return result;\n }\n};",
"memory": "142100"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n int count = 0;\n int last=0;\n for (auto x:meetings) {\n if (x[0]>last) {\n count += x[0] - last-1;\n }\n last=max(last,x[1]);\n }\n count += days - last;\n return count;\n}\n};",
"memory": "142200"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n /*\n vector<bool> result(days, false);\n for (auto m : meetings) {\n for (int i = m[0] - 1; i <= m[1] - 1; i++) {\n result[i] = true;\n }\n }\n int count = 0;\n for (auto r : result) {\n if (!r) { count++; }\n }\n return count;\n */\n\n sort(meetings.begin(), meetings.end());\n int count = 0;\n vector<int> prev = meetings[0]; //(2,0); prev[0] = intervals[0][0]; prev[1] = intervals[0][1]; \n count = meetings[0][1] - meetings[0][0] + 1; \n for (auto m : meetings) {\n if ( m[0] > prev[1]) {\n count += m[1] - m[0] + 1;\n prev[1] = m[1];\n prev[0] = m[0];\n } else if (m[1] > prev[1]) {\n count += m[1] - prev[1];\n prev[1] = m[1];\n }\n\n //cout << endl << \" \" << count << \" \" << prev[0] << \" \" << prev[1];\n }\n return days - count;\n }\n};",
"memory": "142300"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n if (meetings.size()==1) return (days-1-(meetings[0][1]-meetings[0][0]));\n sort(meetings.begin(), meetings.end(), [](const vector<int>& a, const vector<int>& b) {\n return a[0] < b[0];\n });\n\n vector<vector<int>> merged;\n vector<int> prev = meetings[0];\n\n for (int i = 1; i < meetings.size(); ++i) {\n vector<int> interval = meetings[i];\n if (interval[0] <= prev[1]) {\n prev[1] = max(prev[1], interval[1]);\n } else {\n merged.push_back(prev);\n prev = interval;\n }\n }\n\n merged.push_back(prev);\n int ans = days;\n for (int i = 0; i<merged.size(); i++) {\n ans -=(merged[i][1]-merged[i][0]+1);\n }\n return ans;\n }\n};",
"memory": "142400"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n vector<vector<int>> merge(vector<vector<int>>& intervals) {\n vector<vector<int>>ans ;\n if(intervals.size() == 0) return intervals ;\n\n sort(intervals.begin(), intervals.end()) ;\n vector<int>tempInterval = intervals[0] ;\n\n for(auto it : intervals){\n if(it[0] <= tempInterval[1]){\n tempInterval[1] = max(it[1], tempInterval[1]) ;\n }\n else{\n ans.push_back(tempInterval) ;\n tempInterval = it ;\n }\n }\n ans.push_back(tempInterval) ;\n return ans ;\n }\npublic:\n int countDays(int days, vector<vector<int>>& meet) {\n vector<vector<int>> meetings = merge(meet) ;\n sort(meetings.begin(), meetings.end()) ;\n int n = meetings.size() ;\n int ans = 0 ;\n int maxi_end = meetings[0][1] ;\n\n for(int i=1; i<n; i++){\n int start = meetings[i][0] ;\n int end = meetings[i][1] ;\n maxi_end = max(maxi_end, end) ;\n int prev = meetings[i-1][1] ;\n\n if(prev < start){\n ans += start - prev - 1;\n }\n }\n ans += (meetings[0][0] - 1) ;\n ans += (days - maxi_end) ;\n\n for(int i=0; i<n; i++){\n cout << meetings[i][0] << \" \" << meetings[i][1] << endl;\n }\n return ans ;\n }\n};",
"memory": "142500"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n vector<vector<int>> mergedIntervals;\n if (meetings.size() == 0) {\n return days; \n }\n\n // Sort the meetings by start time\n sort(meetings.begin(), meetings.end());\n vector<int> tempInterval = meetings[0];\n\n // Merge the intervals\n for (auto it : meetings) {\n if (it[0] <= tempInterval[1]) {\n tempInterval[1] = max(it[1], tempInterval[1]);\n } else {\n mergedIntervals.push_back(tempInterval);\n tempInterval = it;\n }\n }\n mergedIntervals.push_back(tempInterval);\n\n // Calculate the total number of days covered by meetings\n int coveredDays = 0;\n for (auto interval : mergedIntervals) {\n coveredDays += (interval[1] - interval[0] + 1);\n }\n\n // Return the number of days not covered by meetings\n return days - coveredDays;\n }\n};",
"memory": "142600"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> mergeIntervals(vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n vector<vector<int>> merged;\n vector<int> act = meetings[0];\n\n for (vector<int> inter : meetings) {\n if (inter[0] <= act[1]) {\n act[1] = max(act[1], inter[1]);\n } else {\n merged.push_back(act);\n act = inter;\n }\n }\n\n merged.push_back(act);\n return merged;\n }\n\n int countDays(int days, vector<vector<int>>& meetings) {\n int available = 0;\n vector<vector<int>> merged = mergeIntervals(meetings);\n vector<int> actMeet = merged[0];\n\n if (merged.size() == 1) return days - (merged[0][1] - merged[0][0]) - 1;\n if (actMeet[0] != 1) available += (actMeet[0] - 1);\n if(merged[merged.size()-1][1] != days) available += (days - merged[merged.size()-1][1]);\n\n for (int i = 1; i < merged.size(); i++) {\n int sig = actMeet[1] - 1;\n int ant = actMeet[1] + 1;\n\n if ((merged[i][0] != sig) && (merged[i][0] != ant)) {\n available += abs(actMeet[1] - merged[i][0]) - 1;\n actMeet = merged[i];\n continue;\n }\n available += abs(actMeet[1] - merged[i][0]) - 1;\n actMeet = merged[i];\n }\n\n return available;\n }\n};",
"memory": "142700"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n \n\n vector<pair<int,int>>vi;\n int n = meetings.size();\n for(int i=0;i<n;i++)\n {\n vi.push_back({meetings[i][0],1});\n vi.push_back({meetings[i][1]+1,-1});\n\n }\n sort(vi.begin(),vi.end());\n int p = vi.size();\n int sum = 0;\n vector<int> prefix(p+1);\n for(int i=1;i<=p;i++) //range = [0,p-1]; range of i-1\n prefix[i] = prefix[i-1] + vi[i-1].second;\n int ans = (vi[0].first-1);\n for(int i=1;i<p;i++)//range = [0,p-2]; range of i-1 and [0,p-1] i;\n {\n if(prefix[i] <= 0)\n ans += vi[i].first - vi[i-1].first;\n }\n\n if(prefix[p] == 0)\n ans += max(0,(days-vi[p-1].first)+1);\n\n return ans;\n\n }\n};",
"memory": "145500"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n \n\n vector<pair<int,int>>vi;\n int n = meetings.size();\n for(int i=0;i<n;i++)\n {\n vi.push_back({meetings[i][0],1});\n vi.push_back({meetings[i][1]+1,-1});\n\n }\n sort(vi.begin(),vi.end());\n int p = vi.size();\n int sum = 0;\n vector<int> prefix(p+1);\n for(int i=1;i<=p;i++) //range = [0,p-1]; range of i-1\n prefix[i] = prefix[i-1] + vi[i-1].second;\n int ans = (vi[0].first-1);\n for(int i=1;i<p;i++)//range = [0,p-2]; range of i-1 and [0,p-1] i;\n {\n if(prefix[i] <= 0)\n ans += vi[i].first - vi[i-1].first;\n }\n\n if(prefix[p] == 0)\n ans += max(0,(days-vi[p-1].first)+1);\n\n return ans;\n\n }\n};",
"memory": "145600"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool comp(vector<int> &a, vector<int> &b){\n if(a[0]==b[0]) return a[1]<=b[1];\n return a[0]<b[0];\n }\n int countDays(int n, vector<vector<int>>& nums) {\n sort(nums.begin(),nums.end(),comp);\n stack<vector<int>> st;\n st.push(nums[0]);\n // vector<int> flag(n+1,0);\n for(int i=1;i<nums.size();i++){\n auto x =st.top();\n if(x[1]<nums[i][0]){\n st.push(nums[i]);\n // flag[nums[i][0]]=1;\n // flag[nums[i][1]]=1;\n\n }\n else if(x[1]<nums[i][1]){\n st.pop();\n x[1]=nums[i][1];\n st.push(x);\n // flag[x[0]]=1;\n // flag[x[1]]=1;\n }\n }\n int c=0;\n vector<vector<int>> v;\n while(!st.empty()){\n v.push_back(st.top());\n // st.pop();\n st.pop();\n }\n reverse(v.begin(),v.end());\n for(int i=0;i<v.size()-1;i++){\n c=c+v[i+1][0]-v[i][1]-1;\n }\n c=c+n-v[v.size()-1][1]+v[0][0]-1;\n return c;\n }\n};",
"memory": "147900"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n vector<pair<int,int>> vec;\n for(auto i:meetings){\n vec.push_back(make_pair(i[0], i[1]));\n } \n sort(vec.begin(), vec.end());\n int ans=0;\n if(vec[0].first!=1)ans+=(vec[0].first-1);\n int start=vec[0].first, end=vec[0].second;\n for(int i=1; i<vec.size(); i++){\n if(vec[i].second<end)continue;\n else{\n if(vec[i].first>end)ans+=(vec[i].first-end-1);\n end=vec[i].second;\n start=vec[i].first;\n }\n }\n if(end<days)ans+=(days-end);\n return ans;\n }\n};",
"memory": "150200"
} |
3,430 | <p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
<p><strong>Note: </strong>The meetings may overlap.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>Meetings are scheduled for all working days.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= days <= 10<sup>9</sup></code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code><font face="monospace">1 <= meetings[i][0] <= meetings[i][1] <= days</font></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint countDays(int days, vector<vector<int>>& meetings) {\n\tpriority_queue<pair<int,int>,vector<pair<int,int>>,greater<>> pq;\n\tfor(auto m:meetings){\n\t\tpq.push({m[0],m[1]});\n\t}\n\tint ans=0,e=0;\n\twhile(pq.size()){\n\t\tauto [si,ei]=pq.top();\n\t\tpq.pop();\n\t\twhile(ei<e&&pq.size()){\n\t\t\tsi=pq.top().first,ei=pq.top().second;\n\t\t\tpq.pop();\n\t\t}\n\t\tif(si>e){\n\t\t\tans+=si-e-1;\n\t\t}\n\t\te=max(e,ei);\n\t}\n\tans+=days-e;\n\treturn ans;\n}\n\n};",
"memory": "150300"
} |