forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path-sum-iv.cpp
41 lines (39 loc) · 1.08 KB
/
path-sum-iv.cpp
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
// Time: O(n)
// Space: O(p), p is the number of paths
class Solution {
public:
int pathSum(vector<int>& nums) {
if (nums.empty()) {
return 0;
}
int result = 0;
queue<node> q;
node dummy(10);
auto parent_ptr = &dummy;
for (const auto& num : nums) {
node child(num);
while (!parent_ptr->isParent(child)) {
result += parent_ptr->leaf ? parent_ptr->val : 0;
parent_ptr = &q.front();
q.pop();
}
parent_ptr->leaf = false;
child.val += parent_ptr->val;
q.emplace(move(child));
}
while (!q.empty()) {
result += q.front().val;
q.pop();
}
return result;
}
private:
struct node {
int level, i, val;
bool leaf;
node(int n) : level(n / 100 - 1), i((n % 100) / 10 - 1), val(n % 10), leaf(true) {};
inline bool isParent(const node& other) {
return level == other.level - 1 && i == other.i / 2;
};
};
};