【数据结构的魅力】007.贪心算法

15 篇文章 0 订阅

贪心算法

求解标准过程

解题套路

套路实战

1) 会议时间

package dataClass.code03;

import java.util.Arrays;
import java.util.Comparator;

public class BestArrange {

    public static class Program {
        public int start;
        public int end;

        public Program(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }

    public static int bestArrange1(Program[] programs) {
        if (programs == null || programs.length == 0) {
            return 0;
        }
        return process(programs, 0, 0);
    }

    //还剩什么会议都放在programs里
    //done之前已经安排了多少会议
    //timeLine目前来到的时间点是什么

    //目前来到timeLine的时间点,已经安排了done场会议,剩下的会议programs自由安排
    //返回能安排的最多会议数量
    public static int process(Program[] programs, int done, int timeLine) {
        if (programs.length == 0) {
            return done;
        }
        int max = done;
        //当前安排的会议是什么会,每一个都枚举
        for (int i = 0; i < programs.length; i++) {
            if (programs[i].start >= timeLine) {
                Program[] next = copyButExcept(programs, i);
                max = Math.max(max, process(next, done + 1, programs[i].end));
            }
        }
        return max;
    }

    public static Program[] copyButExcept(Program[] programs, int i) {
        Program[] ans = new Program[programs.length - 1];
        int index = 0;
        for (int k = 0; k < programs.length; k++) {
            if (k != i) {
                ans[index++] = programs[k];
            }
        }
        return ans;
    }

    public static int bestArrange2(Program[] programs) {
        Arrays.sort(programs, new ProgramComparator());
        int timeLine = 0;
        int result = 0;
        for (int i = 0; i < programs.length; i++) {
            if (timeLine <= programs[i].start) {
                result++;
                timeLine = programs[i].end;
            }
        }
        return result;
    }

    public static class ProgramComparator implements Comparator<Program> {
        @Override
        public int compare(Program o1, Program o2) {
            return o1.end - o2.end;
        }
    }
}

2)路灯问题

package dataClass.code03;

import java.util.HashSet;

public class Light {
    public static int minLight1(String road){
        if(road == null || road.length() == 0){
            return 0;
        }
        return process(road.toCharArray(),0,new HashSet<>());
    }

    //str[index....]位置,自由选择放不放灯
    //str[0~inde-1] 已经作完决定了,那些放了灯的位置,存在了lights里
    //要求选出能照亮所有点的方案,并且在这些有效的方案中,最少需要几盏灯
    public static int process(char[] str,int index,HashSet<Integer> lights){
        if(index == str.length){//结束的时候
            for(int i=0;i<str.length;i++){
                if(str[i]!='X'){//必是'.'
                    if(!lights.contains(i-1)&&!lights.contains(i)&&!lights.contains(i+1)){
                        return Integer.MAX_VALUE;
                    }
                }
            }
            return lights.size();
        }else {//str未结束
            int no = process(str,index+1,lights);
            int yes = Integer.MAX_VALUE;
            if(str[index]=='.'){
                lights.add(index);
                yes = process(str,index+1,lights);
                lights.remove(index);
            }
            return Math.min(no,yes);
        }
    }

    public static int minLight2(String road){
        char[] str = road.toCharArray();
        int index = 0;
        int light = 0;
        while (index<str.length){
            if(str[index] == 'X'){
                index++;
            }else {
                light++;
                if(index+1 == str.length){
                    break;
                }else {
                    if(str[index+1] == 'X'){
                        index = index+2;
                    }else {
                        index = index+3;
                    }
                }
            }
        }
        return light;
    }
}

3)金条问题

package dataClass.code03;

import java.util.PriorityQueue;

public class LessMoneySplitGold {
    public static int lessMoney(int[] arr) {
        PriorityQueue<Integer> pQ = new PriorityQueue<>();
        for (int i = 0; i < arr.length; i++) {
            pQ.add(arr[i]);
        }
        int sum = 0;
        int cur = 0;
        while (pQ.size() > 1) {
            cur = pQ.poll() + pQ.poll();
            sum += cur;
            pQ.add(cur);
        }
        return sum;
    }
}

4)项目资金问题

package dataClass.code03;

import java.util.Comparator;
import java.util.PriorityQueue;

public class IPO {
    public static class Program {
        public int p;
        public int c;

        public Program(int p, int c) {
            this.p = p;
            this.c = c;
        }
    }

    public static int findMaximizedCapital(int k, int w, int[] Profits, int[] Capital) {
        PriorityQueue<Program> minCostQ = new PriorityQueue<>(new MinCostComparator());
        PriorityQueue<Program> maxProfitQ = new PriorityQueue<>(new MaxProfitComparator());
        for (int i = 0; i < Profits.length; i++) {
            minCostQ.add(new Program(Profits[i], Capital[i]));
        }
        for (int i = 0; i < k; i++) {
            while (!minCostQ.isEmpty() && minCostQ.peek().c <= w) {
                maxProfitQ.add(new Program(Profits[i], Capital[i]));
            }
            if (maxProfitQ.isEmpty()) {
                return w;
            }
            w += maxProfitQ.poll().p;
        }
        return w;
    }

    //根据花费组织的小根堆比较器
    public static class MinCostComparator implements Comparator<Program> {
        @Override
        public int compare(Program o1, Program o2) {
            return o1.c - o2.c;
        }
    }

    //根据利润组织的大根堆比较器
    public static class MaxProfitComparator implements Comparator<Program> {
        @Override
        public int compare(Program o1, Program o2) {
            return o2.p - o1.p;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值