forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create: 0149-max-points-on-a-line.rs / .ts / .js
- Loading branch information
1 parent
ee7a3bc
commit ef6cecd
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |