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.