/*
* function Point(a, b){
* this.x = a || 0;
* this.y = b || 0;
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param N int整型 N
* @param V int整型 V
* @param A int整型一维数组 A
* @param list Point类一维数组 List
* @return int整型
*/
function Travel( N , V , A , list ) {
// write code here
let inDegreeList = new Array(N).fill(0);
let successorList = new Array(N).fill([]);
let topologyArr = [];
let resList = [];
for (let item of list) {
inDegreeList[item.y - 1]++;
successorList[item.x - 1] = [...successorList[item.x - 1], item.y - 1];
}
for (let i = 0; i < N; i++) {
if (inDegreeList[i] === 0) {
topologyArr.push(i);
}
}
topologyArr.sort((b, a) => {
return A[a] - A[b];
});
while (topologyArr.length) {
let cur = topologyArr.pop();
resList.push(cur + 1);
for (let item of successorList[cur]) {
inDegreeList[item]--;
if (inDegreeList[item] === 0) {
topologyArr.push(item);
}
}
topologyArr.sort((b, a) => {
return A[a] - A[b];
});
}
let res = 0;
resList.forEach(item => {
if (V - A[item-1] >= 0) {
V -= A[item-1];
res++;
}
});
return res;
}
module.exports = {
Travel : Travel
};
js解,最初想法,没有优化

京公网安备 11010502036488号