Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1966 from AkifhanIlgaz/0149
Browse files Browse the repository at this point in the history
Create: 0149-max-points-on-a-line.rs / .ts / .js
  • Loading branch information
tahsintunan authored Jan 9, 2023
2 parents 542c2c8 + ef6cecd commit c725f43
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
27 changes: 27 additions & 0 deletions javascript/0149-max-points-on-a-line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @param {number[][]} points
* @return {number}
*/
var maxPoints = function (points) {
let res = 1;

for (let i = 0; i < points.length; i++) {
const count = new Map();
const point1 = points[i];
for (let j = i + 1; j < points.length; j++) {
const point2 = points[j];
let slope;
if (point2[0] === point1[0]) {
slope = Number.MAX_SAFE_INTEGER;
} else {
slope = (point2[1] - point1[1]) / (point2[0] - point1[0]);
}
!count.has(slope)
? count.set(slope, 2)
: count.set(slope, count.get(slope) + 1);

res = Math.max(res, count.get(slope));
}
}
return res;
};
27 changes: 27 additions & 0 deletions rust/0149-max-points-on-a-line.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::collections::HashMap;

impl Solution {
pub fn max_points(points: Vec<Vec<i32>>) -> i32 {
let mut res = 1;

for i in 0..points.len() {
let point1 = &points[i];
let mut count = HashMap::new();
for j in (i + 1)..points.len() {
let point2 = &points[j];
let slope;
if point2[0] == point1[0] {
slope = i32::MAX;
} else {
slope = ((point2[1] as f64 - point1[1] as f64)
/ (point2[0] as f64 - point1[0] as f64)
* 100000.0) as i32;
}

*count.entry(slope).or_insert(1) += 1;
res = res.max(*count.get(&slope).unwrap());
}
}
res
}
}
23 changes: 23 additions & 0 deletions typescript/0149-max-points-on-a-line.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function maxPoints(points: number[][]): number {
let res = 1;

for (let i = 0; i < points.length; i++) {
const count = new Map();
const point1 = points[i];
for (let j = i + 1; j < points.length; j++) {
const point2 = points[j];
let slope;
if (point2[0] === point1[0]) {
slope = Number.MAX_SAFE_INTEGER;
} else {
slope = (point2[1] - point1[1]) / (point2[0] - point1[0]);
}
!count.has(slope)
? count.set(slope, 2)
: count.set(slope, count.get(slope) + 1);

res = Math.max(res, count.get(slope));
}
}
return res;
}

0 comments on commit c725f43

Please sign in to comment.