Title: #397 - Integer Replacement - LeetCode JavaScript Solutions
Open Graph Title: #397 - Integer Replacement - LeetCode JavaScript Solutions
X Title: #397 - Integer Replacement - LeetCode JavaScript Solutions
Description: Given a positive integer n, you can apply one of the following operations: If n is even, replace n with n / 2. If n is odd, replace n with ...
Open Graph Description: Given a positive integer n, you can apply one of the following operations: If n is even, replace n with n / 2. If n is odd, replace n with ...
X Description: Given a positive integer n, you can apply one of the following operations: If n is even, replace n with n / 2. If n is odd, replace n with ...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/integer-replacement
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/integer-replacement","name":"#397 - Integer Replacement - LeetCode JavaScript Solutions","description":"Given a positive integer n, you can apply one of the following operations: If n is even, replace n with n / 2. If n is odd, replace n with ...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number} n\n * @return {number}\n */\n/**\n * @param {number} n\n * @param {number} count\n * @return {number}\n */\nvar integerReplacement = function(n, count = 0) {\n if (n === 1) return count;\n if (n % 2 === 0) {\n return integerReplacement(n / 2, count + 1);\n } else {\n return Math.min(integerReplacement(n + 1, count + 1), integerReplacement(n - 1, count + 1));\n }\n};","keywords":"LeetCode 397, #397 - Integer Replacement, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/integer-replacement |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/integer-replacement/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0397-integer-replacement.js |
| Dynamic Programming | https://leetcodejavascript.com/tags/dynamic-programming |
| Greedy | https://leetcodejavascript.com/tags/greedy |
| Bit Manipulation | https://leetcodejavascript.com/tags/bit-manipulation |
| Memoization | https://leetcodejavascript.com/tags/memoization |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width,initial-scale=1
Robots: index, follow