3
\$\begingroup\$

Problem:

Source (with example): https://leetcode.com/problems/island-perimeter/

You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water.

Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).

The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.

Below is my solution. I haven't coded in Java since college and I'm trying to pick it up again. How is my style and is there any new Java features I can apply to the method to make it more concise? Thank you in advance!

Code:

class Solution {
    private int m;
    private int n;

    public int islandPerimeter(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        int perimeter = 0;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    perimeter += getPerimeter(i, j, grid);
                }
            }
        }
        return perimeter;
    }

    private int getPerimeter(int i, int j, int[][] grid) {
        int perimeter = 0;
        if (i == 0 || grid[i-1][j] == 0) {
            perimeter++;
        }
        if (j == 0 || grid[i][j-1] == 0) {
            perimeter++;
        }
        if (i == m-1 || grid[i+1][j] == 0) {
            perimeter++;
        }
        if (j == n-1 || grid[i][j+1] == 0) {
            perimeter++;
        }
        return perimeter;
    }
}
\$\endgroup\$
3
\$\begingroup\$

Instead of looking for cool new features of Java i would strongly suggest that you concentrate on the basics of Java: Objects:

Think in Objects:

So far you have identified these Objects: Map, a Cell and an Island - so make some Objects of that type.

Map map = new Map(int[][] src);
Island island = map.extractIsland();
int diameter = island.getPerimater();

the Map needs some methods to properly extract the island

Cell cell = getCellAt(int x, int y);

and a method to gain relationship between each Cells on the map:

List<Cell> getNeighbours(Cell center);

and some elementary methods on the Cell class

boolean isWater();
boolean isLand();
int getAmountCoasts();

if you would have these elementary Objects you can simply create readable Code - see this example:

Cell center; //yes, here center.isLand() = true
List<Cell> neigbour = map.getNeighbours(center);
for (Cell neigbour: neigbours){
    if (neigbour.isWater()){
        center.addCoast();
    }
}

once you are here you can use features from java 8

List<Cell> landCells = cells.stream.filter(Cell::isLand).collect(CollectorsToList());
int perimeter = landCells.stream.mapToInt(Cell::.getAmountCoasts).sum();

Hint: you better let the Island class handle that, see first hints - Responsibility for coastLine is within the Island class!)

NOTE:

it's hard to think in objects whenever you try minimal code snippets as suggested from leet code. if minimal code is required, look maybe you are interested at CodeGolf...

\$\endgroup\$
1
\$\begingroup\$

Storing properties of grid, which is a method parameter, into instance fields m and n, is bad practise. Now the reader has to wonder why their scope is exposed outside the method. M and n should stay in the same scope (they should be method variables).

M and n are bad names for width and height. There's nothing wrong with width and height and these would communicate their intended purpose immediately.

While i and j are common loop index variables, x and y are more commonly used for indexing a two dimensional grid. Some people use row and col, which are fine too.

While scanning every element is clean and works well for small input, you could just find the first element that has a "coastline" and check it's neighbors, ignoring all elements that don't have a "coastline". Follow the coastline clockwise and stop once you reach the first element again.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.