|
| 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