5790. Minimum Absolute Difference Queries

  1. Minimum Absolute Difference Queries 显示英文描述
    https://leetcode-cn.com/contest/weekly-contest-246/problems/minimum-absolute-difference-queries/

The minimum absolute difference of an array a is defined as the minimum value of |a[i] - a[j]|, where 0 <= i < j < a.length and a[i] != a[j]. If all elements of a are the same, the minimum absolute difference is -1.

For example, the minimum absolute difference of the array [5,2,3,7,2] is |2 - 3| = 1. Note that it is not 0 because a[i] and a[j] must be different.
You are given an integer array nums and the array queries where queries[i] = [li, ri]. For each query i, compute the minimum absolute difference of the subarray nums[li…ri] containing the elements of nums between the 0-based indices li and ri (inclusive).

Return an array ans where ans[i] is the answer to the ith query.

A subarray is a contiguous sequence of elements in an array.

The value of |x| is defined as:

x if x >= 0.
-x if x < 0.

Example 1:

Input: nums = [1,3,4,8], queries = [[0,1],[1,2],[2,3],[0,3]]
Output: [2,1,4,1]
Explanation: The queries are processed as follows:

  • queries[0] = [0,1]: The subarray is [1,3] and the minimum absolute difference is |1-3| = 2.
  • queries[1] = [1,2]: The subarray is [3,4] and the minimum absolute difference is |3-4| = 1.
  • queries[2] = [2,3]: The subarray is [4,8] and the minimum absolute difference is |4-8| = 4.
  • queries[3] = [0,3]: The subarray is [1,3,4,8] and the minimum absolute difference is |3-4| = 1.
    Example 2:

Input: nums = [4,5,2,2,7,10], queries = [[2,3],[0,2],[0,5],[3,5]]
Output: [-1,1,1,3]
Explanation: The queries are processed as follows:

  • queries[0] = [2,3]: The subarray is [2,2] and the minimum absolute difference is -1 because all the
    elements are the same.
  • queries[1] = [0,2]: The subarray is [4,5,2] and the minimum absolute difference is |4-5| = 1.
  • queries[2] = [0,5]: The subarray is [4,5,2,2,7,10] and the minimum absolute difference is |4-5| = 1.
  • queries[3] = [3,5]: The subarray is [2,7,10] and the minimum absolute difference is |7-10| = 3.

Constraints:

2 <= nums.length <= 105
1 <= nums[i] <= 100
1 <= queries.length <= 2 * 104
0 <= li < ri < nums.length

[code]
[code]
[code]
[code]
[code]
O(N^2), however still run out of time, meaning NOT most optimal solution.

class Solution {
    public int[] minDifference(int[] nums, int[][] queries) {

        int n = nums.length;
        int[][] dp = new int[n][n];
        
        for(int i=0;i<n;i++){
            for(int j=i;j<n;j++){
                dp[i][j] = 100;
                
                int diff = 100;
                for(int k=j-1;k>=i;k--){
                    diff = smaller(diff, Math.abs(nums[k]-nums[j]));
                    dp[k][j] = smaller(dp[k][j-1], diff);
                }
            }
        }
                
        int m = queries.length;
        int[] ret = new int[m];
        for(int i=0;i<m;i++){
            int l = queries[i][0];               
            int r = queries[i][1];
            ret[i] = dp[l][r]==100?-1:dp[l][r];
        }
        return ret;        
    }
    
    int smaller(int a, int b){
        
        if(a==0){
            return b;
        }
        else if(b==0){
            return a;
        }

        return a<b?a:b;        
    }
}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值