【找出缺失的观测数据】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
相关推荐
用户2519162427113 分钟前
Python之语言特点
python
刘立军25 分钟前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
聚客AI2 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
数据智能老司机4 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
大怪v4 小时前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
数据智能老司机5 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i6 小时前
django中的FBV 和 CBV
python·django
c8i6 小时前
python中的闭包和装饰器
python
惯导马工6 小时前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农8 小时前
【React用到的一些算法】游标和栈
算法·react.js