FastPrepCount Minimum Operations to Reduce Dimensions

Count Minimum Operations to Reduce Dimensions

Salesforce logoSalesforce● EasyOA

Problem statement

Given a rectangle with dimensions (h,w). Count the minimum number of operations to make its dimensions reduce to (h1, w1). An operation is to fold the rectangle paper along any of its side.

Function

countMinimumOperations(h: int, w: int, h1: int, w1: int) → int

Complete the function countMinimumOperations in the editor.

countMinimumOperations has the following parameters:

  1. 1. int h: the original height of the rectangle
  2. 2. int w: the original width of the rectangle
  3. 3. int h1: the reduced height of the rectangle
  4. 4. int w1: the reduced width of the rectangle

Returns int: the minimum number of operations required

Examples

Example 1

h = 8w = 4h1 = 6w1 = 1return = 3

(Feel free to lmk if you find anything wrong in this explanation Many thanks in advance! :) To reduce the dimensions from (8,4) to (6,1), the following operations can be performed: 1. Fold along the width to reduce the width from 4 to 2 (1 operation). 2. Fold along the width again to reduce the width from 2 to 1 (1 operation). 3. Fold along the height to reduce the height from 8 to 6 (1 operation). Therefore, a total of 3 operations are required.

Constraints

  • h1 <= h
  • w1 <= w

More Salesforce problems

See Salesforce hiring insights
Saved
public int countMinimumOperations(int h, int w, int h1, int w1) {
  // write your code here
}
h8
w4
h16
w11
expected3
Checking account…