Skip to content

Commit 2ed8f5a

Browse files
committed
feat: update golang solution to lc problem: No.0149.Max Points on a Line
1 parent 76b88cd commit 2ed8f5a

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

solution/0100-0199/0149.Max Points on a Line/README.md

+43-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Solution:
5757
def maxPoints(self, points: List[List[int]]) -> int:
5858
def gcd(a, b) -> int:
5959
return a if b == 0 else gcd(b, a % b)
60-
60+
6161
n = len(points)
6262
if n < 3:
6363
return n
@@ -122,6 +122,48 @@ class Solution {
122122
}
123123
```
124124

125+
### **Go**
126+
127+
```go
128+
func maxPoints(points [][]int) int {
129+
type pair struct {
130+
first int
131+
second int
132+
}
133+
n := len(points)
134+
if n <= 2 {
135+
return n
136+
}
137+
ans := 0
138+
for i := 0; i < n-1; i++ {
139+
freq := make(map[pair]int)
140+
for j := i + 1; j < n; j++ {
141+
x1, y1, x2, y2 := points[i][0], points[i][1], points[j][0], points[j][1]
142+
dx, dy := x2-x1, y2-y1
143+
g := gcd(dx, dy)
144+
p := pair{dx / g, dy / g}
145+
freq[p]++
146+
ans = max(ans, freq[p]+1)
147+
}
148+
}
149+
return ans
150+
}
151+
152+
func gcd(a, b int) int {
153+
for b != 0 {
154+
a, b = b, a%b
155+
}
156+
return a
157+
}
158+
159+
func max(a, b int) int {
160+
if a > b {
161+
return a
162+
}
163+
return b
164+
}
165+
```
166+
125167
### **...**
126168

127169
```

solution/0100-0199/0149.Max Points on a Line/README_EN.md

+43-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Solution:
4242
def maxPoints(self, points: List[List[int]]) -> int:
4343
def gcd(a, b) -> int:
4444
return a if b == 0 else gcd(b, a % b)
45-
45+
4646
n = len(points)
4747
if n < 3:
4848
return n
@@ -105,6 +105,48 @@ class Solution {
105105
}
106106
```
107107

108+
### **Go**
109+
110+
```go
111+
func maxPoints(points [][]int) int {
112+
type pair struct {
113+
first int
114+
second int
115+
}
116+
n := len(points)
117+
if n <= 2 {
118+
return n
119+
}
120+
ans := 0
121+
for i := 0; i < n-1; i++ {
122+
freq := make(map[pair]int)
123+
for j := i + 1; j < n; j++ {
124+
x1, y1, x2, y2 := points[i][0], points[i][1], points[j][0], points[j][1]
125+
dx, dy := x2-x1, y2-y1
126+
g := gcd(dx, dy)
127+
p := pair{dx / g, dy / g}
128+
freq[p]++
129+
ans = max(ans, freq[p]+1)
130+
}
131+
}
132+
return ans
133+
}
134+
135+
func gcd(a, b int) int {
136+
for b != 0 {
137+
a, b = b, a%b
138+
}
139+
return a
140+
}
141+
142+
func max(a, b int) int {
143+
if a > b {
144+
return a
145+
}
146+
return b
147+
}
148+
```
149+
108150
### **...**
109151

110152
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
func maxPoints(points [][]int) int {
2+
type pair struct {
3+
first int
4+
second int
5+
}
6+
n := len(points)
7+
if n <= 2 {
8+
return n
9+
}
10+
ans := 0
11+
for i := 0; i < n-1; i++ {
12+
freq := make(map[pair]int)
13+
for j := i + 1; j < n; j++ {
14+
x1, y1, x2, y2 := points[i][0], points[i][1], points[j][0], points[j][1]
15+
dx, dy := x2-x1, y2-y1
16+
g := gcd(dx, dy)
17+
p := pair{dx / g, dy / g}
18+
freq[p]++
19+
ans = max(ans, freq[p]+1)
20+
}
21+
}
22+
return ans
23+
}
24+
25+
func gcd(a, b int) int {
26+
for b != 0 {
27+
a, b = b, a%b
28+
}
29+
return a
30+
}
31+
32+
func max(a, b int) int {
33+
if a > b {
34+
return a
35+
}
36+
return b
37+
}

0 commit comments

Comments
 (0)