LeetCode 1130. Minimum Cost Tree From Leaf Values
原题链接在这里:https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/
题目:
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
).
题解:
From the example, see that it is better to remove smaller element first.
Remove small element i and the cost is arr[i] * Math.min(arr[i-1], arr[i+1]). minimum cost happens between smaller values of i-1 and i+1.
Remove until there is only one element and sum of cost is the answer.
Use stack to maintain decreasing order, when there is bigger value num, then pop small value arr[i] and acculate the cost arr[i] * Math.min(num, stk.peek()).
Time Complexity: O(n). n = arr.length.
Space: O(n).
AC Java:
- class Solution {
- public int mctFromLeafValues(int[] arr) {
- if(arr == null || arr.length < 2){
- return 0;
- }
- int res = 0;
- Stack<Integer> stk = new Stack<>();
- stk.push(Integer.MAX_VALUE);
- for(int num : arr){
- while(stk.peek() <= num){
- int mid = stk.pop();
- res += mid*Math.min(stk.peek(), num);
- }
- stk.push(num);
- }
- while(stk.size() > 2){
- res += stk.pop()*stk.peek();
- }
- return res;
- }
- }
LeetCode 1130. Minimum Cost Tree From Leaf Values的更多相关文章
- leetcode1130 Minimum Cost Tree From Leaf Values
思路: 区间dp. 实现: class Solution { public: int mctFromLeafValues(vector<int>& arr) { int n = a ...
- LeetCode 1000. Minimum Cost to Merge Stones
原题链接在这里:https://leetcode.com/problems/minimum-cost-to-merge-stones/ 题目: There are N piles of stones ...
- LeetCode 983. Minimum Cost For Tickets
原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...
- [LeetCode] 857. Minimum Cost to Hire K Workers 雇佣K名工人的最低成本
There are N workers. The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now w ...
- [LeetCode] 857. Minimum Cost to Hire K Workers 雇K个工人的最小花费
There are N workers. The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now w ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [LeetCode] Minimum Cost to Merge Stones 混合石子的最小花费
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. A move consists ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- LeetCode 1135. Connecting Cities With Minimum Cost
原题链接在这里:https://leetcode.com/problems/connecting-cities-with-minimum-cost/ 题目: There are N cities nu ...
随机推荐
- xorm-删除和软删除实例
删除数据Delete方法,参数为struct的指针并且成为查询条件.注意:当删除时,如果user中包含有bool,float64或者float32类型,有可能会使删除失败 package main i ...
- js 数组传统方法
js 数组传统方法 push() 功能:向数组的末尾添加一个或多个元素 var arr = [4]; arr.push(1,2,3); // [4,1,2,3] 返回: 会返回新数组的长度length ...
- 【翻译】REST framework JWT Auth(django rest framework-jwt)
JWT认证的REST框架 原文链接 概述 这个包提供对Django REST framework的JSON Web Token 认证支持. 需要满足条件 Python (2.7, 3.3, 3.4, ...
- centos 7安装jdk并封装service服务
一.概述 有一个Spring Cloud的jar包,文件名为:RDS.jar.必须要jdk1.8版本,需要部署在 Centos 7.5的服务器上面,最好能设置开机自启动! 二.安装jdk 关闭防火墙 ...
- CF241E Flights 差分约束
传送门 差分约束永远是Itst最烂的图论知识点没有之一qwq 先用dfs把在\(1\)到\(N\)的路径上的所有点都拿出来,其他的点和边状态任意都不会影响答案. 然后考虑设\(dis_i\)表示从\( ...
- CentOS7 安装 Docker、最佳Docker学习文档
目录 一.Docker支持 二.安装Docker -1.在新主机上首次安装Docker CE之前,需要设置Docker存储库.之后,就可以从存储库安装和更新Docker. 0.卸载旧版 1.正式安装 ...
- mqtt client api: 阻塞API
fusesource版本:mqtt-client-1.11.jar下载地址:https://github.com/fusesource/mqtt-client fusesource提供三种mqtt c ...
- .net Aop 实现原理
本文实现所有继承BaseModel的类都通过代理拦截 using System; using System.Reflection; using System.Collections.Generic; ...
- aria2 https
https://github.com/aria2/aria2/issues/361 ... and also make sure that aria2 was built with HTTPS sup ...
- Matlab模板模式
在模板模式(Template Pattern)中,一个抽象类公开定义了执行它的方法的方式/模板.它的子类可以按需要重写方法实现,但调用将以抽象类中定义的方式进行.本文以数据库SQL语法为例来阐述模板模 ...