Skip to content

Commit 4de3643

Browse files
refactor 149
1 parent 3065d84 commit 4de3643

File tree

2 files changed

+79
-35
lines changed

2 files changed

+79
-35
lines changed
Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,71 @@
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;
22

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 {
444
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+
}
2359
}
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;
2565
}
26-
66+
2767
int getGcd(int a, int b) {
28-
return b == 0 ? a : getGcd(b, a % b);
68+
return b == 0 ? a : getGcd(b, a % b);
2969
}
70+
}
3071
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
package com.fishercoder;
22

3-
import com.fishercoder.common.classes.Point;
43
import com.fishercoder.solutions._149;
54
import org.junit.BeforeClass;
65
import org.junit.Test;
76

87
import static org.junit.Assert.assertEquals;
98

109
public class _149Test {
11-
private static _149.Solution1 solution1;
12-
private static Point[] points;
10+
private static _149.Solution1 solution1;
11+
private static int[][] points;
1312

14-
@BeforeClass
15-
public static void setup() {
16-
solution1 = new _149.Solution1();
17-
}
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _149.Solution1();
16+
}
1817

19-
@Test
20-
public void test1() {
21-
points = new Point[] {new Point(0, 0), new Point(1, 65536), new Point(65536, 0)};
22-
assertEquals(2, solution1.maxPoints(points));
23-
}
18+
@Test
19+
public void test1() {
20+
points = new int[][]{
21+
{1, 1},
22+
{2, 2},
23+
{3, 3}
24+
};
25+
assertEquals(3, solution1.maxPoints(points));
26+
}
2427
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy