Max-Min Difference in Number Transformation
IntuitionApproachI approached this problem by manipulating the number as a string. To find the maximum value, I iterate through the digits, replacing the first
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nI approached this problem by manipulating the number as a string. To find the maximum value, I iterate through the digits, replacing the first non-9 digit (and all its occurrences) with 9. For the minimum value, I replace all occurrences of the first digit with 0. Finally, I calculate the difference between these modified numbers to get the result.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```typescript []\nfunction minMaxDifference(num: number): number {\n    const numS = num.toString().split("");\n    let maxVal: string[] = [];\n    let minVal: string[] = [];\n\n    for (let i = 0; i < numS.length; i++) {\n      const deep = JSON.parse(JSON.stringify(numS));\n      //if not 9, change to 9 and break, but if last one change to 9\n      if (numS[i] !== "9") {\n        let val = numS[i];\n        deep.forEach((x: string, index: number) => {\n          if (x === val) {\n            deep[index] = "9";\n          }\n        });\n        maxVal.push(...deep);\n        break;\n      }\n      if (i === numS.length - 1) {\n        deep.splice(i, 1, "9");\n        maxVal.push(...deep);\n        break;\n      }\n    }\n\n    const deep = JSON.parse(JSON.stringify(numS));\n    let val = numS[0];\n    deep.forEach((x: string, index: number) => {\n      if (x === val) {\n        deep[index] = "0";\n      }\n    });\n    minVal.push(...deep);\n\n    return Number(maxVal.join("")) - Number(minVal.join(""));\n};\n```
['TypeScript']