zoukankan      html  css  js  c++  java
  • 【leetcode】1620. Coordinate With Maximum Network Quality

    题目如下:

    You are given an array of network towers towers and an integer radius, where towers[i] = [xi, yi, qi] denotes the ith network tower with location (xi, yi) and quality factor qi. All the coordinates are integral coordinates on the X-Y plane, and the distance between two coordinates is the Euclidean distance.

    The integer radius denotes the maximum distance in which the tower is reachable. The tower is reachable if the distance is less than or equal to radius. Outside that distance, the signal becomes garbled, and the tower is not reachable.

    The signal quality of the ith tower at a coordinate (x, y) is calculated with the formula ⌊qi / (1 + d)⌋, where d is the distance between the tower and the coordinate. The network quality at a coordinate is the sum of the signal qualities from all the reachable towers.

    Return the integral coordinate where the network quality is maximum. If there are multiple coordinates with the same network quality, return the lexicographically minimum coordinate.

    Note:

    • A coordinate (x1, y1) is lexicographically smaller than (x2, y2) if either x1 < x2 or x1 == x2 and y1 < y2.
    • ⌊val⌋ is the greatest integer less than or equal to val (the floor function).

    Example 1:

    Input: towers = [[1,2,5],[2,1,7],[3,1,9]], radius = 2
    Output: [2,1]
    Explanation: 
    At coordinate (2, 1) the total quality is 13
    - Quality of 7 from (2, 1) results in ⌊7 / (1 + sqrt(0)⌋ = ⌊7⌋ = 7
    - Quality of 5 from (1, 2) results in ⌊5 / (1 + sqrt(2)⌋ = ⌊2.07⌋ = 2
    - Quality of 9 from (3, 1) results in ⌊9 / (1 + sqrt(1)⌋ = ⌊4.5⌋ = 4
    No other coordinate has higher quality.

    Example 2:

    Input: towers = [[23,11,21]], radius = 9
    Output: [23,11]
    

    Example 3:

    Input: towers = [[1,2,13],[2,1,7],[0,1,9]], radius = 2
    Output: [1,2]
    

    Example 4:

    Input: towers = [[2,1,9],[0,1,9]], radius = 2
    Output: [0,1]
    Explanation: Both (0, 1) and (2, 1) are optimal in terms of quality but (0, 1) is lexicograpically minimal.

    Constraints:

    • 1 <= towers.length <= 50
    • towers[i].length == 3
    • 0 <= xi, yi, qi <= 50
    • 1 <= radius <= 50

    解题思路:最直接的办法就是穷举了,找出信号塔能覆盖的x与y的最大值和最小值,然后计算这个区间内所有的(x,y),找出最大值。

    代码如下:

    class Solution(object):
        def bestCoordinate(self, towers, radius):
            """
            :type towers: List[List[int]]
            :type radius: int
            :rtype: List[int]
            """
            import math
            max_quality = 0
            max_x = 0
            max_y = 0
    
            dic = {}
    
            def calc(x1,y1,x2,y2):
                return math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
    
            def cmpf(x1,y1,x2,y2):
                if x1 != x2:
                    return x1 - x2
                return y1 - y2
    
            for (x,y,q) in towers:
                max_x = max(max_x,x+radius)
                max_y = max(max_y,y+radius)
    
            for i in range(max_x+1):
                for j in range(max_y+1):
                    if i == 2 and j == 1:
                        pass
                    for (x,y,q) in towers:
                        d = calc(i,j,x,y)
                        if d > radius:
                            continue
                        dic[(i, j)] = dic.setdefault((i, j), 0) + math.floor(float(q) / float(1 + d))
                        max_quality = max(max_quality,dic[i,j])
    
            #print max_quality
            if max_quality == 0:
                return [0,0]
            res = []
            for (x,y),val in dic.iteritems():
                if val == max_quality:
                    if len(res) == 0 :res = [x,y]
                    elif cmpf(res[0],res[1],x,y) > 0:
                        res = [x,y]
            return res
  • 相关阅读:
    2018年度总结
    SQL特殊comment语法
    mysql 主从复制change master to
    mysql 主从复制参数slave_net_timeout
    show master status, show slave status中的Executed_Gtid_Set
    mysql重复start slave、stop slave测试
    golang database sql DSN (Data Source Name)中的timeout, readTimeout
    mysql show master status为空值
    Linux 建立 TCP 连接的超时时间分析(解惑)
    golang 原子操作函数
  • 原文地址:https://www.cnblogs.com/seyjs/p/14931159.html
Copyright © 2011-2022 走看看