LintCode 957: Radar Detection

  1. Radar Detection
    There is a bunch of radars on a 2D plane(Radar has x, y coordinates, and a radius r which is the range can be detected). Now, there is a car that passes through the range of y = 0 and y = 1 and cannot be detected by the radar. If the car is detected, return YES, otherwise NO.(You can consider that the car is a line segment of length 1 and goes straight from x = 0 to the right)

Example
Given coordinates = [[0,2]], radius = [1], return “NO”.

Explanation:
There is a radar at (0,2) that can detect a circle with a radius of 1 centered on (0,2) and the car will not be detected.
Given coordinates = [[0,2],[1,2]], radius = [1,2], return “YES”。

Explanation:
There is a radar at (0,2) that can detect a circular area with a radius of 2 with a center of (0,2). Radars at (1,2) can detect (1,2) as Center, circular area with 2 radius. The No. 2 radar can detect the passing of the car.
Notice
The number of radars is n,n <= 1000。
The radar’s coordinate x is a non-negative integer, y is an integer, and r is a positive integer.

解法:
遍历radar list,找出所有雷达的扫描的上界和下界。如果上界<1或下界>0,小车就可以通过,否则通不过。

/**
 * Definition for a point.
 * struct Point {
 *     int x;
 *     int y;
 *     Point() : x(0), y(0) {}
 *     Point(int a, int b) : x(a), y(b) {}
 * };
 */

class Solution {
public:
    /**
     * @param coordinates: The radars' coordinate
     * @param radius: Detection radius of radars
     * @return: The car was detected or not
     */
    string radarDetection(vector<Point> &coordinates, vector<int> &radius) {
        int lenC = coordinates.size();
        int lenR = radius.size();
        if (lenC == 0) return "NO";
        
        int minY = INT_MAX, maxY = INT_MIN;
        int upperbound, lowerbound;
        
        for (int i = 0; i < lenC; ++i) {
            upperbound = coordinates[i].y + radius[i];
            lowerbound = coordinates[i].y - radius[i];
            
            if (upperbound > maxY) maxY = upperbound;
            if (lowerbound < minY) minY = lowerbound;
        }
        
        if ((minY > 0) || (maxY < 1)) return "NO";
        
        return "YES";
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值