2099.Find Subsequence of Length K With the Largest Sum | Sorting | Priority Queue | Array | Leetcode

preview_player
Показать описание
2099.Find Subsequence of Length K With the Largest Sum | Sorting | Priority Queue | Array | Leetcode

In this video, I'll talk about how to solve Leetcode 2099.Find Subsequence of Length K With the Largest Sum

Let's Connect:

Resources you can try:

🎥Channel Playlists

Channel Description:
Hey, I'm Priyanshi Agarwal, Software Engineer at Amazon who loves turning free time into productive moments. On my channel, we're all about cool coding and getting better in life. Let's hang out, learn some code, and grow together! 🚀

#programming #Interviews #leetcode #faang #maang #datastructures #algorithms #leetcodedailychallenge #Rapidsyntax #RapidSyntax
Рекомендации по теме
Комментарии
Автор

Java Code:
class Pair
{
int value, idx;
Pair(int value, int idx)
{
this.value = value;
this.idx = idx;
}
}

class Solution {

public int[] maxSubsequence(int[] arr, int k) {

int ans[] = new int[k];
ArrayList<Pair> list = new ArrayList<>();

for(int i = 0 ; i < arr.length ; i++) list.add(new Pair(arr[i], i));

Collections.sort(list, (a, b) -> b.value - a.value);

int idx[] = new int[k];
for(int i = 0 ; i < k ; i++) idx[i] = list.get(i).idx;

Arrays.sort(idx);

for(int i = 0 ; i < k ; i++) ans[i] = arr[idx[i]];

return ans;
}
}

notorious_not
join shbcf.ru