Skip to main content
added 95 characters in body; edited tags; edited title
Source Link
200_success
  • 144.1k
  • 22
  • 188
  • 473

A best possible solution to containNearbyDuplicates get very low score Leetcode: Detecting duplicate elements in an array within a k-element window

Upon solving the problem 'contain duplicates` in leetcode:

Given an array of integers and an integer k , find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k .

Example 1:

Input: nums = [1,2,3,1] , k = 3 
Output: true

Example 2:

Input: nums = [1,0,1,1] , k = 1 
Output: true

Example 3:

Input: nums = [1,2,3,1,2,3] , k = 2 
Output: false

Given an array of integers and an integer k , find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k .

Example 1:


Input: nums = [1,2,3,1] , k = 3 

Output: true

Example 2:


Input: nums = [1,0,1,1] , k = 1 

Output: true

Example 3:


Input: nums = [1,2,3,1,2,3] , k = 2 

Output: false

I tried best to write pythonica Pythonic style solution and improve the performance.

class Solution2:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        lookup = dict()  #{value:index}

        for cur, val in enumerate(nums):
            prev = lookup.get(val)

            if prev != None and cur - prev <= k: 
                #logging.debug(f"{cur - prev}")
                return True 
            lookup[val] = cur #add it to lookup 

        return False 

Runtime: 68 ms, faster than 12.21% of Python3 online submissions for Contains Duplicate II. Memory Usage: 20.4 MB, less than 13.64% of Python3 online submissions for Contains Duplicate II.

I am confused about the score,. I was 100% assure that it aswas the best possible solution.

What' sWhat's the problem with my solution?

A best possible solution to containNearbyDuplicates get very low score

Upon solving the problem 'contain duplicates` in leetcode:

Given an array of integers and an integer k , find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k .

Example 1:

Input: nums = [1,2,3,1] , k = 3 
Output: true

Example 2:

Input: nums = [1,0,1,1] , k = 1 
Output: true

Example 3:

Input: nums = [1,2,3,1,2,3] , k = 2 
Output: false

I tried best to write pythonic style solution and improve the performance

class Solution2:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        lookup = dict()  #{value:index}

        for cur, val in enumerate(nums):
            prev = lookup.get(val)

            if prev != None and cur - prev <= k: 
                #logging.debug(f"{cur - prev}")
                return True 
            lookup[val] = cur #add it to lookup 

        return False 

Runtime: 68 ms, faster than 12.21% of Python3 online submissions for Contains Duplicate II. Memory Usage: 20.4 MB, less than 13.64% of Python3 online submissions for Contains Duplicate II.

I am confused about the score, I 100% assure that it as the best possible solution.

What' s the problem with my solution?

Leetcode: Detecting duplicate elements in an array within a k-element window

Upon solving the problem 'contain duplicates` in leetcode:

Given an array of integers and an integer k , find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k .

Example 1:


Input: nums = [1,2,3,1] , k = 3 

Output: true

Example 2:


Input: nums = [1,0,1,1] , k = 1 

Output: true

Example 3:


Input: nums = [1,2,3,1,2,3] , k = 2 

Output: false

I tried best to write a Pythonic style solution and improve the performance.

class Solution2:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        lookup = dict()  #{value:index}

        for cur, val in enumerate(nums):
            prev = lookup.get(val)

            if prev != None and cur - prev <= k: 
                #logging.debug(f"{cur - prev}")
                return True 
            lookup[val] = cur #add it to lookup 

        return False 

Runtime: 68 ms, faster than 12.21% of Python3 online submissions for Contains Duplicate II. Memory Usage: 20.4 MB, less than 13.64% of Python3 online submissions for Contains Duplicate II.

I am confused about the score. I was 100% assure that it was the best possible solution.

What's the problem with my solution?

Source Link
Alice
  • 605
  • 5
  • 13

A best possible solution to containNearbyDuplicates get very low score

Upon solving the problem 'contain duplicates` in leetcode:

Given an array of integers and an integer k , find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k .

Example 1:

Input: nums = [1,2,3,1] , k = 3 
Output: true

Example 2:

Input: nums = [1,0,1,1] , k = 1 
Output: true

Example 3:

Input: nums = [1,2,3,1,2,3] , k = 2 
Output: false

I tried best to write pythonic style solution and improve the performance

class Solution2:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        lookup = dict()  #{value:index}

        for cur, val in enumerate(nums):
            prev = lookup.get(val)

            if prev != None and cur - prev <= k: 
                #logging.debug(f"{cur - prev}")
                return True 
            lookup[val] = cur #add it to lookup 

        return False 

Runtime: 68 ms, faster than 12.21% of Python3 online submissions for Contains Duplicate II. Memory Usage: 20.4 MB, less than 13.64% of Python3 online submissions for Contains Duplicate II.

I am confused about the score, I 100% assure that it as the best possible solution.

What' s the problem with my solution?