Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Smallest Rectangle Enclosing Black Pixels

Posted on May 26, 2018July 26, 2020 by braindenny

Smallest Rectangle Enclosing Black Pixels



Similar Problems:

  • Series: Island & Follow-up
  • Number of Distinct Islands
  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #island, #dfs

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:

Input:
[
  "0010",
  "0110",
  "0100"
]
and x = 0, y = 2

Output: 6

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution: dfs island issues

Similar Problems:

  • Number of Distinct Islands
  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #island, #dfs
// https://code.dennyzhang.com/smallest-rectangle-enclosing-black-pixels
// Basic Ideas: dfs
//  region: (max_x-min_x+1)*(max_y-min_y+1)
// Complexity: Time O(n*m), Space O(1)

var min_x, max_x, min_y, max_y int
func dfs(image [][]byte, x int, y int) {
    if x<0 || x>=len(image) || y<0 || y>=len(image[0]) { return }
    if image[x][y] != '1' { return }
    image[x][y] = '2'
    if x<min_x { min_x = x }
    if x>max_x { max_x = x }
    if y<min_y { min_y = y }
    if y>max_y { max_y = y }
    dfs(image, x+1, y)
    dfs(image, x-1, y)
    dfs(image, x, y+1)
    dfs(image, x, y-1)
}
func minArea(image [][]byte, x int, y int) int {
    if len(image) == 0 { return 0 }
    min_x, max_x = x, x
    min_y, max_y = y, y
    dfs(image, x, y)
    return (max_x-min_x+1)*(max_y-min_y+1)
}
linkedin
github
slack

Post Views: 7
Posted in MediumTagged #dfs, island

Post navigation

LeetCode: Graph Valid Tree
LeetCode: Stickers to Spell Word

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.