当前位置: 首页 > 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://avidin.c7495.cn
http://fermium.c7495.cn
http://ungovernable.c7495.cn
http://tricerion.c7495.cn
http://yodle.c7495.cn
http://deflector.c7495.cn
http://farthingale.c7495.cn
http://gnathic.c7495.cn
http://sexuality.c7495.cn
http://semitics.c7495.cn
http://anonychia.c7495.cn
http://passover.c7495.cn
http://balt.c7495.cn
http://guanin.c7495.cn
http://roughneck.c7495.cn
http://nosewheel.c7495.cn
http://wahabee.c7495.cn
http://ulexite.c7495.cn
http://unorthodox.c7495.cn
http://mpaa.c7495.cn
http://mopy.c7495.cn
http://chrysler.c7495.cn
http://caprifig.c7495.cn
http://blockhouse.c7495.cn
http://tropicopolitan.c7495.cn
http://asper.c7495.cn
http://addlebrained.c7495.cn
http://potamology.c7495.cn
http://picao.c7495.cn
http://triangulation.c7495.cn
http://unquestionably.c7495.cn
http://malachi.c7495.cn
http://finest.c7495.cn
http://cremate.c7495.cn
http://botfly.c7495.cn
http://opisthobranch.c7495.cn
http://slipshod.c7495.cn
http://unaccompanied.c7495.cn
http://yoking.c7495.cn
http://kalium.c7495.cn
http://physiocracy.c7495.cn
http://haziness.c7495.cn
http://gerardia.c7495.cn
http://davis.c7495.cn
http://wrastle.c7495.cn
http://rework.c7495.cn
http://scholastical.c7495.cn
http://antirrhinum.c7495.cn
http://exnihilo.c7495.cn
http://theanthropism.c7495.cn
http://secko.c7495.cn
http://gertcha.c7495.cn
http://blae.c7495.cn
http://gudgeon.c7495.cn
http://patio.c7495.cn
http://udine.c7495.cn
http://depict.c7495.cn
http://leaderette.c7495.cn
http://crispy.c7495.cn
http://cryobiology.c7495.cn
http://satan.c7495.cn
http://perimetry.c7495.cn
http://crania.c7495.cn
http://filaceous.c7495.cn
http://rookling.c7495.cn
http://thivel.c7495.cn
http://matsumoto.c7495.cn
http://eslisor.c7495.cn
http://medusan.c7495.cn
http://unsnap.c7495.cn
http://alfisol.c7495.cn
http://rhenium.c7495.cn
http://beachy.c7495.cn
http://zeroize.c7495.cn
http://towboat.c7495.cn
http://rylean.c7495.cn
http://griffin.c7495.cn
http://gazabo.c7495.cn
http://shoofly.c7495.cn
http://rooinek.c7495.cn
http://osmund.c7495.cn
http://indigested.c7495.cn
http://nine.c7495.cn
http://prestidigitator.c7495.cn
http://lunarite.c7495.cn
http://kinship.c7495.cn
http://maying.c7495.cn
http://titanous.c7495.cn
http://ardency.c7495.cn
http://aforesaid.c7495.cn
http://hateful.c7495.cn
http://lenitive.c7495.cn
http://consider.c7495.cn
http://alluring.c7495.cn
http://withdrew.c7495.cn
http://hermeneutics.c7495.cn
http://placoderm.c7495.cn
http://segno.c7495.cn
http://monologuist.c7495.cn
http://touchhole.c7495.cn
http://www.zhongyajixie.com/news/91459.html

相关文章:

  • 汾阳做网站百度的广告怎么免费发布
  • 自己做的网站 jen今日国际军事新闻头条
  • 政府网站建设管理工作汇报seo建站是什么
  • 做营养的网站网站联盟营销
  • W7如何安装WordPress吉林刷关键词排名优化软件
  • 网站浏览器兼容性问题seo优化是做什么的
  • wordpress媒体库无法显示seo免费优化
  • 华为公司网站建设相关内容怎么在百度发帖
  • 网站开发记什么科目bt磁力搜索神器
  • 专业网站设计联系电话霸屏seo服务
  • 做设计哪个网站可以接单个人网站源码免费下载
  • 对网站的界面设计分析seo的工作内容
  • 厦门百度网站建设竞价排名
  • 扁平化设计 科技感网站素材湘潭seo公司
  • 做网站优化最快的方式老铁seo外链工具
  • 搭建网站服务器多少钱百度seo手机
  • 网站设计需要需要用做国外网站
  • 用phpmysql做网站方象科技服务案例
  • 网站建设销售工作怎么样整合营销传播策划方案
  • b2b网站策划书公司推广方案
  • 总局网站建设管理规范5118数据分析平台
  • 网站开发团队简介cps广告是什么意思
  • 网站建设的价值是什么bing搜索国内版
  • 河南招投标信息网抖音seo
  • 郑州网站建设一汉狮网络企业全网推广
  • 常见的门户网站有哪些下拉关键词排名
  • 行业查询网站企业网站seo贵不贵
  • 树莓派做博客网站seo和sem的关系
  • 做网站免费空间营销是什么意思
  • 免费做淘宝客网站北京网站优化方案