-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxPointsonaLine.py
More file actions
37 lines (35 loc) · 1.27 KB
/
maxPointsonaLine.py
File metadata and controls
37 lines (35 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/max-points-on-a-line/
# Author: Miao Zhang
# Date: 2021-01-22
from collections import defaultdict
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
n = len(points)
res = 0
for i in range(n):
count = defaultdict()
same_points = 1
max_points = 0
for j in range(i + 1, n):
if points[i][0] == points[j][0] and points[i][1] == points[j][1]:
same_points += 1
else:
dx = points[j][0] - points[i][0]
dy = points[j][1] - points[i][1]
if dx == 0:
if 'inf' not in count:
count['inf'] = 1
else:
count['inf'] += 1
k = 'inf'
else:
k = 1.0 * dy / dx
if k not in count:
count[k] = 1
else:
count[k] += 1
max_points = max(max_points, count[k])
res = max(res, max_points + same_points)
return res