贪心算法:对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的选择是在某种意义上的局部最优解。

贪心算法并不保证会得到最优解。

找零问题

image.png
思路:先找最大的100元。再进一步看50元。

  1. package com.algorithm.demo.senior;
  2. import com.google.common.collect.Lists;
  3. import java.util.List;
  4. /**
  5. * @Author leijs
  6. * @date 2022/4/15
  7. */
  8. public class ChangeMoney {
  9. private static List<Integer> list = Lists.newArrayList(100, 50, 20, 5, 1);
  10. public static void main(String[] args) {
  11. System.out.println(change(165));
  12. System.out.println(change(266));
  13. System.out.println(change(1377));
  14. }
  15. public static List<Integer> change(int money) {
  16. List<Integer> result = Lists.newArrayList();
  17. for (int i = 0; i < list.size(); i++) {
  18. int denomination = list.get(i);
  19. int cur = money / denomination;
  20. result.add(cur);
  21. money = money % denomination;
  22. if (money == 0) {
  23. break;
  24. }
  25. }
  26. return result;
  27. }
  28. }

背包问题

image.png
image.png

  1. package com.algorithm.demo.senior;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.google.common.collect.Lists;
  4. import lombok.AllArgsConstructor;
  5. import lombok.Data;
  6. import java.util.List;
  7. /**
  8. * @Author leijs
  9. * @date 2022/4/15
  10. */
  11. public class FractionalBackpack {
  12. public static void main(String[] args) {
  13. List<Goods> goods = Lists.newArrayList(new Goods(60, 10),
  14. new Goods(100, 20),
  15. new Goods(120, 30));
  16. int totalWeight = 50;
  17. goods.sort(((o1, o2) -> o2.getPrice() / o2.getWeight() - o1.getPrice() / o1.getWeight()));
  18. System.out.println(JSONObject.toJSONString(goods));
  19. List<Double> weightGoods = Lists.newArrayList();
  20. for (Goods curGoods : goods) {
  21. int curWeight = curGoods.getWeight();
  22. if (curWeight <= totalWeight) {
  23. weightGoods.add(new Double("1"));
  24. totalWeight -= curWeight;
  25. } else {
  26. weightGoods.add(totalWeight / (double) curWeight);
  27. totalWeight = 0;
  28. break;
  29. }
  30. }
  31. System.out.println(JSONObject.toJSONString(weightGoods));
  32. }
  33. }
  34. @Data
  35. @AllArgsConstructor
  36. class Goods {
  37. private int price;
  38. private int weight;
  39. }

拼接最大数字问题

1650028590(1).png

  1. package com.algorithm.demo.senior;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.google.common.collect.Lists;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6. /**
  7. * @Author leijs
  8. * @date 2022/4/15
  9. */
  10. public class NumberJoin {
  11. public static void main(String[] args) {
  12. List<String> li = Lists.newArrayList("32", "94", "128", "1286", "6", "71");
  13. // a = "96" , "b" = "87"
  14. // a + b = "9687" b + a = "8796"
  15. // a + b > b + a
  16. join(li);
  17. System.out.println(JSONObject.toJSONString(li));
  18. System.out.println(li.stream().collect(Collectors.joining()));
  19. }
  20. public static void join(List<String> li) {
  21. // 排序
  22. li.sort((a, b) -> {
  23. String xy = a + b;
  24. String yx = b + a;
  25. return (int) (Long.parseLong(yx) - Long.parseLong(xy));
  26. });
  27. }
  28. }

活动选择问题

image.png
怎么贪心?
image.png

  1. package com.algorithm.demo.senior;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.google.common.collect.Lists;
  4. import lombok.AllArgsConstructor;
  5. import lombok.Getter;
  6. import lombok.ToString;
  7. import java.util.List;
  8. /**
  9. * @Author leijs
  10. * @date 2022/4/15
  11. */
  12. public class ActivitySelection {
  13. static List<Activity> activities = Lists.newArrayList(new Activity(1, 4),
  14. new Activity(3, 5),
  15. new Activity(0, 6),
  16. new Activity(5, 7),
  17. new Activity(3, 9),
  18. new Activity(5, 9),
  19. new Activity(6, 10),
  20. new Activity(8, 11),
  21. new Activity(8, 12),
  22. new Activity(2, 14),
  23. new Activity(12, 16));
  24. public static void main(String[] args) {
  25. List<Activity> res = Lists.newArrayList(activities.get(0));
  26. for (int i = 1; i < activities.size(); i++) {
  27. if (activities.get(i).getStart() >= res.get(res.size() - 1).getEnd()) {
  28. // 不冲突
  29. // 当前活动的开始时间大于等于最后一个活动的结束时间
  30. res.add(activities.get(i));
  31. }
  32. }
  33. System.out.println(JSONObject.toJSONString(res));
  34. }
  35. }
  36. @AllArgsConstructor
  37. @Getter
  38. class Activity {
  39. int start;
  40. int end;
  41. }

总结

  • 最优化问题