We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d26c0de commit e074983Copy full SHA for e074983
scripts/algorithms/M/Minimum Cost Tree From Leaf Values/Minimum Cost Tree From Leaf Values.rs
@@ -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
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