3040. Maximum Number of Operations With the Same Score II Medium
1/**
2 * [3040] Maximum Number of Operations With the Same Score II
3 *
4 * Given an array of integers called nums, you can perform any of the following operation while nums contains at least 2 elements:
5 *
6 * Choose the first two elements of nums and delete them.
7 * Choose the last two elements of nums and delete them.
8 * Choose the first and the last elements of nums and delete them.
9 *
10 * The score of the operation is the sum of the deleted elements.
11 * Your task is to find the maximum number of operations that can be performed, such that all operations have the same score.
12 * Return the maximum number of operations possible that satisfy the condition mentioned above.
13 *
14 * <strong class="example">Example 1:
15 *
16 * Input: nums = [3,2,1,2,3,4]
17 * Output: 3
18 * Explanation: We perform the following operations:
19 * - Delete the first two elements, with score 3 + 2 = 5, nums = [1,2,3,4].
20 * - Delete the first and the last elements, with score 1 + 4 = 5, nums = [2,3].
21 * - Delete the first and the last elements, with score 2 + 3 = 5, nums = [].
22 * We are unable to perform any more operations as nums is empty.
23 *
24 * <strong class="example">Example 2:
25 *
26 * Input: nums = [3,2,6,1,4]
27 * Output: 2
28 * Explanation: We perform the following operations:
29 * - Delete the first two elements, with score 3 + 2 = 5, nums = [6,1,4].
30 * - Delete the last two elements, with score 1 + 4 = 5, nums = [6].
31 * It can be proven that we can perform at most 2 operations.
32 *
33 *
34 * Constraints:
35 *
36 * 2 <= nums.length <= 2000
37 * 1 <= nums[i] <= 1000
38 *
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/maximum-number-of-operations-with-the-same-score-ii/
43// discuss: https://leetcode.com/problems/maximum-number-of-operations-with-the-same-score-ii/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48 pub fn max_operations(nums: Vec<i32>) -> i32 {
49 0
50 }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_3040() {
61 }
62}
63
Back
© 2025 bowen.ge All Rights Reserved.