返回介绍

solution / 2600-2699 / 2607.Make K-Subarray Sums Equal / README_EN

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

2607. Make K-Subarray Sums Equal

中文文档

Description

You are given a 0-indexed integer array arr and an integer k. The array arr is circular. In other words, the first element of the array is the next element of the last element, and the last element of the array is the previous element of the first element.

You can do the following operation any number of times:

  • Pick any element from arr and increase or decrease it by 1.

Return _the minimum number of operations such that the sum of each subarray of length _k_ is equal_.

A subarray is a contiguous part of the array.

 

Example 1:

Input: arr = [1,4,1,3], k = 2
Output: 1
Explanation: we can do one operation on index 1 to make its value equal to 3.
The array after the operation is [1,3,1,3]
- Subarray starts at index 0 is [1, 3], and its sum is 4 
- Subarray starts at index 1 is [3, 1], and its sum is 4 
- Subarray starts at index 2 is [1, 3], and its sum is 4 
- Subarray starts at index 3 is [3, 1], and its sum is 4 

Example 2:

Input: arr = [2,5,5,7], k = 3
Output: 5
Explanation: we can do three operations on index 0 to make its value equal to 5 and two operations on index 3 to make its value equal to 5.
The array after the operations is [5,5,5,5]
- Subarray starts at index 0 is [5, 5, 5], and its sum is 15
- Subarray starts at index 1 is [5, 5, 5], and its sum is 15
- Subarray starts at index 2 is [5, 5, 5], and its sum is 15
- Subarray starts at index 3 is [5, 5, 5], and its sum is 15 

 

Constraints:

  • 1 <= k <= arr.length <= 105
  • 1 <= arr[i] <= 109

Solutions

Solution 1

class Solution:
  def makeSubKSumEqual(self, arr: List[int], k: int) -> int:
    n = len(arr)
    g = gcd(n, k)
    ans = 0
    for i in range(g):
      t = sorted(arr[i:n:g])
      mid = t[len(t) >> 1]
      ans += sum(abs(x - mid) for x in t)
    return ans
class Solution {
  public long makeSubKSumEqual(int[] arr, int k) {
    int n = arr.length;
    int g = gcd(n, k);
    long ans = 0;
    for (int i = 0; i < g; ++i) {
      List<Integer> t = new ArrayList<>();
      for (int j = i; j < n; j += g) {
        t.add(arr[j]);
      }
      t.sort((a, b) -> a - b);
      int mid = t.get(t.size() >> 1);
      for (int x : t) {
        ans += Math.abs(x - mid);
      }
    }
    return ans;
  }

  private int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
  }
}
class Solution {
public:
  long long makeSubKSumEqual(vector<int>& arr, int k) {
    int n = arr.size();
    int g = gcd(n, k);
    long long ans = 0;
    for (int i = 0; i < g; ++i) {
      vector<int> t;
      for (int j = i; j < n; j += g) {
        t.push_back(arr[j]);
      }
      sort(t.begin(), t.end());
      int mid = t[t.size() / 2];
      for (int x : t) {
        ans += abs(x - mid);
      }
    }
    return ans;
  }
};
func makeSubKSumEqual(arr []int, k int) (ans int64) {
  n := len(arr)
  g := gcd(n, k)
  for i := 0; i < g; i++ {
    t := []int{}
    for j := i; j < n; j += g {
      t = append(t, arr[j])
    }
    sort.Ints(t)
    mid := t[len(t)/2]
    for _, x := range t {
      ans += int64(abs(x - mid))
    }
  }
  return
}

func abs(x int) int {
  if x < 0 {
    return -x
  }
  return x
}

func gcd(a, b int) int {
  if b == 0 {
    return a
  }
  return gcd(b, a%b)
}
function makeSubKSumEqual(arr: number[], k: number): number {
  const n = arr.length;
  const g = gcd(n, k);
  let ans = 0;
  for (let i = 0; i < g; ++i) {
    const t: number[] = [];
    for (let j = i; j < n; j += g) {
      t.push(arr[j]);
    }
    t.sort((a, b) => a - b);
    const mid = t[t.length >> 1];
    for (const x of t) {
      ans += Math.abs(x - mid);
    }
  }
  return ans;
}

function gcd(a: number, b: number): number {
  if (b === 0) {
    return a;
  }
  return gcd(b, a % b);
}

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

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

发布评论

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