LeetCode 1906: Minimum Absolute Difference Queries Solution
Master LeetCode problem 1906 (Minimum Absolute Difference Queries), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1906. Minimum Absolute Difference Queries
Problem Explanation
Explanation
To solve this problem, we can precompute the minimum absolute difference for all subarrays of the input array nums. Then, for each query [li, ri], we can directly look up the precomputed minimum absolute difference for the subarray nums[li...ri].
Algorithm:
- Create a prefix array
prefixwhereprefix[i]stores the minimum absolute difference within the subarraynums[0...i]. - Initialize a map
lastIndexwherelastIndex[nums[i]]stores the last index ofnums[i]encountered in the array. - Initialize a result array
ansto store the answers for each query. - Iterate over the input array
numsto compute the values forprefixandlastIndex. - Iterate over each query
[li, ri]:- If all elements in the subarray are the same, set the result as -1.
- Otherwise, find the minimum absolute difference using
prefix[ri] - prefix[li].
Time Complexity:
- Building the
prefixarray andlastIndexmap takes O(n) time. - Answering each query takes O(1) time.
- Overall time complexity is O(n + q), where n is the length of the input array
numsand q is the number of queries.
Space Complexity:
- Additional space for
prefixandlastIndexis O(n). - Space complexity is O(n) due to the prefix array and lastIndex map.
Solution Code
import java.util.*;
class Solution {
public int[] minDifference(int[] nums, int[][] queries) {
int n = nums.length;
int[] prefix = new int[n];
Map<Integer, Integer> lastIndex = new HashMap<>();
for (int i = 0; i < n; i++) {
prefix[i] = lastIndex.containsKey(nums[i]) ? Math.min(Math.abs(i - lastIndex.get(nums[i])), prefix[i - 1]) : (i > 0 ? prefix[i - 1] : Integer.MAX_VALUE);
lastIndex.put(nums[i], i);
}
int[] ans = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
int li = queries[i][0];
int ri = queries[i][1];
if (li < ri && lastIndex.get(nums[li]) < ri) {
ans[i] = -1;
} else {
ans[i] = prefix[ri] - (li > 0 ? prefix[li - 1] : 0);
}
}
return ans;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1906 (Minimum Absolute Difference Queries)?
This page provides optimized solutions for LeetCode problem 1906 (Minimum Absolute Difference Queries) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1906 (Minimum Absolute Difference Queries)?
The time complexity for LeetCode 1906 (Minimum Absolute Difference Queries) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1906 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1906 in Java, C++, or Python.