|
|
楼主能帮我看下这样做对吗?谢谢
- class Solution {
- public static void main(String[] args) {
- Solution sol = new Solution();
- List<List<String>> input = new ArrayList<>();
- input.add(Arrays.asList("2-4", "100"));
- input.add(Arrays.asList("4-8", "200"));
- input.add(Arrays.asList("5-7", "300"));
- System.out.println(sol.findMaxProfit(input));
- }
-
- public int findMaxProfit(List<List<String>> input) {
- if (input == null || input.size() == 0) return 0;
- List<Interval> orders = new ArrayList<>();
- for (List<String> list : input) {
- String[] time = list.get(0).split("-");
- int start = Integer.parseInt(time[0]);
- int end = Integer.parseInt(time[1]);
- int profit = Integer.parseInt(list.get(1));
- orders.add(new Interval(start, end, profit));
- }
- Collections.sort(orders, (o1, o2) -> {
- return Integer.compare(o1.start, o2.start);
- });
- // DP => look back all results
- int[] M = new int[orders.size()];
- M[0] = orders.get(0).profit;
- int global = 0;
- for (int i = 1; i < orders.size(); i++) {
- int max = orders.get(i).profit;
- for (int j = 0; j < i; j++) {
- if (orders.get(j).end <= orders.get(i).start) { // if no overlap
- max = Math.max(max, M[j] + max);
- }
- }
- M[i] = max;
- global = Math.max(global, max);
- }
- return global;
- }
- }
- class Interval {
- int start;
- int end;
- int profit;
- public Interval(int start, int end, int profit) {
- this.start = start;
- this.end = end;
- this.profit = profit;
- }
- }
-
复制代码
补充内容 (2019-1-24 14:04):
32应该是max = Math.max(max, M[j] + orders.get(i).profit);
补充内容 (2019-1-24 14:06):
27 global = M[0] |
|