Cavity Map

Cavity Map

You are given a square map as a matrix of integer strings. Each cell of the map has a value denoting its depth. We will call a cell of the map a cavity if and only if this cell is not on the border of the map and each cell adjacent to it has strictly smaller depth. Two cells are adjacent if they have a common side, or edge.

Find all the cavities on the map and replace their depths with the uppercase character X.

Example

grid = [‘989’, ‘191’, ‘111’]

The grid is rearranged for clarity:

1
2
3
989
191
111

Return:

1
2
3
989
1X1
111

The center cell was deeper than those on its edges: [8,1,1,1]. The deep cells in the top two corners do not share an edge with the center cell, and none of the border cells is eligible.

Function Description

Complete the cavityMap function in the editor below.

cavityMap has the following parameter(s):

string grid[n]: each string represents a row of the grid

Returns

string{n}: the modified grid

Input Format

The first line contains an integer , the number of rows and columns in the grid.

Each of the following lines (rows) contains positive digits without spaces (columns) that represent the depth at .

Constraints

  • 1 <= n <= 100

Sample Input

1
2
3
4
5
6
7
STDIN   Function
----- --------
4 grid[] size n = 4
1112 grid = ['1112', '1912', '1892', '1234']
1912
1892
1234

Sample Output

1
2
3
4
1112
1X12
18X2
1234

Explanation

The two cells with the depth of 9 are not on the border and are surrounded on all sides by shallower cells. Their values are replaced by X.


Solution

Solution 1 with JS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* Complete the 'cavityMap' function below.
*
* The function is expected to return a STRING_ARRAY.
* The function accepts STRING_ARRAY grid as parameter.
*/

function cavityMap(grid) {
// Write your code here
const cavities = grid.map(item => item.split(''));

return cavities.reduce((target, list, index, source) => {
const result = list
.reduce((listTarget, item, itemIndex) => {
listTarget.push(
index >= 1 &&
itemIndex >= 1 &&
index < source.length - 1 &&
itemIndex < list.length &&
item > source[index][itemIndex - 1] &&
item > source[index][itemIndex + 1] &&
item > source[index - 1][itemIndex] &&
item > source[index + 1][itemIndex]
? 'X'
: item
);

return listTarget;
}, [])
.join('');

target.push(result);

return target;
}, []);
}

Solution 2 with TS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
* Complete the 'cavityMap' function below.
*
* The function is expected to return a STRING_ARRAY.
* The function accepts STRING_ARRAY grid as parameter.
*/
function cavityMap(grid: string[]): string[] {
// Write your code here
const cavities: string[][] = grid.map(item => (item || '').split(''));

return cavities.reduce((target, list, index, source) => {
const result = list
.reduce((listTarget, item, itemIndex) => {
listTarget.push(
index >= 1 &&
itemIndex >= 1 &&
index < source.length - 1 &&
itemIndex < list.length &&
item > source[index][itemIndex - 1] &&
item > source[index][itemIndex + 1] &&
item > source[index - 1][itemIndex] &&
item > source[index + 1][itemIndex]
? 'X'
: item
);

return listTarget;
}, [] as string[])
.join('');

target.push(result);

return target;
}, [] as string[]);
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×