Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit500d872

Browse files
authored
Added task 2542
1 parent11c3bf6 commit500d872

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+
packageg2501_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+
importjava.util.Arrays;
7+
importjava.util.PriorityQueue;
8+
9+
publicclassSolution {
10+
privatestaticclassPairInfo {
11+
intval1;
12+
intval2;
13+
14+
publicPairInfo(intval1,intval2) {
15+
this.val1 =val1;
16+
this.val2 =val2;
17+
}
18+
}
19+
20+
publiclongmaxScore(int[]nums1,int[]nums2,intk) {
21+
intn =nums1.length;
22+
PairInfo[]nums =newPairInfo[n];
23+
for (inti =0;i <n; ++i) {
24+
nums[i] =newPairInfo(nums1[i],nums2[i]);
25+
}
26+
Arrays.sort(
27+
nums,
28+
(a,b) -> {
29+
if (a.val2 ==b.val2) {
30+
returna.val1 -b.val1;
31+
}
32+
returna.val2 -b.val2;
33+
});
34+
longsum =0;
35+
longans =0;
36+
PriorityQueue<Integer>pq =newPriorityQueue<>();
37+
for (inti =n -1;i >=0; --i) {
38+
intminVal =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+
returnans;
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+
packageg2501_2600.s2542_maximum_subsequence_score;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importorg.junit.jupiter.api.Test;
7+
8+
classSolutionTest {
9+
@Test
10+
voidmaxScore() {
11+
assertThat(
12+
newSolution().maxScore(newint[] {1,3,3,2},newint[] {2,1,3,4},3),
13+
equalTo(12L));
14+
}
15+
16+
@Test
17+
voidmaxScore2() {
18+
assertThat(
19+
newSolution().maxScore(newint[] {4,2,3,1,1},newint[] {7,5,10,9,6},1),
20+
equalTo(30L));
21+
}
22+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp