2295. Replace Elements in an Array LeetCode Solution
In this guide, you will get 2295. Replace Elements in an Array LeetCode Solution with the best time and space complexity. The solution to Replace Elements in an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Replace Elements in an Array solution in C++
- Replace Elements in an Array solution in Java
- Replace Elements in an Array solution in Python
- Additional Resources
Problem Statement of Replace Elements in an Array
You are given a 0-indexed array nums that consists of n distinct positive integers. Apply m operations to this array, where in the ith operation you replace the number operations[i][0] with operations[i][1].
It is guaranteed that in the ith operation:
operations[i][0] exists in nums.
operations[i][1] does not exist in nums.
Return the array obtained after applying all the operations.
Example 1:
Input: nums = [1,2,4,6], operations = [[1,3],[4,7],[6,1]]
Output: [3,2,7,1]
Explanation: We perform the following operations on nums:
– Replace the number 1 with 3. nums becomes [3,2,4,6].
– Replace the number 4 with 7. nums becomes [3,2,7,6].
– Replace the number 6 with 1. nums becomes [3,2,7,1].
We return the final array [3,2,7,1].
Example 2:
Input: nums = [1,2], operations = [[1,3],[2,1],[3,2]]
Output: [2,1]
Explanation: We perform the following operations to nums:
– Replace the number 1 with 3. nums becomes [3,2].
– Replace the number 2 with 1. nums becomes [3,1].
– Replace the number 3 with 2. nums becomes [2,1].
We return the array [2,1].
Constraints:
n == nums.length
m == operations.length
1 <= n, m <= 105
All the values of nums are distinct.
operations[i].length == 2
1 <= nums[i], operations[i][0], operations[i][1] <= 106
operations[i][0] will exist in nums when applying the ith operation.
operations[i][1] will not exist in nums when applying the ith operation.
Complexity Analysis
- Time Complexity: O(n + m)
- Space Complexity: O(n)
2295. Replace Elements in an Array LeetCode Solution in C++
class Solution {
public:
vector<int> arrayChange(vector<int>& nums, vector<vector<int>>& operations) {
unordered_map<int, int> numToIndex;
for (int i = 0; i < nums.size(); ++i)
numToIndex[nums[i]] = i;
for (const vector<int>& o : operations) {
const int original = o[0];
const int replaced = o[1];
const int index = numToIndex[original];
nums[index] = replaced;
numToIndex.erase(original);
numToIndex[replaced] = index;
}
return nums;
}
};
/* code provided by PROGIEZ */
2295. Replace Elements in an Array LeetCode Solution in Java
class Solution {
public int[] arrayChange(int[] nums, int[][] operations) {
Map<Integer, Integer> numToIndex = new HashMap<>();
for (int i = 0; i < nums.length; ++i)
numToIndex.put(nums[i], i);
for (int[] o : operations) {
final int original = o[0];
final int replaced = o[1];
final int index = numToIndex.get(original);
nums[index] = replaced;
numToIndex.remove(original);
numToIndex.put(replaced, index);
}
return nums;
}
}
// code provided by PROGIEZ
2295. Replace Elements in an Array LeetCode Solution in Python
class Solution:
def arrayChange(
self,
nums: list[int],
operations: list[list[int]],
) -> list[int]:
numToIndex = {num: i for i, num in enumerate(nums)}
for original, replaced in operations:
index = numToIndex[original]
nums[index] = replaced
del numToIndex[original]
numToIndex[replaced] = index
return nums
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.