最大化子序列的分數 Maximum Subsequence Score_Leetcode #2542 精選75題

2024/02/26閱讀時間約 6 分鐘

題目敘述

題目會給兩個陣列nums1和nums2。

題目要求我們從中同步選擇長度為k的子序列,並且最大化子序列的分數,
回傳最高的分數值。

分數的定義:

分數 =
(nums1[i0] + nums1[i1] +...+ nums1[ik - 1]) *
min(nums2[i0] , nums2[i1], ... ,nums2[ik - 1])

= nums1子序列的總和 * nums2子序列的最小值


題目的原文敘述


測試範例

Example 1:

Input: nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3
Output: 12
Explanation:
The four possible subsequence scores are:
- We choose the indices 0, 1, and 2 with score = (1+3+3) * min(2,1,3) = 7.
- We choose the indices 0, 1, and 3 with score = (1+3+2) * min(2,1,4) = 6.
- We choose the indices 0, 2, and 3 with score = (1+3+2) * min(2,3,4) = 12.
- We choose the indices 1, 2, and 3 with score = (3+3+2) * min(1,3,4) = 8.
Therefore, we return the max score, which is 12.

Example 2:

Input: nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1
Output: 30
Explanation:
Choosing index 2 is optimal: nums1[2] * nums2[2] = 3 * 10 = 30 is the maximum possible score.

約束條件

Constraints:

  • n == nums1.length == nums2.length

num1的陣列長度 = num2的陣列長度

  • 1 <= n <= 10^5

陣列長度介於1~十萬。

  • 0 <= nums1[i], nums2[j] <= 10^5

每個陣列元素值都介於0~十萬。

  • 1 <= k <= n

子序列長度介於1~n之間。


演算法 排序 + 最小堆minHeap

分數= nums1子序列的總和 * nums2子序列的最小值

所以,瓶頸點首先是nums2的最小值接著是盡量讓nums1選中的元素值越大越好

以行動支持創作者!付費即可解鎖
本篇內容共 2413 字、0 則留言,僅發佈於Leetcode 精選75題 上機考面試題 詳解你目前無法檢視以下內容,可能因為尚未登入,或沒有該房間的查看權限。
43會員
285內容數
由有業界實戰經驗的演算法工程師, 手把手教你建立解題的框架, 一步步寫出高效、清晰易懂的解題答案。 著重在讓讀者啟發思考、理解演算法,熟悉常見的演算法模板。 深入淺出地介紹題目背後所使用的演算法意義,融會貫通演算法與資料結構的應用。 在幾個經典的題目融入一道題目的多種解法,或者同一招解不同的題目,擴展廣度,並加深印象。
留言0
查看全部
發表第一個留言支持創作者!