leetcode 703. 數據流中的第 K 大元素(堆)

設計一個找到數據流中第 k 大元素的類(class)。注意是排序后的第 k 大元素,不是第 k 個不同的元素。

請實現 KthLargest 類:

KthLargest(int k, int[] nums) 使用整數 k 和整數流 nums 初始化對象。
int add(int val) 將 val 插入數據流 nums 后,返回當前數據流中第 k 大的元素。

示例:

輸入:
[“KthLargest”, “add”, “add”, “add”, “add”, “add”]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
輸出:
[null, 4, 5, 5, 8, 8]

解釋:
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // return 4
kthLargest.add(5); // return 5
kthLargest.add(10); // return 5
kthLargest.add(9); // return 8
kthLargest.add(4); // return 8

解題思路

維護一個大小為k的小根堆,堆頂元素就是第k小的元素

代碼

    class KthLargest {int s;PriorityQueue<Integer> priorityQueue=new PriorityQueue<>();public KthLargest(int k, int[] nums) {s=k;for (int num : nums) {priorityQueue.add(num);}while (priorityQueue.size()>k)priorityQueue.poll();}public int add(int val) {priorityQueue.add(val);if(priorityQueue.size()>s)priorityQueue.poll();return priorityQueue.peek();}}/*** Your KthLargest object will be instantiated and called as such:* KthLargest obj = new KthLargest(k, nums);* int param_1 = obj.add(val);*/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391850.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391850.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391850.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

不知道輸入何時停止_知道何時停止

不知道輸入何時停止In predictive analytics, it can be a tricky thing to know when to stop.在預測分析中&#xff0c;知道何時停止可能是一件棘手的事情。 Unlike many of life’s activities, there’s no definitive finishing line, after which you can say “tick, I…

移動認證_如何在移動設備上實施安全的生物特征認證

移動認證by Kathy Dinh凱西丁(Kathy Dinh) 如何在移動設備上實施安全的生物特征認證 (How to implement secure Biometric Authentication on mobile devices) A quick search for React Native biometric authentication would give you several tutorials. That was the fir…

[Luogu1890]gcd區間

原題鏈接https://www.luogu.org/problem/show?pid1890 暴力中的暴力。 對于每一組詢問l..r&#xff0c;我們先循環暴力枚舉l..r中最大值到1&#xff0c;再暴力循環l..r的每一個數&#xff0c;判斷前一重循環能否整除后一重&#xff0c;如果全部都能&#xff0c;則可判定它就是…

Android Studio自定義模板 做開發竟然可以如此輕松 后篇

###1.概述 最近有很多人反饋&#xff0c;有些哥們不喜歡看文字性的東西&#xff0c;還有一些哥們根本就不知道我在搞啥子&#xff0c;那么以后我就采用博客加視頻的方式&#xff0c;我們可以選擇看視頻講解&#xff1a;http://pan.baidu.com/s/1i5uh2uD   內涵段子項目資料及…

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. 解決方案&#xff1a; 異步更新&#xff08;建議使用&#xff09;強制進行變更檢測&#xff0c;但是會觸發子組件的變更檢測&#xff0c;再次導致父組件屬性改變Parent.Component.…

leetcode 119. 楊輝三角 II

給定一個非負索引 k&#xff0c;其中 k ≤ 33&#xff0c;返回楊輝三角的第 k 行。 在楊輝三角中&#xff0c;每個數是它左上方和右上方的數的和。 示例: 輸入: 3 輸出: [1,3,3,1] 解題思路 因為楊輝三角的下層由上一層決定&#xff0c;所以只需要保存上一層的元素&#x…

掌握大數據數據分析師嗎?_要掌握您的數據嗎? 這就是為什么您應該關心元數據的原因...

掌握大數據數據分析師嗎?Either you are a data scientist, a data engineer, or someone enthusiastic about data, understanding your data is one thing you don’t want to overlook. We usually regard data as numbers, texts, or images, but data is more than that.…

react 使用 mobx_如何使用React和MobX狀態樹構建基于狀態的路由器

react 使用 mobxby Miles Till由Miles Till 如何使用React和MobX狀態樹構建基于狀態的路由器 (How to build a state-based router using React and MobX State Tree) Introducing mobx-state-tree-routerMobx狀態樹路由器簡介 If you want to skip ahead to the finished ex…

docker在Centos上的安裝

Centos6安裝docker 系統&#xff1a;centos6.5 內核&#xff1a;3.10.107-1(已升級)&#xff0c;docker對RHEL/Centos的最低內核支持是2.6.32-431&#xff0c;epel源的docker版本推薦內核為3.10版本。 內核升級可參考&#xff1a;https://www.jslink.org/linux/centos-kernel-u…

Lambda表達式的前世今生

Lambda 表達式 早在 C# 1.0 時&#xff0c;C#中就引入了委托&#xff08;delegate&#xff09;類型的概念。通過使用這個類型&#xff0c;我們可以將函數作為參數進行傳遞。在某種意義上&#xff0c;委托可理解為一種托管的強類型的函數指針。 通常情況下&#xff0c;使用委托來…

matplotlib柱狀圖、面積圖、直方圖、散點圖、極坐標圖、箱型圖

一、柱狀圖 1.通過obj.plot() 柱狀圖用bar表示&#xff0c;可通過obj.plot(kindbar)或者obj.plot.bar()生成&#xff1b;在柱狀圖中添加參數stackedTrue&#xff0c;會形成堆疊圖。 fig,axes plt.subplots(2,2,figsize(10,6)) s pd.Series(np.random.randint(0,10,15),index …

微信支付商業版 結算周期_了解商業周期

微信支付商業版 結算周期Economics is an inexact science, finance and investing even more so (some would call them art). But if there’s one thing in economics that you can consistently count on over the long run, it’s the tendency of things to mean revert …

leetcode 448. 找到所有數組中消失的數字

給定一個范圍在 1 ≤ a[i] ≤ n ( n 數組大小 ) 的 整型數組&#xff0c;數組中的元素一些出現了兩次&#xff0c;另一些只出現一次。 找到所有在 [1, n] 范圍之間沒有出現在數組中的數字。 您能在不使用額外空間且時間復雜度為O(n)的情況下完成這個任務嗎? 你可以假定返回…

前端初學者開發學習視頻_初學者學習前端開發的實用指南

前端初學者開發學習視頻by Nikita Rudenko通過尼基塔魯登科(Nikita Rudenko) 初學者學習前端開發的實用指南 (A practical guide to learning front end development for beginners) I started my coding journey in spring 2018, a bit less than one year ago. I earned som…

weblogic啟動失敗案例(root啟動引起的權限問題)

weblogic的一個domain啟動失敗&#xff0c;在日志中有如下信息提示&#xff1a; **************************************************** To start WebLogic Server, use a username and ** password assigned to an admin-level user. For ** server administration, us…

HTTP請求示例

HTTP請求格式當瀏覽器向Web服務器發出請求時&#xff0c;它向服務器傳遞了一個數據塊&#xff0c;也就是請求信息&#xff0c;HTTP請求信息由3部分組成&#xff1a;l 請求方法URI協議/版本l 請求頭(Request Header)l 請求正文下面是一個HTTP請求的例子&#xff1a;GET/sa…

Bootstrap——可拖動模態框(Model)

還是上一個小項目&#xff0c;o(╥﹏╥)o&#xff0c;要實現點擊一個div或者button或者一個東西然后可以彈出一個浮在最上面的彈框。網上找了找&#xff0c;發現Bootstrap的Model彈出框可以實現該功能&#xff0c;因此學習了一下&#xff0c;實現了基本彈框功能&#xff08;可拖…

mfcc中的fft操作_簡化音頻數據:FFT,STFT和MFCC

mfcc中的fft操作What we should know about sound. Sound is produced when there’s an object that vibrates and those vibrations determine the oscillation of air molecules which creates an alternation of air pressure and this high pressure alternated with low …

leetcode 765. 情侶牽手(并查集)

N 對情侶坐在連續排列的 2N 個座位上&#xff0c;想要牽到對方的手。 計算最少交換座位的次數&#xff0c;以便每對情侶可以并肩坐在一起。 一次交換可選擇任意兩人&#xff0c;讓他們站起來交換座位。 人和座位用 0 到 2N-1 的整數表示&#xff0c;情侶們按順序編號&#xff…

ariel字體_播客第58集:軟件開發人員和freeCodeCamp超級巨星Ariel Leslie

ariel字體On this weeks episode of the freeCodeCamp.org podcast, Abbey interviews Ariel Leslie, a software developer and avid contributor to the freeCodeCamp community.在本周的freeCodeCamp.org播客節目中&#xff0c;Abbey采訪了Ariel Leslie&#xff0c;他是free…