id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n ios_base :: sync_with_stdio(false);\n cin.tie(nullptr);cout.tie(nullptr);\n int ans = 0;\n int start_x = -2;\n vector<int> dau_x(points.size());\n for (int i=0;i<points.size();i++) dau_x[i] = points[i][0];\n\n sort(dau_x.begin(),dau_x.end());\n\n for (int arr:dau_x){\n if (start_x<arr){\n start_x = arr+w;\n ans++;\n }\n }\n return ans;\n }\n};",
"memory": "120100"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n int n = points.size();\n vector<int> xs(n);\n for (int i=0; i<n; i++) xs[i] = points[i][0];\n sort(xs.begin(), xs.end());\n\n int r = 0;\n int right = -1;\n int y = -1;\n int t = n-1;\n while (t>=0 || y>=0)\n {\n int x = y;\n y = -1;\n if (x<0)\n {\n x = xs[t--];\n }\n\n if (right<0)\n {\n right = x;\n r++;\n }\n else\n {\n int left = right-w;\n if (x>=left) continue;\n right = -1;\n y = x;\n }\n }\n return r;\n }\n};\n\nstatic auto init = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n return 0;\n}();",
"memory": "120200"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "#pragma GCC optimize(\"O3\", \"unroll-loops\")\nclass Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n int n=points.size();\n vector<int> x(n,-1);\n for(int i=0; i<n; i++) x[i]=points[i][0];\n sort(x.begin(), x.end());\n int ans=0, k=0;\n while(k<n){\n k=upper_bound(x.begin()+k, x.end(), x[k]+w)-x.begin();\n ans++;\n } \n return ans; \n }\n};\n\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();",
"memory": "120400"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "#pragma GCC optimize(\"O3\", \"unroll-loops\")\nclass Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n int n=points.size();\n vector<int> x(n,-1);\n for(int i=0; i<n; i++) x[i]=points[i][0];\n sort(x.begin(), x.end());\n int ans=0, k=0;\n while(k<n){\n k=upper_bound(x.begin()+k, x.end(), x[k]+w)-x.begin();\n ans++;\n } \n return ans; \n }\n};\n\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();",
"memory": "120500"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n int ans = 0; \n int n = points.size(); \n vector<int> x(n, 0);\n for(int i=0; i<n; i++) x[i] = points[i][0];\n sort(x.begin(), x.end());\n\n int xx = -1; \n for(int i=0; i<n; i++){\n if(xx < x[i]){\n ans++; \n xx = x[i]+w; \n }\n } \n\n return ans; \n }\n};",
"memory": "120600"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n int n=points.size();\n vector<int> x(n,-1);\n for(int i=0; i<n; i++) x[i]=points[i][0];\n sort(x.begin(), x.end());\n int ans=0, k=0;\n while(k<n){\n k=upper_bound(x.begin()+k, x.end(), x[k]+w)-x.begin();\n ans++;\n } \n return ans; \n }\n};",
"memory": "120700"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n int lst=-1;\n int n=points.size();\n vector <pair<int,int>> a(n);\n for(int i=0;i<n;i++)\n {\n a[i].first=points[i][0];\n a[i].second=points[i][1];\n }\n sort(a.begin(),a.end());\n int ans=0;\n for(int i=0;i<n;i++)\n {\n if(a[i].first>lst)\n {\n ans++;\n lst=a[i].first+w;\n }\n }\n return ans;\n }\n};",
"memory": "122000"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n\n int n=points.size();\n\n vector<int> v;\n\n for(int i=0;i<n;i++)\n {\n v.push_back(points[i][0]);\n }\n \n sort(v.begin(),v.end());\n\n int cnt=1;\n int mn=v[0];\n for(int i=1;i<v.size();i++)\n {\n if(v[i]-mn>w)\n {\n mn=v[i];\n cnt++;\n }\n \n }\n\n return cnt;\n }\n};",
"memory": "122300"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n vector<int>x;\n for(int i=0;i<points.size();i++){\n x.push_back(points[i][0]);\n }\n sort(x.begin(),x.end());\n int cnt = 1;\n long long dis = x[0]; // Start from the first sorted x-coordinate\n\n for(int i = 1; i < x.size(); i++) { // Start from the second point\n if(x[i] - dis > w) { // Check if the current point exceeds the width of the rectangle\n cnt++;\n dis = x[i]; // Start a new rectangle\n }\n }\n\n return cnt;\n }\n};",
"memory": "122400"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n vector<int>v;\n for(int i=0;i<points.size();i++)\n {\n v.push_back(points[i][0]);\n }\n sort(v.begin(),v.end());\n int c=0;\n int i=0,j=0;\n int n=v.size();\n while(j<n)\n {\n if(v[i]+w<v[j])\n {\n c++;\n i=j;\n }\n j++;\n }\n c++;\n return c;\n }\n};",
"memory": "122500"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n vector<int>xcor;\n\n for (int i=0;i<points.size();i++){\n xcor.push_back(points[i][0]);\n }\n\n sort(xcor.begin() , xcor.end());\n\n int cnt = 0;\n int j = 0;\n while( j < xcor.size() ) {\n int fr = xcor[j] + w;\n cnt++;\n while(j<xcor.size() && xcor[j] <= fr ){\n j++;\n }\n }\n\n return cnt;\n\n }\n};",
"memory": "122600"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n int n=points.size();\n\n vector<int> x;\n\n for (int i = 0; i < n; i++)\n {\n x.push_back(points[i][0]);\n }\n \n sort(x.begin(),x.end());\n int source=0;\n int count=1;\n for (int i = 1; i < n; i++)\n {\n \n if(abs(x[source]-x[i])>w){\n // cout<<i<<\"\\n\";\n count++;\n source=i;\n }\n }\n \n\n return count;\n }\n};",
"memory": "122600"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n\n sort(points.begin(),points.end());\n\n vector<int> x;\n int count = 0;\n for(int i=0;i<points.size();i++){\n x.push_back(points[i][0]);\n }\n\n for(int i=0;i<x.size();){\n int num = x[i];\n int index = upper_bound(x.begin(),x.end(),num + w)-x.begin();\n count++;\n i = index;\n }\n\n return count;\n \n }\n};\n\n/**\n\n\n[[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]]\n\n[1,0] [1,4] [1,8] [2,1] [3,5] [4,6]\n\n\n**/",
"memory": "122700"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\n public:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n int ans = 0;\n int prevX = -w - 1;\n vector<int> xs;\n\n for (const vector<int>& point : points) {\n const int x = point[0];\n xs.push_back(x);\n }\n\n ranges::sort(xs);\n\n for (const int x : xs)\n if (x > prevX + w) {\n ++ans;\n prevX = x;\n }\n\n return ans;\n }\n};",
"memory": "122900"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n \n vector<long long>x;\n for(int i=0;i<points.size();i++){\n x.push_back(points[i][0]);\n }\n sort(x.begin() ,x.end());\n int b=INT_MIN;\n int ans=0;\n for(int i=0;i<x.size();i++){\n if(x[i] > b+w){\n ans++;\n b=x[i];\n }\n }\n \n return ans;\n \n }\n};",
"memory": "126000"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n\n for(int i = 0;i<points.size();i++){\n pq.push({points[i][0],points[i][1]});\n }\n\n int start = -w-1;\n int count = 0;\n while(!pq.empty()){\n int x = pq.top().first;\n pq.pop();\n if(x - start > w){\n start = x;\n count++;\n }\n }\n\n return count;\n }\n};",
"memory": "126100"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n int n = points.size();\n if (n == 0) return 0; // No points, no rectangles needed\n \n // Priority queue to sort points by their x-coordinate\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n \n for (int i = 0; i < n; i++) {\n pq.push({points[i][0], points[i][1]});\n }\n \n int rectangles = 0; // Number of rectangles needed\n while (!pq.empty()) {\n // Start a new rectangle\n rectangles++;\n \n // Get the leftmost point for the current rectangle\n int startX = pq.top().first;\n pq.pop();\n \n // Remove all points that can be covered by this rectangle\n while (!pq.empty() && pq.top().first - startX <= w) {\n pq.pop();\n }\n }\n \n return rectangles;\n }\n};\n",
"memory": "126200"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n int search(vector<vector<int>>& points, int l, int xMax){\n int n=points.size();\n \n int r = n-1;\n while(l<r){\n int m = l+(r-l)/2;\n \n if(points[m][0] > xMax){\n r=m;\n }else{\n l=m+1;\n }\n }\n \n return (l<n && points[l][0]<=xMax ? l+1:l);\n }\n \n int solve(vector<vector<int>>& points, int idx, int w, vector<int> &memo){\n int n= points.size();\n \n if(idx == n) return 0;\n \n if(memo[idx] != -1) return memo[idx];\n \n int c = solve(points, idx+1, w, memo);\n \n int nxt_idx = search(points, idx+1, points[idx][0]+w);\n c = min(c, solve(points, nxt_idx, w, memo));\n \n return memo[idx] = c+1;\n }\n \n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n sort(points.begin(), points.end());\n \n int n=points.size();\n vector<int> memo(n,-1);\n \n return solve(points, 0, w, memo);\n }\n};",
"memory": "128199"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n // int n = points.size();\n // map<int,int>map; // x-coordinates\n // for(int i=0;i<n;i++){\n // map[points[i][0]]++;\n // }\n // vector<int>ranges;\n // for(auto it: map){\n // ranges.push_back(it.first); \n // }\n // int prev=0;\n // int count=1;\n // for(int i=0;i<ranges.size();i++){\n // if(ranges[i]-ranges[prev] >w){\n // prev=i;\n // count++;\n // }\n\n // }\n\n // return count;\n\n sort(points.begin(), points.end());\n int ans=0;\n int last=-1;\n for(auto a: points){\n if(a[0]>last){\n ans++;\n last = a[0]+w;\n }\n }\n return ans;\n }\n};",
"memory": "132500"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n sort(points.begin(), points.end());\n int res = 1, cur = points[0][0];\n for(auto point: points) {\n if(point[0] > cur + w) {\n cur = point[0];\n ++res;\n }\n }\n return res;\n }\n};",
"memory": "132600"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n sort(points.begin(),points.end());\n int ans=0;\n int last=-1;\n for(auto x:points)\n {\n if(x[0]>last)\n {\n last=x[0]+w;\n ans++;\n }\n }\n return ans;\n }\n};",
"memory": "132700"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n sort(points.begin(),points.end());\n int ans=1,st=points[0][0];\n for(auto point:points) {\n int x = point[0];\n if(x-st>w) {\n // cout<<st<<endl;\n ans++;\n st=x;\n }\n }\n return ans;\n }\n};",
"memory": "132700"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n sort(points.begin(), points.end());\n int cnt = 1;\n int start = points[0][0];\n for(auto it: points){\n if(it[0] > start + w) {\n cnt++;\n start = it[0];\n }\n }\n return cnt;\n }\n};",
"memory": "132800"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n sort(points.begin(),points.end());\n stack<int> st;\n int ans = 0;\n int prev= -1;\n for(auto it:points){\n if(prev==-1 or (prev!=it[0] and it[0]-prev>w)){\n ans++;\n prev = it[0];\n }\n else continue;\n }\n return ans;\n }\n};",
"memory": "133800"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& a, int w) {\n int n =a.size();\n vector<int>x(n);\n int i=0;\n for(auto it:a){\n x[i]= a[i][0];\n i++;\n }\n sort(x.begin(),x.end());\n int cnt=1;\n int pre=x[0];\n for(int i=1;i<n;i++){\n if(x[i]-pre>w){\n cnt++;\n pre=x[i];\n }\n }\n return cnt;\n\n }\n};",
"memory": "134500"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "#include <algorithm>\n#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n vector<int> x_cords;\n x_cords.reserve(points.size());\n\n for (auto point : points) {\n x_cords.push_back(point[0]);\n }\n\n sort(x_cords.begin(), x_cords.end());\n\n int start = x_cords[0];\n\n int square_count = 1;\n\n for (int i = 1; i < x_cords.size(); ++i) {\n int next_cord = x_cords[i];\n\n if (w < next_cord - start) {\n start = next_cord;\n square_count += 1;\n }\n }\n\n return square_count;\n\n }\n};",
"memory": "134600"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "#include <algorithm>\n#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n vector<int> x_cords;\n x_cords.reserve(points.size());\n\n for (auto point : points) {\n x_cords.push_back(point[0]);\n }\n\n sort(x_cords.begin(), x_cords.end());\n\n int start = x_cords[0];\n\n int square_count = 1;\n\n for (int i = 1; i < x_cords.size(); ++i) {\n int next_cord = x_cords[i];\n\n if (w < next_cord - start) {\n start = next_cord;\n square_count += 1;\n }\n }\n\n return square_count;\n\n }\n};",
"memory": "134700"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& p, int w) \n {\n map<int,int>m;\n for(auto &x:p)\n {\n m[x[0]]=x[1];\n }\n int first=(m.begin()->first),count=0;\n for(auto it=m.begin();it!=m.end();it++)\n {\n int x=it->first;\n if(x-first<=w)\n {\n continue;\n }\n else\n {\n count++;\n first=it->first;\n }\n }\n count++;\n return count;\n }\n};\nauto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();",
"memory": "135300"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& p, int w) \n {\n map<int,int>m;\n for(auto &x:p)\n {\n m[x[0]]=x[1];\n }\n int first=(m.begin()->first),count=0;\n for(auto it=m.begin();it!=m.end();it++)\n {\n if(it==m.begin())\n continue;\n int x=it->first;\n if(x-first<=w)\n {\n continue;\n }\n else\n {\n count++;\n first=it->first;\n }\n }\n count++;\n return count;\n }\n};\nauto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();",
"memory": "135400"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n unordered_set<int> distinctx;\n for(int i=0;i<points.size();i++)\n distinctx.insert(points[i][0]);\n vector<int> uniquex(distinctx.begin(),distinctx.end());\n sort(uniquex.begin(),uniquex.end());\n int n=uniquex.size();\n int ans=0;\n int start=0;\n for(int i=1;i<n;i++)\n {\n if(uniquex[i]-uniquex[start]>w)\n {\n ans++;\n start=i;\n }\n }\n return ++ans;\n }\n};",
"memory": "135500"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n map<int,int> m;\n for(auto & point : points){\n m[point[0]]++;\n }\n int k = -1;\n int ans = 0;\n for(auto & val : m){\n if(val.first > k){\n ans++;\n k = val.first + w;\n }\n }\n return ans;\n }\n};",
"memory": "135800"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>> &points, int w) {\n unordered_set<int> st;\n for (const auto &i: points) {\n st.insert(i[0]);\n }\n\n vector<int> storage(st.begin(), st.end());\n std::sort(storage.begin(), storage.end());\n\n int last = -1, cnt = 0;\n for (const auto &i : storage){\n if (i > last){\n cnt++;\n last = i + w;\n }\n }\n return cnt;\n }\n};\n",
"memory": "135900"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n set<int> s;\n for (auto &p : points) {\n s.insert(p[0]);\n }\n int right = -1, ans = 0;\n for (int e : s) {\n if (right < e) {\n right = e + w;\n ++ans;\n }\n }\n return ans;\n }\n};",
"memory": "136000"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n set<int> xs;\n for (auto& point : points)\n xs.insert(point[0]);\n\n int start = -1e9 - 100, ans = 0;\n for (int x : xs) {\n if (x - start > w) {\n start = x;\n ++ans;\n }\n }\n\n return ans;\n }\n};",
"memory": "136100"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n sort(points.begin(),points.end());\n\n priority_queue<int,vector<int>,greater<int>>pq;\n\n for(auto it:points) pq.push(it[0]); int ans=0; int wt=-1;\n\n while(!pq.empty()){\n auto it=pq.top(); pq.pop();\n if(wt<it){\n wt=it+w;\n ans++;\n }\n\n }\n return ans;\n }\n};",
"memory": "136300"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n int ans=0,n=points.size();\n vector<int>v;\n for(auto x:points)\n {\n v.push_back(x[0]);\n }\n sort(v.begin(),v.end());\n for(int i=0;i<n;i++)\n {\n int val=v[i];\n auto it=upper_bound(v.begin(),v.end(),(val+w));\n if(it==v.end())\n {\n break;\n }\n int ind=(it-v.begin());\n i=(ind-1);\n ans++;\n }\n return ans+1;\n }\n};",
"memory": "136400"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n vector<int> xs;\n for(auto p : points) \n xs.push_back(p[0]);\n sort(xs.begin(), xs.end());\n\n int ans = 0;\n int last = - w - 2;\n for(int i = 0; i < size(xs); ++i) {\n if(last < xs[i]) {\n ++ans;\n last = xs[i] + w;\n }\n } \n\n return ans;\n }\n};",
"memory": "136500"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n vector<int>a ;\n int count = 1;\n for(auto x:points){\n a.push_back(x[0]);\n }\n sort(a.begin(),a.end());\n for(auto i:a) cout<<i;\n int start=a[0],i=1;\n while(i<a.size()){\n if(a[i]<=start+w){\n i++;\n }\n else{\n start = a[i];\n count++;\n }\n }\n return count;\n\n }\n};",
"memory": "136600"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n priority_queue<int, vector<int>, greater<int>> minHeap;\n for (auto p : points)\n {\n minHeap.push(p[0]);\n }\n\n int prev = -1;\n int res = 0;\n while (!minHeap.empty())\n {\n prev = minHeap.top();\n minHeap.pop();\n\n while (minHeap.top() - prev <= w && !minHeap.empty())\n {\n minHeap.pop();\n }\n \n res++;\n }\n\n return res;\n }\n};",
"memory": "136700"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n set<int> s;\n for (auto& i : points) {\n s.insert(i[0]); \n }\n vector<int> sorted_points(s.begin(), s.end()); \n sort(sorted_points.begin(), sorted_points.end());\n int ans = 1;\n int l = 0;\n for (int i = 0; i < sorted_points.size(); ++i) {\n if (sorted_points[i] - sorted_points[l] > w) {\n ans++;\n l = i;\n }\n }\n return ans;\n }\n};\n",
"memory": "136900"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n set<int> pset;\n int n = points.size(), i, res = 1, curr;\n for(i = 0; i < n; i++) pset.insert(points[i][0]);\n vector<int> pvec(pset.begin(), pset.end());\n n = pvec.size();\n curr = pvec[0];\n for(i = 1; i < n; i++) if(pvec[i] - curr > w){\n res++;\n curr = pvec[i];\n }\n return res;\n }\n};",
"memory": "137100"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n set<int> se;\n for (int i = 0; i < points.size(); i++) {\n se.insert(points[i][0]);\n }\n vector<int> a(se.begin(), se.end());\n sort(a.begin(), a.end());\n if (w == 0) return a.size();\n int c = 0;\n for (int i = 0; i < a.size(); ) {\n c++; \n int end = a[i] + w;\n while (i < a.size() && a[i] <= end) {\n i++;\n }\n }\n return c;\n \n }\n};",
"memory": "137200"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n unordered_set<int>st;\n for(int i=0;i<points.size();i++)\n {\n st.insert(points[i][0]);\n }\n vector<int>v;\n int count = 0;\n for(auto it:st)\n {\n v.push_back(it);\n }\n sort(v.begin(),v.end());\n int i=0,j=i+1;\n while(i<v.size()-1 && j<v.size())\n {\n if((v[j]-v[i])<=w)\n j++;\n else\n {\n count++;\n i = j;\n j++;\n }\n }\n count++;\n \n return count;\n }\n};",
"memory": "137300"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n set<int> s1;\n int point=points.size();\n for (int i=0;i<point;i++) {\n s1.insert(points[i][0]);\n }\n vector<int> v1;\n for (auto i:s1) v1.push_back(i);\n \n int diff = v1[0]+w;\n int count=1;\n int v1size=v1.size();\n for (int i=1;i<v1size;i++) {\n while(i<v1size&&v1[i]<=diff) {\n i++;\n }\n if (i<v1size){\n count++;\n diff=v1[i]+w;\n }\n }\n return count;\n }\n};",
"memory": "138400"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n set<int> s1;\n\n for (int i=0;i<points.size();i++) {\n s1.insert(points[i][0]);\n }\n vector<int> v1;\n for (auto i:s1) v1.push_back(i);\n \n int diff = v1[0]+w;\n int count=1;\n for (int i=1;i<v1.size();i++) {\n while(i<v1.size()&&v1[i]<=diff) {\n i++;\n }\n if (i<v1.size()){\n count++;\n diff=v1[i]+w;\n }\n }\n return count;\n }\n};",
"memory": "138500"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n vector<int> x;\n map<int,int> mp;\n for(int i=0;i<points.size();i++){\n if(!(mp[points[i][0]]>0))\n x.push_back(points[i][0]);\n mp[points[i][0]]++;\n }\n sort(x.begin(),x.end());\n int ans=0;\n for(int i=0;i<x.size();i++){\n int start=x[i];\n ans++;\n int ind=upper_bound(x.begin(),x.end(),start+w)-x.begin();\n i=ind;\n i--;\n\n }\n return ans;\n\n }\n};",
"memory": "138600"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n vector<pair<int,int>> v;\n\n for(auto it:points){\n\n v.push_back({it[0],it[1]});\n\n }\n\n sort(v.begin(),v.end());\n\n int ans=1;\n int a=83738,b=8487848,c=85748;\n\n int cur=v[0].first;\n\n for(int i=1;i<v.size();i++){\n\n if(v[i].first<=cur+w){continue;}\n\n else{\n\n ans++;\n\n cur=v[i].first;\n\n }\n\n }\n\n return ans;\n\n\n }\n};",
"memory": "139700"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n vector<pair<int, int>>vp;\n\n for(auto it: points){\n vp.push_back({it[0], it[1]});\n }\n\n sort(vp.begin(), vp.end());\n\n\n // for(auto it: vp){\n // cout << it.first << \" \" << it.second << endl;\n // }\n\n int cnt = 1;\n int currcord = vp[0].first;\n\n for(auto it: vp){\n if(it.first- currcord <= w) continue;\n\n else{\n currcord = it.first;\n cnt++;\n }\n }\n\n return cnt;\n }\n};",
"memory": "139900"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n int ans=0;\n vector<vector<int>>dummy;\n dummy=points;\n sort(dummy.begin(),dummy.end());\n for(int i=0;i<dummy.size();){\n int ele = dummy[i][0];\n int maxi=ele+w;\n ans++;\n while(i<dummy.size() && dummy[i][0]<=maxi){\n i++;\n }\n }\n return ans;\n }\n};",
"memory": "141000"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n using ll = long long;\n vector<pair<ll, ll>> arr;\n int n = points.size();\n\n for (auto it : points) {\n arr.push_back({it[0]*1ll, it[1]*1ll});\n }\n\n sort(arr.begin(), arr.end());\n\n ll st = arr[0].first;\n int cnt = 1;\n\n for (int i = 1; i < n; i++) {\n\n if (arr[i].first - st <= w*1ll)\n continue;\n else {\n cnt++;\n st = arr[i].first;\n }\n }\n\n return cnt;\n }\n};",
"memory": "146500"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "const static auto fast = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\\\n set<int> xpoints;\n for(auto p : points) xpoints.insert(p[0]);\n\n int rect = 0, idx = 0;\n while(!xpoints.empty()){\n idx = *xpoints.begin();\n for(int i = idx; i <= idx + w; i++){\n if(xpoints.size() == 0) break;\n if(xpoints.count(i)) xpoints.erase(i);\n }\n rect++;\n }\n return rect;\n }\n};",
"memory": "149100"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n set<int> setX;\n for (auto point : points) {\n setX.insert(point[0]);\n }\n int count = 0;\n while (!setX.empty()) {\n int x = *setX.begin();\n int endX = x + w;\n while (!setX.empty() && *setX.begin() <= endX) {\n setX.erase(setX.begin());\n }\n count++;\n }\n return count;\n }\n};",
"memory": "149500"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n set<int> setX;\n for (auto point : points) {\n setX.insert(point[0]);\n }\n int count = 0;\n while (!setX.empty()) {\n int x = *setX.begin();\n for (int i = 0; i <= w; i++) {\n setX.erase(x+i);\n if (setX.empty()) break;\n }\n count++;\n }\n return count;\n }\n};",
"memory": "149600"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n \n set <int> s;\n\n for(auto it: points){\n s.insert(it[0]);\n }\n\n int prev=-1;\n\n int count=0;\n\n for(auto it: s){\n // cout<<prev<<\" \"<<it<<endl;\n \n if(prev==-1){\n prev=it;\n count++;\n continue;\n }\n else if(it-prev<=w){\n // count++;\n continue;\n }\n else{\n prev=it;\n count++;\n }\n }\n\n return count;\n\n }\n};",
"memory": "149700"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n \n \n map<int,int> mp;\n \n for(auto point : points) {\n mp[point[0]]++;\n }\n \n auto it = mp.begin();\n int prev = it->first;\n \n int count = 0;\n \n for(auto it : mp) {\n if(it.first - prev > w) {\n count++;\n prev = it.first;\n }\n }\n \n return count+1;\n \n \n }\n};",
"memory": "149700"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\n /* int max_on_X(vector<vector<int>>& nums){\n int ans = 0;\n for(auto X : nums){\n ans = max(ans , X[0]);\n }\n return ans;\n } */\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>&po, int w) {\n /* int n = max_on_X(po);\n vector<vector<int>>nums(n);\n for(auto X : po){\n nums[X[0]].pu\n } */\n set<int>S;\n for(auto X : po){\n S.insert(X[0]);\n }\n int count = 0 , prev = -1;\n for(auto X : S){\n if(prev == -1){\n prev = X;\n }\n else{\n if(X == prev + w ){\n count++;\n prev =-1;\n }\n else if(X > prev + w){\n count++;\n prev = X ;\n }\n }\n cout<<prev<<\" \"<<X<<\" ==> \"<<count<<endl;\n }\n if(prev != -1)\n count++;\n return count;\n }\n};",
"memory": "149800"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n unordered_set<int>s;\n for(auto x: points){\n s.insert(x[0]);\n }\n vector<int>k(s.begin(),s.end());\n sort(k.begin(),k.end());\n int ans = 0;\n int vis = -1;\n for(auto x: k){\n if(x>vis){\n ans++;\n vis = x+w;\n }\n }\n return ans;\n }\n};",
"memory": "149900"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n set<int> s;\n for(auto p:points){\n s.insert(p[0]);\n }\n int ans=0;\n vector<int> v (s.begin(),s.end());\n int left=0;\n int right=left+1;\n\n while(right<v.size()){\n if(v[right]-v[left] <=w){\n right++;\n continue;\n }else{\n ans++;\n left=right;\n right++;\n }\n }\n return ans+1;\n }\n};",
"memory": "151200"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n unordered_map<int,int>mp;\n for(auto it : points){\n mp[it[0]]++;\n }\n vector<int>temp;\n for(auto it : mp){\n temp.push_back(it.first);\n }\n sort(temp.begin(),temp.end());\n int rect=0;\n int prevWidth=-1;\n for(int i=0;i<temp.size();i++){\n if(prevWidth<temp[i]){\n prevWidth=temp[i]+w;\n rect++;\n }\n \n }\n return rect;\n }\n};",
"memory": "151400"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n typedef pair<int,int> pr;\n void bfs(int start,vector<pair<int,int>>adj[],vector<int>&distance,vector<int>&disappear,int n) {\n priority_queue<pr,vector<pr>,greater<pr>>pq;\n pq.push({0,start});\n\n distance[start]=0;\n while(!pq.empty()) {\n auto[d,node]=pq.top();\n pq.pop();\n \n if(d > distance[node])\n continue;\n\n for(auto &it:adj[node]) {\n int child=it.first;\n int wt=it.second;\n if(d+wt < distance[child] && d+wt < disappear[child]) {\n distance[child]=d+wt;\n pq.push({distance[child],child});\n }\n }\n }\n\n for(int i=0;i<n;i++) {\n if(distance[i]==1e9)\n distance[i]=-1;\n }\n }\n\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int,int>>adj[n];\n for(auto &it:edges) {\n \n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n \n } \n\n vector<int>distance(n,1e9);\n bfs(0,adj,distance,disappear,n);\n\n return distance;\n }\n};",
"memory": "207775"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n typedef pair<int,int> pr;\n void bfs(int start,vector<pair<int,int>>adj[],vector<int>&distance,vector<int>&disappear,int n) {\n priority_queue<pr,vector<pr>,greater<pr>>pq;\n pq.push({0,start});\n\n distance[start]=0;\n while(!pq.empty()) {\n auto[d,node]=pq.top();\n pq.pop();\n \n if(d > distance[node])\n continue;\n\n for(auto &it:adj[node]) {\n int child=it.first;\n int wt=it.second;\n if(d+wt < distance[child] && d+wt < disappear[child]) {\n distance[child]=d+wt;\n pq.push({distance[child],child});\n }\n }\n }\n\n for(int i=0;i<n;i++) {\n if(distance[i]==1e9)\n distance[i]=-1;\n }\n }\n\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int,int>>adj[n];\n for(auto &it:edges) {\n \n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n \n } \n\n vector<int>distance(n,1e9);\n bfs(0,adj,distance,disappear,n);\n\n return distance;\n }\n};",
"memory": "207775"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "using P2I = pair<int, int>;\nclass Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<P2I> g[n];\n for(auto& edge: edges){\n int x = edge[0];\n int y = edge[1];\n int cost = edge[2];\n g[x].push_back({y, cost});\n g[y].push_back({x, cost});\n }\n\n vector<int> dist(n, INT_MAX);\n int time = 0;\n priority_queue<P2I,vector<P2I>,greater<>> pq;\n pq.push({0, 0});\n while(!pq.empty()){\n auto [cur_cost, cur] = pq.top(); pq.pop();\n if(cur_cost>=dist[cur]) continue;\n\n dist[cur] = cur_cost;\n for(auto& [neighbor, cost]: g[cur]){\n int next_cost = cur_cost + cost;\n if(next_cost>=dist[neighbor] || next_cost>=disappear[neighbor]) continue;\n\n pq.push({next_cost, neighbor});\n }\n }\n for(int i=0; i<n; i++) if(dist[i]==INT_MAX) dist[i] = -1; \n return dist;\n }\n};",
"memory": "209326"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int,int>> adj[n];\n for(int i=0;i<edges.size();i++){\n adj[edges[i][0]].push_back({edges[i][1],edges[i][2]});\n adj[edges[i][1]].push_back({edges[i][0],edges[i][2]});\n }\n\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n vector<int> dist(n,-1);\n pq.push({0,0});\n \n while(!pq.empty()){\n int distance = pq.top().first;\n int node = pq.top().second;\n cout<<node<<\" \"<<distance<<endl;\n pq.pop();\n if(dist[node]!=-1) continue;\n dist[node]=distance;\n \n for(auto& it: adj[node]){\n int n = it.first;\n int d = it.second;\n \n if(dist[n]==-1 && disappear[n]>d+distance){\n \n pq.push({d+distance,n});\n }\n }\n \n }\n \n \n return dist;\n \n }\n};",
"memory": "209326"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pair<int,int>>>adj(n);\n for(int i=0;i<edges.size();i++){\n int u=edges[i][0];\n int v=edges[i][1];\n int wt=edges[i][2];\n adj[u].push_back({v,wt});\n adj[v].push_back({u,wt});\n }\n queue<pair<int,int>>q;\n q.push({0,0});\n vector<int>dist(n,1e9);\n dist[0]=0;\n while(!q.empty()){\n int node=q.front().first;\n int distance=q.front().second;\n q.pop();\n if(distance>dist[node]) continue;\n for(auto it:adj[node]){\n int adjnode=it.first;\n int wt=it.second;\n if(dist[adjnode]>distance+wt && distance+wt<disappear[adjnode]){\n dist[adjnode]=distance+wt;\n q.push({adjnode,distance+wt});\n }\n }\n }\n for(int i=0;i<n;i++){\n if(dist[i]==1e9)\n dist[i]=-1;\n }\n return dist;\n }\n};",
"memory": "210878"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n std::vector<std::vector<std::pair<int, int>>> adjs(n);\n for (auto& edge : edges) {\n auto u = edge[0];\n auto v = edge[1];\n auto w = edge[2];\n adjs[u].emplace_back(w, v);\n adjs[v].emplace_back(w, u);\n }\n\n std::vector<int> dist(n, INT_MAX);\n std::vector<bool> visited(n);\n dist[0] = 0;\n std::priority_queue<std::pair<int, int>> pq;\n pq.emplace(0, 0);\n\n while (!pq.empty()) {\n auto [d, u] = pq.top();\n d = -d;\n pq.pop();\n if (visited[u]) {\n continue;\n }\n visited[u] = true;\n for (auto [w, v] : adjs[u]) {\n int nd = d + w;\n if (nd < disappear[v] && nd < dist[v]) {\n dist[v] = nd;\n pq.emplace(-nd, v);\n }\n }\n }\n\n for (auto& d : dist) {\n if (d == INT_MAX) {\n d = -1;\n }\n }\n\n return dist;\n }\n};",
"memory": "210878"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "// Time: O((|E| + |V|) * log|V|) = O(|E| * log|V|) by using binary heap,\n// if we can further to use Fibonacci heap, it would be O(|E| + |V| * log|V|)\n// Space: O(|E| + |V|) = O(|E|)\n\n// dijkstra's algorithm\nclass Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n static const int INF = numeric_limits<int>::max();\n \n vector<vector<pair<int, int>>> adj(n);\n for (const auto& e : edges) {\n adj[e[0]].emplace_back(e[1], e[2]);\n adj[e[1]].emplace_back(e[0], e[2]);\n }\n const auto& modified_dijkstra = [&](int start) {\n vector<int> best(n, -1);\n best[start] = 0;\n priority_queue<pair<int64_t, int>, vector<pair<int64_t, int>>, greater<pair<int64_t, int>>> min_heap;\n min_heap.emplace(best[start], start);\n while (!empty(min_heap)) {\n const auto [curr, u] = min_heap.top(); min_heap.pop();\n if (curr != best[u]) {\n continue;\n }\n for (auto [v, w] : adj[u]) {\n if (!(w < min(best[v] != -1 ? best[v] : INF, disappear[v]) - curr)) { // modified\n continue;\n }\n best[v] = curr + w;\n min_heap.emplace(best[v], v);\n }\n }\n return best;\n };\n \n return modified_dijkstra(0);\n }\n};",
"memory": "212429"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<int> dist(n, INT_MAX);\n dist[0] = 0;\n\n vector<vector<pair<int,int>>> adj_list(n);\n \n for(int i=0; i<edges.size(); i++) {\n adj_list[edges[i][0]].push_back({edges[i][1], edges[i][2]});\n adj_list[edges[i][1]].push_back({edges[i][0], edges[i][2]});\n }\n\n priority_queue<pair<int, int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n\n pq.push({0,0});\n\n while(!pq.empty()) {\n pair<int, int> curr = pq.top();\n pq.pop();\n\n if(curr.first > dist[curr.second] || curr.first >= disappear[curr.second]) {\n continue;\n }\n for(auto next_node_pair: adj_list[curr.second]) {\n int next_node = next_node_pair.first;\n int wt = next_node_pair.second;\n\n if(wt + curr.first >= disappear[next_node]) {\n continue;\n }\n\n if(dist[next_node] > wt + curr.first) {\n dist[next_node] = wt + curr.first;\n cout<<next_node<<\" \"<<dist[next_node]<<endl;\n pq.push({dist[next_node], next_node});\n }\n }\n }\n\n vector<int> ans;\n\n for(int i=0; i<n; i++) {\n if(dist[i] >= disappear[i]) {\n ans.push_back(-1);\n } else {\n ans.push_back(dist[i]);\n }\n }\n\n return ans;\n }\n};",
"memory": "212429"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges,\n vector<int>& disappear) {\n vector<vector<pair<int, int>>> g(n);\n\n for (auto& it : edges) {\n int u = it[0], v = it[1], wt = it[2];\n g[u].emplace_back(v, wt);\n g[v].emplace_back(u, wt);\n }\n\n const long long INF = 1e10;\n vector<long long> dist(n, INF);\n dist[0] = 0;\n\n priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;\n pq.emplace(0, 0);\n\n while (!pq.empty()) {\n auto [wt, u] = pq.top();\n pq.pop();\n\n if (wt >= disappear[u]) continue;\n\n if (wt > dist[u]) continue;\n\n for (auto& [v, vwt] : g[u]) {\n if (wt + vwt < dist[v] && wt + vwt < disappear[v]) {\n dist[v] = wt + vwt;\n pq.emplace(dist[v], v);\n }\n }\n }\n\n vector<int> result(n, -1);\n for (int i = 0; i < n; i++) {\n if (dist[i] < disappear[i]) {\n result[i] = dist[i];\n }\n }\n\n return result;\n }\n};\n",
"memory": "213980"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "/*\nclass Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pair<int, int>>> graph(n);\n for(int i = 0; i < edges.size(); i++){\n int first = edges[i][0];\n int second = edges[i][1];\n int weight = edges[i][2];\n graph[first].push_back(make_pair(second, weight));\n graph[second].push_back(make_pair(first, weight));\n }\n return dikstra(graph, disappear, n);\n }\n vector<int> dikstra( vector<vector<pair<int, int>>>& graph, vector<int>& disappear, int n){\n priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;\n vector<long long> distance(n, LLONG_MAX);\n vector<bool> visited(n, false);\n distance[0] = 0;\n pq.push(make_pair(0, 0));\n while(!pq.empty()){\n int weight = pq.top().first;\n int node = pq.top().second;\n visited[node] = true;\n for(int i = 0; i < graph[node].size(); i++){\n pair<int, int> child = graph[node][i];\n if((weight + child.second < distance[child.first]) && disappear[child.first] > (weight + child.second)) distance[child.first] = weight + child.second;\n if(visited[child.first] == false) pq.push(make_pair(distance[child.first], child.first));\n }\n }\n for(int i = 0; i < n; i++){\n if(distance[i] == INT_MAX) distance[i] = -1;\n }\n return distance;\n }\n};\n*/\nclass Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pair<int, int>>> graph(n);\n for(int i = 0; i < edges.size(); i++){\n int first = edges[i][0];\n int second = edges[i][1];\n int weight = edges[i][2];\n graph[first].push_back(make_pair(second, weight));\n graph[second].push_back(make_pair(first, weight));\n }\n return dijkstra(graph, disappear, n);\n }\n\n vector<int> dijkstra(vector<vector<pair<int, int>>>& graph, vector<int>& disappear, int n){\n priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;\n vector<long long> distance(n, LLONG_MAX);\n vector<bool> visited(n, false);\n distance[0] = 0;\n pq.push(make_pair(0, 0));\n \n while(!pq.empty()){\n long long weight = pq.top().first;\n int node = pq.top().second;\n pq.pop();\n \n if (visited[node]) continue;\n visited[node] = true;\n \n for(auto& child : graph[node]){\n int nextNode = child.first;\n long long nextWeight = child.second;\n \n if((weight + nextWeight < distance[nextNode]) && disappear[nextNode] > (weight + nextWeight)) {\n distance[nextNode] = weight + nextWeight;\n pq.push(make_pair(distance[nextNode], nextNode));\n }\n }\n }\n\n vector<int> result(n);\n for(int i = 0; i < n; i++){\n if(distance[i] == LLONG_MAX) \n result[i] = -1;\n else \n result[i] = distance[i];\n }\n return result;\n }\n};\n",
"memory": "213980"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<int> ans(n, INT_MAX);\n vector<pair<int, int>> graph[n];\n for (auto& e : edges) {\n graph[e[0]].push_back({e[1], e[2]});\n graph[e[1]].push_back({e[0], e[2]});\n }\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n pq.push({0, 0});\n while (!pq.empty()) {\n auto[time, node] = pq.top(); pq.pop();\n //cout << time << ' ' << node << endl;\n if (ans[node] == -1 || ans[node] != INT_MAX) continue;\n if (time >= disappear[node]) {\n ans[node] = -1;\n continue;\n }\n ans[node] = time;\n for (auto[next, cost] : graph[node]) {\n auto time2 = time + cost;\n pq.push({time2, next});\n }\n }\n for (int i = 0; i < n; ++i) if (ans[i] == INT_MAX) ans[i] = -1;\n return ans;\n }\n};",
"memory": "215531"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n void dfs(int node, vector<int>&vis, vector<int> &dis,vector<vector<pair<int,int>>> &adj,vector<int>& disappear){\n\n for(int i=0;i<adj[node].size();i++){\n int next_node = adj[node][i].first;\n int new_dis = dis[node] + adj[node][i].second;\n //cout<<new_dis<<next_node<<endl;\n if(((dis[next_node] >= new_dis) || (dis[next_node]==-1)) && (new_dis < disappear[next_node])){\n dis[next_node]= new_dis;\n dfs(next_node, vis,dis, adj, disappear);\n }\n }\n\n }\n\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pair<int,int>>> adj(n);\n for(int i=0;i<edges.size();i++){\n adj[edges[i][0]].push_back({edges[i][1],edges[i][2]});\n adj[edges[i][1]].push_back({edges[i][0],edges[i][2]});\n }\n\n vector<int> dis(n,-1);\n\n dis[0]=0; // starting node\n\n vector<int> vis(n,0);\n\n dfs(0,vis,dis,adj,disappear);\n\n return dis;\n\n }\n};",
"memory": "215531"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int,int>>ed[n+1];\n for(int i = 0 ; i < edges.size(); i++){\n ed[edges[i][0]].push_back({edges[i][1],edges[i][2]});\n ed[edges[i][1]].push_back({edges[i][0],edges[i][2]});\n }\n vector<int>dist(n,INT_MAX);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>mset;\n unordered_map<int,int>umap;\n mset.push({0,0});\n dist[0] = 0;\n\n while(mset.size() > 0){\n int dis = mset.top().first;\n int node = mset.top().second;\n mset.pop();\n \n if(umap.find(node) != umap.end())continue;\n umap[node]++;\n\n for(auto x : ed[node]){\n int nnode = x.first;\n int ndis = x.second;\n if((dist[nnode] > dis + ndis) && (dis+ndis) < disappear[nnode]){\n dist[nnode] = dis+ndis;\n mset.push({dist[nnode],nnode});\n }\n }\n }\n \n for(int i = 0 ; i < n; i++){\n if(dist[i] == INT_MAX)dist[i] = -1;\n }\n return dist;\n }\n};",
"memory": "217083"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<int>dist(n, INT_MAX);\n vector<pair<int, int>>adj[n];\n for(int i=0;i<edges.size();i+=1){\n adj[edges[i][0]].push_back({edges[i][1], edges[i][2]});\n adj[edges[i][1]].push_back({edges[i][0], edges[i][2]});\n }\n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq;\n unordered_set<int>s;\n pq.push({0, 0});\n dist[0] = 0;\n while(!pq.empty()){\n int v = pq.top().second;\n int d = pq.top().first;\n pq.pop();\n if(s.find(v) != s.end()) continue;\n s.insert(v);\n for(auto it: adj[v]){\n int u = it.first;\n int wt = it.second;\n if(dist[u] > d + wt && disappear[u] > d + wt){\n dist[u] = d + wt;\n pq.push({dist[u], u});\n }\n }\n }\n\n for(auto &i: dist){\n if(i == INT_MAX) i = -1;\n }\n\n return dist;\n }\n};",
"memory": "217083"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n static bool cmp(vector<int>& a,vector<int>& b)\n {\n return a[2]<b[2];\n }\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<int> ans(n,1e9);\n ans[0]=0;\n vector<vector<pair<int,int>>> v(n);\n for(auto &x :edges){\n v[x[0]].push_back({x[1],x[2]});\n v[x[1]].push_back({x[0],x[2]});\n }\n priority_queue<pair<int,int>> q;\n q.push({0,0});\n set<int> Set;\n while(!q.empty()){\n auto x=q.top();\n q.pop();\n if(Set.find({x.second})!=Set.end())\n continue;\n Set.insert(x.second);\n for(auto &y : v[x.second])\n {\n int new_dist = y.second-x.first;\n if(ans[y.first]>new_dist && new_dist<disappear[y.first])\n {\n ans[y.first]=new_dist;\n q.push({-new_dist,y.first});\n }\n }\n }\n for(int i=0;i<n;i++)\n if(ans[i]==1e9)\n ans[i]=-1;\n return ans;\n }\n};",
"memory": "218634"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "//vector<int> dist(50002 + 2, INT_MAX);\nint dist[50002];\n\nvoid bfs(int i, int n, vector<vector<pair<int, pair<int, int>>>>& g) {\n\tpriority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;\n\tq.push({ 0, i }); // dist, vert\n\tdist[i] = 0;\n vector<bool> vis(n + 1, 0);\n\twhile (!q.empty()) {\n\t\tauto u = q.top();\n\t\tq.pop();\n if (vis[u.second]) continue;\n vis[u.second] = 1;\n\n\t\tfor (auto& v : g[u.second]) {\n\t\t\tint to = v.first;\n\t\t\tint len = v.second.first; // !!\n\n //cout << \"travel: \" << dist[u.second] + len << ' ' << dist[to] << '\\n';\n\t\t\tif (dist[u.second] + len < dist[to] && dist[u.second] + len < v.second.second) {\n\t\t\t\tdist[to] = dist[u.second] + len;\n\t\t\t\tq.push({ dist[to], to });\n\t\t\t}\n\t\t}\n\t}\n}\n\nclass Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& e, vector<int>& d) {\n vector<vector<pair<int, pair<int, int>>>> g(n + 1);\n for (int i = 0; i < e.size(); i++) {\n int x = e[i][0], y = e[i][1], z = e[i][2];\n g[x + 1].push_back({ y + 1, { z, d[y] } });\n g[y + 1].push_back({ x + 1, { z, d[x] } });\n }\n\n for (int i = 1; i <= 50001; i++) dist[i] = INT_MAX;\n bfs(1, n, g);\n\n vector<int> ans;\n for (int i = 1; i <= n; i++)\n ans.push_back(dist[i] == INT_MAX ? -1 : dist[i]);\n\n return ans;\n }\n};",
"memory": "220185"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& a , vector<int>& dis) {\n vector<int> d(n,INT_MAX);\n vector<vector<pair<int,int>>> adj(n);\n \n for(auto& x:a)\n {\n adj[x[0]].push_back({x[1],x[2]});\n adj[x[1]].push_back({x[0],x[2]});\n }\n set<pair<int,int>> q;\n d[0]=0;\n q.insert({0,0});\n while(!q.empty())\n {\n int node=q.begin()->second;\n q.erase(q.begin());\n for(auto& x:adj[node])\n {\n int to=x.first;\n int len=x.second;\n if(d[node]+len<d[to] && d[node]+len<dis[to])\n {\n q.erase({d[to],to});\n d[to]=d[node]+len;\n q.insert({d[to],to});\n }\n }\n }\n\n for(int i=0;i<n;i++)\n if(d[i]==INT_MAX)\n d[i]=-1;\n \n return d;\n\n \n }\n};",
"memory": "220185"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n #define P pair<int,int>\n\n vector<int> findResultCity(int n, vector<int>& SPM, vector<int>& disappear) {\n vector<int> result(n, -1);\n for (int i = 0; i < n; i++) {\n if (SPM[i] < disappear[i]) {\n result[i] = SPM[i];\n } else {\n result[i] = -1;\n }\n }\n return result;\n }\n\n void dijkstra(int n, unordered_map<int, vector<P>>& adj, vector<int>& result, int S, vector<int>& disappear) {\n priority_queue<P, vector<P>, greater<P>> pq;\n pq.push({0, S});\n fill(result.begin(), result.end(), INT_MAX);\n result[S] = 0;\n\n while (!pq.empty()) {\n int d = pq.top().first;\n int node = pq.top().second;\n pq.pop();\n\n if (d > result[node]) continue; // Skip outdated entries\n\n for (auto& p : adj[node]) {\n int adjNode = p.first;\n int dist = p.second;\n\n if (d + dist < result[adjNode] && d + dist < disappear[adjNode]) {\n result[adjNode] = d + dist;\n pq.push({d + dist, adjNode});\n }\n }\n }\n }\n\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<int> SPM(n, INT_MAX);\n SPM[0] = 0;\n\n unordered_map<int, vector<P>> adj;\n for (auto& edge : edges) {\n int u = edge[0];\n int v = edge[1];\n int wt = edge[2];\n adj[u].push_back({v, wt});\n adj[v].push_back({u, wt});\n }\n\n dijkstra(n, adj, SPM, 0, disappear);\n return findResultCity(n, SPM, disappear);\n }\n};\n",
"memory": "221736"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<long long>d(n,INT_MAX);\n vector<int>ans;\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;\n q.push({0,0});\n d[0]=0;\n unordered_set<int>s;\n vector<vector<pair<int,int>>>v(n);\n for(int i=0;i<edges.size();i++){\n v[edges[i][0]].push_back({edges[i][1],edges[i][2]});\n v[edges[i][1]].push_back({edges[i][0],edges[i][2]});\n }\n while(q.size()){\n int i=q.top().second;\n q.pop();\n if(s.find(i)!=s.end())continue;\n s.insert(i);\n for(auto it:v[i]){\n if(d[it.first]>d[i]+it.second && disappear[it.first]>d[i]+it.second){\n d[it.first]=d[i]+it.second;\n q.push({d[it.first],it.first});\n }\n }\n }\n for(int i=0;i<n;i++){\n if(d[i]==INT_MAX)ans.push_back(-1);\n else ans.push_back(d[i]);\n }\n return ans;\n }\n};",
"memory": "221736"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "#define pp pair<int,int>\n#define ll long long\nclass Solution {\npublic:\n vector<int>vis; vector<vector<ll>>info; vector<int>dis;\n priority_queue<pp,vector<pp>,greater<pp>>pq;\n void djisktra(vector<vector<pp>>&graph,int src)\n {\n pq.push({0,0}); info[0][0] = info[0][1] = 0;\n while(!pq.empty())\n {\n int node = pq.top().second;\n int len = pq.top().first; pq.pop();\n if(vis[node]) continue;\n if(dis[node]<=len){\n info[node][0] = INT_MAX; continue;\n }\n vis[node] = 1;\n\n for(auto i:graph[node])\n {\n int ngh = i.first;\n int ngh_len = i.second;\n if(len+ngh_len<info[ngh][0] and ngh!=info[node][1])\n {\n info[ngh][0] = len + ngh_len;\n info[ngh][1] = node;\n pq.push({info[ngh][0],ngh});\n }\n }\n }\n }\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pp>>graph(n,vector<pp>());\n vis.resize(n,0); dis = disappear;\n info.resize(n,vector<ll>(2,INT_MAX));\n\n for(int i=0;i<edges.size();i++){\n graph[edges[i][0]].push_back({edges[i][1],edges[i][2]});\n graph[edges[i][1]].push_back({edges[i][0],edges[i][2]});\n }\n\n djisktra(graph,0); vector<int>ans(n,0);\n for(int i=1;i<n;i++){\n ans[i] = (info[i][0]>=INT_MAX ? -1 : info[i][0]);\n }\n return ans;\n }\n};",
"memory": "223288"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "#include <unordered_map>\n#include <queue>\n\nstruct Solution {\n using pr = pair<int, int>;\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<int> res(n, -1);\n unordered_map<int, vector<pr>> edgeList;\n for (auto &e : edges){\n edgeList[e[0]].push_back({e[1], e[2]});\n edgeList[e[1]].push_back({e[0], e[2]});\n }\n priority_queue<pr> h;\n h.push({0, 0});\n while (!h.empty()){\n int t = h.top().first, node = h.top().second;\n h.pop();\n if (res[node] != -1){\n continue;\n }\n res[node] = -t;\n for (auto &p : edgeList[node]){\n if (res[p.first] == -1 && disappear[p.first] > p.second - t){\n h.push({t - p.second, p.first});\n }\n }\n }\n return res;\n }\n};",
"memory": "223288"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int, int>> adj[n];\n for(auto e: edges)\n {\n adj[e[0]].push_back({e[1], e[2]});\n adj[e[1]].push_back({e[0], e[2]});\n }\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n vector<int> dist(n, 2e9);\n vector<bool> visited(n, 0);\n dist[0] = 0;\n pq.push({0, 0});\n while(!pq.empty())\n {\n auto u = pq.top();\n pq.pop();\n int d = u.first;\n int a = u.second;\n if(d>=disappear[a] || visited[a])\n {\n continue;\n }\n visited[a] = 1;\n for(auto &v: adj[u.second])\n {\n int b = v.first;\n int w = v.second;\n if(dist[b]>dist[a]+w && !visited[b])\n {\n dist[b] = d+w;\n pq.push({d+w, b});\n }\n }\n }\n for(int i=0; i<n; i++)\n {\n if(dist[i]>=disappear[i])\n {\n dist[i] = -1;\n }\n }\n return dist;\n }\n};",
"memory": "224839"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n\n vector<vector<pair<int, int>>> adjlist(n);\n\n for (auto& vec : edges)\n {\n int u = vec[0];\n int v = vec[1];\n int w = vec[2];\n\n adjlist[u].emplace_back(w,v);\n adjlist[v].emplace_back(w,u);\n\n }\n\n vector<int> dist(n, INT_MAX);\n\n dist[0] = 0;\n set<pair<int, int>> s;\n s.insert(make_pair(0, 0));\n\n while (!s.empty())\n {\n auto cur = *s.begin();\n s.erase(cur);\n for (auto n : adjlist[cur.second])\n {\n int source_dis = dist[cur.second] + n.first;\n\n if ( dist[cur.second] < disappear[cur.second] && source_dis < dist[n.second])\n {\n s.erase(make_pair(dist[n.second],n.second));\n dist[n.second] = source_dis;\n s.emplace(dist[n.second],n.second); \n }\n }\n }\n\n vector<int> res(n,-1);\n\n for(int i=0;i<n;i++)\n {\n if (dist[i] != INT_MAX && dist[i]<disappear[i])\n res[i] = dist[i];\n }\n\n return res;\n\n }\n};",
"memory": "224839"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int, int>>adj[n];\n for(auto it: edges){\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n vector<int>v(n, 1e9), vis(n, 0);\n unordered_set<int>s;\n v[0]=0;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>q;\n q.push({0, 0});\n while(!q.empty()){\n int node=q.top().second, dis=q.top().first;\n q.pop();\n if(vis[node])continue;\n vis[node]=1;\n v[node]=dis;\n for(auto it: adj[node]){\n if(disappear[it.first]>it.second+dis && v[it.first]>it.second+dis){\n q.push({it.second+dis, it.first});\n }\n }\n }\n for(int i=0; i<n; i++){\n if(v[i]==1e9)v[i]=-1;\n }\n return v;\n }\n};",
"memory": "226390"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n unordered_map<int, vector<pair<int, int>>> graph;\n for (const auto& edge : edges) {\n int u = edge[0], v = edge[1], length = edge[2];\n graph[u].push_back({v, length});\n graph[v].push_back({u, length});\n }\n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; \n pq.push({0, 0});\n\n vector<int> minTime(n, -1);\n\n while (!pq.empty()) {\n auto [time, node] = pq.top(); pq.pop();\n if (minTime[node] != -1) continue; \n minTime[node] = time; \n\n for (const auto& [neighbor, length] : graph[node]) {\n int nextTime = time + length;\n if (nextTime < disappear[neighbor]) { \n pq.push({nextTime, neighbor});\n }\n }\n }\n \n return minTime;\n }\n};",
"memory": "226390"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pair<int,int>>>adj(n);\n for(auto edge:edges)\n {\n int u=edge[0];\n int v=edge[1];\n int len=edge[2];\n adj[u].push_back({v,len});\n adj[v].push_back({u,len});\n }\n vector<int>ans(n,-1);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n pq.push({0,0});\n while(!pq.empty())\n {\n auto it=pq.top();\n int time=it.first;\n int node=it.second;\n pq.pop();\n if(ans[node]!=-1 && ans[node]<=time) continue;\n ans[node]=time;\n for(auto i:adj[node])\n {\n int node=i.first;\n int nt=time+i.second;\n if(nt>=disappear[node]) continue;\n if(ans[node]==-1 || nt<ans[node])\n {\n pq.push({nt,node});\n }\n }\n }\n return ans;\n }\n};",
"memory": "227941"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pair<int, int>>>adj(n);\n for(auto it:edges){\n int a=it[0];\n int b=it[1];\n int wt=it[2];\n adj[a].push_back({b, wt});\n adj[b].push_back({a, wt});\n }\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq;\n vector<int>dist(n, INT_MAX), vis(n, 0);\n pq.push({0, 0});\n dist[0]=0;\n while(!pq.empty()){\n auto it=pq.top();\n pq.pop();\n int dis=it.first;\n int node=it.second;\n if(vis[node])\n continue;\n\n if(dis>=disappear[node]) continue; \n\n vis[node]=1;\n\n for(auto nbr:adj[node]){\n int next=nbr.first;\n int weight=nbr.second;\n if(dist[next]> dis + weight and dis + weight<=disappear[next]){\n dist[next]= dis + weight;\n pq.push({dist[next], next});\n }\n }\n }\n vector<int>ans(n);\n for(int i=0; i<n; i++){\n if(dist[i]>=disappear[i] or dist[i]==INT_MAX){\n dist[i]=-1;\n }\n }\n return dist;\n }\n};",
"memory": "227941"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<vector<pair<int, int>>> adj(n); // adj[i] = {length, next}\n for (auto edge: edges) {\n int u = edge[0], v = edge[1], length = edge[2];\n adj[u].push_back({length, v});\n adj[v].push_back({length, u});\n }\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;\n vector<int> times(n, INT32_MAX);\n pq.push({0, 0});\n while (!pq.empty()) {\n auto [time, cur] = pq.top();\n pq.pop();\n if (time >= disappear[cur]) continue;\n if (times[cur] != INT32_MAX) continue;\n times[cur] = time;\n for (auto nxt: adj[cur]) {\n auto [nxtLength, next] = nxt;\n if (times[next] != INT32_MAX) continue;\n pq.push({time + nxtLength, next});\n }\n }\n for (int i = 0; i < n; i++) {\n if (times[i] == INT32_MAX) times[i] = -1;\n }\n return times;\n }\n};",
"memory": "229493"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n \n vector<int> v;\n \n void dfs(int s, vector<vector<pair<int, int>>> &adj, vector<int> &dis)\n {\n for(auto x: adj[s])\n {\n if((long)v[s]+x.second<dis[x.first] && (long)v[s]+x.second<v[x.first])\n {\n v[x.first]=v[s]+x.second;\n dfs(x.first, adj, dis);\n }\n }\n }\n \n void dijkshtra(vector<vector<pair<int,int>>> &adj, vector<bool> &vis, vector<int> &dis, int s)\n {\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n pq.push({0, s});\n while(!pq.empty())\n {\n pair<int,int> p=pq.top();\n pq.pop();\n if(vis[p.second]) // mandatory condition, removing this is giving tle because the heap can have multiple instances of that node with different distance values\n continue;\n vis[p.second]=true;\n for(auto x: adj[p.second])\n {\n if(!vis[x.first] && (p.first+x.second<v[x.first]) && (p.first+x.second<dis[x.first]))\n {\n v[x.first]=p.first+x.second;\n pq.push({v[x.first], x.first});\n }\n }\n }\n return;\n }\n \n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n v = vector<int>(n, INT_MAX);\n vector<vector<pair<int,int>>> adj(n);\n for(auto x: edges)\n {\n adj[x[0]].push_back({x[1],x[2]});\n adj[x[1]].push_back({x[0],x[2]});\n }\n vector<bool>vis(n,false);\n // dijkshtra(adj, vis, disappear, 0);\n \n v[0]=0;\n dfs(0, adj, disappear); //use dijkshtra or this\n for(int i=0;i<n;i++)\n if(v[i]>=disappear[i])\n v[i]=-1;\n return v;\n }\n};",
"memory": "229493"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "#pragma GCC optimize(\"03\")\nclass Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int, int>> adj[50000];\n\n int szEdges = edges.size();\n {\n int cnt[50000] = {0};\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n cnt[u]++; cnt[v]++;\n }\n for (int i = 0; i < n; i++) \n adj[i].reserve(cnt[i]);\n }\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n int p = edges[i][2];\n adj[u].push_back({p, v});\n adj[v].push_back({p, u});\n }\n for (int i = 0; i < n; i++) \n sort(adj[i].begin(), adj[i].end());\n \n int dp[50000]; dp[0] = 0;\n for (int i = 1; i < n; i++) dp[i] = INT_MAX;\n\n priority_queue<pair<int, int>> heap;\n heap.push({0, 0});\n while (!heap.empty())\n {\n int u = heap.top().second;\n int cur = -heap.top().first;\n heap.pop();\n\n if (cur > dp[u]) continue;\n if (dp[u] >= disappear[u]) continue;\n\n int l = adj[u].size();\n for (int i = 0; i < l; i++)\n {\n int v = adj[u][i].second;\n int p = adj[u][i].first;\n int update = dp[u] + p;\n if (update >= dp[v]) continue;\n dp[v] = update;\n heap.push({-dp[v], v});\n }\n }\n\n vector<int> ans(n);\n for (int i = 0; i < n; i++) \n {\n bool ch = dp[i] == INT_MAX || dp[i] >= disappear[i];\n ans[i] = ch ? -1 : dp[i];\n }\n return ans;\n }\n};",
"memory": "231044"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int, int>> adj[50000];\n\n int szEdges = edges.size();\n {\n int cnt[50000] = {0};\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n cnt[u]++; cnt[v]++;\n }\n for (int i = 0; i < n; i++) \n adj[i].reserve(cnt[i]);\n }\n for (int i = 0; i < szEdges; i++)\n {\n int u = edges[i][0];\n int v = edges[i][1];\n int p = edges[i][2];\n adj[u].push_back({v, p});\n adj[v].push_back({u, p});\n }\n \n int dp[50000]; dp[0] = 0;\n for (int i = 1; i < n; i++) dp[i] = INT_MAX;\n\n priority_queue<pair<int, int>> heap;\n heap.push({0, 0});\n while (!heap.empty())\n {\n int u = heap.top().second;\n int cur = -heap.top().first;\n heap.pop();\n\n if (cur > dp[u]) continue;\n if (dp[u] >= disappear[u]) continue;\n\n int l = adj[u].size();\n for (int i = 0; i < l; i++)\n {\n int v = adj[u][i].first;\n int p = adj[u][i].second;\n int update = dp[u] + p;\n if (update >= dp[v]) continue;\n dp[v] = update;\n heap.push({-dp[v], v});\n }\n }\n\n vector<int> ans(n);\n for (int i = 0; i < n; i++) \n {\n bool ch = dp[i] == INT_MAX || dp[i] >= disappear[i];\n ans[i] = ch ? -1 : dp[i];\n }\n return ans;\n }\n};",
"memory": "231044"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int,int>> adj[n];\n \n for(auto it:edges){\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n }\n \n vector<int> dist(n,INT_MAX);\n \n set<pair<int,int>> st;\n \n dist[0]=0;\n st.insert({0,0});\n \n while(!st.empty()){\n auto [d,u]=*st.begin();\n st.erase(st.begin());\n \n for(auto [v,w]:adj[u]){\n \n if(d+w<dist[v] && d+w<disappear[v]){\n st.erase({dist[v],v});\n dist[v]=d+w;\n st.insert({dist[v],v});\n }\n } \n }\n \n for(auto &it:dist){\n if(it==INT_MAX){\n it=-1;\n }\n }\n \n return dist; \n \n }\n};",
"memory": "232595"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& dis) {\n vector<int> dist(n,1e9);\n vector<pair<int,int>> adj[n];\n for(auto x:edges){\n adj[x[0]].push_back({x[1],x[2]});\n adj[x[1]].push_back({x[0],x[2]});\n }\n dist[0]=0;\n set<pair<int,int>> s1;\n s1.insert({0,0});\n while(!s1.empty()){\n int d1=(*s1.begin()).first;\n int node=(*s1.begin()).second;\n s1.erase(s1.begin());\n for(auto x:adj[node]){\n int wt=x.second;\n int v=x.first;\n if(dist[v]>wt+d1){\n if(dist[v]!=1e9){\n s1.erase({dist[v],v});\n }\n if(wt+d1<dis[v]){\n dist[v]=wt+d1;\n s1.insert({dist[v],v});\n }\n }\n }\n }\n for(int i=0;i<n;i++){\n if(dist[i]>dis[i]) dist[i]=-1;\n }\n return dist;\n }\n};",
"memory": "232595"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n vector<pair<int,int>> adj[n];\n for(auto it:edges)\n {\n int u=it[0], v=it[1], wt=it[2];\n adj[u].push_back({v,wt});\n adj[v].push_back({u,wt});\n }\n // priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n ///q.push({0,0});\n set<pair<int,int>> s;\n s.insert({0,0});\n vector<int> dis(n,INT_MAX);\n dis[0]=0;\n while(!s.empty())\n {\n auto it = *(s.begin());\n s.erase(it);\n\n int d=it.first;\n int nd=it.second;\n \n if (d >= disappear[nd]) continue;\n\n for(auto it:adj[nd])\n {\n int v=it.first;\n int wt=it.second;\n if(dis[v]>d+wt && d+wt<disappear[v])\n {\n if (dis[v]!= INT_MAX) {\n s.erase({dis[v],v});\n }\n dis[v]=d+wt;\n s.insert({dis[v],v});\n }\n }\n }\n for(auto &it: dis)\n {\n if(it==INT_MAX) it=-1;\n }\n return dis;\n }\n};",
"memory": "234146"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges,\n vector<int>& disappear) {\n vector<pair<int, int>> g[n];\n\n for (auto it : edges) {\n\n int u = it[0], v = it[1], wt = it[2];\n\n g[u].push_back({v, wt});\n g[v].push_back({u, wt});\n }\n\n const int INF = 1e10;\n vector<int> dist(n, INF);\n dist[0] = 0;\n\n set<pair<int, int>> pq;\n\n pq.insert({0, 0});\n\n while (!pq.empty()) {\n auto curr = *pq.begin();\n\n pq.erase(pq.begin());\n\n int wt = curr.first, u = curr.second;\n\n if(wt > dist[u]) continue; // may be after this got pushed , later it may had a shorter path \n\n for (auto it : g[u]) {\n int v = it.first, vwt = it.second;\n\n if (wt + vwt < dist[v] && wt+vwt < disappear[v]) {\n dist[v] = wt + vwt;\n pq.insert({dist[v], v});\n }\n }\n }\n\n for (int i = 0; i < n; i++) {\n if (dist[i] > disappear[i]) {\n dist[i] = -1;\n }\n }\n\n return dist;\n }\n};",
"memory": "234146"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minpq;\n minpq.push({0, 0});\n vector<vector<pair<int, int>>> pmap(n);\n for ( auto e: edges ) {\n pmap[e[0]].push_back({e[1], e[2]});\n pmap[e[1]].push_back({e[0], e[2]});\n }\n vector<int> cost(n, INT_MAX);\n set<int> vis;\n cost[0] = 0;\n while ( !minpq.empty() ) {\n if ( vis.size() == n ) break;\n auto top = minpq.top(); minpq.pop();\n vis.insert(top.second);\n for ( auto ele: pmap[top.second] ) {\n if ( ele.second + cost[top.second] < cost[ele.first] \n && ((ele.second + cost[top.second]) < disappear[ele.first]) ) {\n cost[ele.first] = ele.second + cost[top.second];\n minpq.push({cost[ele.first], ele.first});\n }\n }\n }\n\n for ( auto i = 0; i < n; i++ ) {\n if ( cost[i] == INT_MAX ) cost[i] = -1;\n }\n\n return cost;\n }\n};",
"memory": "235698"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minpq;\n minpq.push({0, 0});\n vector<vector<pair<int, int>>> pmap(n);\n for ( auto e: edges ) {\n pmap[e[0]].push_back({e[1], e[2]});\n pmap[e[1]].push_back({e[0], e[2]});\n }\n vector<int> cost(n, INT_MAX);\n cost[0] = 0;\n set<int> vis;\n while ( !minpq.empty() ) {\n if ( vis.size() == n ) break;\n auto top = minpq.top(); minpq.pop();\n vis.insert(top.second);\n\n for ( auto ele: pmap[top.second] ) {\n if ( ((ele.second + cost[top.second]) < cost[ele.first]) \n && ((ele.second + cost[top.second]) < disappear[ele.first]) ) {\n cost[ele.first] = ele.second + cost[top.second];\n minpq.push({cost[ele.first], ele.first});\n }\n }\n }\n\n for ( auto i = 0; i < n; i++ ) {\n if ( cost[i] == INT_MAX ) cost[i] = -1;\n }\n\n return cost;\n }\n};",
"memory": "235698"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n // let's get minimum time to reach the source node 0\n // make graph(undirected)\n unordered_map<int, vector<pair<int, int>>> graph;\n\n for(auto edge : edges){\n int u = edge[0];\n int v = edge[1];\n int time = edge[2];\n\n graph[u].push_back({v, time});\n graph[v].push_back({u, time});\n }\n\n // inital state\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n pq.push({0, 0}); // time, node(src)\n vector<int> minTime(n, INT_MAX);\n minTime[0] = 0; // minimum time from src -> src\n\n while(!pq.empty()){\n pair<int, int> nodePair = pq.top();\n pq.pop();\n\n int topNode = nodePair.second;\n int topTime = nodePair.first;\n\n // skip the node if it has already disappeared\n if(topTime > disappear[topNode] || topTime > minTime[topNode])\n continue;\n\n for(auto nbr : graph[topNode]){\n int nbrNode = nbr.first;\n int nbrTime = nbr.second;\n\n // along with checking the minimum time, we also check if the node is available to visit\n if(disappear[nbrNode] > (topTime + nbrTime) && minTime[nbrNode] > (topTime + nbrTime)){\n minTime[nbrNode] = topTime + nbrTime;\n pq.push({minTime[nbrNode], nbrNode});\n }\n }\n }\n\n // update INT_MAX with -1\n for(int i = 0;i < n; i++){\n if(minTime[i] == INT_MAX)\n minTime[i] = -1;\n }\n\n return minTime;\n }\n};",
"memory": "237249"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n unordered_map<int, vector<pair<int, int>> > adj;\n for(auto x:edges){\n adj[x[0]].push_back({x[1], x[2]});\n adj[x[1]].push_back({x[0], x[2]});\n }\n vector<int> time(n, INT_MAX);\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> >pq;\n pq.push({0, 0});\n time[0] = 0;\n while(!pq.empty()){\n auto [steps, node] = pq.top();\n pq.pop();\n if(time[node] < steps) continue;\n cout<<node<<\" \";\n for(auto [x, y]:adj[node]){\n if(steps+y < time[x] && disappear[x] > steps+y){\n time[x]= steps+y;\n pq.push({time[x], x});\n }\n }\n }\n for(int i=0;i<n;i++)\n {\n if(time[i]==INT_MAX)\n {\n time[i]=-1;\n }\n }\n return time;\n }\n};",
"memory": "237249"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n unordered_map<int, vector<pair<int, int>>> adjlist;\n for (auto edge: edges)\n {\n adjlist[edge[0]].push_back({edge[1], edge[2]});\n adjlist[edge[1]].push_back({edge[0], edge[2]});\n }\n\n vector<int> dist(n, -1);\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minHeap;\n minHeap.push({0, 0});\n\n while (!minHeap.empty())\n {\n auto curr = minHeap.top();\n minHeap.pop();\n\n int cnode = curr.second;\n int time = curr.first;\n\n if (dist[cnode] != -1)\n continue;\n \n dist[cnode] = time;\n \n for (auto neigh: adjlist[curr.second])\n {\n if (dist[neigh.first] == -1 && time + neigh.second < disappear[neigh.first])\n {\n minHeap.push({time + neigh.second, neigh.first});\n }\n }\n }\n\n return dist;\n }\n};",
"memory": "238800"
} |
3,389 | <p>There is an undirected graph of <code>n</code> nodes. You are given a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.</p>
<p>Additionally, you are given an array <code>disappear</code>, where <code>disappear[i]</code> denotes the time when the node <code>i</code> disappears from the graph and you won't be able to visit it.</p>
<p><strong>Note</strong> that the graph might be <em>disconnected</em> and might contain <em>multiple edges</em>.</p>
<p>Return the array <code>answer</code>, with <code>answer[i]</code> denoting the <strong>minimum</strong> units of time required to reach node <code>i</code> from node 0. If node <code>i</code> is <strong>unreachable</strong> from node 0 then <code>answer[i]</code> is <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is our starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>. Unfortunately, it disappears at that moment, so we won't be able to visit it.</li>
<li>For node 2, we need at least 4 units of time to traverse <code>edges[2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/11/output-onlinepngtools-1.png" style="width: 350px; height: 210px;" /></p>
<p>We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.</p>
<ul>
<li>For node 0, we don't need any time as it is the starting point.</li>
<li>For node 1, we need at least 2 units of time to traverse <code>edges[0]</code>.</li>
<li>For node 2, we need at least 3 units of time to traverse <code>edges[0]</code> and <code>edges[1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2, edges = [[0,1,1]], disappear = [1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>Exactly when we reach node 1, it disappears.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= edges.length <= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code></li>
<li><code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>disappear.length == n</code></li>
<li><code>1 <= disappear[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {\n // apply dijkstra's algo\n vector<int>dist(n,1e9);\n unordered_map<int,vector<pair<int,int>>>adj;\n for(auto it:edges){\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n }\n vector<int>vis(n,0);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n dist[0]=0;\n pq.push({0,0}); // dist,node\n // push the node in pq only if you can visit them before disappearance\n while(!pq.empty()){\n int d = pq.top().first;\n int node = pq.top().second;\n pq.pop();\n if(vis[node] || d>=disappear[node]) continue;\n vis[node]=1;\n for(auto it: adj[node]){\n int child_node = it.first;\n int distance = it.second;\n if(vis[child_node])continue;\n if(disappear[child_node]<=distance) continue;\n\n if(distance+d < dist[child_node]){\n dist[child_node] = distance+d;\n pq.push({dist[child_node], child_node});\n }\n }\n }\n\n for(int i=0; i<n; i++) cout<<dist[i]<<\" \";\n cout<<endl;\n vector<int>ans(n);\n for(int i=0; i<n; i++){\n if(dist[i]<disappear[i]) ans[i] = dist[i];\n else ans[i] = -1;\n }\n return ans;\n }\n};",
"memory": "238800"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.