pythonalgorithmsliding-window

Sliding subarray beauty working on IDE but not on leetcode


Problem

2653. Sliding Subarray Beauty

Given an integer array nums containing n integers, find the beauty of each subarray of size k.

The beauty of a subarray is the xth smallest integer in the subarray if it is negative, or 0 if there are fewer than x negative integers.

Return an integer array containing n - k + 1 integers, which denote the beauty of the subarrays in order from the first index in the array.

Code

class Solution(object):
    def getSubarrayBeauty(self, nums, k, x):
        """
        :type nums: List[int]
        :type k: int
        :type x: int
        :rtype: List[int]
        """
        eleToFreq = {m: 0 for m in range(-50, 0)}
        i = j = 0
        toReturn = list()
        while j < len(nums):
            if j >= k - 1:
                if nums[j] < 0:
                    eleToFreq[nums[j]] += 1
                a = 0
                for key, value in eleToFreq.items():
                    if value > 0 and a == x - 1:
                        toReturn.append(key)
                        a += 1
                    elif value > 0:
                        a += value
                if a < x :
                    toReturn.append(0)
                if nums[i] < 0:
                    eleToFreq[nums[i]] -= 1
                i += 1
                j += 1
            else:
                if nums[j] < 0:
                    eleToFreq[nums[j]] += 1
                j += 1
        return toReturn


solution = Solution()
v = solution.getSubarrayBeauty([1, -1, -3, -2, 3], 3, 2)  # expected output [-1,-2,-2]
print(v)

v = solution.getSubarrayBeauty([-1, -2, -3, -4, -5], 2, 2)  # expected output [-1,-2,-3,-4]
print(v)

v = solution.getSubarrayBeauty([-3, 1, 2, -3, 0, -3], 2, 1)  # expected output [-3,0,-3,-3,-3]
print(v)

v = solution.getSubarrayBeauty([1,-1,-3,-2,3], 3, 2)  # expected output [-1,-2,-2]
print(v)

v = solution.getSubarrayBeauty([-43], 1, 1)  # expected output [-43]
print(v)

Issue

These cases work in IDE but not in Leetcode. I get all the expected answers in Intellij but same code gives me wrong answer in leetcode on the following cases

nums=[1,-1,-3,-2,3] k=3 x=2 expected output: [-1,-2,-2] actual output [-3,-3,-2]
nums=[-1,-2,-3,-4,-5] k=2 x=2 expected output: [-1,-2,-3,-4] actual output [-2,-2,-3,-4]

Solution

  • The result on your local environment is different from LeetCode's because you have chosen Python version 2 on the LeetCode site. Your algorithm needs the iteration of the dict eleToFreq to happen in insertion order. But before Python 3.7 there was no guarantee that iteration over a dict would maintain insertion order.

    The solution is to choose "Python3" instead of "Python" (which is version 2) as engine for your LeetCode submission.

    Other issue

    There is another problem though in your algorithm:

    When value is greater than 1, and a == x - 1 is not true, then there is no verification whether any other of the duplicates of a count as the xth occurrence. This test only verifies whether the first occurrence of a would be the xth occurrence.

    To fix that issue, replace this code:

    if value > 0 and a == x - 1:
        toReturn.append(key)
        a += 1
    elif value > 0:
        a += value
    

    ...with this:

    if value > 0 and a <= x - 1 < a + value:
        toReturn.append(key)
    a += value