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;
}
}