查找列表元素之间冲突相对顺序的算法

时间:2016-01-06 04:40:29

标签: algorithm

我不知道这个问题的名称(如果我这样做,我可能会找到一个解决方案)。

问题

给定两个整数列表double[,] qk[k] = { { Xnew[k + 1] }, { Ynew[k + 1] }, { Anew[k + 1] } };A,确定任何两个元素Bx之间是否存在排序冲突,其中yx } yA都存在B

例如,请考虑以下两个列表:

A : 2 3 8 9
B : 3 1 7 2

在这种情况下,存在排序冲突{ 2, 3 },因为这些元素在列表A中以相对于彼此的相反顺序显示,就像它们在列表B中那样。

琐碎案件

AB没有共同点;没有冲突。

AB是相同的列表;没有冲突。

问题

有哪些算法可以解决这个问题?

5 个答案:

答案 0 :(得分:1)

编辑:

在那种情况下,

  1. 在两个列表中创建公共元素的有序列表(按列表2排序)
  2. 使用新列表,执行相同的操作,创建常用元素的有序列表(按列表1排序)。
  3. 检查两个列表是否相同。
  4. 要创建有序列表,请在另一个列表中搜索要设置订单的列表中的每个元素。如果找到,请将其添加到新列表中。

    这将是O(n * m)。

    您还必须检查在创建新列表时可能完成的重复项。

答案 1 :(得分:0)

使用键作为第一个数组的值创建一个哈希(或任何快速查找数据结构),并将值作为数组中的索引(位置)。让我们称之为S

def Validate(A, B):
    S = CreateLookupTable(A)
    minValueTillNow = -∞
    for i in B:
      if i in S:
         indexInA = lookup(S, i)
         if indexInA < minValueTillNow:
            return false
         minValueTillNow = indexInA

    return true

答案 2 :(得分:0)

不确定这是否是最佳选择,但以下是一种解决方法: -

  1. 创建A&amp; A的交集。 B,称之为C
  2. C的元素具有属性(indexA,indexB)
  3. 当我们遍历C的元素时,indexA&amp; indexB应该是递减或递增。如果indexA正在增加且indexB正在减少,那么我们就会有相对冲突的订单。
  4. 例如:

      A : 2 3 8 9
      B : 3 1 7 2
      C : 2(1,4),3(2,1)
    

    正如我们所见,indexA正在增加,而indexB正在减少,因此存在冲突。

答案 3 :(得分:0)

您尚未提供详细信息,但现在可以在O(n)中说明问题:

int[] A = new[] {2, 8, 9, 10, 3};
int[] B = new[] {2, 1, 7, 3};

int x = 2, y = 3;

int a1 = -1, a2 = -1, b1 = -1, b2 = -1;

for (int i = 0; i < (A.Length > B.Length ? A.Length : B.Length); i++)
{
    if (a1 == -1 && A.Length > i && A[i] == x) a1 = i;
    if (a2 == -1 && A.Length > i && A[i] == y) a2 = i;
    if (b1 == -1 && B.Length > i && B[i] == x) b1 = i;
    if (b2 == -1 && B.Length > i && B[i] == y) b2 = i;

    if (a1 >= 0 && a2 >= 0 && b1 >= 0 && b2 >= 0) 
        break;
}

var result = (a1 < a2) == (b1 < b2) && ((a1 == -1 && b1 == -1) || (a1 != -1 && b1 != -1));

只进行一次数组,直到数组结束或直到分配所有变量。然后简单检查订单是否相同。

答案 4 :(得分:0)

  1. 获取两个列表之间的所有公共元素值

  2. 创建2个仅包含按其排序的公共元素的列表 各自的源列表(由值中的索引确定 各自的来源清单)

  3. 通过删除调整器将这些列表仅减少到无序元素 列出我们正在测试的列表中的元素(只保留我们测试的列表中的&#34;乱序&#34;元素)

  4. 数据结构做了很多工作。因此,了解每个人的能力非常重要。简而言之,set将值视为原样,而list则考虑排序和值。我们可以通过使用LinkedHashSet变得更疯狂,或者使用bitset做一些疯狂的东西并添加漂亮的lambda,但希望这很直接。

    示例代码

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    
    public class OrderingEnforcer {
    
        public static List<Integer> getImproperlyOrderedElements(List<Integer> ordinator, List<Integer> toTest) {
    
            // create a distinct intersection of a and b (order insensitive)
            Set<Integer> intersection = new HashSet<Integer>(ordinator);
            intersection.retainAll(new HashSet<Integer>(toTest));
    
            // create a sorted list of our intersected set using the
            // indexOf(element)
            // to determine the list ordering
            List<Integer> ordinatorOrderedIntersection = new ArrayList<Integer>(intersection);
            Collections.sort(ordinatorOrderedIntersection, createIndexComparator(ordinator));
    
            List<Integer> toTestOrderedIntersection = new ArrayList<Integer>(intersection);
            Collections.sort(toTestOrderedIntersection, createIndexComparator(toTest));
    
            // Now we can create a difference of the two Lists
            toTestOrderedIntersection.removeAll(ordinatorOrderedIntersection);
            return toTestOrderedIntersection;
        }
    
        private static Comparator<Integer> createIndexComparator(List<Integer> list) {
            return (a, b) -> {
                return Integer.compare(list.indexOf(a), list.indexOf(b));
            };
        }
    }
    
相关问题