forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2.cpp
More file actions
19 lines (19 loc) · 663 Bytes
/
s2.cpp
File metadata and controls
19 lines (19 loc) · 663 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// OJ: https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/
// Author: github.com/lzl124631x
// Time: O(N^2)
// Space: O(1)
// Ref: https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/discuss/339959/One-Pass-O(N)-Time-and-Space
class Solution {
public:
int mctFromLeafValues(vector<int>& A) {
int ans = 0;
while (A.size() > 1) {
auto it = min_element(A.begin(), A.end());
int left = it == A.begin() ? INT_MAX : *(it - 1);
int right = it == A.end() - 1 ? INT_MAX : *(it + 1);
ans += *it * min(left, right);
A.erase(it);
}
return ans;
}
};