일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Python
- 프로그래머스
- 109. Convert Sorted List to Binary Search Tree
- Generator
- Python Implementation
- Substring with Concatenation of All Words
- Python Code
- 컴퓨터의 구조
- 715. Range Module
- concurrency
- Class
- 30. Substring with Concatenation of All Words
- attribute
- 43. Multiply Strings
- 파이썬
- 운영체제
- Protocol
- DWG
- Regular Expression
- Convert Sorted List to Binary Search Tree
- 315. Count of Smaller Numbers After Self
- shiba
- kaggle
- 밴픽
- 시바견
- Decorator
- LeetCode
- iterator
- t1
- data science
Archives
- Today
- Total
Scribbling
LeetCode: 302. Smallest Rectangle Enclosing Black Pixels 본문
Computer Science/Coding Test
LeetCode: 302. Smallest Rectangle Enclosing Black Pixels
focalpoint 2022. 1. 25. 13:47Perhaps, the most thinkable solution would be DFS or BFS.
Traverse all the '1' points and update mins and maxs.
Time complexity would be O(M*N) in this case.
class Solution:
def minArea(self, image: List[List[str]], x: int, y: int) -> int:
m, n = len(image), len(image[0])
ymin, ymax, xmin, xmax = m, -1, n, -1
dy, dx = [0, 1, 0, -1], [1, 0, -1, 0]
stack, visited = [(x, y)], set()
while stack:
i, j = stack.pop()
ymin, ymax, xmin, xmax = \
min(ymin, i), max(ymax, i), min(xmin, j), max(xmax, j)
visited.add((i, j))
for d in range(4):
next_y, next_x = i + dy[d], j + dx[d]
if 0 <= next_y < m and 0 <= next_x < n and \
image[next_y][next_x] == '1' and (next_y, next_x) not in visited:
stack.append((next_y, next_x))
return (ymax - ymin + 1) * (xmax - xmin + 1)
There's a more efficient way to solve this problem, however. We can binary-search mins and maxs of each coordinate. Time complexity would be O(N*logM).
class Solution:
def minArea(self, image: List[List[str]], x: int, y: int) -> int:
m, n = len(image), len(image[0])
y, x = x, y
# search ymin
ymin = y
l, r = 0, y
while l <= r:
mid = (l + r) // 2
if '1' in image[mid]:
ymin = min(ymin, mid)
r = mid - 1
else:
l = mid + 1
# search ymax
ymax = y
l, r = y, m - 1
while l <= r:
mid = (l + r) // 2
if '1' in image[mid]:
ymax = max(ymax, mid)
l = mid + 1
else:
r = mid - 1
# search xmin
xmin = x
l, r = 0, x
while l <= r:
mid = (l + r) // 2
if '1' in [image[i][mid] for i in range(m)]:
xmin = min(xmin, mid)
r = mid - 1
else:
l = mid + 1
# search xmax
xmax = x
l, r = x, n - 1
while l <= r:
mid = (l + r) // 2
if '1' in [image[i][mid] for i in range(m)]:
xmax = max(xmax, mid)
l = mid + 1
else:
r = mid - 1
return (ymax - ymin + 1) * (xmax - xmin + 1)
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 366. Find Leaves of Binary Tree (0) | 2022.02.02 |
---|---|
LeetCode: 109. Convert Sorted List to Binary Search Tree (0) | 2022.01.26 |
LeetCode: 65. Valid Number (0) | 2022.01.20 |
LeetCode: 30. Substring with Concatenation of All Words (0) | 2022.01.17 |
LeetCode: 27. Remove Element (0) | 2022.01.17 |