Skip to main content

2566.js

/** ------------------------------------------------------------------------------
*
* 2566. Maximum Difference by Remapping a Digit
* Topics: Greedy
* https://leetcode.com/problems/maximum-difference-by-remapping-a-digit/description/?envType=daily-question&envId=2025-06-14
*
------------------------------------------------------------------------------ */
/**
* @param {number} num
* @return {number}
*/
var minMaxDifference = function (num) {
let arr = num.toString().split("")
let nonNine = arr.find((a) => a !== "9")
let nonZero = arr.find((a) => a !== "0")
let max = arr.map((a) => {
if (nonNine === a) {
return "9"
} else {
return a
}
})

let min = arr.map((a) => {
if (nonZero === a) {
return "0"
} else {
return a
}
})

return Number(max.join("")) - Number(min.join(""))
}

console.log(console.log(minMaxDifference(11891)))