【找出缺失的观测数据】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
相关推荐
To_OC6 小时前
LC 207 课程表:刚学图论那会儿,我连这是拓扑排序都没看出来
javascript·算法·leetcode
To_OC6 小时前
LC 208 实现 Trie 前缀树:曾被名字劝退,写完发现是送分题
javascript·算法·leetcode
BadBadBad__AK8 小时前
线段树维护区间 k 次方和
c++·数学·算法·stl
Warson_L13 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅13 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅13 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L13 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅14 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L14 小时前
python的类&继承
python
Warson_L14 小时前
类型标注/type annotation
python