【问题标题】:Find the positions in which the elements located in these have the least distance between them找出其中的元素之间距离最小的位置
【发布时间】:2019-05-19 01:14:54
【问题描述】:

我试图实现其中的元素之间距离最小的位置?我在 java 11 中有这段代码:

public class Main {
    public static void main(String[] args) {
        int [] arr= {5, 50, 3, 42, 18, 16, 8, 30, 44};      // Array
        menorD l = new menorD();        
        menorD.MinD(arr);   
    }
}

public class  menorD {

    static int arr_zise;

    public static void minD (int [] arr) {
        arr_zise = arr.length;
        int i, j;
        int minD=0;

        for(i=0;i<arr_zise; i++) {
            for(j=0;j<arr_zise; j++) {
                if(arr[i]!=arr[j]) {
                    minD=arr[i]-arr[j];
                    System.out.print(" i="+ arr[i]+ " j="+ arr[j]+ " minD es: "+Math.abs(min));
                    System.out.println();
                }
            }
        }
    }
}

我试图找到这个:

arr = {5, 50, 3, 42, 18, 16, 8, 30, 44}

我的 Dmin 将是它们之间距离较小的数字之间的差异,在这种情况下,

Dmin1 = 5-3 = 2;

Dmin2 = 18-16 = 2;

Dmin3 44-42 = 2;

不重复数组中数字的索引。我已经编写了这段代码,但我很难找到我要找的东西。

【问题讨论】:

  • 我实际上没有看到解释您要达到的目标的问题陈述。您可能希望在此处将其添加到您的问题中以获得最佳结果。
  • 请澄清您的问题。您是否不仅要查找任意两个值之间的最小距离,还要查找与最小距离相对应的那些值的索引?
  • 是的,兄弟! @PhilipWrage

标签: java algorithm design-patterns lambda functional-interface


【解决方案1】:

理想情况下,当处理逻辑上组合在一起的数据时,您应该抽象出一个类来封装它。在您的情况下,您希望跟踪每个可能的 distance 组合。每个Combination 都应该跟踪:

  1. 值的索引。
  2. 值本身。
  3. 距离。
  4. 哪个低,哪个高。

在这 3. 和 4. 中,可以从 1. 和 2. 计算出来。

    class Combination {
        int indexA, indexB, valueA, valueB;

        public Combination(int[] array, int indexA, int indexB) {
            this.indexA = indexA;
            this.indexB = indexB;
            this.valueA = array[indexA];
            this.valueB = array[indexB];
        }

        public int getDistance() { ... }
        public int getHigh() { ... }
        public int getLow() { ... }
        public int getHighIndex() { ... }
        public int getLowIndex() { ... }
    }

有了这样的数据结构(类),您可以为每个可能的组合构造对象(当然无需重复 - 注意j 如何在i + 1 开始可变重复可能的组合):

        List<Combination> combinations = new ArrayList<>();
        for (int i = 0; i < array.length; i++) 
            for (int j = i + 1; j < array.length; j++) 
                combinations.add(new Combination(array, i, j));

然后使用Combination 中的List,您可以计算它们之间的最小距离:

        int min = combinations.stream()
                .mapToInt(Combination::getDistance)
                .min().getAsInt();

最后,您可以选择与之前计算的最小距离匹配的那些组合:

        combinations.stream()
            .filter(c -> c.getDistance() == min)
            .forEach(c -> System.out.println(c));

关键是将Combination类抽象到它自己的封装类中,因此它可以单独负责提供必要的API来检查特定组合:索引、值、距离、高值、低值甚至String (toString) 表示。

以下是此方法的完整工作演示,运行它来感受一下:

import java.util.ArrayList;
import java.util.List;

public class MinimumDistance {

    public static void main(String[] args) {
        printMinimums(5, 50, 3, 42, 18, 16, 8, 30, 44);
    }

    public static void printMinimums(int... array) {
        List<Combination> combinations = new ArrayList<>();
        for (int i = 0; i < array.length; i++) 
            for (int j = i + 1; j < array.length; j++) 
                combinations.add(new Combination(array, i, j));

        int min = combinations.stream()
                .mapToInt(Combination::getDistance)
                .min().getAsInt();

        combinations.stream()
            .filter(c -> c.getDistance() == min)
            .forEach(c -> System.out.println(c));
    }

    static class Combination {
        int indexA, indexB, valueA, valueB;

        public Combination(int[] array, int indexA, int indexB) {
            this.indexA = indexA;
            this.indexB = indexB;
            this.valueA = array[indexA];
            this.valueB = array[indexB];
        }

        public int getDistance() {
            return getHigh() - getLow();
        }

        public boolean isValueAHigh() {
            return valueA > valueB;
        }

        public int getHigh() {
            return isValueAHigh() ? valueA : valueB; 
        }

        public int getLow() {
            return isValueAHigh() ? valueB : valueA; 
        }

        public int getHighIndex() {
            return isValueAHigh() ? indexA : indexB; 
        }

        public int getLowIndex() {
            return isValueAHigh() ? indexB : indexA; 
        }

        public String toString() {
            return String.format("%d[%d] - %d[%d] = %d", 
                                    getHigh(), getHighIndex(), 
                                    getLow(), getLowIndex(), 
                                    getDistance());
        }
    }
}

Complete code on GitHub

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我在互联网上找到了这个。你在做这个练习吗?

    参考:https://www.geeksforgeeks.org/find-the-minimum-distance-between-two-numbers/

    public class MinimumDistance {
        int minDist(int arr[], int n, int x, int y)  
        { 
            int i, j; 
            int min_dist = Integer.MAX_VALUE; 
            for (i = 0; i < n; i++)  
            { 
                for (j = i + 1; j < n; j++)  
                { 
                    if ((x == arr[i] && y == arr[j] 
                        || y == arr[i] && x == arr[j]) 
                        && min_dist > Math.abs(i - j))  
                        min_dist = Math.abs(i - j); 
                } 
            } 
            return min_dist; 
        } 
    
        public static void main(String[] args)  
        { 
            MinimumDistance min = new MinimumDistance(); 
            int arr[] = {3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}; 
            int n = arr.length; 
            int x = 3; 
            int y = 6; 
    
            System.out.println("Minimum distance between " + x + " and " + y  
                    + " is " + min.minDist(arr, n, x, y)); 
        } 
    }
    

    【讨论】:

    • 看起来很多。但我不必给出 x 或 y 的任何值。无论如何,我会尝试使用该参考代码。 @Wing Kui Tsoi
    • 您的 Dmin1、Dmin2、Dmin3 没有意义,因为很难找到两个数组项的差异?
    【解决方案3】:

    在遍历您的 for 循环时,您可以将距离为 Integer 的 Map 填充到索引 Map&lt;Integer,List&lt;Integer&gt;&gt; 的 List (甚至取决于您的用例的实际值)。如果您总是将两个索引一起添加到List,那么您知道它们在List 中总是彼此相邻,以便以后成对检索。然后只需使用您的最小距离作为地图的关键来拉出相邻对的列表。请记住,如上所述,此列表将有重复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-09
      相关资源
      最近更新 更多