classSolution { public: intgcd(int a, int b){ return b ? gcd(b, a % b) : a; }
intmaxPoints(vector<vector<int>>& points){ int n = points.size(); if (n <= 2) { return n; } int ret = 0; for (int i = 0; i < n; i++) { if (ret >= n - i || ret > n / 2) { break; } unordered_map<int, int> mp; for (int j = i + 1; j < n; j++) { int x = points[i][0] - points[j][0]; int y = points[i][1] - points[j][1]; if (x == 0) { y = 1; } elseif (y == 0) { x = 1; } else { if (y < 0) { x = -x; y = -y; } int gcdXY = gcd(abs(x), abs(y)); x /= gcdXY, y /= gcdXY; } mp[y + x * 20001]++; } int maxn = 0; for (auto& [_, num] : mp) { maxn = max(maxn, num + 1); } ret = max(ret, maxn); } return ret; } };
publicclassSolution { publicintMaxPoints(int[][] points) { int n = points.Length; if (n <= 2) { return n; } int ret = 0; for (int i = 0; i < n; i++) { if (ret >= n - i || ret > n / 2) { break; } Dictionary<int, int> dictionary = new Dictionary<int, int>(); for (int j = i + 1; j < n; j++) { int x = points[i][0] - points[j][0]; int y = points[i][1] - points[j][1]; if (x == 0) { y = 1; } elseif (y == 0) { x = 1; } else { if (y < 0) { x = -x; y = -y; } int gcdXY = gcd(Math.Abs(x), Math.Abs(y)); x /= gcdXY; y /= gcdXY; } int key = y + x * 20001; if (!dictionary.ContainsKey(key)) { dictionary.Add(key, 1); } else { dictionary[key]++; } } int maxn = 0; foreach (int num in dictionary.Values) { maxn = Math.Max(maxn, num + 1); } ret = Math.Max(ret, maxn); } return ret; }
publicintgcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; } }
funcmaxPoints(points [][]int) (ans int) { n := len(points) if n <= 2 { return n } for i, p := range points { if ans >= n-i || ans > n/2 { break } cnt := map[int]int{} for _, q := range points[i+1:] { x, y := p[0]-q[0], p[1]-q[1] if x == 0 { y = 1 } elseif y == 0 { x = 1 } else { if y < 0 { x, y = -x, -y } g := gcd(abs(x), abs(y)) x /= g y /= g } cnt[y+x*20001]++ } for _, c := range cnt { ans = max(ans, c+1) } } return }
funcgcd(a, b int)int { for a != 0 { a, b = b%a, a } return b }
funcabs(x int)int { if x < 0 { return -x } return x }
funcmax(a, b int)int { if a > b { return a } return b }
var maxPoints = function(points) { const n = points.length; if (n <= 2) { return n; } let ret = 0; for (let i = 0; i < n; i++) { if (ret >= n - i || ret > n / 2) { break; } const map = newMap(); for (let j = i + 1; j < n; j++) { let x = points[i][0] - points[j][0]; let y = points[i][1] - points[j][1]; if (x === 0) { y = 1; } elseif (y === 0) { x = 1; } else { if (y < 0) { x = -x; y = -y; } const gcdXY = gcd(Math.abs(x), Math.abs(y)); x /= gcdXY; y /= gcdXY; } const key = y + x * 20001; map.set(key, (map.get(key) || 0)+ 1); } let maxn = 0; for (const num of map.values()) { maxn = Math.max(maxn, num + 1); } ret = Math.max(ret, maxn); } return ret; };
constgcd = (a, b) => { return b != 0 ? gcd(b, a % b) : a; }