-
Notifications
You must be signed in to change notification settings - Fork 0
/
1130.minimum-cost-tree-from-leaf-values.java
89 lines (82 loc) · 2.18 KB
/
1130.minimum-cost-tree-from-leaf-values.java
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* @lc app=leetcode id=1130 lang=java
*
* [1130] Minimum Cost Tree From Leaf Values
*
* https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/description/
*
* algorithms
* Medium (64.92%)
* Likes: 792
* Dislikes: 64
* Total Accepted: 24.2K
* Total Submissions: 37.1K
* Testcase Example: '[6,2,4]'
*
* Given an array arr of positive integers, consider all binary trees such
* that:
*
*
* Each node has either 0 or 2 children;
* The values of arr correspond to the values of each leaf in an in-order
* traversal of the tree. (Recall that a node is a leaf if and only if it has
* 0 children.)
* The value of each non-leaf node is equal to the product of the largest leaf
* value in its left and right subtree respectively.
*
*
* Among all possible binary trees considered, return the smallest possible sum
* of the values of each non-leaf node. It is guaranteed this sum fits into a
* 32-bit integer.
*
*
* Example 1:
*
*
* Input: arr = [6,2,4]
* Output: 32
* Explanation:
* There are two possible trees. The first has non-leaf node sum 36, and the
* second has non-leaf node sum 32.
*
* 24 24
* / \ / \
* 12 4 6 8
* / \ / \
* 6 2 2 4
*
*
*
* Constraints:
*
*
* 2 <= arr.length <= 40
* 1 <= arr[i] <= 15
* It is guaranteed that the answer fits into a 32-bit signed integer (ie. it
* is less than 2^31).
*
*/
// @lc code=start
class Solution {
public int mctFromLeafValues(int[] arr) {
if (arr == null || arr.length == 0) return 0;
int sum = 0;
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < arr.length; i++) {
while (!stack.isEmpty() && arr[i] >= stack.peek()) {
int v = stack.pop();
if (stack.isEmpty()) {
sum += v * arr[i];
} else {
sum += v * Math.min(stack.peek(), arr[i]);
}
}
stack.push(arr[i]);
}
while(stack.size() > 1) {
sum += stack.pop() * stack.peek();
}
return sum;
}
}
// @lc code=end