2099. Find Subsequence of Length K With the Largest Sum | leetcode - 2099 | Java Solution

preview_player
Показать описание
🎬 LeetCode 2099 – Find Subsequence of Length K With the Largest Sum
🧠 Approach: 2 PriorityQueues | Time: O(n log k)

---

🗣️ Narration Script:

"In this video, let’s solve LeetCode 2099 using a clean and efficient heap-based approach in O(n log k) time.

We need to find a subsequence of length k that has the largest sum, but also keeps the original order of elements from the array.

To do this, I’m using two priority queues:

🔹 First, we maintain a min-heap of size k.
This helps us efficiently track the top k largest elements. Each element is stored along with its original index.

🔹 Once we have these k elements, we sort them by their original indices.
This ensures the final subsequence maintains the order from the original array.

This two-step approach guarantees both:

the largest possible sum, and

the correct sequence.

This pattern is very common in problems involving top-k elements with order constraints.

Let me know in the comments if you’ve seen similar patterns elsewhere, and don’t forget to like and subscribe for more Java-based LeetCode videos."

#LeetCode #JavaCoding #PriorityQueue #TechInterviewPrep #CodingInterview #TopKElements #LeetCodeSolutions #HeapProblems
Комментарии
Автор

Bilkul mann nahi tha aaj problem solve krne ka. Youtube khola aapka video dekha and just solved question. Thank you for uploading daily videos❤❤

RahulRastogi-iv
Автор

i watched your vid kth smallest of 2 sorted array i it amazing video all over the tutorial yu breaks problem very well. so pls continue making video but your voice is low i used earphone.

ashkshah
Автор

I come to thee video i watch it understood nothing I leave the video bross english😂😂😂

premmore
join shbcf.ru