|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +CREATED AT: 2022年11月02日 |
| 4 | + |
| 5 | +URL: https://leetcode.com/problems/coordinate-with-maximum-network-quality/ |
| 6 | + |
| 7 | +GITHUB: https://github.com/Jiezhi/myleetcode |
| 8 | + |
| 9 | +FileName: 1620-CoordinateWithMaximumNetworkQuality |
| 10 | + |
| 11 | +Difficulty: Medium |
| 12 | + |
| 13 | +Desc: |
| 14 | + |
| 15 | +Tag: |
| 16 | + |
| 17 | +See: |
| 18 | + |
| 19 | +""" |
| 20 | +from tool import * |
| 21 | + |
| 22 | + |
| 23 | +class Solution: |
| 24 | + def bestCoordinate(self, towers: List[List[int]], radius: int) -> List[int]: |
| 25 | + """ |
| 26 | + Runtime: 3601 ms, faster than 40.84% |
| 27 | + Memory Usage: 13.9 MB, less than 84.17% |
| 28 | + |
| 29 | + 1 <= towers.length <= 50 |
| 30 | + towers[i].length == 3 |
| 31 | + 0 <= xi, yi, qi <= 50 |
| 32 | + 1 <= radius <= 50 |
| 33 | + """ |
| 34 | + min_x, min_y, max_x, max_y = towers[0][0], towers[0][1], towers[0][0], towers[0][1] |
| 35 | + for x, y, _ in towers: |
| 36 | + min_x = min(min_x, x) |
| 37 | + min_y = min(min_y, y) |
| 38 | + max_x = max(max_x, x) |
| 39 | + max_y = max(max_y, y) |
| 40 | + |
| 41 | + def calc(a, b) -> int: |
| 42 | + r = 0 |
| 43 | + for x, y, q in towers: |
| 44 | + d = ((x - a) ** 2 + (y - b) ** 2) ** 0.5 |
| 45 | + if d <= radius: |
| 46 | + r += math.floor(q / (1 + d)) |
| 47 | + return r |
| 48 | + |
| 49 | + ret = (calc(0, 0), 0, 0) |
| 50 | + for x in range(min_x, max_x + 1): |
| 51 | + for y in range(min_y, max_y + 1): |
| 52 | + r = calc(x, y) |
| 53 | + if ret[0] < r: |
| 54 | + ret = (r, x, y) |
| 55 | + return [ret[1], ret[2]] |
| 56 | + |
| 57 | + |
| 58 | +def test(): |
| 59 | + assert Solution().bestCoordinate([[44, 31, 4], [47, 27, 27], [7, 13, 0], [13, 21, 20], [50, 34, 18], [47, 44, 28]], |
| 60 | + 13) == [47, 27] |
| 61 | + assert Solution().bestCoordinate(towers=[[1, 2, 5], [2, 1, 7], [3, 1, 9]], radius=2) == [2, 1] |
| 62 | + assert Solution().bestCoordinate(towers=[[23, 11, 21]], radius=9) == [23, 11] |
| 63 | + assert Solution().bestCoordinate(towers=[[1, 2, 13], [2, 1, 7], [0, 1, 9]], radius=2) == [1, 2] |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == '__main__': |
| 67 | + test() |
0 commit comments