Saturday, August 29, 2015

Leetcode 223. Rectangle Area

https://leetcode.com/problems/rectangle-area/

The thought is simple: two areas of rectangles subtract overlapping area. The key is to get the overlapping area.

Solution:
# T:O(1) S:O(1)
class Solution(object):
    def computeArea(self, A, B, C, D, E, F, G, H):
        """
        :type A: int
        :type B: int
        :type C: int
        :type D: int
        :type E: int
        :type F: int
        :type G: int
        :type H: int
        :rtype: int
        """
        return (D - B) * (C - A) + \
               (G - E) * (H - F) - \
               max(0, (min(C, G) - max(A, E))) * \
               max(0, (min(D, H) - max(B, F)))
Run Time: 148 ms

No comments:

Post a Comment