|
| 1 | +--- |
| 2 | +id: maximum-number-of-operations-with-the-same-score-ii |
| 3 | +title: Maximum Number of Operations With the Same Score II |
| 4 | +sidebar_label: 3040 - Maximum Number of Operations With the Same Score II |
| 5 | +tags: [Array, Greedy] |
| 6 | +description: Find the maximum number of operations with the same score on an array of integers using specific deletion operations. |
| 7 | +--- |
| 8 | + |
| 9 | +## Problem Statement |
| 10 | + |
| 11 | +### Problem Description |
| 12 | + |
| 13 | +Given an array of integers called `nums`, you can perform any of the following operations while `nums` contains at least 2 elements: |
| 14 | + |
| 15 | +1. Choose the first two elements of `nums` and delete them. |
| 16 | +2. Choose the last two elements of `nums` and delete them. |
| 17 | +3. Choose the first and the last elements of `nums` and delete them. |
| 18 | + |
| 19 | +The score of the operation is the sum of the deleted elements. |
| 20 | + |
| 21 | +Your task is to find the maximum number of operations that can be performed, such that all operations have the same score. |
| 22 | + |
| 23 | +Return the maximum number of operations possible that satisfy the condition mentioned above. |
| 24 | + |
| 25 | +### Example |
| 26 | + |
| 27 | +**Example 1:** |
| 28 | +``` |
| 29 | +Input: nums = [3,2,1,2,3,4] |
| 30 | +Output: 3 |
| 31 | +``` |
| 32 | +**Explanation:** We perform the following operations: |
| 33 | + |
| 34 | +- Delete the first two elements, with score 3 + 2 = 5, nums = [1,2,3,4]. |
| 35 | +- Delete the first and the last elements, with score 1 + 4 = 5, nums = [2,3]. |
| 36 | +- Delete the first and the last elements, with score 2 + 3 = 5, nums = []. |
| 37 | +- We are unable to perform any more operations as nums is empty. |
| 38 | + |
| 39 | + |
| 40 | +### Constraints |
| 41 | + |
| 42 | +- 2 ≤ `nums.length` ≤ 2000 |
| 43 | +- 1 ≤ `nums[i]` ≤ 1000 |
| 44 | + |
| 45 | +## Solution |
| 46 | + |
| 47 | +### Intuition |
| 48 | + |
| 49 | +To solve this problem, we need to identify how many operations can be performed with the same score. We can use the following approach: |
| 50 | + |
| 51 | +1. **Calculate Possible Scores:** Compute possible scores for each operation type (first two elements, last two elements, first and last elements). |
| 52 | +2. **Count Operations for Each Score:** Use a dictionary to count the number of times each score can be achieved by applying each operation type. |
| 53 | +3. **Find the Maximum Valid Score:** Determine the maximum count of operations where all operations yield the same score. |
| 54 | + |
| 55 | +### Time and Space Complexity |
| 56 | + |
| 57 | +- **Time Complexity:** The time complexity is $O(n)$, where $n$ is the length of `nums`. This includes calculating possible scores and counting their frequencies. |
| 58 | + |
| 59 | +- **Space Complexity:** The space complexity is $O(n)$ due to the use of a dictionary to store score counts. |
| 60 | + |
| 61 | +### Code |
| 62 | + |
| 63 | +#### C++ |
| 64 | + |
| 65 | +```cpp |
| 66 | +class Solution { |
| 67 | +public: |
| 68 | + int maxOperations(vector<int>& nums) { |
| 69 | + int n = nums.size(); |
| 70 | + unordered_map<int, int> scoreCount; |
| 71 | + |
| 72 | + int maxOps = 0; |
| 73 | + for (int i = 0; i < n / 2; ++i) { |
| 74 | + int score1 = nums[i] + nums[i + 1]; |
| 75 | + int score2 = nums[n - 1 - i] + nums[n - 2 - i]; |
| 76 | + int score3 = nums[i] + nums[n - 1 - i]; |
| 77 | + |
| 78 | + scoreCount[score1]++; |
| 79 | + scoreCount[score2]++; |
| 80 | + scoreCount[score3]++; |
| 81 | + |
| 82 | + maxOps = max(maxOps, max({scoreCount[score1], scoreCount[score2], scoreCount[score3]})); |
| 83 | + } |
| 84 | + |
| 85 | + return maxOps; |
| 86 | + } |
| 87 | +}; |
| 88 | +``` |
| 89 | +
|
| 90 | +#### Java |
| 91 | +```java |
| 92 | +import java.util.HashMap; |
| 93 | +import java.util.Map; |
| 94 | +
|
| 95 | +class Solution { |
| 96 | + public int maxOperations(int[] nums) { |
| 97 | + int n = nums.length; |
| 98 | + Map<Integer, Integer> scoreCount = new HashMap<>(); |
| 99 | + |
| 100 | + int maxOps = 0; |
| 101 | + for (int i = 0; i < n / 2; ++i) { |
| 102 | + int score1 = nums[i] + nums[i + 1]; |
| 103 | + int score2 = nums[n - 1 - i] + nums[n - 2 - i]; |
| 104 | + int score3 = nums[i] + nums[n - 1 - i]; |
| 105 | + |
| 106 | + scoreCount.put(score1, scoreCount.getOrDefault(score1, 0) + 1); |
| 107 | + scoreCount.put(score2, scoreCount.getOrDefault(score2, 0) + 1); |
| 108 | + scoreCount.put(score3, scoreCount.getOrDefault(score3, 0) + 1); |
| 109 | + |
| 110 | + maxOps = Math.max(maxOps, Math.max(scoreCount.get(score1), Math.max(scoreCount.get(score2), scoreCount.get(score3)))); |
| 111 | + } |
| 112 | + |
| 113 | + return maxOps; |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +#### Python |
| 119 | +```python |
| 120 | +class Solution: |
| 121 | + def maxOperations(self, nums: List[int]) -> int: |
| 122 | + from collections import defaultdict |
| 123 | + |
| 124 | + n = len(nums) |
| 125 | + score_count = defaultdict(int) |
| 126 | + |
| 127 | + max_ops = 0 |
| 128 | + for i in range(n // 2): |
| 129 | + score1 = nums[i] + nums[i + 1] |
| 130 | + score2 = nums[n - 1 - i] + nums[n - 2 - i] |
| 131 | + score3 = nums[i] + nums[n - 1 - i] |
| 132 | + |
| 133 | + score_count[score1] += 1 |
| 134 | + score_count[score2] += 1 |
| 135 | + score_count[score3] += 1 |
| 136 | + |
| 137 | + max_ops = max(max_ops, score_count[score1], score_count[score2], score_count[score3]) |
| 138 | + |
| 139 | + return max_ops |
| 140 | +``` |
| 141 | + |
0 commit comments