【问题标题】:Finding min distance between elements in an array [closed]查找数组中元素之间的最小距离[关闭]
【发布时间】:2022-01-02 14:15:21
【问题描述】:

我正在尝试找到数组的 2 个给定元素之间的最小距离。 例如,在以下数组 {1,2,3,4,5,6,7} 中,元素 3,7 之间的距离为 4。 我编写了一个成功计算该距离的代码,但如果有重复,它就不起作用。 以下是我到目前为止所写的。我错过了什么?我应该添加任何条件吗? 此外,它应该以不大于 O(n) 的复杂度完成。

public static int findMinDiff(int [] a, int x, int y) {
             
    //previous index and min distance
    int next = 0,curr = 0, min_dist = Integer.MAX_VALUE;
         
    for(int i=0 ; i<a.length ; i++)
    {
        if(a[i]==x)
        {
            curr = i; 
        }
        else if(a[i] == y){
            next = i;
        }
            
    }
    
    min_dist = Math.abs(curr - next);
    if(min_dist==Integer.MAX_VALUE)
        return -1;
     
    return min_dist;
}

【问题讨论】:

  • 如果给定的数组是确定排序的,你可以尝试二分查找以获得更好的复杂性。对于重复项,您可以检查最右边或最左边。 This might help
  • 我应该在代码中添加什么来检查最左/最右?顺便说一句,数组不一定是排序的
  • 如果它是像1, 2, 3, 3, 4, 5, 6, 6这样的排序数组并且你输入36,你需要找到最右边的3和最左边的6,因为你知道6大于3所以它必须在正确的位置。要找到最正确的,只需 1 比 1 或进行二进制搜索,直到你没有得到 3。如果数组没有排序,这可能不起作用。
  • 如果数组没有排序并且你需要最小距离(你没有说你需要什么样的距离)你可以像我之前所说的那样逐个检查每个3你找到最近的6 距离。在每个3 之后保存排序距离并返回它。

标签: java arrays loops time-complexity


【解决方案1】:
public static int findMinDiff(int [] a, int x, int y) {
    //previous index and min distance
    int next = -1,curr = -1, min_dist = Integer.MAX_VALUE;

    for(int i=0 ; i<a.length ; i++)
    {
        if(a[i]==x)
        {
            curr = i;
            if(next != -1) {
                min_dist = Math.min(min_dist, Math.abs(curr-next));
            }
        }
        else if(a[i] == y){
            next = i;
            if(curr != -1){
                min_dist = Math.min(min_dist, Math.abs(curr-next));
            }
        }

    }

    if(min_dist==Integer.MAX_VALUE)
        return -1;

    return min_dist;
}

这可能会有所帮助。

【讨论】:

  • 不适用于 {1,2,3,3,5,6,7}
  • @Sigma9 给定输入中 3 和 7 之间的最小距离是 3 我相信我已经运行了这段代码,它给出了正确的输出。请让我知道您是否提到 3 和 7 作为输入。代码的想法是在找到输入对时计算最小值。
  • 不,伙计,这是我的错误,抱歉。你的代码很好用。谢谢!
【解决方案2】:

如果数组中有重复的值,您的算法将不起作用,因为它始终只记住在数组中遇到的最右边的 x 和 y 值,并且最后只计算它们之间的距离。

即使数组没有排序,你仍然可以满足 O(n) 的要求,通过在遍历数组时跟踪 3 个变量:

  1. 最后遇到的 x 索引,
  2. 最后遇到的 y 索引
  3. 目前最短的距离

每次遇到 x 值或 y 值时,更新您正在跟踪的所有 3 个变量。这意味着您将计算新找到的对的距离并将其与当前最短距离进行比较。如果较短,则保存,否则忽略。

由于距离计算和更新值需要恒定时间,当到达数组末尾并且最短距离变量将包含最短距离时,您仍然会以 O(n) 时间复杂度结束。

你算法的关键就在这里

        if(a[i]==x)
        {
            curr = i;
            // here if you found a complete pair, calculate the
            // distance and, if it is less than the current shortest
            // distance, update the shortest distance variable
        }
        else if(a[i] == y){
            next = i;
            // here if you found a complete pair, calculate the
            // distance and, if it is less than the current shortest
            // distance, update the shortest distance variable
        }

同时删除旧的距离计算min_dist = Math.abs(curr - next);

【讨论】:

  • 我明白你的逻辑,我很难实现你写的东西..
  • @Sigma9 不用担心,因为 Mahadev 在代码中发布了一个完全可行的解决方案。而且,是的,它也适用于{1,2,3,3,5,6,7},除非您在问题中以错误的方式指定了要求。这是另一个测试输入供您尝试{1,2,3,4,7,1,7,3,1,7}
  • 你是对的。我的测试仪出错了。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
  • 2014-01-21
  • 2021-12-25
  • 2011-07-23
相关资源
最近更新 更多