返回介绍

solution / 1500-1599 / 1590.Make Sum Divisible by P / README_EN

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

1590. Make Sum Divisible by P

中文文档

Description

Given an array of positive integers nums, remove the smallest subarray (possibly empty) such that the sum of the remaining elements is divisible by p. It is not allowed to remove the whole array.

Return _the length of the smallest subarray that you need to remove, or _-1_ if it's impossible_.

A subarray is defined as a contiguous block of elements in the array.

 

Example 1:

Input: nums = [3,1,4,2], p = 6
Output: 1
Explanation: The sum of the elements in nums is 10, which is not divisible by 6. We can remove the subarray [4], and the sum of the remaining elements is 6, which is divisible by 6.

Example 2:

Input: nums = [6,3,5,2], p = 9
Output: 2
Explanation: We cannot remove a single element to get a sum divisible by 9. The best way is to remove the subarray [5,2], leaving us with [6,3] with sum 9.

Example 3:

Input: nums = [1,2,3], p = 3
Output: 0
Explanation: Here the sum is 6. which is already divisible by 3. Thus we do not need to remove anything.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109
  • 1 <= p <= 109

Solutions

Solution 1

class Solution:
  def minSubarray(self, nums: List[int], p: int) -> int:
    k = sum(nums) % p
    if k == 0:
      return 0
    last = {0: -1}
    cur = 0
    ans = len(nums)
    for i, x in enumerate(nums):
      cur = (cur + x) % p
      target = (cur - k + p) % p
      if target in last:
        ans = min(ans, i - last[target])
      last[cur] = i
    return -1 if ans == len(nums) else ans
class Solution {
  public int minSubarray(int[] nums, int p) {
    int k = 0;
    for (int x : nums) {
      k = (k + x) % p;
    }
    if (k == 0) {
      return 0;
    }
    Map<Integer, Integer> last = new HashMap<>();
    last.put(0, -1);
    int n = nums.length;
    int ans = n;
    int cur = 0;
    for (int i = 0; i < n; ++i) {
      cur = (cur + nums[i]) % p;
      int target = (cur - k + p) % p;
      if (last.containsKey(target)) {
        ans = Math.min(ans, i - last.get(target));
      }
      last.put(cur, i);
    }
    return ans == n ? -1 : ans;
  }
}
class Solution {
public:
  int minSubarray(vector<int>& nums, int p) {
    int k = 0;
    for (int& x : nums) {
      k = (k + x) % p;
    }
    if (k == 0) {
      return 0;
    }
    unordered_map<int, int> last;
    last[0] = -1;
    int n = nums.size();
    int ans = n;
    int cur = 0;
    for (int i = 0; i < n; ++i) {
      cur = (cur + nums[i]) % p;
      int target = (cur - k + p) % p;
      if (last.count(target)) {
        ans = min(ans, i - last[target]);
      }
      last[cur] = i;
    }
    return ans == n ? -1 : ans;
  }
};
func minSubarray(nums []int, p int) int {
  k := 0
  for _, x := range nums {
    k = (k + x) % p
  }
  if k == 0 {
    return 0
  }
  last := map[int]int{0: -1}
  n := len(nums)
  ans := n
  cur := 0
  for i, x := range nums {
    cur = (cur + x) % p
    target := (cur - k + p) % p
    if j, ok := last[target]; ok {
      ans = min(ans, i-j)
    }
    last[cur] = i
  }
  if ans == n {
    return -1
  }
  return ans
}
function minSubarray(nums: number[], p: number): number {
  let k = 0;
  for (const x of nums) {
    k = (k + x) % p;
  }
  if (k === 0) {
    return 0;
  }
  const last = new Map<number, number>();
  last.set(0, -1);
  const n = nums.length;
  let ans = n;
  let cur = 0;
  for (let i = 0; i < n; ++i) {
    cur = (cur + nums[i]) % p;
    const target = (cur - k + p) % p;
    if (last.has(target)) {
      const j = last.get(target)!;
      ans = Math.min(ans, i - j);
    }
    last.set(cur, i);
  }
  return ans === n ? -1 : ans;
}

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

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

发布评论

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