Created
March 8, 2025 20:05
-
-
Save primaryobjects/dea2f595b48a84016d64e83523fb880b to your computer and use it in GitHub Desktop.
Rectangle overlap https://leetcode.com/problems/rectangle-overlap/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isRectangleOverlap(rec1: number[], rec2: number[]): boolean { | |
const [ rec1_x1, rec1_y1, rec1_x2, rec1_y2 ] = rec1; | |
const [ rec2_x1, rec2_y1, rec2_x2, rec2_y2 ] = rec2; | |
const isOverlapX: boolean = rec1_x1 < rec2_x2 && rec2_x1 < rec1_x2; | |
const isOverlapY: boolean = rec1_y1 < rec2_y2 && rec2_y1 < rec1_y2; | |
return isOverlapX && isOverlapY; | |
}; | |
function isRectangleOverlap(rec1: number[], rec2: number[]): boolean { | |
const isOverlapX: boolean = Math.min(rec1[2], rec2[2]) > Math.max(rec1[0], rec2[0]); | |
const isOverlapY: boolean = Math.min(rec1[3], rec2[3]) > Math.max(rec1[1], rec2[1]); | |
return isOverlapX && isOverlapY; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[0,0,2,2] | |
[1,1,3,3] | |
[0,0,1,1] | |
[1,0,2,1] | |
[0,0,1,1] | |
[2,2,3,3] | |
[7,8,13,15] | |
[10,8,12,20] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment