Skip to content

223. Rectangle Area

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

js
/**
 * @param {number} A
 * @param {number} B
 * @param {number} C
 * @param {number} D
 * @param {number} E
 * @param {number} F
 * @param {number} G
 * @param {number} H
 * @return {number}
 */
var computeArea = function(A, B, C, D, E, F, G, H) {
	return (C - A) * (D - B) + (G - E) * (H - F) -
		Math.max(0, Math.min(C, G) - Math.max(A, E)) *
		Math.max(0, Math.min(D, H) - Math.max(B, F));
};
py
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 (C - A) * (D - B) + (G - E) * (H - F) - max(0, min(C, G) - max(A, E)) * max(0, min(D, H) - max(B, F))