1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
smallest-rectangle-enclosing-black-pixels.py 1.10 KB
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 7年前 . add complexity
# Time: O(nlogn)
# Space: O(1)
import bisect
import itertools
class Solution(object):
def minArea(self, image, x, y):
"""
:type image: List[List[str]]
:type x: int
:type y: int
:rtype: int
"""
def binarySearch(left, right, find, image, has_one):
while left <= right: # O(logn) times
mid = left + (right - left) / 2
if find(image, has_one, mid): # Time: O(n)
right = mid - 1
else:
left = mid + 1
return left
searchColumns = lambda image, has_one, mid: any([int(row[mid]) for row in image]) == has_one
left = binarySearch(0, y - 1, searchColumns, image, True)
right = binarySearch(y + 1, len(image[0]) - 1, searchColumns, image, False)
searchRows = lambda image, has_one, mid: any(itertools.imap(int, image[mid])) == has_one
top = binarySearch(0, x - 1, searchRows, image, True)
bottom = binarySearch(x + 1, len(image) - 1, searchRows, image, False)
return (right - left) * (bottom - top)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助