713996. Maximum Intersecting Lines
Maximum Intersecting Lines
Slug: maximum-intersecting-lines--170647 Difficulty: Medium Id: 713996 Topic Tags: Mathematical, Sorting, Geometric, Algorithms Company Tags: Swiggy
Summary
The problem is to find the maximum number of lines that intersect with each other. Given a set of lines in the form of (x1, y1) and (x2, y2), where (x1, y1) and (x2, y2) are the coordinates of the two points defining the line. The task is to find the maximum number of lines that intersect with each other.
Detailed Explanation
The problem can be solved by sorting all the lines based on their slopes. If two lines have the same slope, then we sort them based on their y-intercepts. Now, for each pair of adjacent lines in the sorted order, if they are parallel or coincident (i.e., having the same slope), we increment a counter. This counter will give us the maximum number of lines that can intersect with each other.
Here is the step-by-step breakdown:
- Initialize a variable
maxIntersectionsto 0. - Sort all the lines based on their slopes.
- Iterate over all the sorted lines and for each pair of adjacent lines, check if they are parallel or coincident.
- If they are, increment
maxIntersections. - Return
maxIntersections.
The time complexity of this algorithm is O(n log n) due to sorting, where n is the number of lines. The space complexity is O(1) as we only need a constant amount of space.
Optimized Solutions
Java
java
public class Main {
public static int maximumIntersections(int[][] coordinates) {
Arrays.sort(coordinates, (a, b) -> {
double slope1 = (double)(b[3] - a[3]) / (b[2] - a[2]);
double slope2 = (double)(b[1] - a[1]) / (b[0] - a[0]);
if (slope1 == slope2) {
return Double.compare(a[3], b[3]);
}
return Double.compare(slope1, slope2);
});
int maxIntersections = 0;
for (int i = 0; i < coordinates.length - 1; i++) {
double slope1 = (double)(coordinates[i + 1][3] - coordinates[i][3]) / (coordinates[i + 1][2] - coordinates[i][2]);
double slope2 = (double)(coordinates[i + 1][1] - coordinates[i][1]) / (coordinates[i + 1][0] - coordinates[i][0]);
if (slope1 == slope2) {
maxIntersections++;
}
}
return maxIntersections;
}
}
Python
python
def maximum_intersections(coordinates):
coordinates.sort(key=lambda x: ((x[3] - x[1]) / (x[2] - x[0]), x[1]))
max_intersections = 0
for i in range(len(coordinates) - 1):
slope1 = (coordinates[i + 1][1] - coordinates[i][1]) / (coordinates[i + 1][0] - coordinates[i][0])
slope2 = (coordinates[i + 1][3] - coordinates[i][3]) / (coordinates[i + 1][2] - coordinates[i][2])
if slope1 == slope2:
max_intersections += 1
return max_intersections
C++
cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int maximumIntersections(vector<vector<int>>& coordinates) {
sort(coordinates.begin(), coordinates.end(),
[](const vector<int>& a, const vector<int>& b) {
double slope1 = (double)(b[3] - a[3]) / (b[2] - a[2]);
double slope2 = (double)(b[1] - a[1]) / (b[0] - a[0]);
if (slope1 == slope2) {
return a[3] < b[3];
}
return slope1 < slope2;
});
int maxIntersections = 0;
for (int i = 0; i < coordinates.size() - 1; i++) {
double slope1 = (double)(coordinates[i + 1][3] - coordinates[i][3]) / (coordinates[i + 1][2] - coordinates[i][2]);
double slope2 = (double)(coordinates[i + 1][1] - coordinates[i][1]) / (coordinates[i + 1][0] - coordinates[i][0]);
if (slope1 == slope2) {
maxIntersections++;
}
}
return maxIntersections;
}
int main() {
vector<vector<int>> coordinates = {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}};
cout << maximumIntersections(coordinates) << endl;
return 0;
}
JavaScript
javascript
function maximumIntersections(coordinates) {
coordinates.sort((a, b) => {
const slope1 = (b[3] - a[3]) / (b[2] - a[2]);
const slope2 = (b[1] - a[1]) / (b[0] - a[0]);
if (slope1 === slope2) {
return a[3] - b[3];
}
return slope1 - slope2;
});
let maxIntersections = 0;
for (let i = 0; i < coordinates.length - 1; i++) {
const slope1 = (coordinates[i + 1][3] - coordinates[i][3]) / (coordinates[i + 1][2] - coordinates[i][2]);
const slope2 = (coordinates[i + 1][1] - coordinates[i][1]) / (coordinates[i + 1][0] - coordinates[i][0]);
if (slope1 === slope2) {
maxIntersections++;
}
}
return maxIntersections;
}
Not available