Skip to content

Commit cfc6b93

Browse files
committed
leetcode
1 parent b48cd44 commit cfc6b93

File tree

4 files changed

+293
-0
lines changed

4 files changed

+293
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/*
2+
3+
4+
5+
6+
-* 149. Max Points on a Line *-
7+
8+
9+
Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.
10+
11+
12+
13+
Example 1:
14+
15+
16+
Input: points = [[1,1],[2,2],[3,3]]
17+
Output: 3
18+
Example 2:
19+
20+
21+
Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
22+
Output: 4
23+
24+
25+
Constraints:
26+
27+
1 <= points.length <= 300
28+
points[i].length == 2
29+
-104 <= xi, yi <= 104
30+
All the points are unique.
31+
32+
33+
34+
*/
35+
import 'dart:collection';
36+
import 'dart:math';
37+
38+
class Point {
39+
late int x;
40+
late int y;
41+
// Point() {
42+
// x = 0;
43+
// y = 0;
44+
// }
45+
Point(int a, int b) {
46+
x = a;
47+
y = b;
48+
}
49+
}
50+
51+
class A {
52+
int maxPoints(List<List<int>> points) {
53+
if (points.length <= 0) return 0;
54+
if (points.length <= 2) return points.length;
55+
int result = 0;
56+
int x = 0;
57+
int y = 0;
58+
for (int i = 0; i < points.length; i++) {
59+
HashMap<double, int> hm = HashMap();
60+
int sameX = 1;
61+
int sameP = 0;
62+
for (int j = 0; j < points.length; j++) {
63+
if (j != i) {
64+
if ((points[j][x] == points[i][x]) &&
65+
(points[j][y] == points[i][y])) {
66+
sameP++;
67+
}
68+
if (points[j][x] == points[i][x]) {
69+
sameX++;
70+
continue;
71+
}
72+
double k =
73+
(points[j][y] - points[i][y]) / (points[j][x] - points[i][x]);
74+
if (hm.containsKey(k)) {
75+
hm[k] = (hm[k] ?? 0) + 1;
76+
} else {
77+
hm[k] = 2;
78+
}
79+
result = max(result, (hm[k] ?? 0) + sameP);
80+
}
81+
}
82+
result = max(result, sameX);
83+
}
84+
return result;
85+
}
86+
}
87+
88+
class B {
89+
int getGcd(int a, int b) {
90+
if (a == 0) return b;
91+
return getGcd(b % a, a);
92+
}
93+
94+
int maxPoints(List<List<int>> points) {
95+
int v = 0;
96+
int res = 0;
97+
int len = points.length;
98+
if (len < 3) return len;
99+
for (int i = 0; i + res < len; i++) {
100+
HashMap<String, int> m = HashMap();
101+
int x1 = points[i][0], y1 = points[i][1], v = 0, maxv = 0, dups = 0;
102+
int nIdentical = 0;
103+
// start with i+1, since if any previous point is on the same line,
104+
// then this was already calculated then that point was a starting point
105+
for (int j = i + 1; j < len; j++) {
106+
int x2 = points[j][0], y2 = points[j][1];
107+
int dx = x2 - x1, dy = y2 - y1;
108+
109+
if (dx == 0 && dy == 0) {
110+
dups++;
111+
} else {
112+
// we need the slope: dx/dy. but float rounds up the end and produces slightly different results,
113+
// so instead we keep both dx and dy as the key.
114+
// to make them identical for the identical slope, use GCD: greatest common divisor
115+
int gcd = getGcd(dx, dy);
116+
dy ~/= gcd;
117+
dx ~/= gcd;
118+
119+
// dx and dy define the slope.
120+
// we keep the map for the current point i, so the full key is point[i]+slope excludes parallel lines.
121+
// vertical line: dx==0, horizontal line: dy==0. GCD will set vertical: dx=0, dy=1, horizontal: dx=1, dy=0
122+
String key = dx.toString() + '/' + dy.toString();
123+
//m[key] != m.entries
124+
if (m.containsKey(key)) {
125+
// m[key]++;
126+
m[key] = (m[key] ?? 0) + 1;
127+
v = m[key] ?? 0;
128+
} else {
129+
m[key] = 1;
130+
v = 1;
131+
}
132+
}
133+
// duplicates should increase our best result found with point[i]
134+
if (maxv < v) maxv = v;
135+
if (res < dups + maxv + 1) res = dups + maxv + 1;
136+
}
137+
}
138+
return res;
139+
}
140+
}
141+
142+
class C {
143+
int getGcd(int a, int b) {
144+
if (a == 0) return b;
145+
return getGcd(b % a, a);
146+
}
147+
148+
int maxPoints(List<List<int>> points) {
149+
int res = 0, len = points.length;
150+
if (len < 3) return len;
151+
for (int i = 0; i + res < len; i++) {
152+
Map<String, int> m = {};
153+
int x1 = points[i][0], y1 = points[i][1], v = 0, maxV = 0, dups = 0;
154+
int nIdentical = 0;
155+
for (int j = i + 1; j < len; j++) {
156+
int x2 = points[j][0], y2 = points[j][1];
157+
int dx = x2 - x1, dy = y2 - y1;
158+
if (dx == 0 && dy == 0) {
159+
dups++;
160+
} else {
161+
int gcd = getGcd(dx, dy);
162+
dy ~/= gcd;
163+
dx ~/= gcd;
164+
String key = "$dx/$dy";
165+
if (m.containsKey(key)) {
166+
// m[key]++;
167+
// v = m[key];
168+
m[key] = (m[key] ?? 0) + 1;
169+
v = m[key] ?? 0;
170+
} else {
171+
m[key] = 1;
172+
v = 1;
173+
}
174+
}
175+
if (maxV < v) maxV = v;
176+
if (res < dups + maxV + 1) res = dups + maxV + 1;
177+
}
178+
}
179+
return res;
180+
}
181+
}
182+
183+
class D {
184+
int maxPoints(List<List<int>> pt) {
185+
int ans = 1;
186+
int n = pt.length;
187+
for (int i = 0; i < n - 1; i++) {
188+
HashMap<double, int> mp = HashMap();
189+
for (int j = i + 1; j < n; j++) {
190+
if (pt[j][1] == pt[i][1]) {
191+
mp[-90] = (mp[-90] ?? 0) + 1;
192+
} else {
193+
double x = (pt[j][0] - pt[i][0]).toDouble() /
194+
(pt[j][1] - pt[i][1]).toDouble();
195+
mp[x] = (mp[x] ?? 0) + 1;
196+
}
197+
}
198+
int temp = 0;
199+
for (MapEntry<double, int> it in mp.entries) {
200+
temp = max(temp, it.value + 1);
201+
}
202+
ans = max(temp, ans);
203+
}
204+
return ans;
205+
}
206+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
func maxPoints(points [][]int) int {
4+
ans := 1
5+
n := len(points)
6+
for i := 0; i < n-1; i++ {
7+
mp := make(map[float64]int)
8+
for j := i + 1; j < n; j++ {
9+
if points[j][1] == points[i][1] {
10+
mp[-90]++
11+
} else {
12+
x := float64(points[j][0]-points[i][0]) / float64(points[j][1]-points[i][1])
13+
mp[x]++
14+
}
15+
}
16+
temp := 0
17+
for _, v := range mp {
18+
temp = max(temp, v+1)
19+
}
20+
ans = max(temp, ans)
21+
}
22+
return ans
23+
}
24+
25+
func max(x, y int) int {
26+
if x > y {
27+
return x
28+
}
29+
return y
30+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 🔥 Go Fast 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Approach
4+
5+
Idea is very simple
6+
7+
We will select point and for every selected point we will calculate the slope wr.t to another point and store into the map
8+
In map we will store <slope,that selected point>
9+
for example we have point :-[[1,1],[2,2],[3,3]......
10+
we pick point [1,1]and for that [1,1] we will calculate the slope
11+
w.r.t [2,2] ,[3,3]....... and store like this-
12+
for understanding purpose i'm writing
13+
mp[slope1,[1,1]]
14+
mp[slope2,[1,1]]
15+
mp[slope1,[1,1]].........
16+
This type we will store the slope for [2,2] , [3,3],....
17+
After storing the slope into map we will calculate the max point for that slope
18+
For example after storing the slope map can look like this
19+
[2/3,3]
20+
[-90,1]//for perpendicular case
21+
[1/3,2]...
22+
find the max number of points having the same slope w.r.t. that selected point :-means which slope have maximum point
23+
:- here 2/3 have 3 point so our answer will be 3+1(we are adding that point as well)
24+
25+
## Solution - GO
26+
27+
```go
28+
func maxPoints(points [][]int) int {
29+
ans := 1
30+
n := len(points)
31+
for i := 0; i < n-1; i++ {
32+
mp := make(map[float64]int)
33+
for j := i + 1; j < n; j++ {
34+
if points[j][1] == points[i][1] {
35+
mp[-90]++
36+
} else {
37+
x := float64(points[j][0]-points[i][0]) / float64(points[j][1]-points[i][1])
38+
mp[x]++
39+
}
40+
}
41+
temp := 0
42+
for _, v := range mp {
43+
temp = max(temp, v+1)
44+
}
45+
ans = max(temp, ans)
46+
}
47+
return ans
48+
}
49+
50+
func max(x, y int) int {
51+
if x > y {
52+
return x
53+
}
54+
return y
55+
}
56+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
179179
- [**2244.** Minimum Rounds to Complete All Tasks](MinimumRoundsToCompleteAllTasks/minimum_rounds_to_complete_all_tasks.dart)
180180
- [**452.** Minimum Number of Arrows to Burst Balloons](MinimumNumberOfArrowsToBurstBalloons/minimum_number_of_arrows_to_burst_balloons.dart)
181181
- [**1833.** Maximum Ice Cream Bars](MaximumIceCreamBars/maximum_ice_cream_bars.dart)
182+
- [**149.** Max Points on a Line](MaxPointsOnALine/max_points_on_a_line.dart)
182183

183184
## Reach me via
184185

0 commit comments

Comments
 (0)