Skip to content

Commit 1fb02f0

Browse files
Sync LeetCode submission Runtime - 96 ms (28.04%), Memory - 307.4 MB (10.55%)
1 parent f6e5909 commit 1fb02f0

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<p>You are given a 2D integer array <code>towers</code>, where <code>towers[i] = [x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>]</code> represents the coordinates <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and quality factor <code>q<sub>i</sub></code> of the <code>i<sup>th</sup></code> tower.</p>
2+
3+
<p>You are also given an integer array <code>center = [cx, cy​​​​​​​]</code> representing your location, and an integer <code>radius</code>.</p>
4+
5+
<p>A tower is <strong>reachable</strong> if its <strong>Manhattan distance</strong> from <code>center</code> is <strong>less than or equal</strong> to <code>radius</code>.</p>
6+
7+
<p>Among all reachable towers:</p>
8+
9+
<ul>
10+
<li>Return the coordinates of the tower with the <strong>maximum</strong> quality factor.</li>
11+
<li>If there is a tie, return the tower with the <strong>lexicographically smallest</strong> coordinate. If no tower is reachable, return <code>[-1, -1]</code>.</li>
12+
</ul>
13+
The <strong>Manhattan Distance</strong> between two cells <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and <code>(x<sub>j</sub>, y<sub>j</sub>)</code> is <code>|x<sub>i</sub> - x<sub>j</sub>| + |y<sub>i</sub> - y<sub>j</sub>|</code>.
14+
15+
<p>A coordinate <code>[x<sub>i</sub>, y<sub>i</sub>]</code> is <strong>lexicographically smaller</strong> than <code>[x<sub>j</sub>, y<sub>j</sub>]</code> if <code>x<sub>i</sub> &lt; x<sub>j</sub></code>, or <code>x<sub>i</sub> == x<sub>j</sub></code> and <code>y<sub>i</sub> &lt; y<sub>j</sub></code>.</p>
16+
17+
<p><code>|x|</code> denotes the <strong>absolute</strong> <strong>value</strong> of <code>x</code>.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<div class="example-block">
23+
<p><strong>Input:</strong> <span class="example-io">towers = [[1,2,5], [2,1,7], [3,1,9]], center = [1,1], radius = 2</span></p>
24+
25+
<p><strong>Output:</strong> <span class="example-io">[3,1]</span></p>
26+
27+
<p><strong>Explanation:</strong></p>
28+
29+
<ul>
30+
<li>Tower <code>[1, 2, 5]</code>: Manhattan distance = <code>|1 - 1| + |2 - 1| = 1</code>, reachable.</li>
31+
<li>Tower <code>[2, 1, 7]</code>: Manhattan distance = <code>|2 - 1| + |1 - 1| = 1</code>, reachable.</li>
32+
<li>Tower <code>[3, 1, 9]</code>: Manhattan distance = <code>|3 - 1| + |1 - 1| = 2</code>, reachable.</li>
33+
</ul>
34+
35+
<p>All towers are reachable. The maximum quality factor is 9, which corresponds to tower <code>[3, 1]</code>.</p>
36+
</div>
37+
38+
<p><strong class="example">Example 2:</strong></p>
39+
40+
<div class="example-block">
41+
<p><strong>Input:</strong> <span class="example-io">towers = [[1,3,4], [2,2,4], [4,4,7]], center = [0,0], radius = 5</span></p>
42+
43+
<p><strong>Output:</strong> <span class="example-io">[1,3]</span></p>
44+
45+
<p><strong>Explanation:</strong></p>
46+
47+
<ul>
48+
<li>Tower <code>[1, 3, 4]</code>: Manhattan distance = <code>|1 - 0| + |3 - 0| = 4</code>, reachable.</li>
49+
<li>Tower <code>[2, 2, 4]</code>: Manhattan distance = <code>|2 - 0| + |2 - 0| = 4</code>, reachable.</li>
50+
<li>Tower <code>[4, 4, 7]</code>: Manhattan distance = <code>|4 - 0| + |4 - 0| = 8</code>, not reachable.</li>
51+
</ul>
52+
53+
<p>Among the reachable towers, the maximum quality factor is 4. Both <code>[1, 3]</code> and <code>[2, 2]</code> have the same quality, so the lexicographically smaller coordinate is <code>[1, 3]</code>.</p>
54+
</div>
55+
56+
<p><strong class="example">Example 3:</strong></p>
57+
58+
<div class="example-block">
59+
<p><strong>Input:</strong> <span class="example-io">towers = [[5,6,8], [0,3,5]], center = [1,2], radius = 1</span></p>
60+
61+
<p><strong>Output:</strong> <span class="example-io">[-1,-1]</span></p>
62+
63+
<p><strong>Explanation:</strong></p>
64+
65+
<ul>
66+
<li>Tower <code>[5, 6, 8]</code>: Manhattan distance = <code>|5 - 1| + |6 - 2| = 8</code>, not reachable.</li>
67+
<li>Tower <code>[0, 3, 5]</code>: Manhattan distance = <code>|0 - 1| + |3 - 2| = 2</code>, not reachable.</li>
68+
</ul>
69+
70+
<p>No tower is reachable within the given radius, so <code>[-1, -1]</code> is returned.</p>
71+
</div>
72+
73+
<p>&nbsp;</p>
74+
<p><strong>Constraints:</strong></p>
75+
76+
<ul>
77+
<li><code>1 &lt;= towers.length &lt;= 10<sup>5</sup></code></li>
78+
<li><code>towers[i] = [x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>]</code></li>
79+
<li><code>center = [cx, cy]</code></li>
80+
<li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>, cx, cy &lt;= 10<sup>5</sup></code>​​​​​​​</li>
81+
<li><code>0 &lt;= radius &lt;= 10<sup>5</sup></code></li>
82+
</ul>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
vector<int> bestTower(vector<vector<int>>& towers, vector<int>& center, int radius) {
4+
auto dist = [&](int x, int y) {
5+
int cx = center[0];
6+
int cy = center[1];
7+
return abs(cx - x) + abs(cy - y);
8+
};
9+
10+
vector<vector<int>> reach;
11+
for (vector<int>& tower : towers) {
12+
int x = tower[0];
13+
int y = tower[1];
14+
int q = tower[2];
15+
if (dist(x, y) > radius) continue;
16+
reach.push_back({x, y, q});
17+
}
18+
19+
if (reach.empty()) return {-1, -1};
20+
21+
sort(reach.begin(), reach.end(), [](const vector<int>& a, const vector<int>& b) {
22+
if (a[2] > b[2]) return true;
23+
if (a[2] < b[2]) return false;
24+
if (a[0] < b[0]) return true;
25+
if (a[0] > b[0]) return false;
26+
return a[1] < b[1];
27+
});
28+
29+
return {reach[0][0], reach[0][1]};
30+
}
31+
};

0 commit comments

Comments
 (0)