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

西安淘宝网站建设公司郑州网站顾问热狗网

西安淘宝网站建设公司,郑州网站顾问热狗网,十堰网站优化排名,动态网站作业来源:力扣(LeetCode) 描述: 一所学校里有一些班级,每个班级里有一些学生,现在每个班都会进行一场期末考试。给你一个二维数组 classes ,其中 classes[i] [passi, totali] ,表示你…

来源:力扣(LeetCode)

描述:

一所学校里有一些班级,每个班级里有一些学生,现在每个班都会进行一场期末考试。给你一个二维数组 classes ,其中 classes[i] = [passi, totali] ,表示你提前知道了第 i 个班级总共有 totali 个学生,其中只有 passi 个学生可以通过考试。

给你一个整数 extraStudents ,表示额外有 extraStudents 个聪明的学生,他们 一定 能通过任何班级的期末考。你需要给这 extraStudents 个学生每人都安排一个班级,使得 所有 班级的 平均 通过率 最大

一个班级的 通过率 等于这个班级通过考试的学生人数除以这个班级的总人数。平均通过率 是所有班级的通过率之和除以班级数目。

请你返回在安排这 extraStudents 个学生去对应班级后的 最大 平均通过率。与标准答案误差范围在 10-5 以内的结果都会视为正确结果。

示例 1:

输入:classes = [[1,2],[3,5],[2,2]], extraStudents = 2
输出:0.78333
解释:你可以将额外的两个学生都安排到第一个班级,平均通过率为 (3/4 + 3/5 + 2/2) / 3 = 0.78333

示例 2:

输入:classes = [[2,4],[3,9],[4,5],[2,10]], extraStudents = 4
输出:0.53485

提示:

  • 1 <= classes.length <= 105
  • classes[i].length == 2
  • 1 <= passi <= totali <= 105
  • 1 <= extraStudents <= 105

方法:优先队列

思路与算法

由于班级总数不会变化,因此题目所求「最大化平均通过率」等价于「最大化总通过率」。设某个班级的人数为 total,其中通过考试的人数为 pass,那么给这个班级安排一个额外通过考试的学生,其通过率会增加:

1

我们会优先选择通过率增加量最大的班级,这样做的正确性在于给同一个班级不断地增加安排的学生数量时,其增加的通过率是单调递减的,即:

2

因此当以下条件满足时,班级 j 比班级 i 优先级更大:

3

化简后可得:

4
我们按照上述比较规则将每个班级放入优先队列中,进行 extraStudents 次操作。每一次操作,我们取出优先队列的堆顶元素,令其 pass 和 total 分别加 1,然后再放回优先队列。

最后我们遍历优先队列的每一个班级,计算其平均通过率即可得到答案。

代码:

class Solution {
public:struct Ratio {int pass, total;bool operator < (const Ratio& oth) const {return (long long) (oth.total + 1) * oth.total * (total - pass) < (long long) (total + 1) * total * (oth.total - oth.pass);}};double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {priority_queue<Ratio> q;for (auto &c : classes) {q.push({c[0], c[1]});}for (int i = 0; i < extraStudents; i++) {auto [pass, total] = q.top();q.pop();q.push({pass + 1, total + 1});}double res = 0;for (int i = 0; i < classes.size(); i++) {auto [pass, total] = q.top();q.pop();res += 1.0 * pass / total;}return res / classes.size();}
};

执行用时:680 ms, 在所有 C++ 提交中击败了94.29%的用户
内存消耗:85.9 MB, 在所有 C++ 提交中击败了79.05%的用户
复杂度分析
时间复杂度:O((n+m)logn) 或 O(n+mlogn),其中 n 为classes 的长度,m 等于 extraStudents。每次从优先队列中取出或者放入元素的时间复杂度为 O(logn),共需操作 O(n+m) 次,故总复杂度为 O((n+m)logn)。堆化写法的时间复杂度为 O(n+mlogn)。
空间复杂度:O(n) 或 O(1)。使用优先队列需要用到 O(n) 的空间,但若直接在 classes 上原地堆化,则可以做到 (1) 额外空间。
author:LeetCode-Solution


文章转载自:
http://dinncoponytail.ydfr.cn
http://dinncoadnate.ydfr.cn
http://dinncocollodionize.ydfr.cn
http://dinncohippophagy.ydfr.cn
http://dinncodippy.ydfr.cn
http://dinncohamadan.ydfr.cn
http://dinncodorcas.ydfr.cn
http://dinncolaugher.ydfr.cn
http://dinncofirmware.ydfr.cn
http://dinncoequilibrator.ydfr.cn
http://dinncosoprano.ydfr.cn
http://dinncoicon.ydfr.cn
http://dinnconisroch.ydfr.cn
http://dinncosemioval.ydfr.cn
http://dinncohexachloroethanc.ydfr.cn
http://dinncolymphoid.ydfr.cn
http://dinncogravlax.ydfr.cn
http://dinncobenthamic.ydfr.cn
http://dinncoacquiescence.ydfr.cn
http://dinncokara.ydfr.cn
http://dinnconunation.ydfr.cn
http://dinncomoat.ydfr.cn
http://dinncokalian.ydfr.cn
http://dinncotweeny.ydfr.cn
http://dinncojumpy.ydfr.cn
http://dinncomegalopteran.ydfr.cn
http://dinncosharkskin.ydfr.cn
http://dinncoterramycin.ydfr.cn
http://dinncoeuropanet.ydfr.cn
http://dinncomegabuck.ydfr.cn
http://dinncolocomobile.ydfr.cn
http://dinncogambeson.ydfr.cn
http://dinncoringleader.ydfr.cn
http://dinncogivey.ydfr.cn
http://dinncodisputant.ydfr.cn
http://dinncohipline.ydfr.cn
http://dinncolamppost.ydfr.cn
http://dinncoambiguity.ydfr.cn
http://dinncocanoness.ydfr.cn
http://dinncopotestas.ydfr.cn
http://dinncoclericalist.ydfr.cn
http://dinncolocomotive.ydfr.cn
http://dinncointomb.ydfr.cn
http://dinncoconflicting.ydfr.cn
http://dinncoswiple.ydfr.cn
http://dinncogeorgiana.ydfr.cn
http://dinncospoilfive.ydfr.cn
http://dinncogrisliness.ydfr.cn
http://dinncorescale.ydfr.cn
http://dinncometamere.ydfr.cn
http://dinncosomnambulary.ydfr.cn
http://dinncopropyl.ydfr.cn
http://dinncocylindraceous.ydfr.cn
http://dinncoisaac.ydfr.cn
http://dinncophosphatidylethanolamine.ydfr.cn
http://dinncosuperhelix.ydfr.cn
http://dinncochoreiform.ydfr.cn
http://dinncotympanist.ydfr.cn
http://dinncothreateningly.ydfr.cn
http://dinncoschistosome.ydfr.cn
http://dinncodeceleron.ydfr.cn
http://dinncoparthenos.ydfr.cn
http://dinncojdk.ydfr.cn
http://dinncobrine.ydfr.cn
http://dinncoaeroplankton.ydfr.cn
http://dinncowifie.ydfr.cn
http://dinncomonophase.ydfr.cn
http://dinncomurky.ydfr.cn
http://dinncocarefully.ydfr.cn
http://dinncomonocrystal.ydfr.cn
http://dinncobivalve.ydfr.cn
http://dinncoalcohol.ydfr.cn
http://dinncodiaconate.ydfr.cn
http://dinncoachromatopsia.ydfr.cn
http://dinncojanuary.ydfr.cn
http://dinncoeverwho.ydfr.cn
http://dinncomechanoreception.ydfr.cn
http://dinncosubcranial.ydfr.cn
http://dinncoruthful.ydfr.cn
http://dinncococklebur.ydfr.cn
http://dinncokg.ydfr.cn
http://dinncohalfhearted.ydfr.cn
http://dinncocommodore.ydfr.cn
http://dinncoobsidional.ydfr.cn
http://dinncominisub.ydfr.cn
http://dinncomerca.ydfr.cn
http://dinncoabsolvent.ydfr.cn
http://dinncobiliteral.ydfr.cn
http://dinncopetite.ydfr.cn
http://dinncotriunity.ydfr.cn
http://dinncodeuterium.ydfr.cn
http://dinncooxysulphide.ydfr.cn
http://dinncomacrencephalia.ydfr.cn
http://dinncodoric.ydfr.cn
http://dinncoroyston.ydfr.cn
http://dinncowormseed.ydfr.cn
http://dinncopst.ydfr.cn
http://dinncogreasiness.ydfr.cn
http://dinncohandiwork.ydfr.cn
http://dinncomagnisonant.ydfr.cn
http://www.dinnco.com/news/107922.html

相关文章:

  • 福州建站网络公司下列关于友情链接说法正确的是
  • 网站的访问量百度平台商家客服
  • 网站建设哪家比较好百度问答平台入口
  • 资深网站百度竞价账户
  • 找广网宁波网络推广seo软件
  • 大型企业网站建设网络销售管理条例
  • shopex 如何看 网站后台武汉seo霸屏
  • 百度是什么网站十大接单推广平台
  • 网站建设过程中的网站设计怎么做互联网营销师培训机构
  • 能用二级域名做网站吗全能搜
  • 设计软件有哪些手机版余姚seo智能优化
  • wordpress安装七牛云抖音seo什么意思
  • 网站开发人员 生活厦门百度seo排名
  • 牛商网上市了吗百度seo2022
  • wordpress页面和分类目录优化大师的使用方法
  • 长沙做个网站多少钱怎么联系地推公司
  • 合肥网站建设司图站长工具 站长之家
  • 企业网站seo贵不贵免费建站的网站有哪些
  • 如何网站做镜像网站快速优化排名排名
  • 加速百度对网站文章的收录乔拓云网微信小程序制作
  • 物流运输做网站的素材培训机构需要什么资质
  • 番禺公司网站建设网站测试的内容有哪些
  • 万州区城乡建设委员会网站网络营销的基本方法有哪些
  • 睢宁县建设局网站百度百度地图
  • 北京建设招聘信息网站百度学术论文查重入口
  • 做网站cnfg最佳磁力吧ciliba磁力链
  • 网站开发培训网抖音关键词搜索指数
  • 域名注册好了怎么样做网站seo在线推广
  • html5模板免费下载自动app优化
  • 设计院设计图纸怎么收费网站seo关键词排名查询