Skip to content

Commit 5d5788c

Browse files
authored
Merge pull request #1492 from VaishnaviMankala19/lc-sol-3038
Added Lc sol 3038
2 parents 8b2f2cd + e1600e7 commit 5d5788c

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
id: maximum-number-of-operations-with-the-same-score-i
3+
title: Maximum Number of Operations With the Same Score I (LeetCode)
4+
sidebar_label: 3038-MaximumNumberOfOperationsWithTheSameScoreI
5+
tags:
6+
- Array
7+
- Greedy
8+
description: Find the maximum number of operations with the same score that can be performed on an array of integers.
9+
sidebar_position: 3038
10+
---
11+
12+
## Problem Description
13+
14+
| Problem Statement | Solution Link | LeetCode Profile |
15+
| :---------------- | :------------ | :--------------- |
16+
| [Maximum Number of Operations With the Same Score I](https://leetcode.com/problems/maximum-number-of-operations-with-the-same-score-i/) | [Maximum Number of Operations With the Same Score I Solution on LeetCode](https://leetcode.com/problems/maximum-number-of-operations-with-the-same-score-i/solutions/) | [vaishu_1904](https://leetcode.com/u/vaishu_1904/) |
17+
18+
## Problem Description
19+
20+
Given an array of integers called nums, you can perform the following operation while nums contains at least 2 elements:
21+
22+
Choose the first two elements of nums and delete them.
23+
The score of the operation is the sum of the deleted elements.
24+
25+
Your task is to find the maximum number of operations that can be performed, such that all operations have the same score.
26+
27+
Return the maximum number of operations possible that satisfy the condition mentioned above.
28+
29+
### Example 1
30+
31+
- **Input:** `nums = [3,2,1,4,5]`
32+
- **Output:** `2`
33+
- **Explanation:** We perform the following operations:
34+
- Delete the first two elements, with score 3 + 2 = 5, nums = [1,4,5].
35+
- Delete the first two elements, with score 1 + 4 = 5, nums = [5].
36+
We are unable to perform any more operations as nums contain only 1 element.
37+
38+
### Example 2
39+
40+
- **Input:** `nums = [3,2,6,1,4]`
41+
- **Output:** `1`
42+
- **Explanation:** We perform the following operations:
43+
- Delete the first two elements, with score 3 + 2 = 5, nums = [6,1,4].
44+
We are unable to perform any more operations as the score of the next operation isn't the same as the previous one.
45+
46+
### Constraints
47+
48+
- `2 <= nums.length <= 100`
49+
- `1 <= nums[i] <= 1000`
50+
51+
## Approach
52+
53+
To solve this problem, we can use a greedy approach to find the maximum number of operations with the same score. Here's the approach:
54+
55+
1. Use a frequency map to count the sum of pairs of the first two elements.
56+
2. Identify the most common pair sum that can be repeated the maximum number of times.
57+
58+
### Solution Code
59+
60+
#### Python
61+
62+
```python
63+
from collections import Counter
64+
65+
class Solution:
66+
def maxOperations(self, nums: List[int]) -> int:
67+
if len(nums) < 2:
68+
return 0
69+
70+
freq = Counter(nums[i] + nums[i+1] for i in range(0, len(nums)-1, 2))
71+
return freq.most_common(1)[0][1] if freq else 0
72+
```
73+
74+
#### Java
75+
```java
76+
import java.util.HashMap;
77+
78+
class Solution {
79+
public int maxOperations(int[] nums) {
80+
if (nums.length < 2) return 0;
81+
82+
HashMap<Integer, Integer> freq = new HashMap<>();
83+
for (int i = 0; i < nums.length - 1; i += 2) {
84+
int sum = nums[i] + nums[i+1];
85+
freq.put(sum, freq.getOrDefault(sum, 0) + 1);
86+
}
87+
88+
int max_operations = 0;
89+
for (int count : freq.values()) {
90+
max_operations = Math.max(max_operations, count);
91+
}
92+
return max_operations;
93+
}
94+
}
95+
```
96+
97+
#### C++
98+
```c++
99+
#include <vector>
100+
#include <unordered_map>
101+
using namespace std;
102+
103+
class Solution {
104+
public:
105+
int maxOperations(vector<int>& nums) {
106+
if (nums.size() < 2) return 0;
107+
108+
unordered_map<int, int> freq;
109+
for (int i = 0; i < nums.size() - 1; i += 2) {
110+
freq[nums[i] + nums[i+1]]++;
111+
}
112+
113+
int max_operations = 0;
114+
for (auto& pair : freq) {
115+
max_operations = max(max_operations, pair.second);
116+
}
117+
return max_operations;
118+
}
119+
};
120+
```
121+
122+
### Conclusion
123+
The solutions use a greedy approach to find the maximum number of operations with the same score by
124+
leveraging a frequency map. This ensures an efficient and straightforward way to solve the problem
125+
across different programming languages.

0 commit comments

Comments
 (0)