|
3 | 3 | import java.util.HashMap;
|
4 | 4 | import java.util.Map;
|
5 | 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 | 6 | public class _149 {
|
39 | 7 |
|
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 { |
44 |
| - public int maxPoints(int[][] points) { |
45 |
| - if (points.length < 3) { |
46 |
| - return points.length; |
47 |
| - } |
48 |
| - int max = 0; |
49 |
| - Map<Long, Integer> map = new HashMap<>(); |
50 |
| - for (int i = 0; i < points.length; i++) { |
51 |
| - int dup = 1; |
52 |
| - map.clear(); |
53 |
| - for (int j = i + 1; j < points.length; j++) { |
54 |
| - int dx = points[j][0] - points[i][0]; |
55 |
| - int dy = points[j][1] - points[i][1]; |
56 |
| - if (dx == 0 && dy == 0) { |
57 |
| - dup++; |
58 |
| - } else { |
59 |
| - int gcd = getGcd(dx, dy); |
60 |
| - long slope = ((long) (dy / gcd) << 32) + (dx / gcd); |
61 |
| - map.put(slope, map.getOrDefault(slope, 0) + 1); |
62 |
| - } |
| 8 | + /** |
| 9 | + * credits: https://leetcode.com/problems/max-points-on-a-line/discuss/328269/A-Java-solution-with-my-understanding |
| 10 | + */ |
| 11 | + public static class Solution1 { |
| 12 | + public int maxPoints(int[][] points) { |
| 13 | + if (points.length < 3) { |
| 14 | + return points.length; |
| 15 | + } |
| 16 | + int max = 0; |
| 17 | + Map<Long, Integer> map = new HashMap<>(); |
| 18 | + for (int i = 0; i < points.length; i++) { |
| 19 | + int dup = 1; |
| 20 | + map.clear(); |
| 21 | + for (int j = i + 1; j < points.length; j++) { |
| 22 | + int dx = points[j][0] - points[i][0]; |
| 23 | + int dy = points[j][1] - points[i][1]; |
| 24 | + if (dx == 0 && dy == 0) { |
| 25 | + dup++; |
| 26 | + } else { |
| 27 | + int gcd = getGcd(dx, dy); |
| 28 | + long slope = ((long) (dy / gcd) << 32) + (dx / gcd); |
| 29 | + map.put(slope, map.getOrDefault(slope, 0) + 1); |
| 30 | + } |
| 31 | + } |
| 32 | + max = Math.max(max, dup); |
| 33 | + for (Map.Entry<Long, Integer> entry : map.entrySet()) { |
| 34 | + max = Math.max(max, entry.getValue() + dup); |
| 35 | + } |
| 36 | + } |
| 37 | + return max; |
63 | 38 | }
|
64 |
| - max = Math.max(max, dup); |
65 |
| - for (Map.Entry<Long, Integer> entry : map.entrySet()) { |
66 |
| - max = Math.max(max, entry.getValue() + dup); |
67 |
| - } |
68 |
| - } |
69 |
| - return max; |
70 |
| - } |
71 | 39 |
|
72 |
| - int getGcd(int a, int b) { |
73 |
| - return b == 0 ? a : getGcd(b, a % b); |
| 40 | + int getGcd(int a, int b) { |
| 41 | + return b == 0 ? a : getGcd(b, a % b); |
| 42 | + } |
74 | 43 | }
|
75 |
| - } |
76 | 44 | }
|
0 commit comments