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.
/*
* Complete the 'cavityMap' function below.
*
* The function is expected to return a STRING_ARRAY.
* The function accepts STRING_ARRAY grid as parameter.
*/
public static List<String> cavityMap(List<String> grid) {
int n = grid.size();
List<String> result = new ArrayList<>(grid);
for (int i = 1; i < n - 1; i++) {
char[] row = grid.get(i).toCharArray();
for (int j = 1; j < row.length - 1; j++) {
int current = row[j] - '0';
int top = grid.get(i - 1).charAt(j) - '0';
int bottom = grid.get(i + 1).charAt(j) - '0';
int left = row[j - 1] - '0';
int right = row[j + 1] - '0';
if (current > top && current > bottom && current > left && current > right) {
row[j] = 'X';
}
}
result.set(i, new String(row));
}
return result;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<String> grid = IntStream.range(0, n).mapToObj(i -> {
try {
return bufferedReader.readLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}).collect(toList());
List<String> result = Result.cavityMap(grid);
bufferedWriter.write(
result.stream()
.collect(joining("\n")) + "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cavity Map
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList;
class Result {
}
public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
}