# 贪心算法-IPO问题

1、题目描述

2、思路解析

``````// 最多K个项目
// W是初始资金
// Profits[] Capital[] 一定等长
// 返回最终最大的资金
public static int findMaximizedCapital(int K, int W, int[] Profits, int[] Capital) {
PriorityQueue<Program> minCostQ = new PriorityQueue<>(new MinCostComparator());
PriorityQueue<Program> maxProfitQ = new PriorityQueue<>(new MaxProfitComparator());
for (int i = 0; i < Profits.length; i++) {
}
for (int i = 0; i < K; i++) {
while (!minCostQ.isEmpty() && minCostQ.peek().c <= W) {
}
//可能资本不足，造成现在还能做项目但是大根堆没有项目给你做了
if (maxProfitQ.isEmpty()) {
return W;
}
W += maxProfitQ.poll().p;
}
return W;
}

public static class Program {
public int p;
public int c;

public Program(int p, int c) {
this.p = p;
this.c = c;
}
}

public static class MinCostComparator implements Comparator<Program> {

@Override
public int compare(Program o1, Program o2) {
return o1.c - o2.c;
}

}
public static class MaxProfitComparator implements Comparator<Program> {

@Override
public int compare(Program o1, Program o2) {
return o2.p - o1.p;
}
}
``````

THE END

)">

)">