In this sum if we can check whether the cross product of the vectors ,then we can ensure whether the points are lying in the same lineā¦But how to implement that ā¦please helpā¦
Leetcode doubt easy problem
hello @Somasree
check this->
boolean checkStraightLine(int[][] coordinates) {
int n = coordinates.length;
for(int end=0;end<=n-3;end++) {
int[] c1 = coordinates[end];
int[] c2 = coordinates[end+1];
int[] c3 = coordinates[end+2];
if(!isCollinear(c1[0],c1[1],c2[0],c2[1],c3[0],c3[1])) {
return false;
}
}
return true;
}
boolean isCollinear(int x1, int y1, int x2,int y2, int x3, int y3) {
int result = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2);
return result==0;
}
I hope Iāve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.