Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 1539 · Math / Missing Numbers

Kth Missing Positive Number

LeetCode 1539 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

missing(i) = arr[i] − (i + 1); answer = low + k.

எளிதுArrayBinary Search
LeetCode இல் திற ↗

நான் Kth Missing Positive Number (LeetCode 1539) ஐ இப்படித் தீர்த்தேன் — Math / Missing Numbers பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

At index i, the count of missing numbers before arr[i] is arr[i] − (i + 1). Binary search the last index where fewer than k numbers are missing; the answer is then low + k.

Approach

  1. 1

    Linear version: every array value ≤ k pushes the answer up by one.

  2. 2

    Binary search on missing(mid) < k.

  3. 3

    After the loop, return low + k.

நான் எப்படித் தீர்த்தேன் · முதல் யோசனையிலிருந்து இறுதித் தீர்வு வரை

1
Linear shiftO(n) time

Bump k past each present number.

2
Binary searchAcceptedO(log n) time

Search on the missing count.

Linear shiftPythonO(n) time
class Solution:
    def findKthPositive(self, nums: List[int], k: int) -> int:
        for num in nums:
            if num>k:
                break
            else:
                k=k+1
        return k
Binary searchPythonO(log n) time
class Solution:
    def findKthPositive(self, nums: List[int], k: int) -> int:
        low=0
        high = len(nums)-1
        while low<=high:
            mid = (low+high)//2
            missing = nums[mid]-(mid+1)
            if missing<k:
                low = mid+1
            else:
                high = mid-1
        return low+k
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Kth Missing Positive Number — அடிக்கடி கேட்கப்படும் கேள்விகள்

Kth Missing Positive Number பின்னாலுள்ள intuition என்ன?

At index i, the count of missing numbers before arr[i] is arr[i] − (i + 1). Binary search the last index where fewer than k numbers are missing; the answer is then low + k.

Kth Missing Positive Number (LeetCode 1539) ஐ எப்படித் தீர்ப்பது?

1. Linear version: every array value ≤ k pushes the answer up by one. 2. Binary search on missing(mid) < k. 3. After the loop, return low + k.

Kth Missing Positive Number இன் time மற்றும் space complexity என்ன?

Time: O(log n). Space: O(1). (Accepted approach: Binary search.)

Kth Missing Positive Number இல் பொதுவான தவறுகள் என்ன?

The answer may be beyond the last element.

Kth Missing Positive Number ஐத் தீர்க்க ஒன்றுக்கு மேற்பட்ட வழிகள் உள்ளனவா?

Linear shift (O(n) time): Bump k past each present number. Binary search (O(log n) time): Search on the missing count.