|
| 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> < x<sub>j</sub></code>, or <code>x<sub>i</sub> == x<sub>j</sub></code> and <code>y<sub>i</sub> < 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> </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> </p> |
| 74 | +<p><strong>Constraints:</strong></p> |
| 75 | + |
| 76 | +<ul> |
| 77 | + <li><code>1 <= towers.length <= 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 <= x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>, cx, cy <= 10<sup>5</sup></code></li> |
| 81 | + <li><code>0 <= radius <= 10<sup>5</sup></code></li> |
| 82 | +</ul> |
0 commit comments