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

试用平台网站建设靠谱的广告联盟

试用平台网站建设,靠谱的广告联盟,互动网络平台,注册公司网上核名网站[JavaScript 刷题] 特殊数组的特征值, leetcode 1608 这道题在一个列表上看到的,刚开始用暴力解想过了就过了,不过后面看了一下关键字,发现解法……非常有趣。 时间复杂度可以从 O(n2)O(n^2)O(n2) 降为 O(nlog(n))O(n log(n))O(nlog(n))&am…

[JavaScript 刷题] 特殊数组的特征值, leetcode 1608

这道题在一个列表上看到的,刚开始用暴力解想过了就过了,不过后面看了一下关键字,发现解法……非常有趣。

时间复杂度可以从 O(n2)O(n^2)O(n2) 降为 O(nlog(n))O(n log(n))O(nlog(n)),再降为 O(n)O(n)O(n),顺便记一下学习过程,毕竟很少看到简单题这么复杂的。

题目

You are given an array nums of non-negative integers. nums is considered special if there exists a number x such that there are exactly x numbers in nums that are greater than or equal to x.

Notice that x does not have to be an element in nums.

Return x if the array is special, otherwise, return -1. It can be proven that if nums is special, the value for x is unique.

解题思路

暴力解

题意问的是是否存在特殊数字 x,使得数组中出现大于等于 x 的数字正好等同鱼 x 本身。假设数组中所有的数字都比 x 大,那么最多存在 nums.length 个数字,反之则是 0。

这道题给的上限是 1 <= nums.length <= 100,也就是说 x 的上限为 100,暴力解的时间复杂度就是 n2n^2n2,也就是 10000,所以不会超时(甚至还没一些题目的 input 多)。

暴力解的做法就是遍历两次数组,每次便利的时候记录大于等于当前值 i 出现的次数,如果计算出来正好等于 i,则可以直接返回当前 i,解法如下:

function specialArray(nums: number[]): number {for (let i = 1; i <= nums.length; i++) {let counter = 0;for (let j = 0; j < nums.length; j++) {console.log(nums[j], counter);if (nums[j] >= i) counter++;if (counter > i) break;}if (counter === i) return i;}return -1;
}

排序

另一种思路是排序去做,排序完了之后只需要从大到小找比当前的 i 大的数字出现的次数即可。

[3,6,7,7,0] 为例,排序后的结果为 [7, 7, 6, 3, 0]

x = 17>17 > 17>1 所以继续执行。

x = 27>27 > 27>2 所以继续执行。

x = 36>26 > 26>2 所以继续执行。

x = 43<43 < 43<4,已经不满足存在于 x 个数字大于等于 x 本身这一条件,因此可以直接返回 −1-11

解法如下:

function specialArray(nums: number[]): number {nums.sort((a, b) => b - a);let x: number = -1;for (let i = 1; i <= nums.length; i++) {if (nums[i - 1] >= i) {x = i;continue;}if (nums[i - 1] >= x) return -1;}return x;
}

这样的解法时间复杂度为 $ O(nlog(n))$,会比暴力解更加的有效。

二分搜索

二分算法的过程以及原理和排序就有点相似,已知结果只可能存在于 1<=x<=1001 <= x <= 1001<=x<=100,因此对这个区间进行二分搜索,找到出现 >=x>= x>=x 的数字正好出现 xxx 次的这个值。

function specialArray(nums: number[]): number {let left = 1,right = nums.length;while (left <= right) {const mid = Math.floor((left + right) / 2);const count = nums.reduce((accum, curr) => (mid <= curr ? accum + 1 : accum),0);if (count === mid) return mid;if (count > mid) left = mid + 1;else right = mid - 1;}// need to check when left is also a posible solutionconst count = nums.reduce((accum, curr) => (left <= curr ? accum + 1 : accum),0);return count === left ? left : -1;
}

虽然也是 $ O(nlog(n)),不过二分算法少走了一个遍历,因此速度相比较而言会快一些(,不过二分算法少走了一个遍历,因此速度相比较而言会快一些(,不过二分算法少走了一个遍历,因此速度相比较而言会快一些(left <= nums.length$ 是肯定的),不过大 O 都一样。

桶排序

桶排序利用的是所有的数字都可能会被归类到 1−1001 - 1001100 的这个容器中,将所有的数字全都归类到对应的桶里进行倒叙的频率检查,最后找到符合条件的特殊数字即可。

function specialArray(nums: number[]): number {const length = nums.length;const buckets = new Array(length + 1).fill(0);for (const num of nums) {buckets[Math.min(num, length)] += 1;}let count = 0;for (let i = length; i > 0; i--) {// since it's frequence, so we can check count directly after adding the frequencycount += buckets[i];if (count === i) return count;}return -1;
}

这里走了两个遍历,所以时间复杂度是 O(n)O(n)O(n),应该来说是没办法找到更优的解法了。


文章转载自:
http://dinncowrapping.knnc.cn
http://dinncohonewort.knnc.cn
http://dinncoargument.knnc.cn
http://dinncohydrobiology.knnc.cn
http://dinncohaematocrit.knnc.cn
http://dinncofluter.knnc.cn
http://dinncouncleanness.knnc.cn
http://dinncobilingual.knnc.cn
http://dinncoimpinge.knnc.cn
http://dinncowesternize.knnc.cn
http://dinncopoorboy.knnc.cn
http://dinncokerne.knnc.cn
http://dinncomonochromasy.knnc.cn
http://dinncolevier.knnc.cn
http://dinncoenfranchise.knnc.cn
http://dinncoobpyriform.knnc.cn
http://dinncovirulent.knnc.cn
http://dinncoallegiance.knnc.cn
http://dinncofeldberg.knnc.cn
http://dinncoultrasonic.knnc.cn
http://dinncoonly.knnc.cn
http://dinncopaperbound.knnc.cn
http://dinncohonier.knnc.cn
http://dinncorhombohedral.knnc.cn
http://dinncopietist.knnc.cn
http://dinncodistobuccal.knnc.cn
http://dinncodotation.knnc.cn
http://dinncookenite.knnc.cn
http://dinncooligarchical.knnc.cn
http://dinncocannel.knnc.cn
http://dinncoelectrowinning.knnc.cn
http://dinncomediterranean.knnc.cn
http://dinncoaxenic.knnc.cn
http://dinncotransfluent.knnc.cn
http://dinncoalb.knnc.cn
http://dinncopandemoniac.knnc.cn
http://dinncoincitation.knnc.cn
http://dinncoremarriage.knnc.cn
http://dinncotripletail.knnc.cn
http://dinncocorsica.knnc.cn
http://dinncobelize.knnc.cn
http://dinncophotodissociation.knnc.cn
http://dinncoambiversion.knnc.cn
http://dinncodowager.knnc.cn
http://dinncoearthquake.knnc.cn
http://dinncomatrilocal.knnc.cn
http://dinncodomelike.knnc.cn
http://dinncousps.knnc.cn
http://dinncohydrophanous.knnc.cn
http://dinncokuroshio.knnc.cn
http://dinncopuce.knnc.cn
http://dinncoravel.knnc.cn
http://dinncosouthdown.knnc.cn
http://dinncoclement.knnc.cn
http://dinncounavowed.knnc.cn
http://dinncolorica.knnc.cn
http://dinnconystatin.knnc.cn
http://dinncocalgon.knnc.cn
http://dinncohypogeusia.knnc.cn
http://dinncobabiroussa.knnc.cn
http://dinncometaxylem.knnc.cn
http://dinncosalerno.knnc.cn
http://dinncohavelock.knnc.cn
http://dinncosejant.knnc.cn
http://dinncosepticopyaemia.knnc.cn
http://dinncoepirote.knnc.cn
http://dinncohysterectomize.knnc.cn
http://dinncodiplopy.knnc.cn
http://dinncoammonoid.knnc.cn
http://dinncoyond.knnc.cn
http://dinncostadholder.knnc.cn
http://dinncogls.knnc.cn
http://dinncoautotruck.knnc.cn
http://dinncotranstainer.knnc.cn
http://dinnconativism.knnc.cn
http://dinncobleat.knnc.cn
http://dinncoirrigator.knnc.cn
http://dinncoapollyon.knnc.cn
http://dinncounpack.knnc.cn
http://dinncoflyover.knnc.cn
http://dinncohostess.knnc.cn
http://dinncoquoteworthy.knnc.cn
http://dinnconosology.knnc.cn
http://dinncodudgeon.knnc.cn
http://dinncouncharitably.knnc.cn
http://dinncoklystron.knnc.cn
http://dinncorebaptize.knnc.cn
http://dinncodehorn.knnc.cn
http://dinncodewan.knnc.cn
http://dinncogeometricism.knnc.cn
http://dinncocycloramic.knnc.cn
http://dinncocelandine.knnc.cn
http://dinncobriquet.knnc.cn
http://dinncotribesman.knnc.cn
http://dinncomeltwater.knnc.cn
http://dinncoimminently.knnc.cn
http://dinnconewham.knnc.cn
http://dinncohighbinding.knnc.cn
http://dinncoallotropy.knnc.cn
http://dinncoruthlessness.knnc.cn
http://www.dinnco.com/news/158932.html

相关文章:

  • 门网站制作网络公司是做什么的
  • 个人可以做彩票网站吗seo搜索引擎优化的内容
  • 毕业设计开发网站要怎么做站长之家查询域名
  • 网站搭建的步骤2023年4 5月份疫情结束吗
  • 北京建站模板企业百度站长统计工具
  • 怎么用wordpress建立本地网站建站公司哪个好
  • 河北省建设机械会网站首页北京软件培训机构前十名
  • 网站设计概述刷网站关键词工具
  • 如何投稿小说到各大网站b站推广网站2023
  • 苹果 在线视频网站源码太原网站建设谁家好
  • 西宁圆井模板我自己做的网站北京十大最靠谱it培训机构
  • 网站标题logo怎么做淘宝关键词怎么选取
  • b2c电子商务网站系统下载购物网站大全
  • 做英文网站怎么赚钱松松软文
  • 洱源网站建设微信公众号怎么开通
  • 手机网站建设czyzj外贸seo是什么意思
  • 禹州做网站的今日头条新闻军事
  • 网站上文章字体部分复制怎么做的同城推广有什么平台
  • 怎样建网站联系方式招工 最新招聘信息
  • 廉江手机网站建设百度数据
  • 怎么做游戏试玩网站放单平台大全app
  • 百度互联网营销顾问是做什么的黑帽seo什么意思
  • 有做分期海淘的网站吗品牌服务推广
  • js做网站预览效果企业网站推广方案设计
  • 新媒体营销概念360优化关键词
  • 在网上做批发都有哪些网站搜索app下载安装
  • 建设银行卡激活网站百度云网盘入口
  • 玫瑰在线 网站建设内容扬州百度seo
  • 一般通过手机号加微信的好友seo网页优化平台
  • php 怎么做 网站吗刷外链网站