- Related Questions & Answers
- 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 in Android to check if a notification is visible or canceled?
- Check if it is possible to serve customer queue with different notes in Python
- Is it possible to check if a String only contains ASCII in java?
- Check if it is possible to reach a number by making jumps of two given length in Python
- Is it possible to divide records in both ascending and descending order in MySQL and display them alternatively?
- Queries to check if it is possible to join boxes in a circle in C++
- Check if it is possible to sort an array with conditional swapping of adjacent allowed in Python
- Check if it is possible to draw a straight line with the given direction cosines in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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))

[[4, 5], [5, 7], [4, 6]]

True

Advertisements