-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths0339_nested_list_weight_sum.rs
74 lines (65 loc) · 2.01 KB
/
s0339_nested_list_weight_sum.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
#![allow(unused)]
pub struct Solution {}
#[derive(Debug, PartialEq, Eq)]
pub enum NestedInteger {
Int(i32),
List(Vec<NestedInteger>),
}
impl Solution {
// Complexity Analysis
//
// The algorithm takes O(N) time, where N is the total number of nested elements
// in the input list. For example, the list [ [[[[1]]]], 2 ] contains 4 nested lists and 2
// nested integers (1 and 2), so N=6.
//
// In terms of space, at most O(D) recursive calls are placed on the stack, where D
// is the maximum level of nesting in the input. For example, D=2
// for the input [[1,1],2,[1,1]], and D=3 for the input [1,[4,[6]]].
pub fn depth_sum(nested_list: Vec<NestedInteger>) -> i32 {
if nested_list.len() < 1 {
return 0;
}
fn helper(nested_int: NestedInteger, depth: i32) -> i32 {
match nested_int {
NestedInteger::Int(c) => depth * c,
NestedInteger::List(vec) => {
let mut sum = 0;
for item in vec {
sum += helper(item, depth + 1);
}
sum
}
}
}
let mut ans = 0;
for nested_int in nested_list {
ans += helper(nested_int, 1);
}
ans
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_339() {
assert_eq!(
Solution::depth_sum(vec![
NestedInteger::List(vec![NestedInteger::Int(1), NestedInteger::Int(1),]),
NestedInteger::Int(2),
NestedInteger::List(vec![NestedInteger::Int(1), NestedInteger::Int(1),])
]),
10
);
assert_eq!(
Solution::depth_sum(vec![
NestedInteger::Int(1),
NestedInteger::List(vec![
NestedInteger::Int(4),
NestedInteger::List(vec![NestedInteger::Int(6)])
])
]),
27
);
}
}