HARD Math & Geometry

149. Max Points on a Line

šŸ“– Problem

Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.

🧠 Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • •Array methods (`push`, `pop`, `shift`, `slice`)
  • •Object/Map/Set usage patterns
  • •Function parameter and return typing

Logical Thinking Concepts

  • •Define invariants before coding
  • •Check edge cases first (`[]`, single element, duplicates)
  • •Estimate time/space before implementation

šŸ’” Approach

  • → For each point, calculate slope with every other point
  • → Use hash map to count points with same slope
  • → Max points = max(slope_count + duplicates + 1) for each point
  • → Time: O(n²), Space: O(n²)

šŸ› ļø Hints & Pitfalls

Hints

  • •For each point, calculate slope with every other point
  • •Use hash map to count points with same slope
  • •Max points = max(slope_count + duplicates + 1) for each point

Common Pitfalls

  • •Time: O(n²), Space: O(n²)

🧪 Test Cases

Test Case 1
Not run
Input:
maxPoints(points: number[][]);
Expected:
Computed from hidden reference
Test Case 2
Not run
Input:
maxPoints([[1,1],[2,2],[3,3]]);
Expected:
Computed from hidden reference
Test Case 3
Not run
Input:
maxPoints([[1,1],[3,2],[4,1],[2,3],[1,4]]);
Expected:
Computed from hidden reference

šŸ“ Code Editor

šŸ“š Reference Solution

ā–¼
⌘K Search āŒ˜ā†© Run ⌘S Submit