DEV Community

codingpineapple
codingpineapple

Posted on

LeetCode 220. Contains Duplicate III (javascript solution)

Description:

Given an integer array nums and two integers k and t, return true if there are two distinct indices i and j in the array such that abs(nums[i] - nums[j]) <= t and abs(i - j) <= k.

Return the maximum number of events you can attend.

Solution:

Time Complexity : O(n^2)
Space Complexity: O(1)

var containsNearbyAlmostDuplicate = function(nums, k, t) {
    for(let i = 0; i < nums.length-1; i++) {
        for(let j = i+1; j < nums.length; j++ ) {
            if(Math.abs(nums[i] - nums[j]) <= t && Math.abs(i - j) <= k) {
                return true
            }
        }
    }
    return false
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)