貪心算法-金條切割問題


題目:
一塊金條切成兩半,是需要花費和長度數值一樣的銅板的。比如長度為20的金條,不管切成長度多大的兩半,都要花費20個銅板。
問:一群人想整分整塊金條,怎么分最省銅板? 例如,給定數組{
10,20,30},代表一共三個人,整塊金條長度為10+20+30=60。 金條要分成10,20,30。如果先把長度60的金條分成10和50,花費60;再把長度50的金條分成20和30,花費50;一共花費110銅板。 但是如果先把長度60的金條分成30和30,花費60;再把長度30金條分成10和20,花費30;一共花費90銅板。 輸入一個數組,返回分割的最小代價。

solution:

1、准備一個小根堆
2、把所有樹放到小根堆中 3、然后依次彈出兩個數,求和 4、把和扔到小根堆中 循環3、4步驟

代碼:

package Algorithms.greed;

import java.util.PriorityQueue;

public class LessMoneySplitGold {

    public static int lessMoney(int[] arr) {
        PriorityQueue<Integer> pQ = new PriorityQueue<>(); //1、准備一個小根堆
        for (int i = 0; i < arr.length; i++) { //2、把所有數字扔到小根堆中
            pQ.add(arr[i]);
        }
        int sum = 0;
        int cur = 0;
        while (pQ.size() > 1) {
            cur = pQ.poll() + pQ.poll(); //3、每次彈出兩個數字進行結合
            sum += cur;
            pQ.add(cur); //4、把結合的數扔到小根堆中
        }
        return sum;
    }


    public static void main(String[] args) {
        // solution
        int[] arr = {6, 7, 8, 9};
        System.out.println(lessMoney(arr)); //60
    }
}
package Algorithms.greed;

import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.PriorityQueue;

public class LessMoneySplitGold {

    public static int lessMoney(int[] arr) {
        //1、准備一個小根堆
        //PriorityQueue<Integer> pQ = new PriorityQueue<>();
        Arrays.sort(arr);
        LinkedList<Integer> q = new LinkedList<>();

        for (int i = 0; i < arr.length; i++) { //1、把所有數字扔到小根堆中
            q.add(arr[i]);
        }
        int sum = 0;
        int cur = 0;
        while (q.size() > 1) {
            cur = q.poll() + q.poll(); //2、每次彈出兩個數字進行結合
            System.out.println(cur);
            sum += cur;
            q.add(cur); //3、把結合的數扔到小根堆中
        }
        return sum;
    }

    //比較器構建小根堆
    public static class MinheapComparator implements Comparator<Integer> {

        @Override
        public int compare(Integer o1, Integer o2) {
            return o1 - o2; // < 0  o1 < o2  負數
        }

    }

    //比較器構建大根堆
    public static class MaxheapComparator implements Comparator<Integer> {

        @Override
        public int compare(Integer o1, Integer o2) {
            return o2 - o1; // <   o2 < o1
        }

    }

    public static void main(String[] args) {
        // solution
        int[] arr = { 6, 7, 8, 9 };
        System.out.println(lessMoney(arr));  // 60

        int[] arrForHeap = { 3, 5, 2, 7, 0, 1, 6, 4 };

        // min heap
        PriorityQueue<Integer> minQ1 = new PriorityQueue<>();
        for (int i = 0; i < arrForHeap.length; i++) {
            minQ1.add(arrForHeap[i]);
        }
        while (!minQ1.isEmpty()) {
            System.out.print(minQ1.poll() + " "); //0 1 2 3 4 5 6 7
        }
        System.out.println();

        // min heap use Comparator
        PriorityQueue<Integer> minQ2 = new PriorityQueue<>(new MinheapComparator());
        for (int i = 0; i < arrForHeap.length; i++) {
            minQ2.add(arrForHeap[i]);
        }
        while (!minQ2.isEmpty()) {
            System.out.print(minQ2.poll() + " ");  //
        }
        System.out.println();

        // max heap use Comparator
        PriorityQueue<Integer> maxQ = new PriorityQueue<>(new MaxheapComparator());
        for (int i = 0; i < arrForHeap.length; i++) {
            maxQ.add(arrForHeap[i]);
        }
        while (!maxQ.isEmpty()) {
            System.out.print(maxQ.poll() + " ");
        }

    }
}

/**
 * 13
 * 17
 * 30
 * 60
 * 0 1 2 3 4 5 6 7 
 * 0 1 2 3 4 5 6 7 
 * 7 6 5 4 3 2 1 0 
 */
大根堆、小根堆的構建

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM