Min Cost to Connect Ropes

Min Cost to Connect Ropes 
一 题目

Given n ropes of different lengths, we need to connect these ropes into one rope. We can connect only 2 ropes at a time. The cost required to connect 2 ropes is equal to sum of their lengths. The length of this connected rope is also equal to the sum of their lengths. This process is repeated until n ropes are connected into a single rope. Find the min possible cost required to connect all ropes.

Example 1:

Input: ropes = [8, 4, 6, 12]
Output: 58
Explanation: The optimal way to connect ropes is as follows
1. Connect the ropes of length 4 and 6 (cost is 10). Ropes after connecting: [8, 10, 12]
2. Connect the ropes of length 8 and 10 (cost is 18). Ropes after connecting: [18, 12]
3. Connect the ropes of length 18 and 12 (cost is 30).
Total cost to connect the ropes is 10 + 18 + 30 = 58
Example 2:

Input: ropes = [20, 4, 8, 2]
Output: 54
Example 3:

Input: ropes = [1, 2, 5, 10, 35, 89]
Output: 224
Example 4:

Input: ropes = [2, 2, 3, 3]
Output: 20
二 分析
相对容易理解,有一堆绳子,用数组表示长度,两两链接,求所有链接绳子的和的最小值。

这也是amazon的题目,没有LeetCode的链接

2.1 优先级队列
  一个优先级队列。每次从队列中取出最短的两根绳子,进行链接,然后放回去。

    public static void main(String[] args) {
        int[] files1 = {8, 4, 6, 12};
        int[] files2 = {20, 4, 8, 2};
        int[] files3 = {1, 2, 5, 10, 35, 89};
        int[] files4 = {2, 2, 3, 3};
        System.out.println(mergeFiles(files1));
        System.out.println(mergeFiles(files2));
        System.out.println(mergeFiles(files3));
        System.out.println(mergeFiles(files4));
 
    }
 
    private static int mergeFiles(int[] files) {
        Queue<Integer> queue = new PriorityQueue<Integer>();
        for(int f:files){
          queue.offer(f);
        }
        int result =0;
        while(queue.size()>1 ){
            int tmp = queue.poll()+ queue.poll();
            result += tmp;
            
            queue.offer(tmp);
        }            
            
        return result;
    }
2.2 list
如果用list来实现的话,就是要自己去排序,从最小的开始累加(然后删除,放入结果)。最大的数只加一次。

思路跟上面一样的,比如我一开始没想到优先级队列。那么用list实现就是麻烦些,也能实现。

public static void main(String[] args) {
        int[] files1 = {8, 4, 6, 12};
        int[] files2 = {20, 4, 8, 2};
        int[] files3 = {1, 2, 5, 10, 35, 89};
        int[] files4 = {2, 2, 3, 3};
        System.out.println(mergeFiles(files1));
        System.out.println(mergeFiles(files2));
        System.out.println(mergeFiles(files3));
        System.out.println(mergeFiles(files4));
 
    }
    
    public static int mergeFiles(int[] files) {
        //first sort
        List<Integer> list = new ArrayList<Integer>();
        for(int f:files){
          list.add(f);
        }
       Collections.sort(list);
        int length = files.length;
        int result =0 ;
        while(list.size()>1) {         
               int tmp = list.get( 0)+list.get(1);
               result += tmp;
               list.add(tmp);
               list.remove(0);
               list.remove(0);           
           Collections.sort(list);
       }
       
       return result;    
    }
做题少了,对于这种问题没有形成条件反射,还是用笨办法list实现的(数组不好删除)。

今天做题最大收获是认识几个英语单词。算法没怎么提高。原题链接:

https://leetcode.com/discuss/interview-question/344677/Amazon-or-Online-Assessment-2019-or-Min-Cost-to-Connect-Ropes 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值