惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
The Register - Security
The Register - Security
D
Docker
The Cloudflare Blog
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
月光博客
月光博客
B
Blog RSS Feed
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
B
Blog
IT之家
IT之家
美团技术团队
Engineering at Meta
Engineering at Meta
C
Check Point Blog
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
G
Google Developers Blog
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
S
SegmentFault 最新的问题
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
U
Unit 42
H
Help Net Security
雷峰网
雷峰网
人人都是产品经理
人人都是产品经理
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
博客园 - Franky
PCI Perspectives
PCI Perspectives
J
Java Code Geeks
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
腾讯CDC
A
Arctic Wolf
C
CERT Recently Published Vulnerability Notes
量子位
C
CXSECURITY Database RSS Feed - CXSecurity.com
Latest news
Latest news
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Hacker News
The Hacker News
有赞技术团队
有赞技术团队
Schneier on Security
Schneier on Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - 北叶青藤

2096. Step-By-Step Directions From a Binary Tree Node to Another Find path from root to a target node in Binary Tree When Dijkstra Algorithm Should be Use? 1188. Design Bounded Blocking Queue 1115. Print FooBar Alternately 1114. Print in Order 1242. Web Crawler Multithreaded Python Multi-threading bot ip Log Rate Limiter Same Word of HTML Labels Most Frequent Call Chain remove prefix in a words list 1102. Path With Maximum Minimum Value Property Booking Optimizer minimum number 755. Pour Water Keyword Tagging in Reviews with Overlapping Matches Retryer Function Implementation 1125. Smallest Sufficient Team Print the terrain Split stay Task scheduling problem 滑雪问题 845. Longest Mountain in Array 723. Candy Crush 1650. Lowest Common Ancestor of a Binary Tree III 424. Longest Repeating Character Replacement 843. Guess the Word 551. Student Attendance Record I
1539. Kth Missing Positive Number
北叶青藤 · 2025-01-09 · via 博客园 - 北叶青藤

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.

 比较简单的做法是: 我们比较两个连续的两个数,如果他们之间相差大于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:

2nd is indices:

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     }