Skip to content

Commit 500d872

Browse files
authored
Added task 2542
1 parent 11c3bf6 commit 500d872

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1848,9 +1848,10 @@ implementation 'com.github.javadev:leetcode-in-java:1.21'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2542 |[Maximum Subsequence Score](src/main/java/g2501_2600/s2542_maximum_subsequence_score/Solution.java)| Medium | Array, Sorting, Greedy, Heap_(Priority_Queue) | 94 | 84.75
18511852
| 2541 |[Minimum Operations to Make Array Equal II](src/main/java/g2501_2600/s2541_minimum_operations_to_make_array_equal_ii/Solution.java)| Medium | Array, Math, Greedy | 3 | 100.00
18521853
| 2540 |[Minimum Common Value](src/main/java/g2501_2600/s2540_minimum_common_value/Solution.java)| Easy | Array, Hash_Table, Binary_Search, Two_Pointers | 0 | 100.00
1853-
| 2538 |[Difference Between Maximum and Minimum Price Sum](src/main/java/g2501_2600/s2538_difference_between_maximum_and_minimum_price_sum/Solution.java)| Hard | Array, Dynamic_Programming, Tree, Depth_First_Search | 43 | 95.19
1854+
| 2538 |[Difference Between Maximum and Minimum Price Sum](src/main/java/g2501_2600/s2538_difference_between_maximum_and_minimum_price_sum/Solution.java)| Hard | Array, Dynamic_Programming, Depth_First_Search, Tree | 43 | 95.19
18541855
| 2537 |[Count the Number of Good Subarrays](src/main/java/g2501_2600/s2537_count_the_number_of_good_subarrays/Solution.java)| Medium | Array, Hash_Table, Sliding_Window | 38 | 99.07
18551856
| 2536 |[Increment Submatrices by One](src/main/java/g2501_2600/s2536_increment_submatrices_by_one/Solution.java)| Medium | Array, Matrix, Prefix_Sum | 12 | 88.15
18561857
| 2535 |[Difference Between Element Sum and Digit Sum of an Array](src/main/java/g2501_2600/s2535_difference_between_element_sum_and_digit_sum_of_an_array/Solution.java)| Easy | Array, Math | 3 | 77.42
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g2501_2600.s2542_maximum_subsequence_score;
2+
3+
// #Medium #Array #Sorting #Greedy #Heap_(Priority_Queue)
4+
// #2023_05_09_Time_94_ms_(84.75%)_Space_56.5_MB_(81.92%)
5+
6+
import java.util.Arrays;
7+
import java.util.PriorityQueue;
8+
9+
public class Solution {
10+
private static class PairInfo {
11+
int val1;
12+
int val2;
13+
14+
public PairInfo(int val1, int val2) {
15+
this.val1 = val1;
16+
this.val2 = val2;
17+
}
18+
}
19+
20+
public long maxScore(int[] nums1, int[] nums2, int k) {
21+
int n = nums1.length;
22+
PairInfo[] nums = new PairInfo[n];
23+
for (int i = 0; i < n; ++i) {
24+
nums[i] = new PairInfo(nums1[i], nums2[i]);
25+
}
26+
Arrays.sort(
27+
nums,
28+
(a, b) -> {
29+
if (a.val2 == b.val2) {
30+
return a.val1 - b.val1;
31+
}
32+
return a.val2 - b.val2;
33+
});
34+
long sum = 0;
35+
long ans = 0;
36+
PriorityQueue<Integer> pq = new PriorityQueue<>();
37+
for (int i = n - 1; i >= 0; --i) {
38+
int minVal = nums[i].val2;
39+
while (pq.size() > k - 1) {
40+
sum -= pq.poll();
41+
}
42+
sum += nums[i].val1;
43+
pq.add(nums[i].val1);
44+
if (pq.size() == k) {
45+
ans = Math.max(ans, sum * minVal);
46+
}
47+
}
48+
return ans;
49+
}
50+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2542\. Maximum Subsequence Score
2+
3+
Medium
4+
5+
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`.
6+
7+
For chosen indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, ..., <code>i<sub>k - 1</sub></code>, your **score** is defined as:
8+
9+
* The sum of the selected elements from `nums1` multiplied with the **minimum** of the selected elements from `nums2`.
10+
* It can defined simply as: <code>(nums1[i<sub>0</sub>] + nums1[i<sub>1</sub>] +...+ nums1[i<sub>k - 1</sub>]) * min(nums2[i<sub>0</sub>] , nums2[i<sub>1</sub>], ... ,nums2[i<sub>k - 1</sub>])</code>.
11+
12+
Return _the **maximum** possible score._
13+
14+
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.
15+
16+
**Example 1:**
17+
18+
**Input:** nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3
19+
20+
**Output:** 12
21+
22+
**Explanation:**
23+
24+
The four possible subsequence scores are:
25+
26+
- We choose the indices 0, 1, and 2 with score = (1+3+3) \* min(2,1,3) = 7.
27+
28+
- We choose the indices 0, 1, and 3 with score = (1+3+2) \* min(2,1,4) = 6.
29+
30+
- We choose the indices 0, 2, and 3 with score = (1+3+2) \* min(2,3,4) = 12.
31+
32+
- We choose the indices 1, 2, and 3 with score = (3+3+2) \* min(1,3,4) = 8.
33+
34+
Therefore, we return the max score, which is 12.
35+
36+
**Example 2:**
37+
38+
**Input:** nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1
39+
40+
**Output:** 30
41+
42+
**Explanation:** Choosing index 2 is optimal: nums1[2] \* nums2[2] = 3 \* 10 = 30 is the maximum possible score.
43+
44+
**Constraints:**
45+
46+
* `n == nums1.length == nums2.length`
47+
* <code>1 <= n <= 10<sup>5</sup></code>
48+
* <code>0 <= nums1[i], nums2[j] <= 10<sup>5</sup></code>
49+
* `1 <= k <= n`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g2501_2600.s2542_maximum_subsequence_score;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void maxScore() {
11+
assertThat(
12+
new Solution().maxScore(new int[] {1, 3, 3, 2}, new int[] {2, 1, 3, 4}, 3),
13+
equalTo(12L));
14+
}
15+
16+
@Test
17+
void maxScore2() {
18+
assertThat(
19+
new Solution().maxScore(new int[] {4, 2, 3, 1, 1}, new int[] {7, 5, 10, 9, 6}, 1),
20+
equalTo(30L));
21+
}
22+
}

0 commit comments

Comments
 (0)