From da2d38037d52e15d2d892baff2c5d3a7da43cf4c Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 14 Apr 2026 04:33:26 -0600 Subject: [PATCH] adding algo --- .../ex_131_max_points_in_a_line.py | 26 +++++++++++++ .../ex_131_max_points_in_a_line.ts | 39 +++++++++++++++++++ ...test_131_max_points_in_a_lines_round_22.py | 29 ++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_131_max_points_in_a_line.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_131_max_points_in_a_line.ts create mode 100644 tests/test_150_questions_round_22/test_131_max_points_in_a_lines_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_131_max_points_in_a_line.py b/src/my_project/interviews/top_150_questions_round_22/ex_131_max_points_in_a_line.py new file mode 100644 index 00000000..b0cea4d4 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_131_max_points_in_a_line.py @@ -0,0 +1,26 @@ +from math import gcd +from typing import List + + +class Solution: + def maxPoints(self, points: List[List[int]]) -> int: + n = len(points) + if n <= 2: + return n + result = 2 + for i in range(n): + slopes = {} + for j in range(i + 1, n): + dx = points[j][0] - points[i][0] + dy = points[j][1] - points[i][1] + g = gcd(dx, dy) + dx //= g + dy //= g + if dx < 0: + dx, dy = -dx, -dy + elif dx == 0: + dy = abs(dy) + slopes[(dx, dy)] = slopes.get((dx, dy), 0) + 1 + if slopes: + result = max(result, max(slopes.values()) + 1) + return result \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_131_max_points_in_a_line.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_131_max_points_in_a_line.ts new file mode 100644 index 00000000..8388740b --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_131_max_points_in_a_line.ts @@ -0,0 +1,39 @@ +function maxPoints(points: number[][]): number { + const n = points.length; + if (n <= 2) return n; + let result = 2; + for (let i = 0; i < n; i++) { + const slopes = new Map(); + for (let j = i + 1; j < n; j++) { + let dx = points[j][0] - points[i][0]; + let dy = points[j][1] - points[i][1]; + const g = gcd(Math.abs(dx), Math.abs(dy)); + dx /= g; + dy /= g; + if (dx < 0) { + dx = -dx; + dy = -dy; + } else if (dx === 0) { + dy = Math.abs(dy); + } + const key = `${dx},${dy}`; + slopes.set(key, (slopes.get(key) || 0) + 1); + } + if (slopes.size > 0) { + for (const count of slopes.values()) { + result = Math.max(result, count + 1); + } + } + } + return result; +} + +function gcd(a: number, b: number): number { + while (b !== 0) { + [a, b] = [b, a % b]; + } + return a; +} + +console.log(maxPoints([[1, 1], [2, 2], [3, 3]])); +console.log(maxPoints([[1, 1], [3, 2], [5, 3], [4, 1], [2, 3], [1, 4]])); diff --git a/tests/test_150_questions_round_22/test_131_max_points_in_a_lines_round_22.py b/tests/test_150_questions_round_22/test_131_max_points_in_a_lines_round_22.py new file mode 100644 index 00000000..f02c56eb --- /dev/null +++ b/tests/test_150_questions_round_22/test_131_max_points_in_a_lines_round_22.py @@ -0,0 +1,29 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_131_max_points_in_a_line import Solution + +class MaxPointsInALineTestCase(unittest.TestCase): + + def setUp(self): + self.solution = Solution() + + def test_three_collinear_points(self): + self.assertEqual(3, self.solution.maxPoints([[1, 1], [2, 2], [3, 3]])) + + def test_six_points(self): + self.assertEqual(4, self.solution.maxPoints( + [[1, 1], [3, 2], [5, 3], [4, 1], [2, 3], [1, 4]])) + + def test_single_point(self): + self.assertEqual(1, self.solution.maxPoints([[0, 0]])) + + def test_two_points(self): + self.assertEqual(2, self.solution.maxPoints([[0, 0], [1, 1]])) + + def test_vertical_line(self): + self.assertEqual(3, self.solution.maxPoints([[1, 1], [1, 2], [1, 3]])) + + def test_horizontal_line(self): + self.assertEqual(3, self.solution.maxPoints([[1, 1], [2, 1], [3, 1]])) + +