JavaScript Straight Forward O(1) Time O(1) Space


  • 0
    Z
    /**
     * @param {number[]} p1
     * @param {number[]} p2
     * @param {number[]} p3
     * @param {number[]} p4
     * @return {boolean}
     */
    var validSquare = function(p1, p2, p3, p4) {
        function lExp2(q0,q1){
            return (q0[0]-q1[0])*(q0[0]-q1[0]) + (q0[1]-q1[1])*(q0[1]-q1[1]);
        }
        let l1 = lExp2(p1,p2),
            l2 = lExp2(p1,p3),
            l3 = lExp2(p1,p4),
            l4 = lExp2(p2,p3),
            l5 = lExp2(p2,p4),
            l6 = lExp2(p3,p4),arr=[l1,l2,l3,l4,l5,l6],ma=0,mi=0;
        let max = Math.max(l1,l2,l3,l4,l5,l6),min = Math.min(l1,l2,l3,l4,l5,l6);
        if (max !== 2*min) return false;
        for(let l of arr){
            if(l === max) ma++;
            if(l === min) mi++;
        }
        return ma===2&&mi===4;
    };
    

Log in to reply
 

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