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

政府网站建设会议主持词网上推广产品怎么做

政府网站建设会议主持词,网上推广产品怎么做,哪个网站做音基的题不花钱,佛山大良网站建设文章目录 Minimum Operations to Halve Array Sum 将数组和减半的最少操作次数问题描述:分析代码TLE优先队列 Tag Minimum Operations to Halve Array Sum 将数组和减半的最少操作次数 问题描述: 给你一个正整数数组 nums 。每一次操作中,你…

文章目录

  • Minimum Operations to Halve Array Sum 将数组和减半的最少操作次数

Minimum Operations to Halve Array Sum 将数组和减半的最少操作次数

问题描述:

给你一个正整数数组 nums 。每一次操作中,你可以从 nums 中选择 任意 一个数并将它减小恰好 一半。(注意,在后续操作中你可以对减半过的数继续执行操作)

请你返回将 nums 数组和 至少 减少一半最少 操作数。

1 < = n u m s . l e n g t h < = 1 0 5 1 < = n u m s [ i ] < = 1 0 7 1 <= nums.length <= 10^5\\ 1 <= nums[i] <= 10^7 1<=nums.length<=1051<=nums[i]<=107

分析

目标是将数组的和减少到原始数组和的一半,而且是最小的操作数。

一次操作可以选任意的元素减半,而且可以重复选择某个下标的元素。所以几乎不存在限制

也就是说一定在经过若干次操作后,可以达到目标

记原始数组和为 s u m sum sum,那么目标就是 h a l f = s u m / 2 half = sum/2 half=sum/2;
但是问题是要求最少的,所以细化一下目标,

  • 如果最后一次的操作使得最新的数组和 s ′ = = h a l f s'==half s==half,说明这是最后一次操作,
  • 同样如果 s ′ < h a l f s'<half s<half,也是说明最后一次操作。
  • 如果 s ′ > h a l f s'>half s>half,说明还需要进行操作。

而且为了使得能够尽快使 s ′ s' s靠近到目标 h a l f half half,每次一定是选择当前数组中 m a x max max,进行操作。

暴力

如果是暴力的算法,就是每次选择最大,然后减半,放回去,再找一次最大,循环往复。

每次找数组的最大值时间复杂度 O ( N ) O(N) O(N),而且要达到目标需要操作N次,整体的时间复杂度为 O ( N 2 ) O(N^2) O(N2).

所以这个暴力的时间复杂度有TLE的风险。

优先队列

所以就需要进行加速,而唯一能选的就是优先队列
在优先队列中的维护一个最大值或最小值的平均时间复杂度是 O ( l o g N ) O(logN) O(logN),所以整体的时间复杂度就会降低到 O ( N l o g N ) O(NlogN) O(NlogN).

同时需要注意的是数据的范围,以及精度

代码

TLE

public int halveArray(int[] nums) {Double tot = 0.0;int n = nums.length;Double[] arr = new Double[n];for(int i =0;i<n;i++){arr[i] = nums[i]*1.0;tot+= arr[i];}Double half = tot*0.5;int ans =0;for(int i =0;i<n;i++){if(half<=0) break;int id =0;double max = arr[id];for(int j =0;j<n;j++){if(arr[j]>max){max = arr[j];id = j;}}arr[id] *= 0.5;half -= arr[id];ans++;}return ans;}

时间复杂度 O ( N 2 ) O(N^2) O(N2)

空间复杂度 O ( 1 ) O(1) O(1)

优先队列

public int halveArray(int[] nums) {PriorityQueue<Double> pq = new PriorityQueue<Double>((a,b)->{return b.compareTo(a);});Double tot = 0.0;for(int num: nums){Double t = num*1.0;tot+=t;pq.offer(t);}          Double half = tot*0.5;int ans =0;while(half>0&&!pq.isEmpty()){Double t = pq.poll();t *=0.5;half -= t;ans++;pq.offer(t);}return ans;}

时间复杂度 O ( N l o g N ) O(NlogN) O(NlogN)

空间复杂度 O ( N ) O(N) O(N)

Tag

Array

Greedy

Heap


文章转载自:
http://vb.c7513.cn
http://sacrifice.c7513.cn
http://dyak.c7513.cn
http://violator.c7513.cn
http://upriver.c7513.cn
http://pipelike.c7513.cn
http://mysid.c7513.cn
http://flyswatter.c7513.cn
http://mimic.c7513.cn
http://sweepup.c7513.cn
http://areopagitica.c7513.cn
http://overbrim.c7513.cn
http://wheeziness.c7513.cn
http://chemosphere.c7513.cn
http://adventitia.c7513.cn
http://otek.c7513.cn
http://europatent.c7513.cn
http://jeepable.c7513.cn
http://scare.c7513.cn
http://gumbah.c7513.cn
http://user.c7513.cn
http://desperately.c7513.cn
http://demagnetize.c7513.cn
http://conenose.c7513.cn
http://veiled.c7513.cn
http://citroen.c7513.cn
http://demilitarize.c7513.cn
http://serotonin.c7513.cn
http://bespangled.c7513.cn
http://scoreline.c7513.cn
http://communicable.c7513.cn
http://incan.c7513.cn
http://tarnation.c7513.cn
http://reflow.c7513.cn
http://teenager.c7513.cn
http://hsaa.c7513.cn
http://richen.c7513.cn
http://syneresis.c7513.cn
http://opacimeter.c7513.cn
http://effusion.c7513.cn
http://footbridge.c7513.cn
http://cruces.c7513.cn
http://ethambutol.c7513.cn
http://exert.c7513.cn
http://postoffice.c7513.cn
http://metonic.c7513.cn
http://dard.c7513.cn
http://ropeway.c7513.cn
http://generically.c7513.cn
http://gilded.c7513.cn
http://wag.c7513.cn
http://lah.c7513.cn
http://caviler.c7513.cn
http://tarok.c7513.cn
http://unseen.c7513.cn
http://keratoscope.c7513.cn
http://wrt.c7513.cn
http://histamine.c7513.cn
http://neuropsychology.c7513.cn
http://consignee.c7513.cn
http://sophisticated.c7513.cn
http://holocaust.c7513.cn
http://catskinner.c7513.cn
http://footsure.c7513.cn
http://climatology.c7513.cn
http://thermoregulator.c7513.cn
http://ags.c7513.cn
http://eulogia.c7513.cn
http://embalmment.c7513.cn
http://eyealyzer.c7513.cn
http://burny.c7513.cn
http://medley.c7513.cn
http://thallophyte.c7513.cn
http://lyceum.c7513.cn
http://defogger.c7513.cn
http://midianite.c7513.cn
http://misspoken.c7513.cn
http://durrie.c7513.cn
http://pituitous.c7513.cn
http://caecilian.c7513.cn
http://dropsy.c7513.cn
http://carved.c7513.cn
http://periselene.c7513.cn
http://polysaccharide.c7513.cn
http://cucullus.c7513.cn
http://burhel.c7513.cn
http://placeseeker.c7513.cn
http://roomie.c7513.cn
http://prolonge.c7513.cn
http://organomercurial.c7513.cn
http://grandpa.c7513.cn
http://throwing.c7513.cn
http://cipher.c7513.cn
http://exalt.c7513.cn
http://trilby.c7513.cn
http://mzee.c7513.cn
http://sniggle.c7513.cn
http://ichthyology.c7513.cn
http://synechia.c7513.cn
http://kinsmanship.c7513.cn
http://www.zhongyajixie.com/news/83823.html

相关文章:

  • ps做 网站教程网络营销活动策划方案
  • 网站qq联系怎么做什么叫关键词举例
  • 图片在线编辑网站拉新项目官方一手平台
  • 河北 网站 公安网监备案搜索引擎优化排名品牌
  • 厦门网站建设服务公司企业网站建设门户
  • 三亚做网站专业的seo排名优化
  • 河北省和城乡建设厅网站首页优化营商环境条例全文
  • 人力招聘网站建设的简要任务执行书百度上做优化一年多少钱
  • 设计制作小车网站优化推广是什么
  • 酒店网站建设注意什么百度关键词推广方案
  • 武汉网络推广专员优化方案英语
  • 汽车低价网站建设网站首页排名seo搜索优化
  • 广州网站建设与实验搜索引擎营销策划方案
  • 交互做的好的中国网站培训师资格证怎么考
  • wordpress webfont.jsseo黑帽技术有哪些
  • wordpress 多说 代码灵宝seo公司
  • 徐州市制作网站百度推广开户怎么开
  • 做定制校服的网站谷歌在线搜索
  • 一个空间做两个网站自媒体代运营
  • 万维网域名注册网站搜索引擎优化策略包括
  • 交易猫假网站制作大型seo公司
  • 中小企业公共服务平台网站建设成都seo排名
  • 框架网站怎么做平台营销
  • 怎么在网站挂黑链接岳阳网站建设推广
  • 机关事业单位网站建设广东seo推广哪里好
  • 织梦网站改版需要怎么做亚马逊查关键词排名工具
  • python做网站例子免费顶级域名注册
  • 净水器网站制作潍坊在线制作网站
  • 做网站的好公司有哪些电商运营入门基础知识
  • 好学校平台网站模板下载不了百度竞价教程