We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
/*
Helper functions.
*/
public static String point(int x, int y) { return String.format("%d,%d", x, y); }
public static String point(int[] p) { return String.format("%d,%d", p[0], p[1]); }
public static int[] add(int[] a, int[] b) { return new int[]{a[0] + b[0], a[1] + b[1]}; }
/*
* Returns a list of sets where each set
* contains cell positions that make up a valid plus.
* Starting at center x, y expand outwards as much as possible.
* We save each plus we find in order to compare all possible pluses.
*/
public static List<Set<String>> allPluses(int x, int y, List<String> grid, Set<String> bad)
{
List<Set<String>> allCombos = new ArrayList<>();
int N = grid.size();
int M = grid.get(0).length();
int[][] directions = {
{-1, 0}, //up
{1, 0}, //down
{0, -1}, //left
{0, 1} //right
};
int[] up = new int[]{x, y};
int[] down = new int[]{x, y};
int[] left = new int[]{x, y};
int[] right = new int[]{x, y};
Set<String> cells = new HashSet<>();
while(up[0] >= 0 && down[0] < N && left[1] >= 0 && right[1] < M)
{
if(bad.contains(point(up)) || bad.contains(point(left)) || bad.contains(point(right)) ||
bad.contains(point(down)))
{
break;
}
Set<String> curr = new HashSet<>(cells);
curr.add(point(up));
curr.add(point(down));
curr.add(point(left));
curr.add(point(right));
allCombos.add(curr);
cells.addAll(curr);
up = add(up, directions[0]);
down = add(down, directions[1]);
left = add(left, directions[2]);
right = add(right, directions[3]);
}
return allCombos;
}
public static int twoPluses(List<String> grid) {
// Write your code here
final int N = grid.size();
final int M = grid.get(0).length();
//create set of all positions that are bad cells.
Set<String> badCells = new HashSet<>();
for(int x = 0; x < N; x++)
for(int y = 0; y < M; y++)
if(grid.get(x).charAt(y) != 'G')
badCells.add(point(x,y));
//find every possible plus you can make for a cell.
Map<String, List<Set<String>>> pluses = new HashMap<>();
for(int x = 0; x < N; x++)
for(int y = 0; y < M; y++)
pluses.put(point(x,y), allPluses(x, y, grid, badCells));
/*
* for every 2 cells compare each possible plus combination
* keep the max.
*/
int maxArea = 0;
for(String cell_one : pluses.keySet())
for(String cell_two : pluses.keySet())
if(!cell_one.equals(cell_two))
for(Set<String> c1_plus : pluses.get(cell_one))
for(Set<String> c2_plus : pluses.get(cell_two))
if(Collections.disjoint(c1_plus, c2_plus))
maxArea = Math.max(maxArea, c1_plus.size() * c2_plus.size());
return maxArea;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ema's Supercomputer
You are viewing a single comment's thread. Return to all comments →
/* Helper functions. */ public static String point(int x, int y) { return String.format("%d,%d", x, y); } public static String point(int[] p) { return String.format("%d,%d", p[0], p[1]); } public static int[] add(int[] a, int[] b) { return new int[]{a[0] + b[0], a[1] + b[1]}; }