{"id":22513759,"url":"https://github.com/emahtab/kth-largest-element-in-a-stream","last_synced_at":"2026-02-07T08:32:08.408Z","repository":{"id":79525593,"uuid":"242303455","full_name":"eMahtab/kth-largest-element-in-a-stream","owner":"eMahtab","description":"kth Largest element in a stream","archived":false,"fork":false,"pushed_at":"2020-02-22T08:10:25.000Z","size":4,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-08T19:23:34.646Z","etag":null,"topics":["heap","leetcode","problem-solving"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-02-22T08:02:31.000Z","updated_at":"2020-02-22T08:11:12.000Z","dependencies_parsed_at":"2023-05-10T17:15:29.461Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/kth-largest-element-in-a-stream","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/kth-largest-element-in-a-stream","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fkth-largest-element-in-a-stream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fkth-largest-element-in-a-stream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fkth-largest-element-in-a-stream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fkth-largest-element-in-a-stream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/kth-largest-element-in-a-stream/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fkth-largest-element-in-a-stream/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29190195,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-07T07:37:03.739Z","status":"ssl_error","status_checked_at":"2026-02-07T07:37:03.029Z","response_time":63,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["heap","leetcode","problem-solving"],"created_at":"2024-12-07T03:14:21.883Z","updated_at":"2026-02-07T08:32:08.403Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# kth largest element in a stream\n## https://leetcode.com/problems/kth-largest-element-in-a-stream\n\nDesign a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.\n\nYour KthLargest class will have a constructor which accepts an integer k and an integer array nums, which contains initial elements from the stream. For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.\n```\nExample:\n\nint k = 3;\nint[] arr = [4,5,8,2];\nKthLargest kthLargest = new KthLargest(3, arr);\nkthLargest.add(3);   // returns 4\nkthLargest.add(5);   // returns 5\nkthLargest.add(10);  // returns 5\nkthLargest.add(9);   // returns 8\nkthLargest.add(4);   // returns 8\n```\n\n**Note: You may assume that nums' length ≥ k-1 and k ≥ 1.**\n\n## Implementation : Min Heap\n\n```java\nclass KthLargest {\n        private PriorityQueue\u003cInteger\u003e minHeap;\n        private int k;\n\n        public KthLargest(int k, int[] nums) {\n            this.k = k;\n            minHeap = new PriorityQueue\u003c\u003e(k);\n            for (int num : nums)\n                add(num);\n        }\n\n        public int add(int num) {\n            if (minHeap.size() \u003c k)\n                minHeap.add(num);\n            else if (minHeap.peek() \u003c num) {\n                minHeap.poll();\n                minHeap.add(num);\n            }\n            return minHeap.peek();\n        }\n}\n```\n\n### Key points :\nNote that in the `add(num)` function, if the size of minHeap is less than k, we simply add the num to minHeap, otherwise we check whether new element is greater than the peek element (smallest element) in the minHeap, if thats the case only then we add the new element to minHeap by first removing the smallest element from minHeap of size k.\n\nThis is how we maintain the minHeap of size k, so the kth largest element will always be peek of minHeap.\n\n# References :\nhttps://leetcode.com/problems/kth-largest-element-in-a-stream/discuss/149050/Java-Priority-Queue\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fkth-largest-element-in-a-stream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fkth-largest-element-in-a-stream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fkth-largest-element-in-a-stream/lists"}