package dataClass.code03;
import java.util.Arrays;
import java.util.Comparator;
public class BestArrange {
public static class Program {
public int start;
public int end;
public Program(int start, int end) {
this.start = start;
this.end = end;
}
}
public static int bestArrange1(Program[] programs) {
if (programs == null || programs.length == 0) {
return 0;
}
return process(programs, 0, 0);
}
//还剩什么会议都放在programs里
//done之前已经安排了多少会议
//timeLine目前来到的时间点是什么
//目前来到timeLine的时间点,已经安排了done场会议,剩下的会议programs自由安排
//返回能安排的最多会议数量
public static int process(Program[] programs, int done, int timeLine) {
if (programs.length == 0) {
return done;
}
int max = done;
//当前安排的会议是什么会,每一个都枚举
for (int i = 0; i < programs.length; i++) {
if (programs[i].start >= timeLine) {
Program[] next = copyButExcept(programs, i);
max = Math.max(max, process(next, done + 1, programs[i].end));
}
}
return max;
}
public static Program[] copyButExcept(Program[] programs, int i) {
Program[] ans = new Program[programs.length - 1];
int index = 0;
for (int k = 0; k < programs.length; k++) {
if (k != i) {
ans[index++] = programs[k];
}
}
return ans;
}
public static int bestArrange2(Program[] programs) {
Arrays.sort(programs, new ProgramComparator());
int timeLine = 0;
int result = 0;
for (int i = 0; i < programs.length; i++) {
if (timeLine <= programs[i].start) {
result++;
timeLine = programs[i].end;
}
}
return result;
}
public static class ProgramComparator implements Comparator<Program> {
@Override
public int compare(Program o1, Program o2) {
return o1.end - o2.end;
}
}
}
2)路灯问题
package dataClass.code03;
import java.util.HashSet;
public class Light {
public static int minLight1(String road){
if(road == null || road.length() == 0){
return 0;
}
return process(road.toCharArray(),0,new HashSet<>());
}
//str[index....]位置,自由选择放不放灯
//str[0~inde-1] 已经作完决定了,那些放了灯的位置,存在了lights里
//要求选出能照亮所有点的方案,并且在这些有效的方案中,最少需要几盏灯
public static int process(char[] str,int index,HashSet<Integer> lights){
if(index == str.length){//结束的时候
for(int i=0;i<str.length;i++){
if(str[i]!='X'){//必是'.'
if(!lights.contains(i-1)&&!lights.contains(i)&&!lights.contains(i+1)){
return Integer.MAX_VALUE;
}
}
}
return lights.size();
}else {//str未结束
int no = process(str,index+1,lights);
int yes = Integer.MAX_VALUE;
if(str[index]=='.'){
lights.add(index);
yes = process(str,index+1,lights);
lights.remove(index);
}
return Math.min(no,yes);
}
}
public static int minLight2(String road){
char[] str = road.toCharArray();
int index = 0;
int light = 0;
while (index<str.length){
if(str[index] == 'X'){
index++;
}else {
light++;
if(index+1 == str.length){
break;
}else {
if(str[index+1] == 'X'){
index = index+2;
}else {
index = index+3;
}
}
}
}
return light;
}
}
3)金条问题
package dataClass.code03;
import java.util.PriorityQueue;
public class LessMoneySplitGold {
public static int lessMoney(int[] arr) {
PriorityQueue<Integer> pQ = new PriorityQueue<>();
for (int i = 0; i < arr.length; i++) {
pQ.add(arr[i]);
}
int sum = 0;
int cur = 0;
while (pQ.size() > 1) {
cur = pQ.poll() + pQ.poll();
sum += cur;
pQ.add(cur);
}
return sum;
}
}
4)项目资金问题
package dataClass.code03;
import java.util.Comparator;
import java.util.PriorityQueue;
public class IPO {
public static class Program {
public int p;
public int c;
public Program(int p, int c) {
this.p = p;
this.c = c;
}
}
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++) {
minCostQ.add(new Program(Profits[i], Capital[i]));
}
for (int i = 0; i < k; i++) {
while (!minCostQ.isEmpty() && minCostQ.peek().c <= w) {
maxProfitQ.add(new Program(Profits[i], Capital[i]));
}
if (maxProfitQ.isEmpty()) {
return w;
}
w += maxProfitQ.poll().p;
}
return w;
}
//根据花费组织的小根堆比较器
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;
}
}
}