首页 > 其他分享 >[LeetCode] 2542. Maximum Subsequence Score

[LeetCode] 2542. Maximum Subsequence Score

时间:2023-07-13 09:57:53浏览次数:46  
标签:2542 int Score indices Subsequence score pair nums1 nums2

You are given two 0-indexed integer arrays nums1 and nums2 of equal length n and a positive integer k. You must choose a subsequence of indices from nums1 of length k.

For chosen indices i0i1, ..., ik - 1, your score is defined as:

  • The sum of the selected elements from nums1 multiplied with the minimum of the selected elements from nums2.
  • It can defined simply as: (nums1[i0] + nums1[i1] +...+ nums1[ik - 1]) * min(nums2[i0] , nums2[i1], ... ,nums2[ik - 1]).

Return the maximum possible score.

A subsequence of indices of an array is a set that can be derived from the set {0, 1, ..., n-1} by deleting some or no elements.

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
  • 1 <= n <= 105
  • 0 <= nums1[i], nums2[j] <= 105
  • 1 <= k <= n

最大子序列的分数。

给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,两者长度都是 n ,再给你一个正整数 k 。你必须从 nums1 中选一个长度为 k 的 子序列 对应的下标。

对于选择的下标 i0 ,i1 ,..., ik - 1 ,你的 分数 定义如下:

nums1 中下标对应元素求和,乘以 nums2 中下标对应元素的 最小值 。
用公示表示: (nums1[i0] + nums1[i1] +...+ nums1[ik - 1]) * min(nums2[i0] , nums2[i1], ... ,nums2[ik - 1]) 。
请你返回 最大 可能的分数。

一个数组的 子序列 下标是集合 {0, 1, ..., n-1} 中删除若干元素得到的剩余集合,也可以不删除任何元素。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/maximum-subsequence-score
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是贪心,具体做法需要用到排序和最小堆。

求分数的公式不难理解,参考例子一,每个子序列的分数 = nums1 中子序列的和 * nums2 中对应子序列的最小值。注意这里涉及到最小值,所以可能需要考虑对 nums2 降序排列,这样,排序后对于任何一个 nums2[i] ,他就可以被当做以 nums2[i] 结尾的子序列的最小值,同时找子序列的范围也就只会在 index = i 的左侧。这里我们不能单单只排序 nums2,所以我们把 nums1 和 nums2 中的元素变成一个二维数组然后对这个二维数组排序。

排序之后我们遍历每个 pair,当元素个数 = K 的时候,我们计算以 pair[1](这是 nums2[i])为结尾的子序列的分数,最后用一个变量 res 记录全局最大的分数。

时间O(nlogn)

空间O(n)

Java实现

 1 class Solution {
 2     public long maxScore(int[] nums1, int[] nums2, int k) {
 3         int n = nums1.length;
 4         int[][] pairs = new int[n][2];
 5         for (int i = 0; i < n; i++) {
 6             int[] pair = pairs[i];
 7             pair[0] = nums1[i];
 8             pair[1] = nums2[i];
 9         }
10         
11         // pairs按nums2从大到小排序
12         Arrays.sort(pairs, (a, b) -> b[1] - a[1]);
13         // 最小堆,存来自nums1的元素
14         PriorityQueue<Integer> minHeap = new PriorityQueue<>();
15         long res = 0L;
16         long sum = 0L;
17         for (int[] pair : pairs) {
18             sum += pair[0];
19             minHeap.offer(pair[0]);
20             if (minHeap.size() > k) {
21                 sum -= minHeap.poll();
22             }
23             if (minHeap.size() == k) {
24                 res = Math.max(res, sum * pair[1]);
25             }
26         }
27         return res;
28     }
29 }

 

LeetCode 题目总结

标签:2542,int,Score,indices,Subsequence,score,pair,nums1,nums2
From: https://www.cnblogs.com/cnoodle/p/17549555.html

相关文章

  • 【CF1621G】Weighted Increasing Subsequences 题解(优化树状数组)
    CF传送门|LG传送门。优化树状数组+反向处理。Solution发现直接做不好下手。难点主要在求出所有的上升子序列并计算它们分别的贡献。所以需要反向考虑每个单点在什么情况下产生贡献。一个单点会产生多少贡献。一个单点产生贡献的条件很容易得到。一个是在一个上升子序......
  • Codeforces 1787H - Codeforces Scoreboard(平衡树优化 dp)
    令\(c_i=b_i-a_i\),等价于我们钦定一个排列\(p\),最小化\(\sum\min(p_ik_i,c_i)\),拿\(\sumb_i\)减去之就是答案。我们钦定一些\(i\)满足\(p_ik_i<c_i\),根据排序不等式,这些\(p_i\)肯定按\(k\)从大到小的顺序依次填入\(1,2,3,\cdots\)。这样就可以DP了:将\(k\)从大......
  • Scoring Subsequences
    ScoringSubsequencestimelimitpertest2.5secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputThe score ofasequence [s1,s2,…,sd][1,2,…,] isdefinedas s1⋅s2⋅…⋅sdd!1⋅2⋅…⋅!,where d!=1⋅2⋅…⋅d!=1......
  • 「解题报告」CF1621G Weighted Increasing Subsequences
    比较套路的拆贡献题。考虑直接枚举那个\(j\),求有多少包含\(j\)的上升子序列满足这个子序列最后一个数的后面有大于\(a_j\)的数。首先对于\(j\)前面的选择方案是没有影响的,可以直接拿树状数组DP一遍得到。后面的过程我们可以找到从后往前第一个大于\(a_j\)的数的位置......
  • RookScore
    [ABC298F]RookScore关键在于如何排除交叉位置计算两次的干扰。首先进行离散化,行和列是独立的,所以可以分别对行和列离散化。考虑到可以枚举每一行,而每次覆盖到的点可以在线段树中减去这一行的点,然后查询答案;最后再修改回去。也可以直接用一个set,然后搞一个数组存储现在的情......
  • 终端运行roscore时,报错:Unable to contact my own server at...
    问题现象:问题原因:以上问题是由于ROS环境变量ROS_MASTER_URI设置错误导致的,重新设置该变量即可。解决方法:打开~/.bashrc文件,添加或修改环境变量ROS_HOSTNAME和ROS_MASTER,即改为:exportROS_HOSTNAME=localhostexportROS_MASTER_URI=http://localhost:11311修改并保存~/.......
  • 2023-06-18:给定一个长度为N的一维数组scores, 代表0~N-1号员工的初始得分, scores[i] =
    2023-06-18:给定一个长度为N的一维数组scores,代表0~N-1号员工的初始得分,scores[i]=a,表示i号员工一开始得分是a,给定一个长度为M的二维数组operations,operations[i]={a,b,c}。表示第i号操作为:如果a==1,表示将目前分数<b的所有员工,分数改成b,c这个值无用,如果a==2,表示将......
  • SquareSubsequence
    SquareSubsequence一眼DP。首先状态:\(f[i][j]\)表示第一个\(T\)在\(1\simi\)中,第二个\(T\)在\(i+1\simj\),然后必选\(s[i],s[j]\)的方案数。可以想到基本的转移\(f[i][j]+=f[a][b](a<i,i<b<j,s[a]=s[b])\)。当然这样会有重复,样例2就给了我们启示:zzz中不管是......
  • 【原】iOSCoreAnimation动画系列教程(一):CABasicAnimation【包会】
    【原】iOSCoreAnimation动画系列教程(一):CABasicAnimation【包会】 在iOS中,图形可分为以下几个层次: 越上层,封装程度越高,动画实现越简洁越简单,但是自由度越低;反之亦然。本文着重介绍CoreAnimation层的基本动画实现方案。在iOS中,展示动画可以类比于显示生活中的“拍电影”。拍电影有......
  • leetcode 674. Longest Continuous Increasing Subsequence
    Givenanunsortedarrayofintegers,findthelengthoflongestcontinuousincreasingsubsequence(subarray).Example1:Input:[1,3,5,4,7]Output:3Explanation:Thelongestcontinuousincreasingsubsequenceis[1,3,5],itslengthis3.Eventhough[1,3,5......