【问题标题】:Largest subset of rectangles such that no rectangle fits in any other rectangle矩形的最大子集,使得没有矩形适合任何其他矩形
【发布时间】:2020-09-12 13:15:06
【问题描述】:

给定 N 个矩形的高度和宽度。任务是找到最大子集的大小,使得没有一对矩形相互适合。请注意,如果 H1 ≤ H2 且 W1 ≤ W2 则矩形 1 适合矩形 2。

Input: arr[] = {{1, 3}, {2, 2}, {1, 3}}
Output: 2
The required sub-set is {{1, 3}, {2, 2}}
{1, 3} is included only once as it can fit in {1, 3}

Input: arr[] = {{1, 5}, {2, 4}, {1, 1}, {3, 3}}
Output: 3

谁能解释一下这个问题背后的动态规划直觉?

【问题讨论】:

    标签: algorithm dynamic-programming


    【解决方案1】:

    是的。让我们按高度升序对输入进行排序,并考虑我们选择任何一个矩形的情况。

    (1) 我们现在不能选择任何第二个具有相同宽度或高度的矩形,因为不相等尺寸中较小的那个可以包含在较大的那个中(例如,2×6 可以包含在2×8,或者2×4可以包含在2×6中)

    (2) 我们现在不能选择具有更高高度和更高宽度的任何第二个矩形,因为它显然可以包含第一个矩形。

    由于我们是按高度升序排列矩形,因此我们必须选择第二个高度更大、宽度更小的矩形,从而得出以下算法:

    Order by height ascending.
    
    Find the longest sequence of strictly
    ascending height and strictly decreasing
    width.
    

    【讨论】:

    • 简单而优雅的解释
    • 您能否详细说明第二个示例的答案(在我的问题中)?它将是 {1,5},{2,4},{3,3}。对?这个解决方案的时间复杂度也是 O(N^2) 吗?
    • @jamesgem 是的 {1,5},{2,4},{3,3} 是有道理的,因为 {1,1} 可以融入其他人。我想我们可以采用一种方法来为 O(n log n) 解决方案找到 longest increasing subsequence
    猜你喜欢
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 2011-08-12
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多