ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[leetcode/lintcode 题解] Amazon面试题:连接棒材的最低费用

2020-07-22 16:01:31  阅读:358  来源: 互联网

标签:q1 面试题 q2 棒材 sticks 题解 merge res poll


为了装修新房,你需要加工一些长度为正整数的棒材 sticks。

如果要将长度分别为 X 和 Y 的两根棒材连接在一起,你需要支付 X + Y 的费用。 由于施工需要,你必须将所有棒材连接成一根。

返回你把所有棒材 sticks 连成一根所需要的最低费用。注意你可以任意选择棒材连接的顺序。

  • ​​1≤sticks.length≤10^4
  • ​​1≤sticks[i]≤10^4

在线评测地址:https://www.lintcode.com/problem/minimum-cost-to-connect-sticks/?utm_source=sc-bky-zq

样例 1:

输入:
 [2,4,3]
输出:14
解释:先将 2 和 3 连接成 5,花费 5;再将 5 和 4 连接成 9;总花费为 14

样例 2:

输入:
 [1,8,3,5]
输出:30

【题解】

根据题意,考虑贪心,我们每次将所有棒材的最短的两根合并,将合并后的棒材放入棒材堆,重复合并最短的,直到棒材只剩下一根。

// minheap 暴力
// 直接将所有值压入minheap,每次取前两个值相加成merge,同时将merge压入minheap
public class Solution {
    /**
     * @param sticks: the length of sticks
     * @return: Minimum Cost to Connect Sticks
     */
    public int MinimumCost(List<Integer> sticks) {
        if (sticks.size() < 2) {
            return 0;
        }
        PriorityQueue<Integer> minHeap = new PriorityQueue<>();
        for (int num : sticks) {
            minHeap.add(num);
        }
        int res = 0;
        while (minHeap.size() > 1) {
            int merge = minHeap.poll() + minHeap.poll();
            res += merge;
            minHeap.add(merge);
        }
        return res;
    }
}
// 排序,双队列
// 先将数组排序,然后开始合并,合并后的值放入q2末尾,能够保证q2中被押入的值是
// 一定大于前面的值的,每次合并我们考虑q1,q2非空以及比较q1[0]和q2[0]的大小
public class Solution {
    /**
     * @param sticks: the length of sticks
     * @return: Minimum Cost to Connect Sticks
     */
    public int MinimumCost(List<Integer> sticks) {
        Collections.sort(sticks);
        Queue<Integer> q1 = new LinkedList<Integer>();
        Queue<Integer> q2 = new LinkedList<Integer>();
        for (int i : sticks) {
            q1.add(i);
        }
        int res = 0,merge;
        while(q1.size() + q2.size() > 1){
            if(q2.isEmpty()){
                merge = q1.poll();
                merge += q1.poll();
                q2.add(merge);
                res += merge;
            }
            else if (q1.isEmpty()){
                merge = q2.poll();
                merge += q2.poll();
                q2.add(merge);
                res += merge;
            }
            else {
                if(q1.peek() > q2.peek()){
                    merge = q2.poll();
                }
                else {
                    merge = q1.poll();
                }

                if (q1.isEmpty()){
                    merge += q2.poll();
                    q2.add(merge);
                    res += merge;
                }
                else if(q2.isEmpty()){
                    merge += q1.poll();
                    q2.add(merge);
                    res += merge;
                }
                else {
                    if(q1.peek() > q2.peek()){
                        merge += q2.poll();
                        q2.add(merge);
                        res += merge;
                    }
                    else {
                        merge += q1.poll();
                        q2.add(merge);
                        res += merge;
                    }   
                }
            }
        }
        return res;
    }
}

更多题解参见:https://www.jiuzhang.com/solution/minimum-cost-to-connect-sticks/?utm_source=sc-bky-zq

标签:q1,面试题,q2,棒材,sticks,题解,merge,res,poll
来源: https://www.cnblogs.com/jiuzhangsuanfa01/p/13361253.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有