• leetcode149


     1   /*
     2      *  A line is determined by two factors,say y=ax+b
     3      *  
     4      *  If two points(x1,y1) (x2,y2) are on the same line(Of course). 
     5 
     6      *  Consider the gap between two points.
     7 
     8      *  We have (y2-y1)=a(x2-x1),a=(y2-y1)/(x2-x1) a is a rational, b is canceled since b is a constant
     9 
    10      *  If a third point (x3,y3) are on the same line. So we must have y3=ax3+b
    11 
    12      *  Thus,(y3-y1)/(x3-x1)=(y2-y1)/(x2-x1)=a
    13 
    14      *  Since a is a rational, there exists y0 and x0, y0/x0=(y3-y1)/(x3-x1)=(y2-y1)/(x2-x1)=a
    15 
    16      *  So we can use y0&x0 to track a line;
    17      */
    18     
    19     public class Solution{
    20         public int maxPoints(Point[] points) {
    21             if (points==null) return 0;
    22             if (points.length<=2) return points.length;
    23             
    24             Map<Integer,Map<Integer,Integer>> map = new HashMap<Integer,Map<Integer,Integer>>();
    25             int result=0;
    26             for (int i=0;i<points.length;i++){ 
    27                 map.clear();
    28                 int overlap=0,max=0;
    29                 for (int j=i+1;j<points.length;j++){
    30                     int x=points[j].x-points[i].x;
    31                     int y=points[j].y-points[i].y;
    32                     if (x==0&&y==0){
    33                         overlap++;
    34                         continue;
    35                     }
    36                     int gcd=generateGCD(x,y);
    37                     if (gcd!=0){
    38                         x/=gcd;
    39                         y/=gcd;
    40                     }
    41                     
    42                     if (map.containsKey(x)){
    43                         if (map.get(x).containsKey(y)){
    44                             map.get(x).put(y, map.get(x).get(y)+1);
    45                         }else{
    46                             map.get(x).put(y, 1);
    47                         }                       
    48                     }else{
    49                         Map<Integer,Integer> m = new HashMap<Integer,Integer>();
    50                         m.put(y, 1);
    51                         map.put(x, m);
    52                     }
    53                     max=Math.max(max, map.get(x).get(y));
    54                 }
    55                 result=Math.max(result, max+overlap+1);
    56             }
    57             return result;
    58             
    59             
    60         }
    61         private int generateGCD(int a,int b){
    62     
    63             if (b==0) return a;
    64             else return generateGCD(b,a%b);
    65             
    66         }
    67     }

    参考:https://leetcode.com/problems/max-points-on-a-line/discuss/47113/A-java-solution-with-notes

  • 相关阅读:
    poj1673EXOCENTER OF A TRIANGLE
    poj1474Video Surveillance(半平面交)
    poj1584A Round Peg in a Ground Hole
    poj1696Space Ant(逆时针螺旋形)
    点分治(树分治)
    LCT(link cut tree) 动态树
    树链剖分
    Codeforces Round #475 (Div. 2)
    openCV学习——一、Mat类
    openCV学习——一、图像读取、显示、输出
  • 原文地址:https://www.cnblogs.com/asenyang/p/10496370.html
Copyright © 2020-2023  润新知