|
1 |
| -**Updated solution** [credits](https://leetcode.com/problems/max-points-on-a-line/discuss/328269/A-Java-solution-with-my-understanding) |
| 1 | +package com.fishercoder.solutions; |
2 | 2 |
|
3 |
| -class Solution { |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +/** |
| 7 | + * 149. Max Points on a Line |
| 8 | + * |
| 9 | + * Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. |
| 10 | + * |
| 11 | + * Example 1: |
| 12 | + * Input: [[1,1],[2,2],[3,3]] |
| 13 | + * Output: 3 |
| 14 | + * Explanation: |
| 15 | + * ^ |
| 16 | + * | |
| 17 | + * | o |
| 18 | + * | o |
| 19 | + * | o |
| 20 | + * +-------------> |
| 21 | + * 0 1 2 3 4 |
| 22 | + * |
| 23 | + * Example 2: |
| 24 | + * Input: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] |
| 25 | + * Output: 4 |
| 26 | + * Explanation: |
| 27 | + * ^ |
| 28 | + * | |
| 29 | + * | o |
| 30 | + * | o o |
| 31 | + * | o |
| 32 | + * | o o |
| 33 | + * +-------------------> |
| 34 | + * 0 1 2 3 4 5 6 |
| 35 | + * |
| 36 | + * NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature. |
| 37 | + */ |
| 38 | +public class _149 { |
| 39 | + |
| 40 | + /** |
| 41 | + * credits: https://leetcode.com/problems/max-points-on-a-line/discuss/328269/A-Java-solution-with-my-understanding |
| 42 | + */ |
| 43 | + public static class Solution1 { |
4 | 44 | public int maxPoints(int[][] points) {
|
5 |
| - if(points.length < 3)return points.length; |
6 |
| - int max = 0; |
7 |
| - HashMap<Long, Integer> map = new HashMap<Long, Integer>(); |
8 |
| - for(int i = 0;i < points.length;i++) { |
9 |
| - int dup = 1; |
10 |
| - map.clear(); |
11 |
| - for(int j = i + 1;j < points.length;j++) { |
12 |
| - int dx = points[j][0] - points[i][0], dy = points[j][1] - points[i][1]; |
13 |
| - if(dx == 0 && dy == 0)dup++; |
14 |
| - else { |
15 |
| - int gcd = getGcd(dx, dy); |
16 |
| - long slope = ((long)(dy / gcd) << 32) + (dx / gcd); |
17 |
| - map.put(slope, map.getOrDefault(slope, 0) + 1); |
18 |
| - } |
19 |
| - } |
20 |
| - max = Math.max(max, dup); |
21 |
| - for(Map.Entry<Long, Integer> entry : map.entrySet()) |
22 |
| - max = Math.max(max, entry.getValue() + dup); |
| 45 | + if (points.length < 3) return points.length; |
| 46 | + int max = 0; |
| 47 | + Map<Long, Integer> map = new HashMap<>(); |
| 48 | + for (int i = 0; i < points.length; i++) { |
| 49 | + int dup = 1; |
| 50 | + map.clear(); |
| 51 | + for (int j = i + 1; j < points.length; j++) { |
| 52 | + int dx = points[j][0] - points[i][0], dy = points[j][1] - points[i][1]; |
| 53 | + if (dx == 0 && dy == 0) dup++; |
| 54 | + else { |
| 55 | + int gcd = getGcd(dx, dy); |
| 56 | + long slope = ((long) (dy / gcd) << 32) + (dx / gcd); |
| 57 | + map.put(slope, map.getOrDefault(slope, 0) + 1); |
| 58 | + } |
23 | 59 | }
|
24 |
| - return max; |
| 60 | + max = Math.max(max, dup); |
| 61 | + for (Map.Entry<Long, Integer> entry : map.entrySet()) |
| 62 | + max = Math.max(max, entry.getValue() + dup); |
| 63 | + } |
| 64 | + return max; |
25 | 65 | }
|
26 |
| - |
| 66 | + |
27 | 67 | int getGcd(int a, int b) {
|
28 |
| - return b == 0 ? a : getGcd(b, a % b); |
| 68 | + return b == 0 ? a : getGcd(b, a % b); |
29 | 69 | }
|
| 70 | + } |
30 | 71 | }
|
0 commit comments