标签:blog load rem max 排列 pre 三次 val ddc
排名不高,再接再厉
class ParkingSystem {
int[] carContainers;
public ParkingSystem(int big, int medium, int small) {
carContainers = new int[]{big,medium,small};
}
public boolean addCar(int carType) {
return carContainers[carType-1]-->0;
}
}
class Solution {
public List<String> alertNames(String[] keyName, String[] keyTime) {
List<String> result = new ArrayList<>();
Map<String, List<Integer>> map = new HashMap<>();
// 建立map, key-value:name-timeList
for(int i=0; i<keyName.length; i++){
if(!map.containsKey(keyName[i])){
map.put(keyName[i], new ArrayList<>());
}
String[] HM = keyTime[i].split(":");
int timeCur = Integer.parseInt(HM[0])* 60 + Integer.parseInt(HM[1]);
map.get(keyName[i]).add(timeCur);
}
// 遍历所有员工,判断时间差
for(String name:map.keySet()){
List<Integer> timeList = map.get(name);
if(timeList.size()>=3){
Collections.sort(timeList);
for(int j=2; j<timeList.size(); j++){
int cal = timeList.get(j) - timeList.get(j-2);
if(cal <=60 && cal>=0){
result.add(name);
break;
}
}
}
}
Collections.sort(result);
return result;
}
}
class Solution {
// 回溯
int rows;
int cols;
public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
rows = rowSum.length;
cols = colSum.length;
int[][] result = new int[rows][cols];
dfs(result,rowSum,colSum,0,0);
return result;
}
public boolean dfs(int[][] result, int[] rowSum, int[] colSum, int row, int col){
if(allZero(rowSum, colSum))return true;
if(col == cols){
return dfs(result,rowSum,colSum,row+1,0);
}
if(row == rows)return false;
//先选择最大可选的数字
int chooseNum = Math.min(rowSum[row], colSum[col]);
for(int i=chooseNum; i>=0; i--){
rowSum[row] -= i;
colSum[col] -= i;
if(dfs(result,rowSum,colSum,row,col+1)){
result[row][col] = i;
return true;
}
rowSum[row] += i;
colSum[col] += i;
}
return false;
}
public boolean allZero(int[] rowSum, int[] colSum){
int sum=0;
for(int n:rowSum)sum+=n;
for(int n:colSum)sum+=n;
return sum==0;
}
}
class Solution {
public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
int rows = rowSum.length;
int cols = colSum.length;
int[][] result = new int[rows][cols];
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
result[i][j] = Math.min(rowSum[i],colSum[j]);
rowSum[i]-=result[i][j];
colSum[j]-=result[i][j];
}
}
return result;
}
}
class Solution {
class BusinessServer{
int server;
int endTime;
public BusinessServer(int server, int endTime){
this.server = server;
this.endTime = endTime;
}
public int getServer(){
return server;
}
public int getEndTime(){
return endTime;
}
}
public List<Integer> busiestServers(int k, int[] arrival, int[] load) {
List<Integer> result = new ArrayList<>();
int[] num = new int[k];
PriorityQueue<BusinessServer> bs = new PriorityQueue<>((o1,o2)->o1.getEndTime()-o2.getEndTime());
TreeSet<Integer> freeServer = new TreeSet();
//填充空闲服务器
for(int i=0; i<k; i++){
freeServer.add(i);
}
//遍历请求
for(int i=0; i<arrival.length; i++){
while(!bs.isEmpty() && bs.peek().getEndTime()<= arrival[i] ){
freeServer.add(bs.poll().getServer());
}
Integer nextServer = freeServer.ceiling(i%k);
if(nextServer==null){
nextServer = freeServer.ceiling(0);
}
if(nextServer!=null){
bs.add(new BusinessServer(nextServer, arrival[i]+load[i]));
freeServer.remove(nextServer);
num[nextServer]++;
}
}
int max = 0;
for(int i=0; i<k; i++){
max = Math.max(num[i],max);
}
for(int i=0; i<k; i++){
if(num[i]==max){
result.add(i);
}
}
return result;
}
}
标签:blog load rem max 排列 pre 三次 val ddc
原文地址:https://www.cnblogs.com/Yuasin/p/13768282.html