![]() |
VOOZH | about |
Consider a Painting and a Frame having length and breadth as L1, B1 and L2, B2 respectively. Then your task is to output minimum number of moves required to fit that painting into frame by using below operation considering that you are allowed to rotate painting 90° as many times as you want. In each move, you are allowed to cut length or breadth of painting into half . More Formally, update L1 or B1 into (L1/2) or (B1/2).
Note: Fitting painting into frame should be considered as (L1 <= L2 and B1 <= B2) or (L1 <= B2 or L2 <= B1).
Example:
Input: L1 = 8, B1 = 13, L2 = 6, B2 = 10
Output: = 1
Explanation: Fold the Painting by breadth, then (B1/2) = 13/2 = 6. Now the condition (L1 <= B2 or L2 <= B1)holds trueInput: L1 = 4, B1 = 8, L2 = 3, B2 = 10
Output: 1
Explanation: Fold the Painting by length, then (L1/2) = 4/2 = 2. Now, the condition (L1 <= L2 and B1 <= B2).
Approach: Implement the idea below to solve the problem
The main idea to solve the problem is to tracks two counts one for non-rotated placement let say Count1 and another for rotated placement let say Count2. The final result is the minimum of these two representing the minimum number of moves needed for the optimal placement strategy.
Steps were taken to solve the problem:
Below is the code to implement the approach:
1
Time Complexity: O(N)
Auxiliary Space: O(1)