Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Overlapping = Set Unions solution in Clear category for Rectangles Union by JamesArruda
"""
Take advantage of the integer-only condition and just build sets of unit squares
that the rectangles cover. Then the set unions takes care of overlapping for us.
"""
from typing import List, Tuple, Set
def rectangle_block(lx: int, ly: int, rx: int, ry: int) -> Set:
blocks = set()
for x in range(lx, rx):
for y in range(ly, ry):
blocks.add((x,y))
return blocks
def rectangles_union(recs: List[Tuple[int]]) -> int:
blocks = set()
for rec in recs:
blocks.update(rectangle_block(*rec))
return len(blocks)
if __name__ == '__main__':
print("Example:")
print(rectangles_union([
(6, 3, 8, 10),
(4, 8, 11, 10),
(16, 8, 19, 11)
]))
# These "asserts" are used for self-checking and not for an auto-testing
assert rectangles_union([
(6, 3, 8, 10),
(4, 8, 11, 10),
(16, 8, 19, 11)
]) == 33
assert rectangles_union([
(16, 8, 19, 11)
]) == 9
assert rectangles_union([
(16, 8, 19, 11),
(16, 8, 19, 11)
]) == 9
assert rectangles_union([
(16, 8, 16, 8)
]) == 0
assert rectangles_union([
]) == 0
print("Coding complete? Click 'Check' to earn cool rewards!")
Jan. 28, 2019