ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

算法题:连接棒材的最低费用

2021-05-25 13:31:13  阅读:233  来源: 互联网

标签:棒材 sticks int res System 算法 println 连接 out


连接棒材的最低费用

  • 描述

为了装修新房,你需要加工一些长度为正整数的棒材 sticks。 如果要将长度分别为 X 和 Y 的两根棒材连接在一起,你需要支付 X + Y 的费用。 由于施工需要,你必须将所有棒材连接成一根。 返回你把所有棒材 sticks 连成一根所需要的最低费用。 注意你可以任意选择棒材连接的顺序

1≤sticks.length≤1041 \leq sticks.length \leq 10^41≤sticks.length≤104
1≤sticks[i]≤1041 \leq sticks[i] \leq 10^41≤sticks[i]≤104
  • 样例

样例 1:

输入:

[2,4,3]

输出:

14

解释:

先将 2 和 3 连接成 5,花费 5;再将 5 和 4 连接成 9;总花费为 14

样例 2:

输入:

 [1,8,3,5]

输出:

30

思路

[ 2 4 3 ] => 2+3=5 5+4=9 9+5=14 => 2*2+2*3+1*4=14
[ 1 8 3 5] => 1+3=4 4+5=9 9+8=17 4+9+17=30 => (1+3)+(1+3+5)+(1+3+5+8)=1*3 + 3*3 + 5*2 + 8*1 = 30

可以看出,计算逻辑是把list排序,然后取最小俩棒材 A,B求和price,然后生成一个新的长度的棒材C,花费增加price,棒材集合去除A,B,添加C,花费集合costList中添加price,然后重新排序list,重复操作,直至list.size() == 1时,返回costList数据和

所以我一开始的代码是:

public static int MinimumCostLow(List<Integer> sticks) {
        // write your code here
        System.out.println("开始:" + sticks);
        // 第二步:循环计算
        int total = sticks.size();
        int res = 0;
        List<Integer> cost = new ArrayList<>();

        for (int c = total; c > 1; c--) {
            System.out.println("第" + c + "次循环,stick长度:" + sticks.size());
            for (int i = 0; i < sticks.size(); i++) {
                int min = i;
                for (int j = i; j < sticks.size(); j++) {

                    if (sticks.get(min) > sticks.get(j)) {
                        min = j;
                    }
                }
                if (i != min) {
                    int oldMin = sticks.get(i);
                    sticks.set(i, sticks.get(min));
                    sticks.set(min, oldMin);
                }
            }
            System.out.println("第" + c + "次循环,stick排序后:" + sticks);

            res = sticks.get(0) + sticks.get(1);
            sticks.remove(0);
            sticks.set(0, res);
            cost.add(res);
        }

        res = 0;
        for (int i = 0; i < cost.size(); i++) {
            res += cost.get(i);
        }
        System.out.println("结果:" + res);

        return res;
    }
  • 主方法
public static void main(String args[]) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            list.add((int) (Math.random() * 100) + 100);
        }
        MinimumCost(list);
    }

但是上面MinimumCostLow时间复杂度太高,多次循环影响效率

  • 优化后:
public static int MinimumCost(List<Integer> sticks) {
        // write your code here
        int res = 0;

        // 直接将list当成参数传给PriorityQueue的话,会调用heapify,实际时间复杂度只有O(n);
        // 不要将元素一个个offer进去,这样的时间复杂度是O(nlog n),更慢
        System.out.println("初始:" + sticks);
        PriorityQueue<Integer> minHeap = new PriorityQueue<>(sticks);
        System.out.println("入栈" + minHeap);

        while (minHeap.size() >= 2) {
            System.out.println("--------------栈内长度:" + minHeap.size() + "----------------");
            System.out.println("原栈结构:" + minHeap);
            int min1 = minHeap.poll();
            System.out.println("一取栈顶:" + minHeap + ",  栈顶值:" + min1);
            int min2 = minHeap.poll();
            System.out.println("二取栈顶:" + minHeap + ",  栈顶值:" + min2);
            // 累加费用
            res += min1 + min2;
            System.out.println("当前花费:" + res + "元");

            minHeap.offer(min1 + min2);
            System.out.println("入栈结构:" + minHeap + ",  入栈值:" + (min1 + min2));
        }
        System.out.println(res);
        return res;
    }

用栈PriorityQueue处理 效率会提升很多

标签:棒材,sticks,int,res,System,算法,println,连接,out
来源: https://blog.csdn.net/qq_42191373/article/details/117253825

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

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

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

ICode9版权所有