Personal Blog
  • 💻Notes for Computer Science
  • Leetcode
    • Array
      • Container with most water
      • 3Sum
      • Next Permutation
      • Valid Sudoku
      • Permutation II
      • Combination Sum
      • Triangle
      • Maximal Square
      • Pairs of Songs with Total Duration Divisible by 60
      • Numbers At Most N Given Digit Set
      • Possible Sum
      • Swap Lex Order
      • Partition Equal Subset Sum
      • Domino and Tromino
      • Numbers At Most N Given Digits
      • Car Pooling
      • Surrounding Regions
      • Min Size Subarray Sum
      • Burst Balloons
      • Jump Game I
      • Jump Game II
      • House Robber II
      • Delete and Earn
      • Word Break
      • Decode Ways
      • Longest Increasing Subsequence
      • Cherry Pickup
      • Rotate Image
    • LinkedList
      • IsListPalindrome
      • Linked List Cycle
      • MergeTwoLinkedList
      • ReverseNodeInKGroup
      • RearrangeLastN
      • Remove Duplicates From Sorted List
      • RemoveKFromList
    • String
      • Generate Parentheses
      • Longest Valid Parentheses
      • Longest Common Subsequence
      • Count and Say
      • Decode String
      • Permutation in String
    • Tree
      • House Robber III
      • Convert Sorted Array to Binary Search Tree
      • Restore Binary Tree
      • Populating Next Right Pointers in Each Node II
      • Subtree of Another Tree
    • Graph
      • All Paths from Source to Target
      • Reorder Routes to Make All Paths Lead to the City Zero
      • Max Points on a Line
  • DBMS
    • DBMS Notes
  • Web App
    • Web Design
    • JavaScript
    • React.js
    • ReactNative
    • Mobile Design
    • Dialogue Flow
  • AnaplanIntern
    • Splunk
    • Docker
    • Kubernetes
  • 💰 Notes for Finance Concept
  • Analysis Concept
    • Volume Spread Analysis
    • Smart Money Concepts
Powered by GitBook
On this page
  • Idea
  • Code
  1. Leetcode
  2. Graph

Max Points on a Line

ID:149

PreviousReorder Routes to Make All Paths Lead to the City ZeroNextDBMS Notes

Last updated 3 years ago

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.

Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4

Idea

Math + Hashtable

Since the deterministic factor to decide whether two points are on a line, the slope of two is important. Therefore, we need to record the number of points with the same slope

But a key point is, like in the above example, (1,1),(2,3) and (4,1),(5,3) has the same slope, but they cannot be counted together since they are not on the same line.

Therefore, we need to construct a seperate table for each base point. For example, for base point(1,1), only (2,3) can be counted since they are of slope 2, but (4,1) and (5,3) will not be counted.

There's a special case, where two points are on a vertical line. In this case, the slope of deltaY/deltaX is undefined, since deltaX = 0. Therefore, we count this special case with another if condition.

Code

public int maxPoints(int[][] points) {
    if (points.length <= 2) return points.length;
    int result = 0;
    for (int i = 0;i<points.length;i++){
        Map<Double, Integer> map = new HashMap<>();
        int count = 1;
        int same = 0;
        for (int j = 0;j<points.length;j++){
            if(j != i){
                int firstX = points[i][0];
                int firstY = points[i][1];
            
                int secondX = points[j][0];
                int secondY = points[j][1];
    
                // vertical line
                if (firstX == secondX) {
                    count++;                    
                    continue;
                }
                double k = (double)(secondY - firstY) / (double)(secondX - firstX);
                map.put(k, map.getOrDefault(k, 1)+1);
                result = Math.max(result, map.get(k));
            }
        }
        result = Math.max(result, count);
    }
    return result;
}