Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 149 · Other

Max Points on a Line

LeetCode 149 · ಕಠಿಣ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Per anchor, group other points by slope.

ಕಠಿಣArrayHash TableMathGeometryEuclidean Algorithm
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Max Points on a Line (LeetCode 149) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Other ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಕಠಿಣ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

Fix one point as an anchor. Every other point lies on a line through the anchor determined by its slope, so points with equal slope are on the same line. Count slopes per anchor and keep the largest group (plus the anchor).

Approach

  1. 1

    For each anchor i, a fresh slope → count map.

  2. 2

    Vertical lines get slope ∞, horizontal 0.

  3. 3

    Increment the count for each later point's slope.

  4. 4

    Answer = best count + 1 (the anchor itself).

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ

Slope hashingPythonO(n²) time
class Solution:
    def maxPoints(self, points: List[List[int]]) -> int:
        #we need to calculate slope
        #if x axis is same - infinite slope
        #if y axis is same - slope is zero
        #hash the slope, key - slope, val - points
        #get the max
        def calculate_slope(point1,point2):
            x1,y1 = point1
            x2,y2 = point2
            if x1==x2:
                return float("inf")
            elif y1==y2:
                return 0
            return (y2-y1)/(x2-x1)
        max_points = 0
        n = len(points)
        for i in range(n):
            slope_hash = defaultdict(int)
            point1 = points[i]
            for j in range(i+1,n):
                point2= points[j]
                slope = calculate_slope(point1,point2)
                slope_hash[slope]+=1
                max_points=max(max_points,slope_hash[slope])
        return max_points+1 #add the starting point
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Max Points on a Line — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Max Points on a Line ಹಿಂದಿನ intuition ಏನು?

Fix one point as an anchor. Every other point lies on a line through the anchor determined by its slope, so points with equal slope are on the same line. Count slopes per anchor and keep the largest group (plus the anchor).

Max Points on a Line (LeetCode 149) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. For each anchor i, a fresh slope → count map. 2. Vertical lines get slope ∞, horizontal 0. 3. Increment the count for each later point's slope. 4. Answer = best count + 1 (the anchor itself).

Max Points on a Line ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(n²). Space: O(n). (Accepted approach: Slope hashing.)

Max Points on a Line ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

Floating-point slopes can collide or differ by rounding; a reduced (dy, dx) pair with gcd is exact.