大生意

https://www.lintcode.com/zh-cn/problem/big-business/

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Solution {
    /**
     * @param a: The cost of the film
     * @param b: The price of the selling of the film
     * @param k: The principal
     * @return: The answer
     */
    public int bigBusiness(int[] a, int[] b, int k) {
        // Write your code here
        List list = new ArrayList<>();
        for (int i = 0; i < a.length; i++) {
            if (b[i] > a[i]) {
                list.add(new Node(a[i], b[i]));
            }
        }
        Collections.sort(list, new Comparator() {
            @Override
            public int compare(Node o1, Node o2) {
//                以价格排序
                return o1.key - o2.key;
            }
        });
        for (int i = 0; i < list.size(); i++) {
            Node node = list.get(i);
            if (k >= node.key) {
                k += node.value - node.key;
            } else {
                break;
            }
        }
        return k;
    }

    private class Node {
        int key;
        int value;

        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }
}

你可能感兴趣的:(大生意)