Skip to content

Commit 3dff9ec

Browse files
committed
LC 1502. Can Make Arithmetic Progression From Sequence (Rust)
1 parent 49a80af commit 3dff9ec

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
434434
| [1480. Running Sum of 1d Array][lc1480] | 🟢 Easy | [![python](res/py.png)][lc1480py] |
435435
| [1491. Average Salary Excluding the Minimum and Maximum Salary][lc1491] | 🟢 Easy | [![rust](res/rs.png)][lc1491rs] |
436436
| [1498. Number of Subsequences That Satisfy the Given Sum Condition][lc1498] | 🟠 Medium | [![python](res/py.png)][lc1498py] |
437+
| [1502. Can Make Arithmetic Progression From Sequence][lc1502] | 🟢 Easy | [![rust](res/rs.png)][lc1502rs] |
437438
| [1519. Number of Nodes in the Sub-Tree With the Same Label][lc1519] | 🟠 Medium | [![python](res/py.png)][lc1519py] |
438439
| [1523. Count Odd Numbers in an Interval Range][lc1523] | 🟢 Easy | [![python](res/py.png)][lc1523py] [![rust](res/rs.png)][lc1523rs] |
439440
| [1539. Kth Missing Positive Number][lc1539] | 🟢 Easy | [![python](res/py.png)][lc1539py] [![rust](res/rs.png)][lc1539rs] |
@@ -1475,6 +1476,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
14751476
[lc1491rs]: leetcode/average-salary-excluding-the-minimum-and-maximum-salary.rs
14761477
[lc1498]: https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/
14771478
[lc1498py]: leetcode/number-of-subsequences-that-satisfy-the-given-sum-condition.py
1479+
[lc1502]: https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/
1480+
[lc1502rs]: leetcode/can-make-arithmetic-progression-from-sequence.rs
14781481
[lc1519]: https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label/
14791482
[lc1519py]: leetcode/number-of-nodes-in-the-sub-tree-with-the-same-label.py
14801483
[lc1523]: https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)