Created
August 7, 2024 05:50
-
-
Save colonelpanic8/bcdae800aca099de8dcf43210a6e895d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
from typing import List | |
Y_AXIS = object() | |
X_AXIS = object() | |
def max_points(points: List[List[int]]) -> int: | |
if len(points) == 1: | |
return 1 | |
if not points: | |
return 0 | |
slope_intercept_forms = { | |
} | |
points = [tuple(point) for point in points] | |
for point1 in points: | |
x1, y1 = point1 | |
for point2 in points: | |
if point1 == point2: | |
continue | |
x2, y2 = point2 | |
rise = y2 - y1 | |
run = x2 - x1 | |
if run == 0: | |
slope = float("inf") | |
intercept = x1 | |
slope = Y_AXIS | |
elif rise == 0: | |
run = float("inf") | |
intercept = y1 | |
slope = X_AXIS | |
else: | |
slope = rise / run | |
intercept = y1 - (x1 * slope) | |
intercept_2 = y2 - (x2 * slope) | |
form = (slope, intercept) | |
if form in slope_intercept_forms: | |
slope_intercept_forms[form].add(point2) | |
slope_intercept_forms[form].add(point1) | |
else: | |
slope_intercept_forms[form] = set([point1, point2]) | |
maximizing_pair = max(slope_intercept_forms.items(), key=lambda kv: len(kv[1])) | |
print(maximizing_pair) | |
return len(maximizing_pair[1]) | |
class Solution: | |
def maxPoints(self, points: List[List[int]]) -> int: | |
return max_points(points) | |
points = [[7,3],[19,19],[-16,3],[13,17],[-18,1],[-18,-17],[13,-3],[3,7],[-11,12],[7,19],[19,-12],[20,-18],[-16,-15],[-10,-15],[-16,-18],[-14,-1],[18,10],[-13,8],[7,-5],[-4,-9],[-11,2],[-9,-9],[-5,-16],[10,14],[-3,4],[1,-20],[2,16],[0,14],[-14,5],[15,-11],[3,11],[11,-10],[-1,-7],[16,7],[1,-11],[-8,-3],[1,-6],[19,7],[3,6],[-1,-2],[7,-3],[-6,-8],[7,1],[-15,12],[-17,9],[19,-9],[1,0],[9,-10],[6,20],[-12,-4],[-16,-17],[14,3],[0,-1],[-18,9],[-15,15],[-3,-15],[-5,20],[15,-14],[9,-17],[10,-14],[-7,-11],[14,9],[1,-1],[15,12],[-5,-1],[-17,-5],[15,-2],[-12,11],[19,-18],[8,7],[-5,-3],[-17,-1],[-18,13],[15,-3],[4,18],[-14,-15],[15,8],[-18,-12],[-15,19],[-9,16],[-9,14],[-12,-14],[-2,-20],[-3,-13],[10,-7],[-2,-10],[9,10],[-1,7],[-17,-6],[-15,20],[5,-17],[6,-6],[-11,-8]] | |
print( max_points(points)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment