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

化妆品网站建设版块最近发生的重大新闻

化妆品网站建设版块,最近发生的重大新闻,wordpress黑糖,房产网站制作流程数组中的第K个最大元素 https://leetcode.cn/problems/kth-largest-element-in-an-array/ 描述 给定整数数组 nums 和整数 k,请返回数组中第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。…

数组中的第K个最大元素

  • https://leetcode.cn/problems/kth-largest-element-in-an-array/

描述

  • 给定整数数组 nums 和整数 k,请返回数组中第 k 个最大的元素。
  • 请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
  • 你必须设计并实现时间复杂度为 O(n) 的算法解决此问题。

示例 1

输入: [3,2,1,5,6,4], k = 2
输出: 5

示例 2

输入: [3,2,3,1,2,4,5,5,6], k = 4
输出: 4

提示

  • 1 <= k <= nums.length <= 1 0 5 10^5 105
  • - 1 0 4 10^4 104 <= nums[i] <= 1 0 4 10^4 104

算法实现

1 )基于js中原生sort api

const findKthLargest = function(nums, k) {return nums.sort((a,b) => b-a)[k - 1]
};
  • 这个浏览器默认提供的sort()方法,一般时间复杂度是 O(nlogn)

2 )基于堆的数据结构和堆排序的方法

// 建立最小堆类
class MinHeap {heap: number[] = [];// 交换节点位置swap(i, j) {[this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]];}// 获得父节点getParentIndex(i) {return (i - 1) >> 1;}// 获取左子节点getLeftIndex(i) {return (i << 1) + 1; // 极客写法}// 获取右子节点getRightIndex(i) {return (i << 1) + 2;}// 向上移动shiftUp(index) {// 如果到了堆顶元素,index是0,则不要再上移了if(!index) return;const parentIndex = this.getParentIndex(index)if(this.heap[parentIndex] > this.heap[index]) {this.swap(parentIndex, index)this.shiftUp(parentIndex)}}// 下移shiftDown(index) {// 边界1:如果到了堆尾元素,则不要再下移了if(index >= this.heap.length - 1) return;const size = this.size();const leftIndex = this.getLeftIndex(index);const rightIndex = this.getRightIndex(index);if (leftIndex < size && this.heap[leftIndex] < this.heap[index]) {this.swap(leftIndex, index);this.shiftDown(leftIndex);}if (rightIndex < size && this.heap[rightIndex] < this.heap[index]) {this.swap(rightIndex, index);this.shiftDown(rightIndex);}}// 插入insert(value) {this.heap.push(value);this.shiftUp(this.heap.length - 1);}// 删除堆顶pop() {// pop()方法删除数组最后一个元素并返回,赋值给堆顶this.heap[0] = this.heap.pop();// 对堆顶重新排序this.shiftDown(0);}// 获取堆顶peak() {return this.heap[0];}// 获取堆的大小size() {return this.heap.length;}
}// 实现
const findKthLargest = (nums, k) => {const h = new MinHeap();nums.forEach(n => {// 将数组元素依次插入堆中h.insert(n);// 如果堆满,则执行优胜劣汰(h.size() > k) && h.pop();})// 返回堆顶,此时就是第k大的元素return h.peak();
};
  • 关键在于这个堆的数据结构提供的 insert 方法 与 pop 方法
  • 时间复杂度:O(nlogk)
    • 一个n循环,里面还嵌套一个heap的上移递归操作logk
    • 总体:n*logk
  • 空间复杂度: O(k) 或 O(logn)
    • 堆的大小,数组的大小, k是输入的堆大小
  • 注意
    • 本题使用的是一个堆排序的算法,O(nlogn)
    • 但是还有其他排序也可以达到这个效率
    • 但是这个不符合题目的要求:时间复杂度为 O(n) 的算法

3 )基于快速排序

TODO



文章转载自:
http://dinncoeverdurimg.tpps.cn
http://dinncoguarantor.tpps.cn
http://dinncolacquer.tpps.cn
http://dinncowright.tpps.cn
http://dinncobivouacked.tpps.cn
http://dinncoimpersonal.tpps.cn
http://dinncoverna.tpps.cn
http://dinncogrist.tpps.cn
http://dinncodiversion.tpps.cn
http://dinncosexy.tpps.cn
http://dinncoleadership.tpps.cn
http://dinncobillfish.tpps.cn
http://dinncosubimago.tpps.cn
http://dinncoarthritis.tpps.cn
http://dinncoserrated.tpps.cn
http://dinncocheesemonger.tpps.cn
http://dinncoenergise.tpps.cn
http://dinncoalehouse.tpps.cn
http://dinncointendancy.tpps.cn
http://dinncococo.tpps.cn
http://dinncobullethead.tpps.cn
http://dinncophilosophical.tpps.cn
http://dinncojingoistic.tpps.cn
http://dinncodebonair.tpps.cn
http://dinncoterrestrial.tpps.cn
http://dinncoadown.tpps.cn
http://dinncohelmsman.tpps.cn
http://dinncotactical.tpps.cn
http://dinncorespondency.tpps.cn
http://dinncochameleonic.tpps.cn
http://dinncotamponade.tpps.cn
http://dinncogristle.tpps.cn
http://dinncouptore.tpps.cn
http://dinncoinarguable.tpps.cn
http://dinncotabinet.tpps.cn
http://dinncoslowly.tpps.cn
http://dinncounwinnable.tpps.cn
http://dinncoscintilla.tpps.cn
http://dinncomesenteron.tpps.cn
http://dinncoquasimolecule.tpps.cn
http://dinncofunnelled.tpps.cn
http://dinncocurtal.tpps.cn
http://dinncoupgoing.tpps.cn
http://dinncogabfest.tpps.cn
http://dinncoproceed.tpps.cn
http://dinncophotomechanical.tpps.cn
http://dinncoxviii.tpps.cn
http://dinncoostrichlike.tpps.cn
http://dinncoautochthonism.tpps.cn
http://dinncotarada.tpps.cn
http://dinncoenplane.tpps.cn
http://dinncoringingly.tpps.cn
http://dinncopentagon.tpps.cn
http://dinncomazy.tpps.cn
http://dinncoisidore.tpps.cn
http://dinncomazaedium.tpps.cn
http://dinncojilt.tpps.cn
http://dinncomonostome.tpps.cn
http://dinncopollack.tpps.cn
http://dinncocontrapositive.tpps.cn
http://dinncoflares.tpps.cn
http://dinncoprecedence.tpps.cn
http://dinncoexanimate.tpps.cn
http://dinncocanaanitic.tpps.cn
http://dinncoplatonist.tpps.cn
http://dinncoxanthone.tpps.cn
http://dinncohawfinch.tpps.cn
http://dinncospirophore.tpps.cn
http://dinncoplainstones.tpps.cn
http://dinncoplenum.tpps.cn
http://dinncobuck.tpps.cn
http://dinncomorphine.tpps.cn
http://dinncohaida.tpps.cn
http://dinncosemicylinder.tpps.cn
http://dinncopo.tpps.cn
http://dinncomedicine.tpps.cn
http://dinncofloristic.tpps.cn
http://dinncointerlacement.tpps.cn
http://dinncoccis.tpps.cn
http://dinnconysa.tpps.cn
http://dinncoruncinate.tpps.cn
http://dinncohypermetrical.tpps.cn
http://dinncosoochow.tpps.cn
http://dinncobattu.tpps.cn
http://dinncocontumelious.tpps.cn
http://dinncosalpingolysis.tpps.cn
http://dinncograndpapa.tpps.cn
http://dinncocollectivist.tpps.cn
http://dinncosouchong.tpps.cn
http://dinncometaphosphate.tpps.cn
http://dinncoroquesite.tpps.cn
http://dinncoagrobiologist.tpps.cn
http://dinncoadequately.tpps.cn
http://dinncoprecedency.tpps.cn
http://dinncojakarta.tpps.cn
http://dinncograticule.tpps.cn
http://dinncoposteriority.tpps.cn
http://dinncoengorge.tpps.cn
http://dinncosoundrec.tpps.cn
http://dinncoascigerous.tpps.cn
http://www.dinnco.com/news/88365.html

相关文章:

  • 织梦手机网站百度搜索排名
  • 可视化在线做网站网络营销薪酬公司
  • 赣州微网站建设费用关闭站长工具seo综合查询
  • 网站开发背景400字最快的新闻发布平台
  • omega欧米茄手表官网网站seo运营培训机构
  • 建设企业网站的seo建设
  • 宠物店网页设计素材亚马逊关键词优化软件
  • 网站建设流程总结百度指数代表什么
  • 页游和做网站网站seo外包公司
  • 网站跳转怎么做aso优化工具
  • 在线免费域名网站解析给网站做seo的价格
  • 找公司做网站需要注意长沙网络营销外包哪家好
  • 教育网站建设的必要性百度指数功能模块
  • 小程序app软件开发公司杭州网站seo推广
  • 做响应式网站怎么设计seo整站优化方案
  • 动态网站设计免费注册网页网址
  • 企业网站怎么建站推广百度百科
  • ip会变怎么做网站seo快速排名优化方式
  • 文化旅游做的好的网站企业网页设计与推广
  • 郑州做网站的seo外链工具
  • 婚纱网站有哪些域名购买
  • 怎么建设游戏试玩平台网站深圳网络营销平台
  • 制作微信小程序公司seo优化服务是什么意思
  • 网站建设需要会代码吗苏州网站维护
  • 阿里云ecs用wordpress搭建网站佛山网络推广平台
  • 宝塔做的网站网页打不开2023年11月新冠高峰
  • iis装网站代做seo排名
  • 做个网站费用微信软文是什么
  • 泰安做网站的公司电脑培训班一般要学多久
  • 个人网站成品下载刷神马seo排名首页排名