原题链接在这里: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;
}
}

跟上Burst Balloons.

LeetCode 1130. Minimum Cost Tree From Leaf Values的更多相关文章

  1. leetcode1130 Minimum Cost Tree From Leaf Values

    思路: 区间dp. 实现: class Solution { public: int mctFromLeafValues(vector<int>& arr) { int n = a ...

  2. LeetCode 1000. Minimum Cost to Merge Stones

    原题链接在这里:https://leetcode.com/problems/minimum-cost-to-merge-stones/ 题目: There are N piles of stones ...

  3. LeetCode 983. Minimum Cost For Tickets

    原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...

  4. [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 ...

  5. [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 ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. [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 ...

  8. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  9. LeetCode 1135. Connecting Cities With Minimum Cost

    原题链接在这里:https://leetcode.com/problems/connecting-cities-with-minimum-cost/ 题目: There are N cities nu ...

随机推荐

  1. redis的redisvCommand的%b

    如下代码,向redis发送命令 SendCommand("HSET %b %b %b",key.data(),key.size(),filed.data(),filed.size( ...

  2. C语言学习笔记01——C语言概述

    作者:Eventi 出处:http://www.cnblogs.com/Eventi 欢迎转载,也请保留这段声明.谢谢! 1 C语言的起源 1972年,贝尔实验室的丹尼斯·里奇(Dennis Ritc ...

  3. .NET 使用 JustAssembly 比较两个不同版本程序集的 API 变化

    原文:.NET 使用 JustAssembly 比较两个不同版本程序集的 API 变化 最近我大幅度重构了我一个库的项目结构,使之使用最新的项目文件格式(基于 Microsoft.NET.Sdk)并使 ...

  4. 如何在ArcGIS饼状图中下方添加文字

    内容源自:ArcGIS10.2基础教程(丁华) 书上要求在统计图的饼状图下方显示“总面积组成”,以及图例是只显示文字. 该如何操作呢? 其实就是在高级属性中选择标题-副标题-显示“总面积组成”即可 而 ...

  5. vscode入门使用教程(页面调试)

    初次使用vscode时各种不适应,所有需要用到的功能貌似都需要单独安装插件才能用.这让很多初次使用vscode的朋友有点无所适从. 下面本人就带各位朋友学习下如何使用vscode来进行最基本的工作—— ...

  6. 一个多进程爬虫下载图片的demo

    import os,re import pickle import requests import random import time from bs4 import BeautifulSoup f ...

  7. 十大Intellij IDEA快捷键(附IDEA快捷键详细列表及使用技巧)

    十大Intellij IDEA快捷键(附IDEA快捷键详细列表及使用技巧) Intellij IDEA中有很多快捷键让人爱不释手,stackoverflow上也有一些有趣的讨论.每个人都有自己的最爱, ...

  8. bootstrap fileinput实现限制图片上传数量及如何控制分批多次上传

    废话没有,直奔主题 问题点: fileinput提供了一个maxFileCount用于限制图片上传的数量,设置maxFileCount为1时,一次性选择超过一张会有如下提示: 当选择一张,不点上传,再 ...

  9. c# 泛型study

    1.引用类型约束:  类型实参包含任何类,接口,数组,委托,或者是已知是引用类型的另一个类型参数 class demo<T> where T:class 有效的封闭区间demo<St ...

  10. vue v-for中的item改变无法引起视图的更新

    写过angularjs的同学知道,如果ng-repeat中的item绑定到对应的model,item改变是会引起视图的更新的,但是vue中不起作用,具体的解决办法: 在vue脚手架中,首先引入vue ...