Lintcode600 · Smallest Rectangle Enclosing Black Pixels go

/**
600 · Smallest Rectangle Enclosing Black Pixels
Algorithms
Hard
Accepted Rate
38%

DescriptionSolutionNotesDiscussLeaderboard
Description
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.

Example
Example 1:

Input:[“0010”,“0110”,“0100”],x=0,y=2
Output:6
Explanation:
The upper left coordinate of the matrix is (0,1), and the lower right coordinate is (2,2).
Example 2:

Input:[“1110”,“1100”,“0000”,“0000”], x = 0, y = 1
Output:6
Explanation:
The upper left coordinate of the matrix is (0, 0), and the lower right coordinate is (1,2).
Tags
Company
Google

https://blog.csdn.net/roufoo/article/details/103387086

https://www.lintcode.com/problem/600/
*/

//WA, 没找到错误的地方,希望懂得的大佬指点下

import "fmt"

/**
 * @param image: a binary matrix with '0' and '1'
 * @param x: the location of one of the black pixels
 * @param y: the location of one of the black pixels
 * @return: an integer
 */
func minArea(image [][]byte, x int, y int) int {
	// write your code here
	var m int = len(image)
	if m == 0 {
		return 0
	}
	var n int = len(image[0])
	var image2 [][]byte = image
	var image3 [][]byte = image

	for i := 1; i < m; i++ {
		for j := 0; j < n; j++ {
			if image2[i-1][j] == '1' {
				image2[i][j] = '1'
			}
		}
	}

	for i := 0; i < m; i++ {
		for j := 1; j < n; j++ {
			if image3[i][j-1] == '1' {
				image3[i][j] = '1'
			}
		}
	}

	var widthX, widthY int = 0, 0
	for i := 0; i < n; i++ {
		if image2[m-1][i] == '1' {
			widthX += 1
		}
	}

	for i := 0; i < m; i++ {
		if image3[i][n-1] == '1' {
			widthY += 1
		}
	}

	return widthX * widthY
}

func main() {
	// ["0010","0110","0100"] 0 2
	var b1 [][]byte = [][]byte{[]byte("0010"), []byte("0110"), []byte("0100")}
	var r1 int = minArea(b1, 0, 2)
	fmt.Println(r1)
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值