- 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";
}
};