3572. Maximize Y‑Sum by Picking a Triplet of Distinct X‑Values Medium

@problem@discussion
#Array#Hash Table#Greedy#Sorting#Heap (Priority Queue)



1/**
2 * [3572] Maximize Y‑Sum by Picking a Triplet of Distinct X‑Values
3 *
4 * You are given two integer arrays x and y, each of length n. You must choose three distinct indices i, j, and k such that:
5 * 
6 * 	x[i] != x[j]
7 * 	x[j] != x[k]
8 * 	x[k] != x[i]
9 * 
10 * Your goal is to maximize the value of y[i] + y[j] + y[k] under these conditions. Return the maximum possible sum that can be obtained by choosing such a triplet of indices.
11 * If no such triplet exists, return -1.
12 *  
13 * <strong class="example">Example 1:
14 * <div class="example-block">
15 * Input: <span class="example-io">x = [1,2,1,3,2], y = [5,3,4,6,2]</span>
16 * Output: <span class="example-io">14</span>
17 * Explanation:
18 * 
19 * 	Choose i = 0 (x[i] = 1, y[i] = 5), j = 1 (x[j] = 2, y[j] = 3), k = 3 (x[k] = 3, y[k] = 6).
20 * 	All three values chosen from x are distinct. 5 + 3 + 6 = 14 is the maximum we can obtain. Hence, the output is 14.
21 * </div>
22 * <strong class="example">Example 2:
23 * <div class="example-block">
24 * Input: <span class="example-io">x = [1,2,1,2], y = [4,5,6,7]</span>
25 * Output: <span class="example-io">-1</span>
26 * Explanation:
27 * 
28 * 	There are only two distinct values in x. Hence, the output is -1.
29 * </div>
30 *  
31 * Constraints:
32 * 
33 * 	n == x.length == y.length
34 * 	3 <= n <= 10^5
35 * 	1 <= x[i], y[i] <= 10^6
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/maximize-ysum-by-picking-a-triplet-of-distinct-xvalues/
41// discuss: https://leetcode.com/problems/maximize-ysum-by-picking-a-triplet-of-distinct-xvalues/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn max_sum_distinct_triplet(x: Vec<i32>, y: Vec<i32>) -> i32 {
47        0
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_3572() {
59    }
60}
61

Back
© 2026 bowen.ge All Rights Reserved.