# **Leetcode筆記(Max Points on a Line)** :::info :information_source: 題目 : Max Points on a Line, 類型 : math , 等級 : hard 日期 : 2024/03/31 ::: ### 嘗試 ```python ``` --- ### **優化** 一個座標 不停的和後面的座標配對 dict 紀錄的是 這個特定的slope 有幾個相同的後面座標 ```python class Solution: def maxPoints(self, points: List[List[int]]) -> int: # 一個座標 不停的和後面的座標配對 # dict 紀錄的是 這個特定的slope 有幾個相同的後面座標 res = 0 for i, p1 in enumerate(points): record = collections.defaultdict(int) for j, p2 in enumerate(points[i + 1:]): # 計算slope # 垂直 (y2 - y1 / x2 - x1)數學會報錯 slope設為無限 if p1[0] == p2[0]: slope = float("inf") else: slope = (p2[1] - p1[1]) / (p2[0] - p1[0]) record[slope] += 1 res = max(res, record[slope]) # 加上p1自己本身 return res + 1 ``` --- **思路** **講解連結** https://www.youtube.com/watch?v=Bb9lOXUOnFw Provided by. Neetcode
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up