Skip to content

Commit e074983

Browse files
committed
Runtime: 1 ms (Top 83.3%) | Memory: 2.16 MB (Top 16.6%)
1 parent d26c0de commit e074983

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Runtime: 1 ms (Top 83.3%) | Memory: 2.16 MB (Top 16.6%)
2+
3+
use std::cmp::min;
4+
impl Solution {
5+
pub fn mct_from_leaf_values(mut arr: Vec<i32>) -> i32 {
6+
let n = arr.len();
7+
let mut A:Vec<i32> = vec![];
8+
A.push(std::i32::MAX);
9+
A.append(&mut arr);
10+
A.push(std::i32::MAX);
11+
let mut res = 0;
12+
while A.len()>3{
13+
let min_index = min_index(&A);
14+
res+=min(A[min_index-1],A[min_index+1]) * A[min_index];
15+
A.remove(min_index);
16+
}
17+
return res;
18+
}
19+
}
20+
pub fn min_index(array: &Vec<i32>) -> usize {
21+
let mut i = 0;
22+
for (j, &value) in array.iter().enumerate() {
23+
if value < array[i] {
24+
i = j;
25+
}
26+
}
27+
return i;
28+
}

0 commit comments

Comments
 (0)