[LeetCode] Find max subsquare whose border values are all 1

相关问题1:01矩阵中最大1矩形面积

相关问题2:[LeetCode] 01矩阵中最大正方形 Maximal Square

Imagine you have a square matrix, where each cell is filled with either black or white. Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels.

Thoughts:
We are going to scan column by column, checking to see if this column can be the left-border of the desired subsquare. Along each column, we build “sliding windows”, from large size to small size. The size of the window is the size of the subsquare. The winodw starts at different row, moving from top to bottom. When the window is fixed in a position, we chan check if this subsquare is valid or not. If so, we update the max subsquare we have found then continue.

Example:
111
011
000

  1. 1st column:
    1. 1st row: window size = 3: {{1,1,1,},{0,1,1},{0,0,0}} is not valid; window size = 2: {{1,1,},{0,1}} is not valid; window size = 1: {{1}} is valid, so update it.
    2. 2nd row: window size = 2: {{0,1},{0,0}} is not valid; window size = 1: no need to check. already have a current max subsquare with size 1.
  2. 2nd column:
    1. 1st row: window size = 2: {{1,1},{1,1}} is valid, so update it; window size = 1: no need to check.
    2. 2nd row: no need to check. Max subsquare can be found is of size 2, but we already have a valid one with size 2.
  3. 3rd column:No need to check.
	public static int[][] findMaxSubsquare(int[][] square) {
		int[][] maxSubsquare = null;
		int maxSize = 0;
		for (int col = 0; square.length - col > maxSize; ++col) {
			for (int row = 0; square.length - row > maxSize; ++row) {
				for (int size = square.length - Math.max(row, col); 
							size > maxSize; --size) {
					if (isValidSubsquare(square, row, col, size)) {
						maxSize = size;
						maxSubsquare = new int[size][size];
						for (int i = row; i < size + row; ++i)
							System.arraycopy(square[i], col, maxSubsquare[i
									- row], 0, size);
					}
				}
			}
		}
		return maxSubsquare;
	}

	private static boolean isValidSubsquare(int[][] square, int row, int col,
			int size) {
		// up and bottom border
		for (int j = col; j < col + size; ++j) {
			if (square[row][j] == 0)
				return false;
			if (square[row + size - 1][j] == 0)
				return false;
		}
		// left and right border
		for (int i = row; i < row + size; ++i) {
			if (square[i][col] == 0)
				return false;
			if (square[i][col + size - 1] == 0)
				return false;
		}
		return true;
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值