bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
MediumGreedyLC 397

Integer Replacement

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

  1. If n is even, replace n with n /
  2. 2. If n is odd, replace n with either n + 1 or n -
  3. Return the minimum number of operations needed for n to become 1.
Asked at 5 companies
AmazonBaiduBloomberg
Hints
  • 1.Think about which data structure fits best
  • 2.Consider the time complexity of your approach
  • 3.Look for patterns in the constraints
LeetCode