import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param k int整型
     * @param w int整型
     * @param profits int整型一维数组
     * @param costs int整型一维数组
     * @return int整型
     */
    public int findMaximizedProfits (int k, int w, int[] profits, int[] costs) {
        int n = profits.length;
        int temp = w;
        int[][] cows = new int[n][2];
        for (int i = 0; i < n; i++) {
            cows[i][0] = costs[i];
            cows[i][1] = profits[i];
        }
        Arrays.sort(cows, (a, b) -> a[0] - b[0]);
        PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> b - a);
        int cur = 0;
        for (int i = 0; i < k; i++) {
            while (cur < n && cows[cur][0] <= w) {
                q.offer(cows[cur][1]);
                cur++;
            }
            if (!q.isEmpty()) {
                w += q.poll();
            } else {
                break;
            }
        }
        return w - temp;
    }
}

本题知识点分析:

1.贪心算法

2.有序集合存取

3.集合排序

4.优先级队列自定义排序(Lambda表达式)

5.数学模拟

本题解题思路分析:

1.先将两个数组合并成一个二维数组

2.按成本对二维数组进行升序排序

3.优先级队列存储利润,按降序排序

4.最多K次投资,因此优先选择利润高的项目

本题使用编程语言: Java