|
| 1 | +// 1502. Can Make Arithmetic Progression From Sequence |
| 2 | +// 🟢 Easy |
| 3 | +// |
| 4 | +// https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/ |
| 5 | +// |
| 6 | +// Tags: Array - Sorting |
| 7 | + |
| 8 | +use std::collections::HashSet; |
| 9 | + |
| 10 | +struct Solution; |
| 11 | +impl Solution { |
| 12 | + /// Create a duplicate of the input array and sort it, check the difference |
| 13 | + /// between any two contiguous items and make sure the difference is the |
| 14 | + /// same between any two items in the sorted array. |
| 15 | + /// |
| 16 | + /// Time complexity: O(n*log(n)) - Sorting has the highest time complexity. |
| 17 | + /// Space complexity: O(n) - We make a copy of the input array. Besides that |
| 18 | + /// sorting the array also takes extra memory, O(log(n)) |
| 19 | + /// |
| 20 | + /// Runtime 0 ms Beats 100% |
| 21 | + /// Memory 2.1 MB Beats 40% |
| 22 | + pub fn can_make_arithmetic_progression(arr: Vec<i32>) -> bool { |
| 23 | + if arr.len() < 3 { |
| 24 | + return true; |
| 25 | + } |
| 26 | + let mut nums = arr.clone(); |
| 27 | + nums.sort(); |
| 28 | + let diff = nums[1] - nums[0]; |
| 29 | + for i in 2..nums.len() { |
| 30 | + if nums[i] - nums[i - 1] != diff { |
| 31 | + return false; |
| 32 | + } |
| 33 | + } |
| 34 | + true |
| 35 | + } |
| 36 | + |
| 37 | + /// Find the minimum and maximum values in the input, compute the difference |
| 38 | + /// as (max-min) / num elements-1 Then create an array of flags that |
| 39 | + /// represents whether we have already seen the element that needs to go at |
| 40 | + /// any given position in the sequence. Start iterating over the input |
| 41 | + /// elements matching their values to the indexes along the sequence at |
| 42 | + /// which they should be found, if that value has been seen before, or the |
| 43 | + /// element does not match an index, return false, otherwise mark that |
| 44 | + /// index as seen. |
| 45 | + /// |
| 46 | + /// Time complexity: O(n) - We iterate two times over all the elements in |
| 47 | + /// the input array, for each we do O(1) work. |
| 48 | + /// Space complexity: O(n) - The array of flags uses extra memory. |
| 49 | + /// |
| 50 | + /// Runtime 0 ms Beats 100% |
| 51 | + /// Memory 2 MB Beats 40% |
| 52 | + pub fn can_make_arithmetic_progression_2(arr: Vec<i32>) -> bool { |
| 53 | + let n = arr.len() as i32; |
| 54 | + let (mut min, mut max) = (i32::MAX, i32::MIN); |
| 55 | + for num in arr.iter() { |
| 56 | + if *num > max { |
| 57 | + max = *num; |
| 58 | + } |
| 59 | + if *num < min { |
| 60 | + min = *num; |
| 61 | + } |
| 62 | + } |
| 63 | + if (max - min) % (n - 1) != 0 { |
| 64 | + return false; |
| 65 | + } |
| 66 | + let diff = (max - min) / (n - 1); |
| 67 | + if diff == 0 { |
| 68 | + return arr.into_iter().collect::<HashSet<i32>>().len() == 1; |
| 69 | + } |
| 70 | + let mut seen = vec![false; arr.len()]; |
| 71 | + for num in arr { |
| 72 | + if (num - min) % diff != 0 { |
| 73 | + return false; |
| 74 | + } |
| 75 | + let idx = ((num - min) / diff) as usize; |
| 76 | + if seen[idx] { |
| 77 | + return false; |
| 78 | + } |
| 79 | + seen[idx] = true; |
| 80 | + } |
| 81 | + true |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Tests. |
| 86 | +fn main() { |
| 87 | + let tests = [ |
| 88 | + (vec![3, 1, 5], true), |
| 89 | + (vec![1, 2, 4], false), |
| 90 | + (vec![0, 0, 0, 0], true), |
| 91 | + ]; |
| 92 | + for t in tests { |
| 93 | + assert_eq!(Solution::can_make_arithmetic_progression(t.0.clone()), t.1); |
| 94 | + assert_eq!( |
| 95 | + Solution::can_make_arithmetic_progression_2(t.0.clone()), |
| 96 | + t.1 |
| 97 | + ); |
| 98 | + } |
| 99 | + println!("\x1b[92m» All tests passed!\x1b[0m") |
| 100 | +} |
0 commit comments