C# solution: loop each point to calculate k=(x1-x2)/(y1-y2)


  • 0
    B
    public class Solution 
    {
        public int MaxPoints(Point[] points) 
        {
            var globalMax = 0;
            for (int i = 0; i < points.Length; i++)
            {
                var kAndCount = new Dictionary<double, int>();
    
                var samePointCount = 0;
                var kEqualsZeroCount = 0;
    
                for (int j = 0; j < points.Length; j++)
                {
    
                    var pointX = points[i];
                    var pointY = points[j];
    
                    if (pointX.x == pointY.x && pointX.y == pointY.y)
                    {
                        samePointCount++;
                    }
                    else if (pointX.y == pointY.y)
                    {
                        kEqualsZeroCount++;
                    }
                    else
                    {
                        var k = (double)(pointX.x - pointY.x)/(pointX.y - pointY.y);
    
                        if (!kAndCount.ContainsKey(k))  kAndCount[k] = 0;
    
                        kAndCount[k]++;
                    }
                }
    
                var localMax = 0;
    
                if (kAndCount.Values.Count == 0)    localMax = kEqualsZeroCount + samePointCount;
                else                                localMax = Math.Max(kAndCount.Values.Max(), kEqualsZeroCount) + samePointCount;
    
                globalMax = Math.Max(globalMax, localMax);
            }
    
            return globalMax;
        }
    }
    

Log in to reply
 

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