Tap here to switch tabs
Problem
Submissions

Problem: Integer Replacement

med
30 min
Try to solve the Integer Replacement problem.

Statement

Given a positive integer n, you can perform one of the following operations:

  • If n is even, replace n with n // 22.

  • If n is odd, replace n with either n ++ 11 or n −- 11.

Return the minimum number of operations required for n to become 11.

Constraints:

  • 1≤1 \leq n ≤231−1\leq 2^{31} - 1

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Integer Replacement

med
30 min
Try to solve the Integer Replacement problem.

Statement

Given a positive integer n, you can perform one of the following operations:

  • If n is even, replace n with n // 22.

  • If n is odd, replace n with either n ++ 11 or n −- 11.

Return the minimum number of operations required for n to become 11.

Constraints:

  • 1≤1 \leq n ≤231−1\leq 2^{31} - 1