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