【问题标题】:All 2 element combinations of arrays that follow a rule遵循规则的数组的所有 2 个元素组合
【发布时间】:2021-07-13 06:30:05
【问题描述】:

我一直在研究算法和优化,但我陷入了以下挑战,我正在寻求一些关于如何解决这个问题的指导。

给定 2 个 desc 有序的不同数组 NM 大小 (N>M),编写一个函数返回一个数组,该数组包含两个数组的所有可能的 2 元素 (x, y) 组合,遵循规则:如果 @ 987654324@ 然后y1 > y2

例子:

Input:

A = [4,3,2,1]
B = [20,10]


Output:
[
  [(4,20), (3,10)],
  [(4,20), (2,10)],
  [(4,20), (1,10)],
  [(3,20), (2,10)],
  [(3,20), (1,10)],
  [(2,20), (1,10)]
]

我正在使用 Java 执行此功能,但我希望能提供任何编程语言的答案。

【问题讨论】:

  • 你能发布你的代码吗?
  • 既然数组是降序排列的,不是所有的pairs吗?
  • 我要么不理解规则,要么不理解示例。您显示的示例遵循规则x1 > x2 and y1 > y2。对于您给出的 if-then 规则,示例输出会大得多(例如,它应该包含 [[(3, 20), (4, 10)], [(3, 10), (4, 20)]],并且对于所有其他带有 x1 < x2 的对都相同)。

标签: algorithm recursion multidimensional-array language-agnostic dynamic-programming


【解决方案1】:

由于两个数组都是按降序排列的,因此您只需选择第一个元素右侧的另一个元素,就可以保证这对数组的顺序。

显然你想对两个参数都这样做,并从它们中配对。

在部分不惯用的 Haskell 中(使用索引,因此它更接近于命令式伪代码);使用列表推导:

foo xs ys  =  [                               -- list of 
                 [                            --  lists of
                   [ (x1,y1), (x2,y2) ]       --   pairs of pairs, where
                     | (j,y1) <- jys,         --    for each elt y1 in ys, pick each
                          y2  <- drop j ys    --    y2 to the right of it in ys
                   ]
                 |                            --  where
                 (i,x1) <- ixs,               --   for each x1 in xs,
                    x2  <- drop i xs ]        --   pick each x2 to the right of it in xs
    where
    ixs = zip [1..] xs     -- make indexed pairs
    jys = zip [1..] ys

运行它:

> foo [4,3,2,1] [30,20,10]
[ [[(4,30),(3,20)],[(4,30),(3,10)],[(4,20),(3,10)]] ,
  [[(4,30),(2,20)],[(4,30),(2,10)],[(4,20),(2,10)]] ,
  [[(4,30),(1,20)],[(4,30),(1,10)],[(4,20),(1,10)]] ,
  [[(3,30),(2,20)],[(3,30),(2,10)],[(3,20),(2,10)]] ,
  [[(3,30),(1,20)],[(3,30),(1,10)],[(3,20),(1,10)]] ,
  [[(2,30),(1,20)],[(2,30),(1,10)],[(2,20),(1,10)]] ]

列表推导只是循环。

按照您的示例,这需要将其平展一层。这可以通过使用专门的展平功能(在 Haskell 中为 concat)进行后处理来实现,但也可以通过重新排列循环来实现:

bar xs ys  =  [  [ (x1,y1), (x2,y2) ]
                 |                       --
                 (i,x1) <- ixs,          -- for each x1 in xs,
                    x2  <- drop i xs,    -- pick each x2 to the right of x1 in xs
                 (j,y1) <- jys,          -- and for each y1 in ys,
                    y2  <- drop j ys ]   -- pick each y2 to the right of y1 in ys
    where
    ixs = zip [1..] xs     -- make indexed pairs
    jys = zip [1..] ys
----
----
> bar [4,3,2,1] [30,20,10]
[ [(4,30),(3,20)],[(4,30),(3,10)],[(4,20),(3,10)],
  [(4,30),(2,20)],[(4,30),(2,10)],[(4,20),(2,10)],
  [(4,30),(1,20)],[(4,30),(1,10)],[(4,20),(1,10)],
  [(3,30),(2,20)],[(3,30),(2,10)],[(3,20),(2,10)],
  [(3,30),(1,20)],[(3,30),(1,10)],[(3,20),(1,10)],
  [(2,30),(1,20)],[(2,30),(1,10)],[(2,20),(1,10)] ]

这就是你想要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2021-12-06
    • 2023-04-10
    • 2017-10-28
    • 1970-01-01
    相关资源
    最近更新 更多