【找出缺失的观测数据】python

思路:

主要在于分配剩余的部分分配问题

代码:

python 复制代码
class Solution:
    def missingRolls(self, rolls: List[int], mean: int, n: int) -> List[int]:
        m = len(rolls)
        total_sum = (n + m) * mean
        toset = total_sum - sum(rolls)
        # 检查 toset 是否在可能的范围内
        if toset < n or toset > 6 * n:
            return []
        # 计算平均值
        new_mean = toset // n
        remainder = toset % n
        # 可以被 n 整除
        if remainder == 0:
            return [new_mean] * n
        # 不能被整除,分配剩余的部分
        ans = [new_mean + 1] * remainder + [new_mean] * (n - remainder)
        return ans
相关推荐
CyrusYiVPP1 小时前
手写 MCP Server:给问答机器人接上本地文件工具,Python 实战第 13 期
人工智能·python
Delightrs1 小时前
Rust LeetCode238 算法题解
开发语言·算法·rust
特立独行的猫A1 小时前
cj-tauri:用仓颉写桌面与鸿蒙 PC 应用
java·后端·python
自信150413057591 小时前
重生之从0开始学习c++之——vector-中篇
c++·学习·算法
文丑颜不良啊1 小时前
LeetCode2501~2520题目解析
java·leetcode
qq_1588954111110 小时前
AI 音乐生成完成后,音频 URL 为什么会过期?一套可恢复的交付方案
python·api·ai音乐
Warson_L12 小时前
高性能架构设计
python·go
databook12 小时前
新数据到手,先跑哪几步检查
python·数据分析
GoAI13 小时前
DeepSeek Harness Memory (一)主流记忆插件总结
后端·算法·面试