2653. Sliding Subarray Beauty

Explanation

To solve this problem, we can use a sliding window approach. We will maintain a sliding window of size k and keep track of the count of negative numbers in the window. At each step, we update the beauty value for the current subarray based on the xth smallest negative number in the window.

  1. Initialize a hashmap to store the count of each number in the current window.
  2. Initialize pointers left and right to define the window boundaries.
  3. Iterate through the array:
    • Update the count of the number at right in the hashmap.
    • Increment right until the window size is k.
    • While the window size is greater than k:
      • Decrement the count of the number at left in the hashmap.
      • Increment left.
    • Update the beauty value for the current window based on the xth smallest negative number.
    • Move to the next window by incrementing right.
import java.util.*;

class Solution {
    public int[] slidingSubarrayBeauty(int[] nums, int k, int x) {
        int n = nums.length;
        int[] result = new int[n - k + 1];
        Map<Integer, Integer> countMap = new HashMap<>();
        int left = 0, right = 0;
        
        while (right < n) {
            countMap.put(nums[right], countMap.getOrDefault(nums[right], 0) + 1);
            
            if (right - left + 1 == k) {
                int negatives = 0;
                PriorityQueue<Integer> pq = new PriorityQueue<>();
                for (int i = left; i <= right; i++) {
                    if (nums[i] < 0) {
                        pq.offer(nums[i]);
                        negatives++;
                    }
                }
                
                if (negatives < x) {
                    result[left] = 0;
                } else {
                    int count = 0;
                    int beauty = 0;
                    while (!pq.isEmpty() && count < x) {
                        beauty = pq.poll();
                        count++;
                    }
                    result[left] = beauty;
                }
                
                countMap.put(nums[left], countMap.get(nums[left]) - 1);
                if (countMap.get(nums[left]) == 0) {
                    countMap.remove(nums[left]);
                }
                
                left++;
            }
            
            right++;
        }
        
        return result;
    }
}

Code Editor (Testing phase)

Improve Your Solution

Use the editor below to refine the provided solution. Select a programming language and try the following:

  • Add import statement if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.