这是 2026-10-23 的上一版评测(LiveCodeBench 最新 35 题)。最新的阶梯评测见 首页。

题目 › 3717

Minimum Operations to Make Elements Within K Subarrays Equal

LeetCode 周赛 443 · 2025-03-29 · 困难

每次可把一个元素加 1 或减 1;求最少操作次数,使数组中出现至少 k 个互不重叠、长度恰为 x 且元素全部相等的子数组。

查看原题

结果

Kimi K3.1 ProKimi

✓

通过

  • 函数调用,官方测试全部通过判题
  • 14.1k输出 token(含思考) ↓
  • 214 秒延迟 ↓
  • 1.00 秒(最快)最慢测试的 CPU 时间 ↓
  • 37 MB峰值内存 ↓
  • 8Pylint 问题 ↓
  • 8.84Pylint 得分 ↑
  • 31圈复杂度 ↓
  • 49.4可维护性指数 ↑
  • 69有效行
  • 1SonarQube 问题 ↓

DeepSeek-V4.1-FlashDS-Flash

✓

通过

  • 函数调用,官方测试全部通过判题
  • 7.6k输出 token(含思考) ↓
  • 40 秒延迟 ↓
  • 1.40 秒最慢测试的 CPU 时间 ↓
  • 41 MB峰值内存 ↓
  • 13Pylint 问题 ↓
  • 8.54Pylint 得分 ↑
  • 21圈复杂度 ↓
  • 34.1可维护性指数 ↑
  • 95有效行
  • 2SonarQube 问题 ↓

DeepSeek-V4-ProDS-Pro

✓

通过

  • 函数调用,官方测试全部通过判题
  • 30.1k输出 token(含思考) ↓
  • 215 秒延迟 ↓
  • 1.21 秒最慢测试的 CPU 时间 ↓
  • 37 MB峰值内存 ↓
  • 15Pylint 问题 ↓
  • 8.12Pylint 得分 ↑
  • 25圈复杂度 ↓
  • 46.5可维护性指数 ↑
  • 81有效行
  • 2SonarQube 问题 ↓

↑ 越大越好,↓ 越小越好,有效行只描述代码长短。CPU 时间与内存只统计能区分性能的题(最慢答案扣除进程启动后仍 ≥ 0.1 秒),括号内为相对最快者的倍数(≥ 2 时标出)。静态质量按 arXiv:2610.19167 的口径(见 原理)。判题方法见 原理 › 判题。

代码

Kimi K3.1 Pro · 94 行
from typing import List


class _Fenwick:
    __slots__ = ("n", "count", "sums", "step")

    def __init__(self, n: int):
        self.n = n
        self.count = [0] * (n + 1)
        self.sums = [0] * (n + 1)
        self.step = 1 << (n.bit_length() - 1)

    def update(self, index: int, count_delta: int, sum_delta: int) -> None:
        i = index + 1
        while i <= self.n:
            self.count[i] += count_delta
            self.sums[i] += sum_delta
            i += i & -i

    def prefix(self, index: int):
        """Count and sum over compressed coordinates [0, index]."""
        i = index + 1
        count = 0
        total = 0
        while i > 0:
            count += self.count[i]
            total += self.sums[i]
            i -= i & -i
        return count, total

    def kth(self, k: int) -> int:
        """Return the 0-based compressed coordinate of the kth element."""
        index = 0
        bit = self.step
        while bit:
            nxt = index + bit
            if nxt <= self.n and self.count[nxt] < k:
                index = nxt
                k -= self.count[nxt]
            bit >>= 1
        return index


class Solution:
    def minOperations(self, nums: List[int], x: int, k: int) -> int:
        n = len(nums)

        # Coordinate compression for a sliding-window frequency/sum BIT.
        values = sorted(set(nums))
        compressed = {value: i for i, value in enumerate(values)}
        bit = _Fenwick(len(values))

        window_sum = sum(nums[:x])
        for value in nums[:x]:
            bit.update(compressed[value], 1, value)

        window_count = n - x + 1
        costs = [0] * window_count
        median_rank = (x + 1) // 2

        for start in range(window_count):
            median_index = bit.kth(median_rank)
            median = values[median_index]

            count_le, sum_le = bit.prefix(median_index)
            left_cost = median * count_le - sum_le
            right_cost = (window_sum - sum_le) - median * (x - count_le)
            costs[start] = left_cost + right_cost

            if start + 1 < window_count:
                outgoing = nums[start]
                incoming = nums[start + x]
                if outgoing != incoming:
                    bit.update(compressed[outgoing], -1, -outgoing)
                    bit.update(compressed[incoming], 1, incoming)
                    window_sum += incoming - outgoing

        # prev[i]: minimum cost using the previous number of subarrays
        # within the prefix nums[:i].
        prev = [0] * (n + 1)
        inf = 10**30

        for chosen in range(1, k + 1):
            cur = [inf] * (n + 1)

            # Fewer than chosen*x elements cannot contain enough subarrays.
            for end in range(chosen * x, n + 1):
                skip = cur[end - 1]
                take = prev[end - x] + costs[end - x]
                cur[end] = take if take < skip else skip

            prev = cur

        return prev[n]