Day 21 - LeetCode 223. Rectangle Area

LeetCode 223. Rectangle Area

題目

Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Assume that the total area is never beyond the maximum possible value of int.

翻譯

計算兩個長方形所覆蓋的面積,每個長方形都是由左下頂點與右上頂點決定,如圖所示。
假設覆蓋面積不會過int的最大值。

想法

1.先計算兩個長方形
2.列出不重疊的條件ex: A >= G, B >= H
3.有交疊的話可以用, max()、min()取得重疊面積

詳細參閱

https://discuss.leetcode.com/topic/84906/my-simple-java-accepted-10-line-code


Code

/**
 * @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) {
    //分別計算兩個面積
    let one = Math.abs(A - C) * Math.abs(B - D);
    let two = Math.abs(E - G) * Math.abs(F - H);
    
    //符合這幾個條件代表未重疊
    if ( A >= G  || B >= H || E >= C || F >= D ) {
        return one + two;
    }
    
    //有重疊的話
    let left_bottom = Math.max(A, E);
    let right_bottom = Math.min(C, G);
    let left_high = Math.max(B, F);
    let right_high = Math.min(D, H);
    let overlapping = Math.abs(left_bottom - right_bottom) * Math.abs(left_high - right_high);

    return one + two - overlapping;
};

/**
 * @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) {
    //分別計算兩個面積
    let one = Math.abs(A - C) * Math.abs(B - D);
    let two = Math.abs(E - G) * Math.abs(F - H);

    let left = Math.max(A, E);
    let right = Math.min(C, G);
    let bottom = Math.max(B, F);
    let top = Math.min(D, H);
    let overlapping = 0;
    //有重疊的話
    if ( left < right && bottom < top ) {
        overlapping =  ( right - left ) * ( top - bottom );
    }
    return one + two - overlapping;
};

Run

留言