There are N points (numbered from 0 to N−1) on a plane. Each point is colored either red ('R') or green ('G'). The K-th point is located at coordinates (X[K], Y[K]) and its color is colors[K]. No point lies on coordinates (0, 0).
We want to draw a circle centered on coordinates (0, 0), such that the number of red points and green points inside the circle is equal. What is the maximum number of points that can lie inside such a circle? Note that it is always possible to draw a circle with no points inside.
Write a function:
def solution(X, Y, colors)
that, given two arrays of integers X, Y and a string colors, returns an integer specifying the maximum number of points inside a circle containing an equal number of red points and green points.
Examples:
1. Given X = [4, 0, 2, −2], Y = [4, 1, 2, −3] and colors = "RGRR", your function should return 2. The circle contains points (0, 1) and (2, 2), but not points (−2, −3) and (4, 4).
2. Given X = [1, 1, −1, −1], Y = [1, −1, 1, −1] and colors = "RGRG", your function should return 4. All points lie inside the circle.
3. Given X = [1, 0, 0], Y = [0, 1, −1] and colors = "GGR", your function should return 0. Any circle that contains more than zero points has an unequal number of green and red points.
4. Given X = [5, −5, 5], Y = [1, −1, −3] and colors = "GRG", your function should return 2.
5. Given X = [3000, −3000, 4100, −4100, −3000], Y = [5000, −5000, 4100, −4100, 5000] and colors = "RRGRG", your function should return 2.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of arrays X and Y is an integer within the range [−20,000..20,000];
- string colors is made only of the characters 'R' and/or 'G';
- no point lies on the coordinates (0, 0).
def solution(X, Y, colors):
distances = [distance(X[i],Y[i]) for i in range(len(X))]
colors = list(colors)
colors = ''.join([colors for _, colors in sorted(zip(distances,colors))])[::-1]
distances.sort(reverse=True)
for i in range(len(distances)):
rCount = 0
gCount = 0
if i>0 and distances[i] == distances[i-1]:
continue
currentColours = colors[i:]
if rCount == gCount:
return rCount+gCount
return 0
def distance(x,y):
def solution(X, Y, colors):
distances = [distance(X[i],Y[i]) for i in range(len(X))]
colors = list(colors)
colors = ''.join([colors for _, colors in sorted(zip(distances,colors))])[::-1]
distances.sort(reverse=True)
for i in range(len(distances)):
if i>0 and distances[i] == distances[i-1]:
continue
currentColours = colors[i:]
rCount = 0
gCount = 0
if rCount == gCount:
return rCount+gCount
return 0
def distance(x,y):
def solution(X, Y, colors):
distances = [distance(X[i],Y[i]) for i in range(len(X))]
colors = list(colors)
colors = ''.join([colors for _, colors in sorted(zip(distances,colors))])[::-1]
distances.sort(reverse=True)
for i in range(len(distances)):
if i>0 and distances[i] == distances[i-1]:
continue
currentColours = colors[i:]
rCount = currentColours
gCount = 0
if rCount == gCount:
return rCount+gCount
return 0
def distance(x,y):
def solution(X, Y, colors):
distances = [distance(X[i],Y[i]) for i in range(len(X))]
colors = list(colors)
colors = ''.join([colors for _, colors in sorted(zip(distances,colors))])[::-1]
distances.sort(reverse=True)
for i in range(len(distances)):
if i>0 and distances[i] == distances[i-1]:
continue
currentColours = colors[i:]
rCount = currentColours.count('R')
gCount = 0
if rCount == gCount:
return rCount+gCount
return 0
def distance(x,y):
def solution(X, Y, colors):
distances = [distance(X[i],Y[i]) for i in range(len(X))]
colors = list(colors)
colors = ''.join([colors for _, colors in sorted(zip(distances,colors))])[::-1]
distances.sort(reverse=True)
for i in range(len(distances)):
if i>0 and distances[i] == distances[i-1]:
continue
currentColours = colors[i:]
rCount = currentColours.count('R')
gCount = currentColours.count('G')
if rCount == gCount:
return rCount+gCount
return 0
def distance(x,y):
def solution(X, Y, colors):
distances = [distance(X[i],Y[i]) for i in range(len(X))]
colors = list(colors)
colors = ''.join([colors for _, colors in sorted(zip(distances,colors))])[::-1]
distances.sort(reverse=True)
for i in range(len(distances)):
if i>0 and distances[i] == distances[i-1]:
continue
currentColours = colors[i:]
rCount = currentColours.count('R')
gCount = currentColours.count('G')
if rCount == gCount:
return rCount+gCount
return 0
def distance(x,y):
return (x**2+y**2)**(1/2)
def solution(X, Y, colors):
distances = [distance(X[i],Y[i]) for i in range(len(X))]
colors = list(colors)
colors = ''.join([colors for _, colors in sorted(zip(distances,colors))])[::-1]
distances.sort(reverse=True)
for i in range(len(distances)):
if i>0 and distances[i] == distances[i-1]:
continue
currentColours = colors[i:]
rCount = currentColours.count('R')
gCount = currentColours.count('G')
if rCount == gCount:
return rCount+gCount
return 0
def distance(x,y):
return (x**2+y**2)**(1/2)
def solution(X, Y, colors):
distances = [distance(X[i],Y[i]) for i in range(len(X))]
colors = list(colors)
colors = ''.join([colors for _, colors in sorted(zip(distances,colors))])[::-1]
distances.sort(reverse=True)
for i in range(len(distances)):
if i>0 and distances[i] == distances[i-1]:
continue
currentColours = colors[i:]
rCount = currentColours.count('R')
gCount = currentColours.count('G')
if rCount == gCount:
return rCount+gCount
return 0
def distance(x,y):
return (x**2+y**2)**(1/2)
The following issues have been detected: timeout errors.
Small tests where all points have distinct distances from the origin. N <= 50.
Small tests where the proportion of color occurences is imbalanced. N <= 50.
Small tests where points are given in increasing order of distance from (0, 0). N <= 50.
Big tests where all points have distinct distances from the origin.
Killed. Hard limit reached: 9.000 sec.
Big tests where the proportion of color occurences is imbalanced.
running time: 7.484 sec., time limit: 3.160 sec.
Big tests where points are very close to the origin.
running time: 3.944 sec., time limit: 2.920 sec.
Big tests where points are given in increasing order of distance from (0, 0).
Killed. Hard limit reached: 8.000 sec.