Question 3071
A topographic survey is stored in a class HeightMap, which uses a two-dimensional array to hold the elevation of each grid cell. Row 0 is the northernmost row and column 0 is the westernmost column. All rows have the same length. The grid is at least \(1 \times 1\).
public class HeightMap
{
/** Elevation grid; heights[r][c] is the elevation of the cell at row r, column c.
* Guaranteed to be a rectangular, non-empty 2D array of integers. */
private int[][] heights;
public HeightMap(int[][] grid)
{
heights = grid;
}
/** Part: see below. */
public boolean[][] peaks() { /* to implement */ }
}
Write the method peaks. The method returns a newly constructed boolean array of the same dimensions as heights. Entry [r][c] in the returned array is true if and only if the elevation at cell (r, c) is strictly greater than the elevation of every one of its orthogonal neighbors that exists in the grid. The orthogonal neighbors of (r, c) are the cells directly north, south, west, and east: (r-1, c), (r+1, c), (r, c-1), and (r, c+1). Neighbors outside the grid are simply ignored when making the comparison.
A corner cell has only two neighbors; an edge cell has only three neighbors; an interior cell has four. A cell with any neighbor at or above its own elevation is not a peak. A cell in a \(1 \times 1\) grid has no neighbors and is therefore a peak.
Let heights be the grid
{ {1, 4, 2}, {3, 2, 5}, {1, 1, 2} }.
The call peaks() returns
{ {false, true, false}, {true, false, true}, {false, false, false} }.
Explanation: (0,1) = 4 beats its existing neighbors 1, 2, 2; (1,0) = 3 beats 1, 2, 1; (1,2) = 5 beats 2, 2, 2. Every other cell has at least one neighbor at least as tall as itself.
public boolean[][] peaks()
Open each "Show solution" accordion above to compare your response against the reference implementation and scoring notes.
Main Solution
Sample Solution
public boolean[][] peaks()
{
int rows = heights.length;
int cols = heights[0].length;
boolean[][] result = new boolean[rows][cols];
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
boolean isPeak = true;
int value = heights[r][c];
if (r - 1 >= 0 && heights[r - 1][c] >= value)
{
isPeak = false;
}
if (r + 1 < rows && heights[r + 1][c] >= value)
{
isPeak = false;
}
if (c - 1 >= 0 && heights[r][c - 1] >= value)
{
isPeak = false;
}
if (c + 1 < cols && heights[r][c + 1] >= value)
{
isPeak = false;
}
result[r][c] = isPeak;
}
}
return result;
}
Scoring notes. Points are earned for: allocating a new boolean[rows][cols] of the correct shape; visiting every cell with a nested loop; correctly bounds-checking each of the four neighbors before indexing; using a strict comparison so that ties disqualify a peak; and returning the result array. Any attempt to access heights[-1][c], heights[rows][c], heights[r][-1], or heights[r][cols] without a guard earns zero for the traversal; so does an off-by-one that reports a tie as a peak. A correct solution for the 1×1 case (no neighbors, so the single cell is a peak) falls out of the isPeak = true initialization when every guard fails.
Answer images

