<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      1539. Kth Missing Positive Number

      Given an array arr of positive integers sorted in a strictly increasing order, and an integer k.

      Return the kth positive integer that is missing from this array.

      Example 1:

      Input: arr = [2,3,4,7,11], k = 5
      Output: 9
      Explanation: The missing positive integers are [1,5,6,8,9,10,12,13,...]. The 5th missing positive integer is 9.
      

      Example 2:

      Input: arr = [1,2,3,4], k = 2
      Output: 6
      Explanation: The missing positive integers are [5,6,7,...]. The 2nd missing positive integer is 6.
      
       比較簡(jiǎn)單的做法是: 我們比較兩個(gè)連續(xù)的兩個(gè)數(shù),如果他們之間相差大于1,我們需要decrease k 直到k變成0. 我們需要考慮兩邊的情況。
       1 class Solution {
       2     public int findKthPositive(int[] arr, int k) {
       3         if (arr == null || arr.length == 0) return -1;
       4         if (arr[0] != 1) {
       5             if (arr[0] - 1 >= k) return k;
       6             k = k - (arr[0] - 1); // arr[0] -1 refers to the missing numbers before arr[0]
       7         } 
       8 
       9         for (int i = 1; i < arr.length; i++) {
      10             int diff = arr[i] - arr[i - 1] - 1;
      11             if (diff >= k) {
      12                 return arr[i - 1] + k;
      13             } else {
      14                 k -= diff;
      15             }
      16             
      17         }
      18         return arr[arr.length - 1] + k;
      19     }

      O(lgn)的解法:

      https://leetcode.com/problems/kth-missing-positive-number/solutions/779999/java-c-python-o-logn/

      My two cents on thought process for binary search:
      Here we have three sorted sequences:
      let n be len(A), then
      1st sorted sequence is array values:

      A[0], A[1], ... A[n-1]

      2nd is indices:

      0, 1, 2, ... n - 1

      The nice part about this question: the indices can help us to get all the positive numbers in sorted order (i + 1 if i denotes the index)
      Hence, A[i] - (i + 1) will be # of missing positives at index i, and this will be our 3rd sorted sequences
      i.e.

      A:            [1,3,4,6]
      A[i] - (i+1): [0,1,1,2]

      let's call array A[i]-(i+1) as B, which is [0, 1, 1, 2] above, then B[i] represents how many missing positives so far at index i.

      So the question becomes: finding the largest index of array B so that B[j] is smaller than K.
      It is the same as finding first/last occurrence 34. find-first-and-last-position-of-element-in-sorted-array

      l, r = 0, len(B)
      while l <= r:
          m = (r + l) / 2
          if B[m] < K:
              l = m + 1
          else:
              r = m - 1

      After while loop is stopped, l - 1 is our target index because, B[l - 1] represents how many positive is missing at index l - 1 that is smaller than K, so result is A[l -1](the largest number in A that is less than result) + K - B[l - 1](offset, how far from result) = (A[l - 1]) + (k - (A[l - 1] - l)) = l + k

       1     public int findKthPositive(int[] A, int k) {
       2         int l = 0, r = A.length - 1, m;
       3         while (l <= r) {
       4             m = (l + r) / 2;
       5             if (A[m] - (1 + m) < k)
       6                 l = m + 1;
       7             else
       8                 r = m - 1;
       9         }
      10         return l + k;
      11     }

       

       

      posted @ 2025-01-09 09:39  北葉青藤  閱讀(21)  評(píng)論(0)    收藏  舉報(bào)
      主站蜘蛛池模板: 青青草原国产精品啪啪视频| 中文字幕在线日韩| 国产成人精品一区二区不卡| 成在线人视频免费视频| 99精品国产综合久久久久五月天 | 99久久国产成人免费网站| 欧美人禽zozo动人物杂交| 精品无码国产自产拍在线观看蜜| 欧洲码亚洲码的区别入口| 欧美三级中文字幕在线观看| 性一交一乱一伦| 日韩毛片在线视频x| 久久精品国产亚洲av麻豆软件| 亚洲中文字幕av天堂| 青青草一区二区免费精品| 国产成AV人片久青草影院| 成全我在线观看免费第二季| 国产成人精品亚洲高清在线| 精品一区二区中文字幕| 国产精品久久久久久久专区| 午夜免费啪视频| 精品国偷自产在线视频99| 国产亚洲欧美另类一区二区| 好姑娘6电影在线观看| 国产网友愉拍精品视频手机| 成人国产亚洲精品一区二区| 亚洲码国产精品高潮在线| 丰满的人妻hd高清日本| 亚洲 一区二区 在线| 无码av免费毛片一区二区| 少妇人妻真实偷人精品| 亚洲中文字幕日产无码成人片| 国产精品高清中文字幕| 女人被狂躁的高潮免费视频| 亚洲熟妇自偷自拍另欧美| 国产精品视频不卡一区二区| 亚洲日韩一区精品射精| 狼人大伊人久久一区二区| 日本喷奶水中文字幕视频| 国产福利社区一区二区| 国产亚洲一区二区三区成人|