From b1f39b0792327006984e99ffce4601eb0f07661e Mon Sep 17 00:00:00 2001 From: keineahnung2345 Date: Sun, 25 Oct 2020 09:55:17 +0800 Subject: [PATCH] 1620. Coordinate With Maximum Network Quality.cpp --- ...oordinate With Maximum Network Quality.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 1620. Coordinate With Maximum Network Quality.cpp diff --git a/1620. Coordinate With Maximum Network Quality.cpp b/1620. Coordinate With Maximum Network Quality.cpp new file mode 100644 index 0000000..ed8a3c8 --- /dev/null +++ b/1620. Coordinate With Maximum Network Quality.cpp @@ -0,0 +1,48 @@ +//brute force +//Runtime: 92 ms, faster than 48.90% of C++ online submissions for Coordinate With Maximum Network Quality. +//Memory Usage: 8.9 MB, less than 7.40% of C++ online submissions for Coordinate With Maximum Network Quality. +class Solution { +public: + double dist(int x1, int y1, int x2, int y2){ + return sqrt((double)((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))); + } + + vector bestCoordinate(vector>& towers, int radius) { + int n = towers.size(); + + if(n == 1 && towers[0][2] > 0){ + return {towers[0][0], towers[0][1]}; + } + + int left = INT_MAX, right = INT_MIN; + int top = INT_MAX, bottom = INT_MIN; + + for(const vector& tower : towers){ + left = min(left, tower[0]); + right = max(right, tower[0]); + top = min(top, tower[1]); + bottom = max(bottom, tower[1]); + } + + double d; + int q; + int maxq = 0; + vector maxcoord(2, 0); + for(int x = left; x <= right; ++x){ + for(int y = top; y <= bottom; ++y){ + q = 0; + for(const vector& tower : towers){ + if((d = dist(x, y, tower[0], tower[1])) > radius) continue; + q += tower[2]/(1+d); + } + if(q > maxq){ + maxq = q; + maxcoord[0] = x; + maxcoord[1] = y; + } + } + } + + return maxcoord; + } +};