Skip to content

Commit a1db0fa

Browse files
committed
max_points_on_a_line
1 parent 4959dba commit a1db0fa

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
132132
#### [146. LRU Cache](https://github.com/hitzzc/go-leetcode/tree/master/lru_cache)
133133
#### [147. insertion sort list](https://github.com/hitzzc/go-leetcode/tree/master/insertion_sort_list)
134134
#### [148. sort list](https://github.com/hitzzc/go-leetcode/tree/master/sort_list)
135+
#### [149. Max Points on a Line](https://github.com/hitzzc/go-leetcode/tree/master/max_points_on_a_line)
135136

136137

137138

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package max_points_on_a_line
2+
3+
type Point struct {
4+
X int
5+
Y int
6+
}
7+
8+
func maxPoints(points []Point) int {
9+
if len(points) < 3 {
10+
return len(points)
11+
}
12+
var max int
13+
for i := range points {
14+
verticalNum := 0
15+
equalNum := 0
16+
m := map[float64]int{}
17+
for j := range points {
18+
if i == j {
19+
continue
20+
}
21+
if points[i].X == points[j].X && points[i].Y == points[j].Y {
22+
equalNum++
23+
} else if points[i].X == points[j].X {
24+
verticalNum++
25+
} else {
26+
ratio := float64(points[i].Y-points[j].Y) / float64(points[i].X-points[j].X)
27+
m[ratio]++
28+
}
29+
}
30+
if verticalNum+equalNum > max {
31+
max = verticalNum + equalNum
32+
}
33+
for k := range m {
34+
if m[k]+equalNum > max {
35+
max = m[k] + equalNum
36+
}
37+
}
38+
}
39+
return max + 1
40+
}

0 commit comments

Comments
 (0)