Tutte le sfide
Difficile 0 risolte 0% di accettazione

Cut a Rectangle Into Squares

Find the fewest straight cuts needed to produce only squares.

On each move, choose one rectangle and cut all the way across it, parallel to a side, making two rectangles with positive integer side lengths. Continue until every piece is a square. Squares may have different sizes. Count cuts, not pieces.

Implement rectangle_cutting(height, width).

Input

height: The starting rectangle height.

width: The starting rectangle width.

Output

Return the minimum number of cuts.

Vincoli

1 ≤ height, width ≤ 500. Rotation does not change the answer.

Esempi

Esempio 1
Input:
width = 3
height = 2

Output atteso:
expected = 2
Input:
long long width = 3;
long long height = 2;

Output atteso:
long long expected = 2;
Input:
long width = 3;
long height = 2;

Output atteso:
long expected = 2;

Spiegazione

Cut off a 2 × 2 square, leaving a 2 × 1 rectangle. One more cut splits the remainder into two unit squares.

Esempio 2
Input:
width = 4
height = 4

Output atteso:
expected = 0
Input:
long long width = 4;
long long height = 4;

Output atteso:
long long expected = 0;
Input:
long width = 4;
long height = 4;

Output atteso:
long expected = 0;

Spiegazione

The starting rectangle is already a square.

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.