Skip to content

Commit 49a80af

Browse files
committed
LC 1232. Check If It Is a Straight Line (Rust Geom)
1 parent 14572e1 commit 49a80af

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
394394
| [1162. As Far from Land as Possible][lc1162] | 🟠 Medium | [![python](res/py.png)][lc1162py] [![rust](res/rs.png)][lc1162rs] |
395395
| [1207. Unique Number of Occurrences][lc1207] | 🟢 Easy | [![python](res/py.png)][lc1207py] |
396396
| [1220. Count Vowels Permutation][lc1220] | 🔴 Hard | [![python](res/py.png)][lc1220py] |
397+
| [1232. Check If It Is a Straight Line][lc1232] | 🟢 Easy | [![rust](res/rs.png)][lc1232rs] |
397398
| [1235. Maximum Profit in Job Scheduling][lc1235] | 🔴 Hard | [![python](res/py.png)][lc1235py] |
398399
| [1239. Maximum Length of a Concatenated String with Unique Characters][lc1239] | 🟠 Medium | [![python](res/py.png)][lc1239py] |
399400
| [1254. Number of Closed Islands][lc1254] | 🟠 Medium | [![rust](res/rs.png)][lc1254rs] |
@@ -1382,6 +1383,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
13821383
[lc1207py]: leetcode/unique-number-of-occurrences.py
13831384
[lc1220]: https://leetcode.com/problems/count-vowels-permutation/
13841385
[lc1220py]: leetcode/count-vowels-permutation.py
1386+
[lc1232]: https://leetcode.com/problems/check-if-it-is-a-straight-line/
1387+
[lc1232rs]: leetcode/check-if-it-is-a-straight-line.rs
13851388
[lc1235]: https://leetcode.com/problems/maximum-profit-in-job-scheduling/
13861389
[lc1235py]: leetcode/maximum-profit-in-job-scheduling.py
13871390
[lc1239]: https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// 1232. Check If It Is a Straight Line
2+
// 🟢 Easy
3+
//
4+
// https://leetcode.com/problems/check-if-it-is-a-straight-line/
5+
//
6+
// Tags: Array - Math - Geometry
7+
8+
struct Solution;
9+
impl Solution {
10+
/// For all the points to be in the same line, they have to satisfy the
11+
/// same linear equation y = mx+b, compute m and b using two points and
12+
/// check if the rest of the points satisfy the condition.
13+
///
14+
/// Time complexity: O(n) - We compute m and b in linear time, then iterate
15+
/// over all the points to check if they satisfy the equation.
16+
/// Space complexity: O(1) - Constant extra memory used.
17+
///
18+
/// Runtime 2 ms Beats 90%
19+
/// Memory 2 MB Beats 90%
20+
pub fn check_straight_line(coordinates: Vec<Vec<i32>>) -> bool {
21+
if coordinates.len() < 3 {
22+
return true;
23+
}
24+
let (ax, ay) = (coordinates[0][0] as f32, coordinates[0][1] as f32);
25+
let (bx, by) = (coordinates[1][0] as f32, coordinates[1][1] as f32);
26+
let m = if ax == bx {
27+
f32::MAX
28+
} else {
29+
(ay - by) / (ax - bx)
30+
};
31+
let b = ay - m * ax;
32+
for point in coordinates {
33+
let (x, y) = (point[0] as f32, point[1] as f32);
34+
if m == f32::MAX {
35+
if x != ax {
36+
return false;
37+
}
38+
} else if y != m * x + b {
39+
return false;
40+
}
41+
}
42+
true
43+
}
44+
45+
/// The slope between any two points needs to be the same, fix one point
46+
/// and make sure that the slope between that point and any other remains
47+
/// constant.
48+
///
49+
/// Time complexity: O(n) - We do one check per point.
50+
/// Space complexity: O(1) - Constant extra memory used.
51+
///
52+
/// Runtime 0 ms Beats 100%
53+
/// Memory 2.1 MB Beats 90%
54+
pub fn check_straight_line_2(coordinates: Vec<Vec<i32>>) -> bool {
55+
let (ax, ay) = (coordinates[0][0], coordinates[0][1]);
56+
let (bx, by) = (coordinates[1][0], coordinates[1][1]);
57+
for point in coordinates {
58+
let (x, y) = (point[0], point[1]);
59+
if (bx - ax) * (y - by) != (x - bx) * (by - ay) {
60+
return false;
61+
}
62+
}
63+
true
64+
}
65+
}
66+
67+
// Tests.
68+
fn main() {
69+
let tests = [
70+
(vec![vec![0, 0], vec![0, 1], vec![0, -1]], true),
71+
(
72+
vec![
73+
vec![1, 2],
74+
vec![2, 3],
75+
vec![3, 4],
76+
vec![4, 5],
77+
vec![5, 6],
78+
vec![6, 7],
79+
],
80+
true,
81+
),
82+
(
83+
vec![
84+
vec![1, 1],
85+
vec![2, 2],
86+
vec![3, 4],
87+
vec![4, 5],
88+
vec![5, 6],
89+
vec![7, 7],
90+
],
91+
false,
92+
),
93+
];
94+
for t in tests {
95+
assert_eq!(Solution::check_straight_line(t.0.clone()), t.1);
96+
assert_eq!(Solution::check_straight_line_2(t.0), t.1);
97+
}
98+
println!("\x1b[92m» All tests passed!\x1b[0m")
99+
}

0 commit comments

Comments
 (0)