
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if it is possible to rearrange rectangles in a non-ascending order of breadths in Python
Suppose we have a list of rectangles represented using its length and breadth. We can rotate any rectangle by 90 degrees so after rotating, the breadth will become length and vice-versa. We have to check whether we can sort the rectangles in non-increasing order of breadth.
So, if the input is like rects = [[4, 5], [5, 7], [4, 6]], then the output will be True as the breadths are [5,7,6] now if we rotate last two rectangles then breadths will be [5,5,4] which is in non-increasing way.
To solve this, we will follow these steps −
- m := 99999
- for i in range 0 to size of rect, do
- if maximum of length and breadth of ith rectangle <= m, then
- m := maximum of length and breadth of ith rectangle
- otherwise when minimum of length and breadth of ith rectangle <= m
- m := minimum of length and breadth of ith rectangle
- otherwise,
- return False
- if maximum of length and breadth of ith rectangle <= m, then
- return True
Example
Let us see the following implementation to get better understanding −
def solve(rect): m = 99999 for i in range(len(rect)): if max(rect[i][0], rect[i][1]) <= m: m = max(rect[i][0], rect[i][1]) elif min(rect[i][0], rect[i][1]) <= m: m = min(rect[i][0], rect[i][1]) else: return False return True rects = [[4, 5], [5, 7], [4, 6]] print(solve(rects))
Input
[[4, 5], [5, 7], [4, 6]]
Output
True
- Related Articles
- Check if it is possible to rearrange a binary string with alternate 0s and 1s in Python
- Check if it is possible to survive on Island in Python
- Check if it is possible to sort the array after rotating it in Python
- Check if it is possible to transform one string to another in Python
- Check if it is possible to create a polygon with a given angle in Python
- Check if it is possible to create a palindrome string from given N in Python
- Check if it is possible to create a polygon with given n sidess in Python
- Is it possible to divide records in both ascending and descending order in MySQL and display them alternatively?
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order in C+=+
- Check if it is possible to serve customer queue with different notes in Python
- Check if it is possible to reach a number by making jumps of two given length in Python
- Rearrange characters to form palindrome if possible in C++
- Is it possible to check if a String only contains ASCII in java?
- Check if it is possible to draw a straight line with the given direction cosines in Python
- Check if it is possible to form string B from A under the given constraint in Python

Advertisements