3 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個贊
這種方法假設(shè)在得分相等的情況下,Alice 應(yīng)該在得分范圍內(nèi)排在第一位,這意味著如果她得分 90,那么她將排在第 2 位,排在 100 分之后,但高于 90 分的其余部分
function calculatePositions(scores, aliceScores) {
let positions = [];
const uniqueScoresSet = new Set([...scores]);
const uniqueScoresArray = Array.from(uniqueScoresSet);
aliceScores.forEach((aliceScore) => {
let position = uniqueScoresArray.findIndex((score) => aliceScore >= score);
position = position === -1 ? scores.length : position + 1;
positions.push(position);
});
return positions;
}
function main() {
const scores = [100, 90, 90, 80, 75, 60];
const alice = [50, 65, 77, 90, 102];
let result = calculatePositions(scores, alice);
console.log(result.join("\n") + "\n");
}
這種方法假設(shè)在得分相同的情況下,Alice 應(yīng)該排在得分括號的最后,這意味著如果她得分 90,那么她將排在第 4 位,排在 100 分和另外兩個 90 分之后。
function calculatePositions(scores, aliceScores) {
let positions = [];
aliceScores.forEach((aliceScore) => {
let position = scores.findIndex((score) => aliceScore > score);
position = position === -1 ? scores.length : position + 1;
positions.push(position);
});
return positions;
}
function main() {
const scores = [100, 90, 90, 80, 75, 60];
const alice = [50, 65, 77, 90, 102];
let result = calculatePositions(scores, alice);
console.log(result.join("\n") + "\n");
}

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個贊
將您的功能更改getPosition為以下并嘗試。剛剛刪除了您的排序功能并使用條件進(jìn)行了完整的數(shù)組搜索。
const getPosition = (element, scores) => {
let length = scores.length;
let rank = 1;
for(let i=0; i<length; i++) {
if(scores[i] > element) {
rank++;
}
}
return rank;
}
const scores = [100, 90, 90, 80, 75, 60];
const alice = [50, 65, 77, 90, 102];
console.log(getPosition(77, scores));

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個贊
這是另一種方法......也許不是最有效的方法,但相信它可以解決問題。這實(shí)際上是 Jonas Wilms 對該問題的評論的一種實(shí)現(xiàn)。
這在某種程度上是一種蠻力解決方案,因?yàn)樗鼤闅v alice 的每個分?jǐn)?shù)的分?jǐn)?shù)數(shù)組。一種更有效的方法是將 alice 的分?jǐn)?shù)從高到低排序(但要跟蹤原始順序以便以正確的順序組織結(jié)果),然后同時遍歷分?jǐn)?shù)數(shù)組和 alice 的數(shù)組。
請注意,下面的解決方案會運(yùn)行問題中的測試用例,此外還會針對 1M 分?jǐn)?shù)數(shù)組運(yùn)行測試用例,該數(shù)組填充有 99,999 到 0 范圍內(nèi)的隨機(jī)分?jǐn)?shù)。
function climbingLeaderboard(scores, alice) {
scores.sort( (a, b) => b - a );
aliceRank = [];
for ( let aliceScore of alice ) {
let scoreIndex = 0;
let rank = 0;
while ( scoreIndex < scores.length ) {
if ( scoreIndex === 0 || scores[ scoreIndex - 1 ] !== scores[ scoreIndex ] ) {
rank++;
}
if ( scores[ scoreIndex ] <= aliceScore ) {
aliceRank.push( rank++ );
break;
}
scoreIndex++;
}
if ( scoreIndex === scores.length ) {
aliceRank.push( ++rank );
}
}
return aliceRank;
}
function main() {
const scores = [100, 90, 90, 80, 75, 60];
const alice = [50, 65, 77, 90, 102];
let result = climbingLeaderboard(scores, alice);
console.log(result);
console.log( 'Generating array of 1M scores' );
let scores2 = new Array( 1000000 );
for ( let i = 0; i < scores2.length; i++ ) {
scores2[ i ] = Math.floor( 100000 * Math.random() );
}
alice2 = [50000, 65000, 77000, 90000, 102000, -1];
let result2 = climbingLeaderboard(scores2, alice2);
console.log( `First of the 1M scores is ${scores2[0]} and last score is ${scores2[999999]}` );
console.log( result2 );
}
main();
添加回答
舉報