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 x^th 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.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1
Inputnums = [1,-1,-3,-2,3], k = 3, x = 2
Output[-1,-2,-2]
There are 3 subarrays of size 3, and their 2nd smallest negative integers are -1, -2, and -2 respectively.
Example 2
Inputnums = [-1,-2,-3,-4,-5], k = 2, x = 2
Output[-1,-2,-3,-4]
There are 4 subarrays of size 2, and their 2nd smallest negative integers are -1, -2, -3, and -4 respectively.

Constraints

  • n == nums.length
  • 1 <= n <= 10^5
  • 1 <= k <= n
  • 1 <= x <= k
  • -50 <= nums[i] <= 50

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate