C# Solution 228ms


  • 0
    B
    using System.Collections.Concurrent;
    
    /**
     * Definition for a point.
     * public class Point {
     *     public int x;
     *     public int y;
     *     public Point() { x = 0; y = 0; }
     *     public Point(int a, int b) { x = a; y = b; }
     * }
     */
            
    public class Solution {
        public int MaxPoints(Point[] points) {
            if (points.Length < 2) return points.Length;
            int result = 0;
            
            for (var i = 0; i < points.Length; i++){
                ConcurrentDictionary <double, int> slopes = new ConcurrentDictionary <double, int>();
                int duplicates = 0;
                
                for (var j = 0; j < points.Length; j++){
                    if (points[i].x == points[j].x && points[i].y == points[j].y){
                        duplicates++;
                        continue;
                    }
                    
                    slopes.AddOrUpdate(CalculateSlope(points[i], points[j]), 1, (id, val) => val + 1);
                }
                
                result = slopes.Values.Any() ? Math.Max(result, slopes.Values.Max() + duplicates) : Math.Max(result, duplicates);
                
            }
            
            return result;
        }
        
        public double CalculateSlope(Point a, Point b){
            double rise = b.y - a.y;
            double run = b.x - a.x;
            
            return rise == 0 || run == 0 ? 0 : rise/run;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.