LeetCode 836. Rectangle Overlap Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

Nice approach! Can also be done with 'OR' condition-
if( Math.min(x3, x4) >= Math.max(x1, x2) || Math.min(y3, y4) >= Math.max(y1, y2)
|| Math.min(x1, x2) >= Math.max(x3, x4) || Math.min(y1, y2) >= Math.max(y3, y4) ){
return false;
}
else{
return true;
}

AjaySingh-xdnz
Автор

This is the updated solution which also checks if the rectangle is a straight line or not. ( Passes updated test cases.)

class Solution {
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
if(rec1[0] - rec1[2] == 0 || rec1[1] - rec1[3] == 0) return false;
if(rec2[0] - rec2[2] == 0 || rec2[1] - rec2[3] == 0) return false;
return rec1[0] < rec2[2] && rec1[1] < rec2[3] && rec2[0] < rec1[2] && rec2[1] < rec1[3];
}
}

prakharagarwal
Автор

This solution doesn't work anymore in leetcode, they added new test cases

CoolnesXcore
Автор

This solution does not work anymore in Leetcode probably due to updated added test cases.

prakharagarwal
Автор

This solution is not working in leetcode...could u please recheck the solution

gajatejeswarareddy
Автор

What if area of overlapping rectangles is asked ?

Spranav
Автор

thanks man, great video(s). for this one I think it easier to think about the opposite condition

omarmeshaal
Автор

Nice explanation and solution. Thanks Nick

sandipchanda
Автор

I wish I saw this video before my interview smh🤦🏾‍♂️

Kmac