当前位置: 首页 > news >正文

Leetcode2028. 找出缺失的观测数据

Every day a Leetcode

题目来源:2028. 找出缺失的观测数据

解法1:模拟

统计当前 m 个元素的总和 curSum = sum(rolls),总共 m+n 个元素和为 total = (m + n) * mean。

排除 2 种情况:

  1. total - curSum > 6 * n:n 个骰子全丢 6 都补不了 total 和 curSum 的差距。
  2. total - curSum < 1 * n:n 个骰子的最小点数和为 n,如果 total - curSum 小于 n,说明数据不合法。

我的答案数组的构造方法:

每个骰子先分配 (total - curSum) / n 个点数,再将 (total - curSum) % n 个骰子的点数加 1。

代码:

/** @lc app=leetcode.cn id=2028 lang=cpp** [2028] 找出缺失的观测数据*/// @lc code=start
class Solution
{
public:vector<int> missingRolls(vector<int> &rolls, int mean, int n){int m = rolls.size();int curSum = accumulate(rolls.begin(), rolls.end(), 0);int total = (m + n) * mean;if (total - curSum > 6 * n || total - curSum < 1 * n)return {};vector<int> ans(n, (total - curSum) / n);for (int i = 0; i < (total - curSum) % n; i++)ans[i]++;return ans;}
};
// @lc code=end

结果:

在这里插入图片描述

复杂度分析:

时间复杂度:O(m),其中 m 是数字 rolls 的长度。

空间复杂度:O(n)。

http://www.lryc.cn/news/359275.html

相关文章:

  • 如何在CentOS中合理划分磁盘空间以优化系统性能
  • 算法(十一)贪婪算法
  • Rust之函数式语言特性:迭代器和闭包(一):概述
  • 配置资源管理
  • unity2020打包webGL时卡进程问题
  • 云原生架构相关技术_3.无服务器技术
  • Leetcode:Z 字形变换
  • Python 3 判断文件是否存在
  • (深度学习记录)第TR3周:Transformer 算法详解
  • 谷神前端组件增强:自定义列
  • 31-ESP32-S3-WIFI篇-02 Event Group (事件标记组)
  • 构建企业级AI私有知识库
  • C语言王国——杨氏矩阵
  • 陪玩小程序都需要怎么做?
  • postgressql——子事务可见性判断 性能问题(8)
  • 20240531在飞凌的OK3588-C开发板上跑原厂的Buildroot测试USB摄像头
  • 从0开始学统计-什么是回归?
  • Element-ui使用上传时弹框选择文件类型
  • 原生小程序一键获取手机号
  • ARM虚拟机安装OMV
  • 【协议开发系列】梳理关于TCP和UDP两种协议的区别和使用场景
  • vue blob实现自定义多sheet数据导出到excel文件
  • Python—面向对象小解(3)
  • Nginx超时时间
  • Imgs,GT,Edge,Gradient_all,Gradient_Foreground
  • 自学成才Flutter 弹性布局、线性布局
  • Part 3.1 深度优先搜索
  • 前端Vue小兔鲜儿电商项目实战Day03
  • ORACLE 查询SQL优化
  • Ansible03-Ansible Playbook剧本详解