Approach
We filter out the towers that is inside the radius, and the second step is to do sorting based on the quality, and x to break-tie, and y to break-tie.
An optimization cna be done on the sorting part, since we only have to find the minimum with O(N), no need to do sorting.
Code
class Solution {
public:
vector<int> bestTower(vector<vector<int>>& towers, vector<int>& center, int radius) {
vector<int> quality;
int n = towers.size();
for(int i = 0 ; i < n ; ++i) {
int dist = abs(center[0] - towers[i][0]) + abs(center[1] - towers[i][1]);
if(dist > radius) continue; // =?
quality.push_back(i);
}
if(quality.empty()) return {-1, -1};
sort(quality.begin(), quality.end(), [&](int& a, int& b){
if(towers[a][2] > towers[b][2]) return true; // quality
else if(towers[a][2] < towers[b][2]) return false;
if(towers[a][0] < towers[b][0]) return true; // x
else if(towers[a][0] > towers[b][0]) return false;
if(towers[a][1] < towers[b][1]) return true; // y
return false;
});
int id = quality[0];
return {towers[id][0], towers[id][1]};
}
};