Dare2Solve
The problem asks us to solve [insert specific problem here], where we are given [explain the inputs, outputs, and any other relevant details]. The goal is to [describe what needs to be achieved or computed].
The core idea behind solving this problem is to [describe the insight or thought process that guides the solution]. By understanding that [explain the critical observation], we can approach the problem in a systematic way that leads to an optimal solution.
The time complexity of this solution is [analyze and provide the time complexity, e.g., O(n), O(log n), O(n^2), etc.]. This is because [explain how the complexity is derived based on the steps in the approach].
The space complexity of this solution is [analyze and provide the space complexity, e.g., O(1), O(n), etc.]. This is due to [explain what causes the memory usage, such as auxiliary data structures, recursion, etc.].
class Solution {
public:
int maxOperations(vector<int>& nums, int k) {
unordered_map<int, vector<int>> map;
for (int i = 0; i < nums.size(); i++) {
map[nums[i]].push_back(i);
}
int counter = 0;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] == INT_MAX) {
continue;
}
if (map.count(k - nums[i])) {
if (k - nums[i] == nums[i] && map[nums[i]].size() == 1) continue;
int index = map[k - nums[i]].back();
map[k - nums[i]].pop_back();
if (map[k - nums[i]].empty()) {
map.erase(k - nums[i]);
}
map[nums[i]].erase(map[nums[i]].begin());
if (map[nums[i]].empty()) {
map.erase(nums[i]);
}
nums[index] = INT_MAX;
nums[i] = INT_MAX;
counter++;
}
}
return counter;
}
};
class Solution:
def maxOperations(self, nums: List[int], k: int) -> int:
map = {}
for i in range(len(nums)):
if nums[i] not in map:
map[nums[i]] = []
map[nums[i]].append(i)
counter = 0
for i in range(len(nums)):
if nums[i] == float('inf'):
continue
if (k - nums[i]) in map:
if k - nums[i] == nums[i] and len(map[nums[i]]) == 1:
continue
index = map[k - nums[i]].pop()
if not map[k - nums[i]]:
del map[k - nums[i]]
map[nums[i]].pop(0)
if not map[nums[i]]:
del map[nums[i]]
nums[index] = float('inf')
nums[i] = float('inf')
counter += 1
return counter
public class Solution {
public int maxOperations(int[] nums, int k) {
HashMap<Integer, List<Integer>> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.putIfAbsent(nums[i], new ArrayList<>());
map.get(nums[i]).add(i);
}
int counter = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == Integer.MAX_VALUE) {
continue;
}
if (map.containsKey(k - nums[i])) {
if (k - nums[i] == nums[i] && map.get(nums[i]).size() == 1) continue;
int index = map.get(k - nums[i]).remove(map.get(k - nums[i]).size() - 1);
if (map.get(k - nums[i]).isEmpty()) {
map.remove(k - nums[i]);
}
map.get(nums[i]).remove(0);
if (map.get(nums[i]).isEmpty()) {
map.remove(nums[i]);
}
nums[index] = Integer.MAX_VALUE;
nums[i] = Integer.MAX_VALUE;
counter++;
}
}
return counter;
}
}
var maxOperations = function(nums, k) {
const map = {};
for(let i=0; i< nums.length; i++) {
if(!map[nums[i]]) {
map[nums[i]] = []
}
map[nums[i]].push(i);
}
let counter = 0;
for(let i=0; i<nums.length; i++) {
if(nums[i] === Infinity) {
continue;
}
if(map[k - nums[i]]) {
if(k - nums[i] === nums[i] && map[nums[i]].length === 1) continue;
const index = map[k - nums[i]].pop();
if(map[k - nums[i]].length === 0) {
delete map[k - nums[i]];
}
map[nums[i]].shift();
if(map[nums[i]].length === 0) {
delete map[nums[i]];
}
nums[index] = Infinity;
nums[i] = Infinity;
counter++;
}
}
return counter;
};