第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 JavaScript 大集合中進(jìn)行排序和搜索

如何在 JavaScript 大集合中進(jìn)行排序和搜索

手掌心 2022-10-13 10:49:58
數(shù)組“分?jǐn)?shù)”表示參與比賽的每個人的總分。例如:User A: 100 pointsUser B: 90 pointsUser C: 90 pointsUser D: 80 pointsUser E: 75 pointsUser F: 60 points根據(jù)上面的分?jǐn)?shù),我們會有這個排名:User A: #1User B: #2  User C: #2User D: #3User E: #4User F: #5這種排名方法遵循密集排名方法。然后我們有一個名為 alice 的用戶。如果她得到 55 分,她將排在第 6 位(根據(jù)上面的排名)。如果她獲得 90 分,她將排在第 2 位。等等。我實(shí)際上有一個包含愛麗絲不同“會話”的數(shù)組。例如:[55, 90]這意味著愛麗絲第一次將排在第 6 位。而第二次她將排名第二。我對此進(jìn)行了編碼,并且可以正常工作。但是,這似乎不是很有效。對于分?jǐn)?shù)數(shù)組中有 50 萬個條目的大型數(shù)據(jù)集,它會超時。這是代碼:const getPosition = (element, scores) => {    scores.push(element);    scores.sort(function (a,b) { return b-a; });    return scores.indexOf(element)+1;}function climbingLeaderboard(scores, alice) {    var uniqueSet = new Set(scores);    scores = [...uniqueSet];    var positions = [];    let aliceIndex = 0;    while(aliceIndex < alice.length){        positions.push(getPosition(alice[aliceIndex], scores));        aliceIndex++;    }    return positions;}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.join("\n") + "\n");}我猜“排序”功能和/或使用 indexOf 搜索數(shù)組中的元素是問題所在。但是我找不到讓這兩個操作更高效的方法。
查看完整描述

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");

}


查看完整回答
反對 回復(fù) 2022-10-13
?
狐的傳說

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));


查看完整回答
反對 回復(fù) 2022-10-13
?
郎朗坤

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();


查看完整回答
反對 回復(fù) 2022-10-13
  • 3 回答
  • 0 關(guān)注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號