算法基础篇-09-贪心算法

1. 简介

  • 贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解;
  • 贪心算法并不保证会得到最优解,但是在某些问题上贪心算法的解就是最优解。要会判断一个问题能否用贪心算法来计算。

1.1 题目1: 找零问题

假设商店老板需要找零n元钱,钱币的面额有:100元、50元、20元、5元、1元,如何找零使得所需钱币的数量最少?

import java.util.Arrays;

/**
 * 贪心算法
 *
 * @author wql
 * @date 2022/12/14 19:44
 */
public class GreedySort {
    public static void main(String[] args) {
        int[] t = {100, 50, 20, 10, 5, 1};
        int[] ints = handGreedySort(t, 375);
        System.out.println("ints = " + Arrays.toString(ints));
    }


    /**
     * 贪心算法
     *
     * @param arr 人民币列表倒序,如果没倒序手动排序一下
     * @param n   金钱数额
     */
    public static int[] handGreedySort(int[] arr, int n) {
        //数组最后一位是找不开的零钱
        int[] result = new int[arr.length + 1];
        for (int i = 0; i < arr.length; i++) {
            //计算需要多少张面额的
            result[i] = n / arr[i];
            //取模计算余数
            n = n % arr[i];
        }
        result[arr.length] = n;
        return result;
    }
}

1.2 数字拼接问题

  • 有n个非负整数,将其按照字符串拼接的方式拼接为一个整数。如何拼接可以使得得到的整数最大?
  • 例:32,94,128,1286,6,71可以拼接除的最大整数为94716321286128;
/**
 * 贪心算法
 *
 * @author wql
 * @date 2022/12/14 19:44
 */
public class GreedySort {
    public static void main(String[] args) {
        Long aLong = handGreedySort(new String[]{"32", "94", "128", "1286", "6", "71"});
        System.out.println("aLong = " + aLong);
    }

    public static Long handGreedySort(String[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (Integer.parseInt(arr[j] + arr[j + 1]) < Integer.parseInt(arr[j + 1] + arr[j])) {
                    String temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return Long.parseLong(String.join("", arr));
    }
}

1.3 活动选择问题

  • 假设有n个活动,这些活动要占用同一片场地,而场地在某时刻只能供一个活动使用。
  • 每个活动都有一个开始时间s;和结束时间f(题目中时间以整数表示),表示活动在[s,f)区间占用场地;
  • 问:安排哪些活动能够使该场地举办的活动的个数最多?
    在这里插入图片描述


import java.util.*;

/**
 * 贪心算法
 *
 * @author wql
 * @date 2022/12/14 19:44
 */
public class GreedySort {
    public static void main(String[] args) {
        List<Map<Integer, Integer>> list = new ArrayList<>();
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 4);
        list.add(map);

        Map<Integer, Integer> map2 = new HashMap<>();
        map2.put(3, 5);
        list.add(map2);

        Map<Integer, Integer> map3 = new HashMap<>();
        map3.put(0, 6);
        list.add(map3);

        Map<Integer, Integer> map4 = new HashMap<>();
        map4.put(5, 7);
        list.add(map4);

        Map<Integer, Integer> map5 = new HashMap<>();
        map5.put(3, 9);
        list.add(map5);

        Map<Integer, Integer> map6 = new HashMap<>();
        map6.put(5, 9);
        list.add(map6);

        Map<Integer, Integer> map7 = new HashMap<>();
        map7.put(6, 10);
        list.add(map7);

        Map<Integer, Integer> map8 = new HashMap<>();
        map8.put(8, 11);
        list.add(map8);


        Map<Integer, Integer> map9 = new HashMap<>();
        map9.put(8, 12);
        list.add(map9);

        Map<Integer, Integer> map10 = new HashMap<>();
        map10.put(2, 14);
        list.add(map10);


        Map<Integer, Integer> map11 = new HashMap<>();
        map11.put(12, 16);
        list.add(map11);


        List<Map<Integer, Integer>> maps = handGreedySort(list);
        System.out.println("maps = " + maps);
    }

    public static List<Map<Integer, Integer>> handGreedySort(List<Map<Integer, Integer>> list) {
        //保证活动是按照结束时间排好序的;
        List<Map<Integer, Integer>> result = new ArrayList<>();
        result.add(list.get(0));

        for (int i = 1; i < list.size(); i++) {
            Map<Integer, Integer> temp = list.get(i);
            int kTmp = 0;
            for (Integer k : temp.keySet()) {
                kTmp = k;
            }

            Map<Integer, Integer> mapT = result.get(result.size() - 1);
            int valT = 0;
            for (Integer integer : mapT.keySet()) {
                valT = mapT.get(integer);
            }
            if (kTmp >= valT) {
                result.add(temp);
            }
        }
        return result;
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alan0517

感谢您的鼓励与支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值