3/26网易雷火笔试第一题A了,第二题50%
import java.util.*;
// 第一题,用迪杰斯特拉算法即可解出
public class Solution {
public int getMinimumTime (int[] productList, int[][] drivingTimes) {
Arrays.sort(productList);
int[] dist = dijkstra(drivingTimes,0);
int time = dist[dist.length-1];
int res = 0;
int prev = productList[productList.length-1];
int cnt = 0;
for(int i=productList.length-2; i>=0; --i) {
if(prev+productList[i]>10) {
++cnt;
prev = productList[i];
} else {
prev += productList[i];
}
if(cnt>=4) {
res += time*2;
cnt = 0;
}
}
if(prev>0 || cnt>0) {
res += time;
}
return res;
}
public static int[] dijkstra(int[][] weight, int start) {
int n = weight.length;
int[] shortPath = new int[n];
int[] visited = new int[n];
shortPath[start] = 0;
visited[start] = 1;
for(int count = 1; count < n; count++) {
int k = -1;
int dmin = Integer.MAX_VALUE;
for(int i = 0; i < n; i++) {
if(visited[i] == 0 && weight[start][i] < dmin) {
dmin = weight[start][i];
k = i;
}
}
shortPath[k] = dmin;
visited[k] = 1;
for(int i = 0; i < n; i++) {
if(visited[i] == 0 && weight[start][k] + weight[k][i] < weight[start][i]) {
weight[start][i] = weight[start][k] + weight[k][i];
}
}
}
return shortPath;
}
} #网易雷火实习##网易##笔试题目#
查看9道真题和解析