|
| 1 | +Original link:[coding5.com - Best practices of LeetCode solutions](https://coding5.com/en/leetcode/3478-choose-k-elements-with-maximum-sum) |
| 2 | + |
| 3 | +#3478. Choose K Elements With Maximum Sum - Best practices of LeetCode solutions |
| 4 | + |
| 5 | +LeetCode link:[3478. Choose K Elements With Maximum Sum](https://leetcode.com/problems/choose-k-elements-with-maximum-sum), Difficulty:**Medium**. |
| 6 | + |
| 7 | +##LeetCode description of "3478. Choose K Elements With Maximum Sum" |
| 8 | + |
| 9 | +You are given two integer arrays,`nums1` and`nums2`, both of length`n`, along with a positive integer`k`. |
| 10 | + |
| 11 | +For each index`i` from`0` to`n - 1`, perform the following: |
| 12 | + |
| 13 | +- Find all indices`j` where`nums1[j]` is less than`nums1[i]`. |
| 14 | +- Choose at most`k` values of`nums2[j]` at these indices to maximize the total sum. |
| 15 | + |
| 16 | +Return an array`answer` of size`n`, where`answer[i]` represents the result for the corresponding index`i`. |
| 17 | + |
| 18 | +###[Example 1] |
| 19 | + |
| 20 | +**Input**:`nums1 = [4,2,1,5,3], nums2 = [10,20,30,40,50], k = 2` |
| 21 | + |
| 22 | +**Output**:`[80,30,0,80,50]` |
| 23 | + |
| 24 | +**Explanation**: |
| 25 | + |
| 26 | +``` |
| 27 | +- For `i = 0`: Select the 2 largest values from `nums2` at indices `[1, 2, 4]` where `nums1[j] < nums1[0]`, resulting in `50 + 30 = 80`. |
| 28 | +- For `i = 1`: Select the 2 largest values from `nums2` at index `[2]` where `nums1[j] < nums1[1]`, resulting in `30`. |
| 29 | +- For `i = 2`: No indices satisfy `nums1[j] < nums1[2]`, resulting in `0`. |
| 30 | +- For `i = 3`: Select the 2 largest values from `nums2` at indices `[0, 1, 2, 4]` where `nums1[j] < nums1[3]`, resulting in `50 + 30 = 80`. |
| 31 | +- For `i = 4`: Select the 2 largest values from `nums2` at indices `[1, 2]` where `nums1[j] < nums1[4]`, resulting in `30 + 20 = 50`. |
| 32 | +``` |
| 33 | + |
| 34 | +###[Example 2] |
| 35 | + |
| 36 | +**Input**:`nums1 = [2,2,2,2], nums2 = [3,1,2,3], k = 1` |
| 37 | + |
| 38 | +**Output**:`[0,0,0,0]` |
| 39 | + |
| 40 | +**Explanation**: |
| 41 | + |
| 42 | +``` |
| 43 | +Since all elements in `nums1` are equal, no indices satisfy the condition `nums1[j] < nums1[i]` for any `i`, resulting in `0` for all positions. |
| 44 | +``` |
| 45 | + |
| 46 | +###[Constraints] |
| 47 | + |
| 48 | +-`n == nums1.length == nums2.length` |
| 49 | +-`1 <= n <= 10^5` |
| 50 | +-`1 <= nums1[i], nums2[i] <= 10^6` |
| 51 | +-`1 <= k <= n` |
| 52 | + |
| 53 | +###[Hints] |
| 54 | + |
| 55 | +<details> |
| 56 | + <summary>Hint 1</summary> |
| 57 | + Sort`nums1` and its corresponding`nums2` values together based on`nums1`. |
| 58 | + |
| 59 | + |
| 60 | +</details> |
| 61 | + |
| 62 | +<details> |
| 63 | + <summary>Hint 2</summary> |
| 64 | + Use a max heap to track the top`k` values of`nums2` as you process each element in the sorted order. |
| 65 | + |
| 66 | + |
| 67 | +</details> |
| 68 | + |
| 69 | +##Intuition |
| 70 | + |
| 71 | +* Seeing this, everyone will definitely think of sorting`nums1` from small to large, so that the front is less than or equal to the back, but the indexes will be**messy** when sorting. If there is no good way to solve this problem, the whole question cannot be solved. Please think about it first. |
| 72 | + |
| 73 | + ddd Bring the`index` when sorting, that is, the object to be sorted is an array of tuples of`(num, index)`. This technique**must be mastered**, as it will be used in many questions.ddd |
| 74 | + |
| 75 | + After solving the above problems, the indexes are all there, let's continue reading: |
| 76 | + |
| 77 | +* Requirement 2: Choose at most`k` values of`nums2[j]` at these indices to**maximize** the total sum. |
| 78 | + |
| 79 | + After seeing this, have you thought of any good method? |
| 80 | + |
| 81 | + ddd Heap sort, maintain a large root heap of size`k`. This is also a knowledge point that is often tested,**must be mastered**. ddd |
| 82 | + |
| 83 | + Seeing this, please implement the code according to the above prompts. |
| 84 | + |
| 85 | +* Finally, it is found that the repeated`num` that appear continuously should be specially processed, that is, the values in`answer` corresponding to the same`num` should be the same. There are many ways to deal with it. What is the simplest way to deal with it? |
| 86 | + |
| 87 | + ddd Use a`Map`,`key` is`num`, and the same`key` directly uses the`value` corresponding to`key`. ddd |
| 88 | + |
| 89 | +##Complexity |
| 90 | + |
| 91 | +- Time complexity:`O(N * logN)`. |
| 92 | +- Space complexity:`O(N)`. |
| 93 | + |
| 94 | +##Python |
| 95 | + |
| 96 | +```python |
| 97 | +classSolution: |
| 98 | +deffindMaxSum(self,nums1: List[int],nums2: List[int],k:int) -> List[int]: |
| 99 | + num_index_list= [(num, index)for index, numinenumerate(nums1)]# key 1 |
| 100 | + num_index_list.sort() |
| 101 | + |
| 102 | + answer= [None]*len(nums1) |
| 103 | + k_max_nums= [] |
| 104 | + sum_=0 |
| 105 | + num_to_sum= defaultdict(int)# key 2 |
| 106 | + |
| 107 | +for i, num_indexinenumerate(num_index_list): |
| 108 | + num, index= num_index |
| 109 | + |
| 110 | +if numin num_to_sum: |
| 111 | + answer[index]= num_to_sum[num] |
| 112 | +else: |
| 113 | + answer[index]= sum_ |
| 114 | + num_to_sum[num]= sum_ |
| 115 | + |
| 116 | + heapq.heappush(k_max_nums, nums2[index]) |
| 117 | + sum_+= nums2[index] |
| 118 | + |
| 119 | +iflen(k_max_nums)> k: |
| 120 | + num= heapq.heappop(k_max_nums)# key 3 |
| 121 | + sum_-= num |
| 122 | + |
| 123 | +return answer |
| 124 | +``` |
| 125 | + |
| 126 | +##Other languages |
| 127 | + |
| 128 | +```java |
| 129 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 130 | +``` |