From d8b1e69168773af61dc9a385ae72170f3b50043f Mon Sep 17 00:00:00 2001 From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com> Date: Mon, 1 Jul 2024 13:16:20 +0300 Subject: [PATCH 01/15] upd --- content/5_Plat/Geo_Pri.problems.json | 12 ++++++++ solutions/platinum/leetcode-149.mdx | 41 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 solutions/platinum/leetcode-149.mdx diff --git a/content/5_Plat/Geo_Pri.problems.json b/content/5_Plat/Geo_Pri.problems.json index dbf1c31a3d..f7cde26937 100644 --- a/content/5_Plat/Geo_Pri.problems.json +++ b/content/5_Plat/Geo_Pri.problems.json @@ -144,6 +144,18 @@ "solutionMetadata": { "kind": "internal" } + }, + { + "uniqueId": "leetcode-149", + "name": "Max Points on a Line", + "url": "https://leetcode.com/problems/max-points-on-a-line/description/", + "source": "Kattis", + "difficulty": "Easy", + "isStarred": false, + "tags": [], + "solutionMetadata": { + "kind": "internal" + } } ], "other": [ diff --git a/solutions/platinum/leetcode-149.mdx b/solutions/platinum/leetcode-149.mdx new file mode 100644 index 0000000000..4d47391fed --- /dev/null +++ b/solutions/platinum/leetcode-149.mdx @@ -0,0 +1,41 @@ +--- +id: leetcode-149 +source: Leetcode +title: Max Points on a Line +author: Mihnea Brebenel +--- + +## Explanation + + + +## Implementation + +**Time Complexity:** $\mathcal{O}(N^3)$ + + + +```cpp +class Solution { +public: + int maxPoints(vector>& points) { + if((int)points.size() <= 2) { return (int)points.size(); } + int ans = 0; + for(int i = 0; i < (int)points.size(); i++) { + for(int j = i + 1; j < (int)points.size(); j++) { + int p = 0; + for(int k = j + 1; k < (int)points.size(); k++) { + if((points[j][1]-points[i][1])*(points[i][0]-points[k][0])==(points[i][1]-points[k][1])*(points[j][0]-points[i][0])) { + p++; + } + } + ans = max(ans, p); + } + } + return ans; + } +}; +``` + + + \ No newline at end of file From 83b2796b8eb2c72d1bd2366b255bc905890b5ef3 Mon Sep 17 00:00:00 2001 From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com> Date: Mon, 1 Jul 2024 13:29:44 +0300 Subject: [PATCH 02/15] upd --- solutions/platinum/leetcode-149.mdx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/solutions/platinum/leetcode-149.mdx b/solutions/platinum/leetcode-149.mdx index 4d47391fed..fa891f7953 100644 --- a/solutions/platinum/leetcode-149.mdx +++ b/solutions/platinum/leetcode-149.mdx @@ -7,11 +7,12 @@ author: Mihnea Brebenel ## Explanation - +Because $n \le 300$ we can choose two points and check the other for identical slopes. ## Implementation -**Time Complexity:** $\mathcal{O}(N^3)$ +**Time Complexity:** $\mathcal{O}(n^3)$ + From df7c182e22c68c4472eb1133c8cc6935d45fe917 Mon Sep 17 00:00:00 2001 From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com> Date: Mon, 1 Jul 2024 13:32:20 +0300 Subject: [PATCH 03/15] upd --- solutions/platinum/leetcode-149.mdx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/solutions/platinum/leetcode-149.mdx b/solutions/platinum/leetcode-149.mdx index fa891f7953..4b7aa8adc0 100644 --- a/solutions/platinum/leetcode-149.mdx +++ b/solutions/platinum/leetcode-149.mdx @@ -7,7 +7,8 @@ author: Mihnea Brebenel ## Explanation -Because $n \le 300$ we can choose two points and check the other for identical slopes. +Because $n \le 300$ we can choose two points and check if other point are collinear. +Three points $A$, $B$, and $c$ are collinear if and only if $AB$ has the same slope as $BC$: ## Implementation From e1b5de1ce56a29a454fbd7374279d72a92444744 Mon Sep 17 00:00:00 2001 From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com> Date: Mon, 1 Jul 2024 13:34:18 +0300 Subject: [PATCH 04/15] upd --- solutions/platinum/leetcode-149.mdx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/solutions/platinum/leetcode-149.mdx b/solutions/platinum/leetcode-149.mdx index 4b7aa8adc0..7baffebb78 100644 --- a/solutions/platinum/leetcode-149.mdx +++ b/solutions/platinum/leetcode-149.mdx @@ -10,6 +10,12 @@ author: Mihnea Brebenel Because $n \le 300$ we can choose two points and check if other point are collinear. Three points $A$, $B$, and $c$ are collinear if and only if $AB$ has the same slope as $BC$: +$$ +\frac{A.y-B.y}{A.x-B.x}=\frac{B.y-C.y}{B.x-C.x} +$$ + +In practice you find this formula in a product form + ## Implementation **Time Complexity:** $\mathcal{O}(n^3)$ From 32a45d14a13939cfcfb4f896308d0b371e999b8b Mon Sep 17 00:00:00 2001 From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com> Date: Mon, 1 Jul 2024 13:53:58 +0300 Subject: [PATCH 05/15] upd --- solutions/platinum/leetcode-149.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/platinum/leetcode-149.mdx b/solutions/platinum/leetcode-149.mdx index 7baffebb78..e587c8477e 100644 --- a/solutions/platinum/leetcode-149.mdx +++ b/solutions/platinum/leetcode-149.mdx @@ -14,7 +14,7 @@ $$ \frac{A.y-B.y}{A.x-B.x}=\frac{B.y-C.y}{B.x-C.x} $$ -In practice you find this formula in a product form +In practice you find this formula in a product form to avoid dealing with floating-point values. ## Implementation From 9661570952e4d092659e6e1f8a0caa0bb96b2cfd Mon Sep 17 00:00:00 2001 From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com> Date: Mon, 1 Jul 2024 14:36:28 +0300 Subject: [PATCH 06/15] upd --- solutions/platinum/leetcode-149.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/platinum/leetcode-149.mdx b/solutions/platinum/leetcode-149.mdx index e587c8477e..c4224a22bd 100644 --- a/solutions/platinum/leetcode-149.mdx +++ b/solutions/platinum/leetcode-149.mdx @@ -14,7 +14,7 @@ $$ \frac{A.y-B.y}{A.x-B.x}=\frac{B.y-C.y}{B.x-C.x} $$ -In practice you find this formula in a product form to avoid dealing with floating-point values. +In practice you'll find this formula in a product form to avoid dealing with floating-point values. ## Implementation From 5ebc2c3022137f05a896def69ffa2229984542f8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 1 Jul 2024 11:41:55 +0000 Subject: [PATCH 07/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/platinum/leetcode-149.mdx | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/solutions/platinum/leetcode-149.mdx b/solutions/platinum/leetcode-149.mdx index c4224a22bd..d812d35bf4 100644 --- a/solutions/platinum/leetcode-149.mdx +++ b/solutions/platinum/leetcode-149.mdx @@ -5,9 +5,9 @@ title: Max Points on a Line author: Mihnea Brebenel --- -## Explanation +## Explanation -Because $n \le 300$ we can choose two points and check if other point are collinear. +Because $n \le 300$ we can choose two points and check if other point are collinear. Three points $A$, $B$, and $c$ are collinear if and only if $AB$ has the same slope as $BC$: $$ @@ -16,24 +16,27 @@ $$ In practice you'll find this formula in a product form to avoid dealing with floating-point values. -## Implementation +## Implementation **Time Complexity:** $\mathcal{O}(n^3)$ -```cpp +```cpp class Solution { -public: - int maxPoints(vector>& points) { - if((int)points.size() <= 2) { return (int)points.size(); } + public: + int maxPoints(vector> &points) { + if ((int)points.size() <= 2) { return (int)points.size(); } int ans = 0; - for(int i = 0; i < (int)points.size(); i++) { - for(int j = i + 1; j < (int)points.size(); j++) { + for (int i = 0; i < (int)points.size(); i++) { + for (int j = i + 1; j < (int)points.size(); j++) { int p = 0; - for(int k = j + 1; k < (int)points.size(); k++) { - if((points[j][1]-points[i][1])*(points[i][0]-points[k][0])==(points[i][1]-points[k][1])*(points[j][0]-points[i][0])) { + for (int k = j + 1; k < (int)points.size(); k++) { + if ((points[j][1] - points[i][1]) * + (points[i][0] - points[k][0]) == + (points[i][1] - points[k][1]) * + (points[j][0] - points[i][0])) { p++; } } @@ -41,9 +44,9 @@ public: } } return ans; - } + } }; ``` - \ No newline at end of file + From ef5bd46b5da639198357948be3da18f5a4e5a53f Mon Sep 17 00:00:00 2001 From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com> Date: Mon, 1 Jul 2024 14:52:02 +0300 Subject: [PATCH 08/15] upd --- solutions/platinum/leetcode-149.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/platinum/leetcode-149.mdx b/solutions/platinum/leetcode-149.mdx index d812d35bf4..59d2288617 100644 --- a/solutions/platinum/leetcode-149.mdx +++ b/solutions/platinum/leetcode-149.mdx @@ -7,7 +7,7 @@ author: Mihnea Brebenel ## Explanation -Because $n \le 300$ we can choose two points and check if other point are collinear. +Because $n \le 300$ we can use a brute-force approach: choose two points and check if other point are collinear them. Three points $A$, $B$, and $c$ are collinear if and only if $AB$ has the same slope as $BC$: $$ From 2f199d097f15757be2d5549c550a91ffdd05422b Mon Sep 17 00:00:00 2001 From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com> Date: Mon, 1 Jul 2024 15:09:13 +0300 Subject: [PATCH 09/15] upd --- content/5_Plat/Geo_Pri.problems.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/5_Plat/Geo_Pri.problems.json b/content/5_Plat/Geo_Pri.problems.json index f7cde26937..c77307a41c 100644 --- a/content/5_Plat/Geo_Pri.problems.json +++ b/content/5_Plat/Geo_Pri.problems.json @@ -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": "Kattis", + "source": "Leetcode", "difficulty": "Easy", "isStarred": false, "tags": [], From aef35624791c04bc68c2c0187563a180daab8089 Mon Sep 17 00:00:00 2001 From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com> Date: Mon, 1 Jul 2024 15:10:05 +0300 Subject: [PATCH 10/15] upd --- solutions/platinum/leetcode-149.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/platinum/leetcode-149.mdx b/solutions/platinum/leetcode-149.mdx index 59d2288617..53bc0ed8ab 100644 --- a/solutions/platinum/leetcode-149.mdx +++ b/solutions/platinum/leetcode-149.mdx @@ -7,7 +7,7 @@ author: Mihnea Brebenel ## Explanation -Because $n \le 300$ we can use a brute-force approach: choose two points and check if other point are collinear them. +Because $n \le 300$ we can use a brute-force approach: choose two points and check if other point are collinear with them. Three points $A$, $B$, and $c$ are collinear if and only if $AB$ has the same slope as $BC$: $$ From a6f1b1d64475e922cc12d7bd8034b30d4f2fe1c6 Mon Sep 17 00:00:00 2001 From: SansPapyrus683 <55369003+SansPapyrus683@users.noreply.github.com> Date: Mon, 1 Jul 2024 05:54:23 -0700 Subject: [PATCH 11/15] Update leetcode-149.mdx --- solutions/platinum/leetcode-149.mdx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/solutions/platinum/leetcode-149.mdx b/solutions/platinum/leetcode-149.mdx index 53bc0ed8ab..ea4a873f5d 100644 --- a/solutions/platinum/leetcode-149.mdx +++ b/solutions/platinum/leetcode-149.mdx @@ -27,12 +27,13 @@ In practice you'll find this formula in a product form to avoid dealing with flo class Solution { public: int maxPoints(vector> &points) { - if ((int)points.size() <= 2) { return (int)points.size(); } + if (points.size() <= 2) { return (int)points.size(); } + int ans = 0; - for (int i = 0; i < (int)points.size(); i++) { - for (int j = i + 1; j < (int)points.size(); j++) { + for (int i = 0; i < points.size(); i++) { + for (int j = i + 1; j < points.size(); j++) { int p = 0; - for (int k = j + 1; k < (int)points.size(); k++) { + for (int k = j + 1; k < points.size(); k++) { if ((points[j][1] - points[i][1]) * (points[i][0] - points[k][0]) == (points[i][1] - points[k][1]) * @@ -43,6 +44,7 @@ class Solution { ans = max(ans, p); } } + return ans; } }; From fef60e8bd4147d2d26e4596ec6fcea452ca9f690 Mon Sep 17 00:00:00 2001 From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com> Date: Mon, 1 Jul 2024 16:37:04 +0300 Subject: [PATCH 12/15] upd --- solutions/platinum/leetcode-149.mdx | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/solutions/platinum/leetcode-149.mdx b/solutions/platinum/leetcode-149.mdx index ea4a873f5d..7f978a9840 100644 --- a/solutions/platinum/leetcode-149.mdx +++ b/solutions/platinum/leetcode-149.mdx @@ -11,7 +11,7 @@ Because $n \le 300$ we can use a brute-force approach: choose two points and che Three points $A$, $B$, and $c$ are collinear if and only if $AB$ has the same slope as $BC$: $$ -\frac{A.y-B.y}{A.x-B.x}=\frac{B.y-C.y}{B.x-C.x} +\frac{A_y-B_y}{A_x-B_x}=\frac{B_y-C_y}{B_x-C_x} $$ In practice you'll find this formula in a product form to avoid dealing with floating-point values. @@ -25,28 +25,27 @@ In practice you'll find this formula in a product form to avoid dealing with flo ```cpp class Solution { - public: - int maxPoints(vector> &points) { - if (points.size() <= 2) { return (int)points.size(); } - +public: + int maxPoints(vector>& points) { + if((int)points.size() <= 2) { return (int)points.size(); } int ans = 0; - for (int i = 0; i < points.size(); i++) { - for (int j = i + 1; j < points.size(); j++) { - int p = 0; - for (int k = j + 1; k < points.size(); k++) { - if ((points[j][1] - points[i][1]) * - (points[i][0] - points[k][0]) == - (points[i][1] - points[k][1]) * - (points[j][0] - points[i][0])) { + for(int i = 0; i < (int)points.size(); i++) { + for(int j = i + 1; j < (int)points.size(); j++) { + int p = 2; + for(int k = j + 1; k < (int)points.size(); k++) { + int dx1 = points[i][0] - points[k][0], dx2 = points[j][0] - points[i][0]; + int 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++; } } ans = max(ans, p); } } - return ans; - } + } }; ``` From 8cd8fa307cd6e5a56b69aed1c100d901891c1f8b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 1 Jul 2024 13:38:16 +0000 Subject: [PATCH 13/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/platinum/leetcode-149.mdx | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/solutions/platinum/leetcode-149.mdx b/solutions/platinum/leetcode-149.mdx index 7f978a9840..b5c9d31289 100644 --- a/solutions/platinum/leetcode-149.mdx +++ b/solutions/platinum/leetcode-149.mdx @@ -25,27 +25,27 @@ In practice you'll find this formula in a product form to avoid dealing with flo ```cpp class Solution { -public: - int maxPoints(vector>& points) { - if((int)points.size() <= 2) { return (int)points.size(); } + public: + int maxPoints(vector> &points) { + if ((int)points.size() <= 2) { return (int)points.size(); } int ans = 0; - for(int i = 0; i < (int)points.size(); i++) { - for(int j = i + 1; j < (int)points.size(); j++) { + for (int i = 0; i < (int)points.size(); i++) { + for (int j = i + 1; j < (int)points.size(); j++) { int p = 2; - for(int k = j + 1; k < (int)points.size(); k++) { - int dx1 = points[i][0] - points[k][0], dx2 = points[j][0] - points[i][0]; - int dy1 = points[i][1] - points[k][1], dy2 = points[j][1] - points[i][1]; + for (int k = j + 1; k < (int)points.size(); k++) { + int dx1 = points[i][0] - points[k][0], + dx2 = points[j][0] - points[i][0]; + int 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++; - } + if (dy1 * dx2 == dy2 * dx1) { p++; } } ans = max(ans, p); } } return ans; - } + } }; ``` From ec91ff4bb0c8d3fc18149c58d58e1f0a5c192ff0 Mon Sep 17 00:00:00 2001 From: SansPapyrus683 <55369003+SansPapyrus683@users.noreply.github.com> Date: Mon, 1 Jul 2024 15:16:26 -0700 Subject: [PATCH 14/15] Update leetcode-149.mdx --- solutions/platinum/leetcode-149.mdx | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/solutions/platinum/leetcode-149.mdx b/solutions/platinum/leetcode-149.mdx index b5c9d31289..605dda8aff 100644 --- a/solutions/platinum/leetcode-149.mdx +++ b/solutions/platinum/leetcode-149.mdx @@ -7,8 +7,8 @@ author: Mihnea Brebenel ## Explanation -Because $n \le 300$ we can use a brute-force approach: choose two points and check if other point are collinear with them. -Three points $A$, $B$, and $c$ are collinear if and only if $AB$ has the same slope as $BC$: +Because $n \le 300$ we can naively go through all pairs of points and check how many other points are collinear with them. +Three points $A$, $B$, and $C$ are collinear if and only if $AB$ has the same slope as $BC$: $$ \frac{A_y-B_y}{A_x-B_x}=\frac{B_y-C_y}{B_x-C_x} @@ -27,12 +27,13 @@ In practice you'll find this formula in a product form to avoid dealing with flo class Solution { public: int maxPoints(vector> &points) { - if ((int)points.size() <= 2) { return (int)points.size(); } + if (points.size() <= 2) { return points.size(); } + int ans = 0; - for (int i = 0; i < (int)points.size(); i++) { - for (int j = i + 1; j < (int)points.size(); j++) { - int p = 2; - for (int k = j + 1; k < (int)points.size(); k++) { + for (int i = 0; i < points.size(); i++) { + for (int j = i + 1; j < points.size(); j++) { + int p = 2; // the 2 points are collinear with themselves + for (int k = j + 1; k < points.size(); k++) { int dx1 = points[i][0] - points[k][0], dx2 = points[j][0] - points[i][0]; int dy1 = points[i][1] - points[k][1], @@ -41,9 +42,11 @@ class Solution { // Which is the same as: dy1 * dx2 = dy2 * dx1 if (dy1 * dx2 == dy2 * dx1) { p++; } } + ans = max(ans, p); } } + return ans; } }; From 972874a41e26538e9a2b527ba7df0da7910487fa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 4 Jul 2024 07:54:29 +0000 Subject: [PATCH 15/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- content/5_Plat/Geo_Pri.problems.json | 1 - 1 file changed, 1 deletion(-) diff --git a/content/5_Plat/Geo_Pri.problems.json b/content/5_Plat/Geo_Pri.problems.json index 84480efef3..517d24fde1 100644 --- a/content/5_Plat/Geo_Pri.problems.json +++ b/content/5_Plat/Geo_Pri.problems.json @@ -146,7 +146,6 @@ } }, { - "uniqueId": "leetcode-149", "name": "Max Points on a Line", "url": "https://leetcode.com/problems/max-points-on-a-line/description/",