返回介绍

solution / 2600-2699 / 2653.Sliding Subarray Beauty / README_EN

发布于 2024-06-17 01:03:01 字数 6815 浏览 0 评论 0 收藏 0

2653. Sliding Subarray Beauty

中文文档

Description

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._

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

 

Example 1:

Input: nums = [1,-1,-3,-2,3], k = 3, x = 2
Output: [-1,-2,-2]
Explanation: There are 3 subarrays with size k = 3. 
The first subarray is [1, -1, -3] and the 2nd smallest negative integer is -1. 
The second subarray is [-1, -3, -2] and the 2nd smallest negative integer is -2. 
The third subarray is [-3, -2, 3] and the 2nd smallest negative integer is -2.

Example 2:

Input: nums = [-1,-2,-3,-4,-5], k = 2, x = 2
Output: [-1,-2,-3,-4]
Explanation: There are 4 subarrays with size k = 2.
For [-1, -2], the 2nd smallest negative integer is -1.
For [-2, -3], the 2nd smallest negative integer is -2.
For [-3, -4], the 2nd smallest negative integer is -3.
For [-4, -5], the 2nd smallest negative integer is -4. 

Example 3:

Input: nums = [-3,1,2,-3,0,-3], k = 2, x = 1
Output: [-3,0,-3,-3,-3]
Explanation: There are 5 subarrays with size k = 2.
For [-3, 1], the 1st smallest negative integer is -3.
For [1, 2], there is no negative integer so the beauty is 0.
For [2, -3], the 1st smallest negative integer is -3.
For [-3, 0], the 1st smallest negative integer is -3.
For [0, -3], the 1st smallest negative integer is -3.

 

Constraints:

  • n == nums.length 
  • 1 <= n <= 105
  • 1 <= k <= n
  • 1 <= x <= k 
  • -50 <= nums[i] <= 50 

Solutions

Solution 1

from sortedcontainers import SortedList


class Solution:
  def getSubarrayBeauty(self, nums: List[int], k: int, x: int) -> List[int]:
    sl = SortedList(nums[:k])
    ans = [sl[x - 1] if sl[x - 1] < 0 else 0]
    for i in range(k, len(nums)):
      sl.remove(nums[i - k])
      sl.add(nums[i])
      ans.append(sl[x - 1] if sl[x - 1] < 0 else 0)
    return ans
class Solution {
  public int[] getSubarrayBeauty(int[] nums, int k, int x) {
    int n = nums.length;
    int[] cnt = new int[101];
    for (int i = 0; i < k; ++i) {
      ++cnt[nums[i] + 50];
    }
    int[] ans = new int[n - k + 1];
    ans[0] = f(cnt, x);
    for (int i = k, j = 1; i < n; ++i) {
      ++cnt[nums[i] + 50];
      --cnt[nums[i - k] + 50];
      ans[j++] = f(cnt, x);
    }
    return ans;
  }

  private int f(int[] cnt, int x) {
    int s = 0;
    for (int i = 0; i < 50; ++i) {
      s += cnt[i];
      if (s >= x) {
        return i - 50;
      }
    }
    return 0;
  }
}
class Solution {
public:
  vector<int> getSubarrayBeauty(vector<int>& nums, int k, int x) {
    int n = nums.size();
    int cnt[101]{};
    for (int i = 0; i < k; ++i) {
      ++cnt[nums[i] + 50];
    }
    vector<int> ans(n - k + 1);
    auto f = [&](int x) {
      int s = 0;
      for (int i = 0; i < 50; ++i) {
        s += cnt[i];
        if (s >= x) {
          return i - 50;
        }
      }
      return 0;
    };
    ans[0] = f(x);
    for (int i = k, j = 1; i < n; ++i) {
      ++cnt[nums[i] + 50];
      --cnt[nums[i - k] + 50];
      ans[j++] = f(x);
    }
    return ans;
  }
};
func getSubarrayBeauty(nums []int, k int, x int) []int {
  n := len(nums)
  cnt := [101]int{}
  for _, x := range nums[:k] {
    cnt[x+50]++
  }
  ans := make([]int, n-k+1)
  f := func(x int) int {
    s := 0
    for i := 0; i < 50; i++ {
      s += cnt[i]
      if s >= x {
        return i - 50
      }
    }
    return 0
  }
  ans[0] = f(x)
  for i, j := k, 1; i < n; i, j = i+1, j+1 {
    cnt[nums[i]+50]++
    cnt[nums[i-k]+50]--
    ans[j] = f(x)
  }
  return ans
}
function getSubarrayBeauty(nums: number[], k: number, x: number): number[] {
  const n = nums.length;
  const cnt: number[] = new Array(101).fill(0);
  for (let i = 0; i < k; ++i) {
    ++cnt[nums[i] + 50];
  }
  const ans: number[] = new Array(n - k + 1);
  const f = (x: number): number => {
    let s = 0;
    for (let i = 0; i < 50; ++i) {
      s += cnt[i];
      if (s >= x) {
        return i - 50;
      }
    }
    return 0;
  };
  ans[0] = f(x);
  for (let i = k, j = 1; i < n; ++i, ++j) {
    cnt[nums[i] + 50]++;
    cnt[nums[i - k] + 50]--;
    ans[j] = f(x);
  }
  return ans;
}

Solution 2

class Solution:
  def getSubarrayBeauty(self, nums: List[int], k: int, x: int) -> List[int]:
    def f(x: int) -> int:
      s = 0
      for i in range(50):
        s += cnt[i]
        if s >= x:
          return i - 50
      return 0

    cnt = [0] * 101
    for v in nums[:k]:
      cnt[v + 50] += 1
    ans = [f(x)]
    for i in range(k, len(nums)):
      cnt[nums[i] + 50] += 1
      cnt[nums[i - k] + 50] -= 1
      ans.append(f(x))
    return ans

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文