-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob0149.go
80 lines (74 loc) · 1.48 KB
/
prob0149.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package prob0149
// 直线定义 y = kx + b
// k = Y / X
type Line struct {
X int
Y int
B int
}
var (
zeroLine = Line{X: 0, Y: 0, B: 0}
)
func maxPoints(points [][]int) int {
res := 0
// 遍历每个点能够与其他点构成的直线,查看最多有多少点落在同一直线上
for i, originPoint := range points {
lineMap := make(map[Line]int)
baseCount := 1
maxPoints4OriginPoint := 0
for j, point := range points {
if i == j {
continue
}
line := getLineByTowPoint(originPoint, point)
if line == zeroLine {
baseCount += 1
continue
}
_, found := lineMap[line]
if found {
lineMap[line] += 1
} else {
lineMap[line] = 1
}
if lineMap[line]+baseCount > maxPoints4OriginPoint {
maxPoints4OriginPoint = lineMap[line] + baseCount
}
}
if maxPoints4OriginPoint > res {
res = maxPoints4OriginPoint
}
if baseCount > res {
res = baseCount
}
}
return res
}
func getLineByTowPoint(a, b []int) Line {
devX := b[0] - a[0]
devY := b[1] - a[1]
if devX == 0 && devY == 0 {
return zeroLine
} else if devX == 0 {
return Line{X: 0, Y: 1, B: a[0]}
} else if devY == 0 {
return Line{X: 1, Y: 0, B: a[1]}
}
g := gcd(absInt(devX), absInt(devY))
moduls := (devX / devX) * (devY / devY)
res := Line{X: absInt(devX) / g, Y: moduls * devY / g}
res.B = (b[1]*res.X - res.Y*b[0])
return res
}
func gcd(a, b int) int {
if a%b == 0 {
return b
}
return gcd(b, a%b)
}
func absInt(i int) int {
if i < 0 {
return -i
}
return i
}