Javascript solution


  • 0
    C
    /**
     * @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) {
    
        function val(val1, val2, val3, val4) {
        	function compare(value1, value2) {
        		if(value1>value2){
        			return 1;
        		}else if(value1<value2){
        			return -1;
        		}else{
        			return 0;
        		}
        	}
        	var arr1 = [val1, val2].sort(compare);
        	var arr2 = [val3, val4].sort(compare);
        	var arr3 = [val1, val2, val3, val4].sort(compare);
        	if((arr1[0]==arr3[0]&&arr1[1]!=arr3[1])||(arr2[0]==arr3[0]&&arr2[1]!=arr3[1])){
        		return arr3[2]-arr3[1];
        	}else{
        		return 0;
        	}
        }
    
        var Area1 = Math.abs(C-A) * Math.abs(D-B);
        var Area2 = Math.abs(G-E) * Math.abs(H-F);
        var Area3 = val(A, C, E, G) * val(B, D, F, H);
    
        return Area1 + Area2 - Area3;
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.