MCPcopy Create free account
hub / github.com/CCCshengjiang/algorithm / IPO

Class IPO

data-structure-algorithm/src/cn/cwblue/greedy/IPO.java:17–58  ·  view source on GitHub ↗

输入:正整数数组costs、正数数组profits、正数K、正数M costs[i] 标识 i 号项目的花费 profits[i] 标识 i 号项目在扣除花费之后还能挣到的钱(利润) K 表示你只能串行的最多做 K 个项目 M 表示你初始的资金 说明:每做完一个项目,马上获得的收益,可以支持你去做下一个项目。不能并行的做项目 输出:你最后获得的最大钱数 @author wen

Source from the content-addressed store, hash-verified

15 * @author wen
16 */
17public class IPO {
18 public static class Program {
19 public int cost;
20 public int profits;
21 public Program(int cost, int profits) {
22 this.cost = cost;
23 this.profits = profits;
24 }
25 }
26
27 public static int findMaximizedCapital(int k, int w, int[] costs, int[] profits) {
28 PriorityQueue<Program> minCostQueue = new PriorityQueue<>(new MinCostComparator());
29 PriorityQueue<Program> maxProfitsQueue = new PriorityQueue<>(new MaxProfitsComparator());
30 for (int i = 0; i < costs.length; i++) {
31 minCostQueue.add(new Program(costs[i], profits[i]));
32 }
33 for (int i = 0; i < k; i++) {
34 while (!minCostQueue.isEmpty() && minCostQueue.peek().cost <= w) {
35 maxProfitsQueue.add(minCostQueue.poll());
36 }
37 if (minCostQueue.isEmpty()) {
38 return w;
39 }
40 w += maxProfitsQueue.poll().profits;
41 }
42 return w;
43 }
44
45 public static class MinCostComparator implements Comparator<Program> {
46 @Override
47 public int compare(Program o1, Program o2) {
48 return o1.cost - o2.cost;
49 }
50 }
51
52 public static class MaxProfitsComparator implements Comparator<Program> {
53 @Override
54 public int compare(Program o1, Program o2) {
55 return o2.profits - o1.profits;
56 }
57 }
58}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected