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:
class Solution { public int solution(int[] X, int[] Y, String 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).
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] X, int[] Y, String colors) {
// Make Tree map with key of square of lengths and color value
TreeMap<Integer, String> map = new TreeMap<>();
for (int i = 0; i < X.length; i++) {
int p = X[i] * X[i] + Y[i] * Y[i];
map.put(p, map.getOrDefault(p, "") + colors.charAt(i));
}
final AtomicInteger res = new AtomicInteger();
final AtomicInteger R = new AtomicInteger();
final AtomicInteger G = new AtomicInteger();
map.forEach((integer, s) -> {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'R') {
R.incrementAndGet();
} else {
G.incrementAndGet();
}
}
final int r = R.get();
if (r == G.get()) {
res.set(r * 2);
}
});
return res.get();
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] X, int[] Y, String colors) {
// Make Tree map with key of square of lengths and color value
TreeMap<Integer, String> map = new TreeMap<>();
for (int i = 0; i < X.length; i++) {
int p = X[i] * X[i] + Y[i] * Y[i];
map.put(p, map.getOrDefault(p, "") + colors.charAt(i));
}
final AtomicInteger res = new AtomicInteger();
final AtomicInteger R = new AtomicInteger();
final AtomicInteger G = new AtomicInteger();
map.forEach((integer, s) -> {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'R') {
R.incrementAndGet();
} else {
G.incrementAndGet();
}
}
final int r = R.get();
if (r == G.get()) {
res.set(r * 2);
}
});
return res.get();
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] X, int[] Y, String colors) {
// Make Tree map with key of square of lengths and color value
TreeMap<Integer, String> map = new TreeMap<>();
for (int i = 0; i < X.length; i++) {
int p = X[i] * X[i] + Y[i] * Y[i];
map.put(p, map.getOrDefault(p, "") + colors.charAt(i));
}
final AtomicInteger res = new AtomicInteger();
final AtomicInteger R = new AtomicInteger();
final AtomicInteger G = new AtomicInteger();
map.forEach((integer, s) -> {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'R') {
R.incrementAndGet();
} else {
G.incrementAndGet();
}
}
final int r = R.get();
if (r == G.get()) {
res.set(r * 2);
}
});
return res.get();
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] X, int[] Y, String colors) {
// Make Tree map with key of square of lengths and color value
TreeMap<Integer, String> map = new TreeMap<>();
for (int i = 0; i < X.length; i++) {
int p = X[i] * X[i] + Y[i] * Y[i];
map.put(p, map.getOrDefault(p, "") + colors.charAt(i));
}
final AtomicInteger res = new AtomicInteger();
final AtomicInteger R = new AtomicInteger();
final AtomicInteger G = new AtomicInteger();
map.forEach((integer, s) -> {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'R') {
R.incrementAndGet();
} else {
G.incrementAndGet();
}
}
final int r = R.get();
if (r == G.get()) {
res.set(r * 2);
}
});
return res.get();
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] X, int[] Y, String colors) {
// Make Tree map with key of square of lengths and color value
TreeMap<Integer, String> map = new TreeMap<>();
for (int i = 0; i < X.length; i++) {
int p = X[i] * X[i] + Y[i] * Y[i];
map.put(p, map.getOrDefault(p, "") + colors.charAt(i));
}
final AtomicInteger res = new AtomicInteger();
final AtomicInteger R = new AtomicInteger();
final AtomicInteger G = new AtomicInteger();
map.forEach((integer, s) -> {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'R') {
R.incrementAndGet();
} else {
G.incrementAndGet();
}
}
final int r = R.get();
if (r == G.get()) {
res.set(r * 2);
}
});
return res.get();
}
}
The solution obtained perfect score.
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.