九章算法 | Amazon 面试题:连接棒材的最低费用

2020-07-22 11:33:08 +08:00
 hakunamatata11

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

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

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

样例 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;
    }
}

面试软技能指导 2020 版 - BQ / Resume / Project

试听内容:

免费试听时间:

北京时间 7 月 27 日 周一 09:30

戳我即可免费报名噢

688 次点击
所在节点    推广
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/692138

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX