My code is hitting a time limit exceeded, but I don't see a way to do this with any fewer loops. I took a look at some of the other solutions in the discussions here and they do not seem to be solving the same question/problem in my eyes, so then I'm wondering if I am misinterpreting the question?

In my interpretation of the question, we need to iterate over all possible combinations of end points and then look at the remaining points and count how many would fall on the line defined by the end points. If that number exceeds the maximum count so far, we update the maximum count. Then we choose two new end points and repeat. So it is an O(n^3) problem by nature.

I have posted my code below. I added the map to add some caching to try and eliminate the time exceeded, but it still exceeds the time.

Am I misinterpreting the question/problem?

```
class Solution {
public:
int maxPoints(vector<Point> &points) {
if(points.size() < 3) {
return points.size();
}
int maxCount = 0;
// choose two points and then given those two points, iterate over all
// of the others and count how many would be on the line given by the
// two chosen points. if that count is greater than the max count so
// far, update the max count.
for(size_t i = 0; i < points.size(); i++) {
for(size_t j = 0; j < points.size(); j++) {
if(i != j) {
int count = 0;
for(size_t k = 0; k < points.size(); k++) {
if ((k != i) && (k != j))
{
if(onLineMap.find(tuple<int,int,int>(i,j,k)) != onLineMap.end()) {
if(onLineMap[tuple<int,int,int>(i,j,k)]) {
count++;
}
}
else if(onLineMap.find(tuple<int,int,int>(i,k,j)) != onLineMap.end()) {
if(onLineMap[tuple<int,int,int>(i,k,j)]) {
count++;
}
}
else if(onLineMap.find(tuple<int,int,int>(j,i,k)) != onLineMap.end()) {
if(onLineMap[tuple<int,int,int>(j,i,k)]) {
count++;
}
}
else if(onLineMap.find(tuple<int,int,int>(j,k,i)) != onLineMap.end()) {
if(onLineMap[tuple<int,int,int>(j,k,i)]) {
count++;
}
}
else if(onLineMap.find(tuple<int,int,int>(k,i,j)) != onLineMap.end()) {
if(onLineMap[tuple<int,int,int>(k,i,j)]) {
count++;
}
}
else if(onLineMap.find(tuple<int,int,int>(k,j,i)) != onLineMap.end()) {
if(onLineMap[tuple<int,int,int>(k,j,i)]) {
count++;
}
}
else {
if(onLine(points[i], points[j], points[k])) {
onLineMap[tuple<int,int,int>(i,j,k)] = true;
count++;
} else {
onLineMap[tuple<int,int,int>(i,j,k)] = false;
}
}
}
}
if(count > maxCount ) {
maxCount = count;
}
}
}
}
return maxCount;
}
bool onLine(const Point& a, const Point& b, const Point& p) {
if(a.x == b.x) {
// vertical
if(p.x == a.x) {
return true;
}
} else if (a.y == b.y) {
// horizontal
if(p.y == a.y) {
return true;
}
} else {
double m = (b.y - a.y) / (b.x - a.x);
// y = mx + b
// b = y - mx
double b = a.y - m * a.x;
return (p.y == ((m * p.x) + b));
}
}
map<tuple<int, int, int>, bool> onLineMap;
};
```