Clean Java Solution Using Recursion


  • 0
    A
    int maxX = 0, minX = 0, maxY = 0, minY = 0;
    
    public int minArea(char[][] image, int x, int y) {
    	maxX = x;
    	minX = x;
    	minY = y;
    	maxY = y;
    	boolean[][] visited = new boolean[image.length][image[0].length];
    	travel(image, x, y, visited);
    	return (maxX - minX + 1) * (maxY - minY + 1);
    
    }
    
    public void travel(char[][] image, int x, int y, boolean[][] visited) {
    	if (x >= 0 && x < image.length && y >= 0 && y < image[0].length
    			&& !visited[x][y] && image[x][y] == '1') {
    		visited[x][y] = !visited[x][y];
    		minX = Math.min(minX, x);
    		maxX = Math.max(maxX, x);
    		minY = Math.min(minY, y);
    		maxY = Math.max(maxY, y);
    		travel(image, x + 1, y, visited);
    		travel(image, x - 1, y, visited);
    		travel(image, x, y + 1, visited);
    		travel(image, x, y - 1, visited);
    	}
    
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.