📌  相关文章
📜  检查是否可以按宽度的非升序重新排列矩形

📅  最后修改于: 2021-10-23 08:34:07             🧑  作者: Mango

给定n个矩形,其长度为L ,宽度为B。我们可以将任何矩形旋转 90 度。换句话说,在转动它们之后,宽度将变为长度,长度将变为宽度。
任务是检查是否有办法使矩形按宽度不升序排列。也就是说,每个矩形的宽度必须不大于前一个矩形的宽度。
注意:您不能更改矩形的顺序。
例子:

方法:下面是解决这个问题的分步算法:

  1. 用它们的长度和宽度初始化n 个矩形。
  2. 从左到右迭代矩形。
  3. 以这样的方式转动每个矩形,使其宽度尽可能大但不大于前一个矩形。
  4. 如果在某些迭代中没有这样的方法来放置矩形,答案是“NO”

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to check if it possible to form
// rectangles with heights as non-ascending
int rotateRec(int n, int L[], int B[])
{
 
    // set maximum
    int m = INT_MAX;
 
    for (int i = 0; i < n; i++) {
        // replace the maximum with previous maximum
        if (max(L[i], B[i]) <= m)
            m = max(L[i], B[i]);
 
        // replace the minimum with previous minimum
        else if (min(L[i], B[i]) <= m)
            m = min(L[i], B[i]);
 
        // print NO if the above
        // two conditions fail at least once
        else {
            return 0;
        }
    }
    return 1;
}
 
// Driver code
int main()
{
    // initialize the number of rectangles
    int n = 3;
 
    // initialize n rectangles with length and breadth
    int L[] = { 5, 5, 6 };
    int B[] = { 6, 7, 8 };
    rotateRec(n, L, B) == 1 ? cout << "YES"
                            : cout << "NO";
 
    return 0;
}


Java
// Java implementation of above approach
import java.io.*;
 
class GFG {
    
// Function to check if it possible to form
// rectangles with heights as non-ascending
 static int rotateRec(int n, int L[], int B[])
{
 
    // set maximum
    int m = Integer.MAX_VALUE;
 
    for (int i = 0; i < n; i++) {
        // replace the maximum with previous maximum
        if (Math.max(L[i], B[i]) <= m)
            m = Math.max(L[i], B[i]);
 
        // replace the minimum with previous minimum
        else if (Math.min(L[i], B[i]) <= m)
            m = Math.min(L[i], B[i]);
 
        // print NO if the above
        // two conditions fail at least once
        else {
            return 0;
        }
    }
    return 1;
}
 
// Driver code
 
    public static void main (String[] args) {
    // initialize the number of rectangles
    int n = 3;
 
    // initialize n rectangles with length and breadth
    int L[] = { 5, 5, 6 };
    int B[] = { 6, 7, 8 };
    if(rotateRec(n, L, B) == 1)
     System.out.println( "YES");
     else
     System.out.println( "NO");
    }
}
 // This Code is contributed by inder_verma..


Python3
# Python3 implementation of above approach
import sys;
 
# Function to check if it possible
# to form rectangles with heights
# as non-ascending
def rotateRec(n, L, B):
 
    # set maximum
    m = sys.maxsize;
 
    for i in range(n):
 
        # replace the maximum with
        # previous maximum
        if (max(L[i], B[i]) <= m):
            m = max(L[i], B[i]);
 
        # replace the minimum
        # with previous minimum
        elif (min(L[i], B[i]) <= m):
            m = min(L[i], B[i]);
 
        # print NO if the above two
        # conditions fail at least once
        else:
            return 0;
     
    return 1;
 
# Driver code
 
# initialize the number
# of rectangles
n = 3;
 
# initialize n rectangles
# with length and breadth
L = [5, 5, 6];
B = [ 6, 7, 8 ];
if(rotateRec(n, L, B) == 1):
    print("YES");
else:
    print("NO");
 
# This code is contributed by mits


C#
// C# implementation of above approach
using System;
 
class GFG
{
     
// Function to check if it possible
// to form rectangles with heights
// as non-ascending
static int rotateRec(int n, int []L,
                            int []B)
{
 
    // set maximum
    int m = int.MaxValue ;
 
    for (int i = 0; i < n; i++)
    {
        // replace the maximum with
        // previous maximum
        if (Math.Max(L[i], B[i]) <= m)
            m = Math.Max(L[i], B[i]);
 
        // replace the minimum with
        // previous minimum
        else if (Math.Min(L[i], B[i]) <= m)
            m = Math.Min(L[i], B[i]);
 
        // print NO if the above
        // two conditions fail
        // at least once
        else
        {
            return 0;
        }
    }
    return 1;
}
 
// Driver code
public static void Main ()
{
    // initialize the number
    // of rectangles
    int n = 3;
     
    // initialize n rectangles with
    // length and breadth
    int []L = { 5, 5, 6 };
    int []B = { 6, 7, 8 };
    if(rotateRec(n, L, B) == 1)
    Console.WriteLine("YES");
    else
    Console.WriteLine("NO");
}
}
 
// This code is contributed
// by inder_verma


PHP


Javascript


输出:
NO

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程