Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update LC-149.mdx #4865

Merged
merged 10 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions content/5_Plat/Geo_Pri.problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"uniqueId": "leetcode-1610",
"name": "Maximum Number of Visible Points",
"url": "https://leetcode.com/problems/maximum-number-of-visible-points/description/",
"source": "LeetCode",
"source": "LC",
"difficulty": "Normal",
"isStarred": false,
"tags": ["Geometry", "Radians", "Sliding Windows"],
Expand Down Expand Up @@ -149,7 +149,7 @@
"uniqueId": "leetcode-149",
"name": "Max Points on a Line",
"url": "https://leetcode.com/problems/max-points-on-a-line/description/",
"source": "Leetcode",
"source": "LC",
"difficulty": "Easy",
"isStarred": false,
"tags": [],
Expand Down Expand Up @@ -188,7 +188,7 @@
"uniqueId": "leetcode-1453",
"name": "Maximum Number of Darts Inside of a Circular Dartboard",
"url": "https://leetcode.com/problems/maximum-number-of-darts-inside-of-a-circular-dartboard/description/",
"source": "Leetcode",
"source": "LC",
"difficulty": "Hard",
"isStarred": false,
"tags": [],
Expand Down
2 changes: 1 addition & 1 deletion solutions/platinum/leetcode-1453.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
id: leetcode-1453
source: Leetcode
source: LC
title: Maximum Number of Darts Inside of a Circular Dartboard
author: Mihnea Brebenel
---
Expand Down
108 changes: 106 additions & 2 deletions solutions/platinum/leetcode-149.mdx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
id: leetcode-149
source: Leetcode
source: LC
title: Max Points on a Line
author: Mihnea Brebenel
contributors: Rameez Parwez
---

## Explanation
Expand All @@ -18,7 +19,7 @@ In practice you'll find this formula in a product form to avoid dealing with flo

## Implementation

**Time Complexity:** $\mathcal{O}(n^3)$
**Time Complexity:** $\mathcal{O}(N^3)$

<LanguageSection>
<CPPSection>
Expand Down Expand Up @@ -84,4 +85,107 @@ class Solution {
```

</JavaSection>
<PySection>

```py
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
n = len(points)
if n <= 2:
return n

ans = 0
for i in range(n):
for j in range(i + 1, n):
p = 2 # the 2 points are collinear with themselves
for k in range(j + 1, n):
dx1 = points[i][0] - points[k][0]
dx2 = points[j][0] - points[i][0]
dy1 = points[i][1] - points[k][1]
dy2 = points[j][1] - points[i][1]

# Check if dy1 / dx1 = dy2 / dx2
# Which is the same as: dy1 * dx2 = dy2 * dx1
if dy1 * dx2 == dy2 * dx1:
p += 1

ans = max(ans, p)

return ans
```

</PySection>
</LanguageSection>

## Efficient Solution

We can take a point $A$ and compute the slopes of all lines connecting $A$ to the other points.
Two different points lie on the same line if they share the same slope.
In this way, we can determine the maximum number of points lying on the same straight line with respect to $A$.

We perform this for all other points.

## Implementation

**Time Complexity:** $\mathcal{O}(N^2)$

<LanguageSection>
<CPPSection>

```cpp
class Solution {
public:
int maxPoints(vector<vector<int>> &points) {
int n = (int)points.size();
int res = 0;
for (int i = 0; i < n; i++) {
unordered_map<float, int> slope_count;
for (int j = i + 1; j < n; j++) {
float slope = 0.0;
if (points[i][0] - points[j][0] == 0) { // avoid division by 0
slope = INT_MAX;
} else {
slope = float(points[i][1] - points[j][1]) /
float(points[i][0] - points[j][0]);
}

slope_count[slope]++;
}
for (auto &[_, pt_num] : slope_count) { res = max(res, pt_num); }
}

return res + 1;
}
};
```

</CPPSection>
<PySection>

```py
from collections import defaultdict


class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
n = len(points)
res = 0
for i in range(n):
slope_count = defaultdict(int)
for j in range(i + 1, n):
if points[i][0] == points[j][0]: # avoid division by zero
slope = float("inf")
else:
slope = (points[i][1] - points[j][1]) / (
points[i][0] - points[j][0]
)

slope_count[slope] += 1

res = max(res, max(slope_count.values(), default=0))

return res + 1
```

</PySection>
</LanguageSection>
Loading