Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 219 · Fixed Window

Contains Duplicate II

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

Last-seen index map; compare distance.

எளிதுArrayHash TableSliding Window
LeetCode இல் திற ↗

நான் Contains Duplicate II (LeetCode 219) ஐ இப்படித் தீர்த்தேன் — Fixed Window பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

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

Intuition

Remember the last index of every value. When the same value shows up again, check whether the distance to its last occurrence is at most k.

Approach

  1. 1

    For each index i and value num.

  2. 2

    If num was seen and i − last ≤ k, return True.

  3. 3

    Update the last index for num.

நான் எப்படித் தீர்த்தேன்

Last-index mapPythonO(n) time · O(n) space
class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        seen = {}
        for i,num in enumerate(nums):
            if num in seen:
                 j = seen.get(num)
                 if abs(i-j)<=k:
                    return True
            seen[num]=i
        return False
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Contains Duplicate II — அடிக்கடி கேட்கப்படும் கேள்விகள்

Contains Duplicate II பின்னாலுள்ள intuition என்ன?

Remember the last index of every value. When the same value shows up again, check whether the distance to its last occurrence is at most k.

Contains Duplicate II (LeetCode 219) ஐ எப்படித் தீர்ப்பது?

1. For each index i and value num. 2. If num was seen and i − last ≤ k, return True. 3. Update the last index for num.

Contains Duplicate II இன் time மற்றும் space complexity என்ன?

Time: O(n). Space: O(n). (Accepted approach: Last-index map.)

Contains Duplicate II இல் பொதுவான தவறுகள் என்ன?

Always update to the latest index — older ones are farther away.