返回介绍

solution / 1900-1999 / 1918.Kth Smallest Subarray Sum / README_EN

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

1918. Kth Smallest Subarray Sum

中文文档

Description

Given an integer array nums of length n and an integer k, return _the _kth _smallest subarray sum._

A subarray is defined as a non-empty contiguous sequence of elements in an array. A subarray sum is the sum of all elements in the subarray.

 

Example 1:

Input: nums = [2,1,3], k = 4
Output: 3
Explanation: The subarrays of [2,1,3] are:
- [2] with sum 2
- [1] with sum 1
- [3] with sum 3
- [2,1] with sum 3
- [1,3] with sum 4
- [2,1,3] with sum 6 
Ordering the sums from smallest to largest gives 1, 2, 3, 3, 4, 6. The 4th smallest is 3.

Example 2:

Input: nums = [3,3,5,5], k = 7
Output: 10
Explanation: The subarrays of [3,3,5,5] are:
- [3] with sum 3
- [3] with sum 3
- [5] with sum 5
- [5] with sum 5
- [3,3] with sum 6
- [3,5] with sum 8
- [5,5] with sum 10
- [3,3,5], with sum 11
- [3,5,5] with sum 13
- [3,3,5,5] with sum 16
Ordering the sums from smallest to largest gives 3, 3, 5, 5, 6, 8, 10, 11, 13, 16. The 7th smallest is 10.

 

Constraints:

  • n == nums.length
  • 1 <= n <= 2 * 104
  • 1 <= nums[i] <= 5 * 104
  • 1 <= k <= n * (n + 1) / 2

Solutions

Solution 1

class Solution:
  def kthSmallestSubarraySum(self, nums: List[int], k: int) -> int:
    def f(s):
      t = j = 0
      cnt = 0
      for i, x in enumerate(nums):
        t += x
        while t > s:
          t -= nums[j]
          j += 1
        cnt += i - j + 1
      return cnt >= k

    l, r = min(nums), sum(nums)
    return l + bisect_left(range(l, r + 1), True, key=f)
class Solution {
  public int kthSmallestSubarraySum(int[] nums, int k) {
    int l = 1 << 30, r = 0;
    for (int x : nums) {
      l = Math.min(l, x);
      r += x;
    }
    while (l < r) {
      int mid = (l + r) >> 1;
      if (f(nums, mid) >= k) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    return l;
  }

  private int f(int[] nums, int s) {
    int t = 0, j = 0;
    int cnt = 0;
    for (int i = 0; i < nums.length; ++i) {
      t += nums[i];
      while (t > s) {
        t -= nums[j++];
      }
      cnt += i - j + 1;
    }
    return cnt;
  }
}
class Solution {
public:
  int kthSmallestSubarraySum(vector<int>& nums, int k) {
    int l = 1 << 30, r = 0;
    for (int& x : nums) {
      l = min(l, x);
      r += x;
    }
    auto f = [&](int s) {
      int cnt = 0, t = 0;
      for (int i = 0, j = 0; i < nums.size(); ++i) {
        t += nums[i];
        while (t > s) {
          t -= nums[j++];
        }
        cnt += i - j + 1;
      }
      return cnt;
    };
    while (l < r) {
      int mid = (l + r) >> 1;
      if (f(mid) >= k) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    return l;
  }
};
func kthSmallestSubarraySum(nums []int, k int) int {
  l, r := 1<<30, 0
  for _, x := range nums {
    l = min(l, x)
    r += x
  }
  f := func(s int) (cnt int) {
    t := 0
    for i, j := 0, 0; i < len(nums); i++ {
      t += nums[i]
      for t > s {
        t -= nums[j]
        j++
      }
      cnt += i - j + 1
    }
    return
  }
  for l < r {
    mid := (l + r) >> 1
    if f(mid) >= k {
      r = mid
    } else {
      l = mid + 1
    }
  }
  return l
}

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

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

发布评论

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