【找出缺失的观测数据】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
相关推荐
信奥卷王1 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵2 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
全栈练习生3 小时前
AI Agent 沙箱
python·ai
徐小黑ACG3 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
码完就睡3 小时前
数据结构——红黑树
数据结构
码爸3 小时前
数据结构详细介绍
数据结构
Navigator_Z4 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
SEO_juper4 小时前
用 Python 写一个 GEO 可见性检查脚本:你的网站现在能被 AI 引用吗
开发语言·人工智能·爬虫·python·seo·外贸独立站
北极有牛4 小时前
cpp学习笔记--常量指针
java·开发语言·算法