<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)274:H指數(shù)

          共 3255字,需瀏覽 7分鐘

           ·

          2021-05-29 07:17

          算法的重要性,我就不多說(shuō)了吧,想去大廠,就必須要經(jīng)過(guò)基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問(wèn)題叫做 H 指數(shù),我們先來(lái)看題面:
          https://leetcode-cn.com/problems/h-index/

          Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper, return compute the researcher's h-index.


          According to the definition of h-index on Wikipedia: A scientist has an index h if h of their n papers have at least h citations each, and the other n ? h papers have no more than h citations each.


          If there are several possible values for h, the maximum one is taken as the h-index.


          給定一位研究者論文被引用次數(shù)的數(shù)組(被引用次數(shù)是非負(fù)整數(shù))。編寫一個(gè)方法,計(jì)算出研究者的 h 指數(shù)。

          h 指數(shù)的定義:h 代表“高引用次數(shù)”(high citations),一名科研人員的 h 指數(shù)是指他(她)的 (N 篇論文中)總共有 h 篇論文分別被引用了至少 h 次。且其余的 N - h 篇論文每篇被引用次數(shù) 不超過(guò) h 次。

          例如:某人的 h 指數(shù)是 20,這表示他已發(fā)表的論文中,每篇被引用了至少 20 次的論文總共有 20 篇。

          示例


          輸入:citations = [3,0,6,1,5]
          輸出:3
          解釋:給定數(shù)組表示研究者總共有 5 篇論文,每篇論文相應(yīng)的被引用了 3, 0, 6, 1, 5 次。
            由于研究者有 3 篇論文每篇 至少 被引用了 3 次,其余兩篇論文每篇被引用 不多于 3 次,所以她的 h 指數(shù)是 3。


          解題

          https://blog.csdn.net/qq_41855420/article/details/88207199

          可能大家會(huì)被這道題看起來(lái)“高大上”的概念所嚇倒,其實(shí)這道題一點(diǎn)都不難。
          首先我們需要直到,H指數(shù)不會(huì)大于這些論文的總數(shù),因?yàn)樽疃嗨械恼撐亩急灰脽o(wú)窮次,這些論文的H指數(shù)也是論文數(shù)量。
          所以,我們定義一個(gè)數(shù)組dp[0,n]表示dp[i]表示被引用超過(guò)i次的論文數(shù)。然后從后往前掃描,判斷dp[i] >= i的第一個(gè)就是結(jié)果。


          class Solution {
          public:
            int hIndex(vector<int>& citations) {
              int citationsSize = citations.size();
              vector<int> dp(citationsSize + 1, 0);//用于記錄大于等于i的論文被引用的多少次
              for (auto num : citations) {
                if (num > citationsSize) {//這篇論文引用超過(guò)citationsSize也只能記作被引用了citationsSize次(因?yàn)镠指數(shù)不會(huì)大于論文的總數(shù))
                  dp[citationsSize] += 1;
                }
                else {//在界限內(nèi)直接自增
                  dp[num] += 1;
                }
              }
                  //因?yàn)樾枰易畲蟮腍值,所以需要從后往前掃描
                  //判斷是否所有的論文引用次數(shù)都大于等于citationsSize次
              if (dp[citationsSize] >= citationsSize) {
                return citationsSize;
              }
                  //然后從后往前開(kāi)始掃描(動(dòng)態(tài)規(guī)劃)
              for (int i = citationsSize - 1; i >= 0; --i) {
                dp[i] += dp[i + 1];//引用次數(shù)大于等于i + 1次的肯定 大于i次,所以需要加上(比如示例中引用次數(shù)為6的計(jì)算為超過(guò)3次的)
                if (dp[i] >= i) {//判斷是否達(dá)到H指數(shù)的標(biāo)準(zhǔn)
                  return i;
                }
              }
              return 0;
            }
          };


          好了,今天的文章就到這里,如果覺(jué)得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

          上期推文:

          LeetCode1-260題匯總,希望對(duì)你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)261:以圖判樹(shù)
          LeetCode刷題實(shí)戰(zhàn)262:行程和用戶
          LeetCode刷題實(shí)戰(zhàn)263:丑數(shù)
          LeetCode刷題實(shí)戰(zhàn)264:丑數(shù) II
          LeetCode刷題實(shí)戰(zhàn)265:粉刷房子II
          LeetCode刷題實(shí)戰(zhàn)266:回文排列
          LeetCode刷題實(shí)戰(zhàn)267:回文排列II
          LeetCode刷題實(shí)戰(zhàn)268:丟失的數(shù)字
          LeetCode刷題實(shí)戰(zhàn)269:火星詞典
          LeetCode刷題實(shí)戰(zhàn)270:最接近的二叉搜索樹(shù)值
          LeetCode刷題實(shí)戰(zhàn)271:字符串的編碼與解碼
          LeetCode刷題實(shí)戰(zhàn)272:最接近的二叉搜索樹(shù)值 II
          LeetCode刷題實(shí)戰(zhàn)273:整數(shù)轉(zhuǎn)換英文表示


          瀏覽 26
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲AV毛片在线观看 | 99s精品 | 国产免费看黄色 | 亚洲无码免费观看视频 | 亚洲精品高清无码视频 |