diff --git a/src/problems/2566-maximum-difference-by-remapping-a-digit.cpp b/src/problems/2566-maximum-difference-by-remapping-a-digit.cpp new file mode 100644 index 0000000..c7a722b --- /dev/null +++ b/src/problems/2566-maximum-difference-by-remapping-a-digit.cpp @@ -0,0 +1,71 @@ +/** +* [2566] Maximum Difference by Remapping a Digit + */ +#include +#include +using namespace std; + + +// submission codes start here + +class Solution +{ +public: + int minMaxDifference(int num) + { + const string numString = to_string(num); + + // Select the first not 9 number when converting to maximum value. + int targetPos = 0; + while (targetPos < numString.size() - 1) + { + if (numString[targetPos] != '9') + { + break; + } + + targetPos += 1; + } + + int maxNum = 0; + + char targetChar = numString[targetPos]; + for (const auto c : numString) + { + if (c == targetChar) + { + maxNum = maxNum * 10 + 9; + } + else + { + maxNum = maxNum * 10 + c - '0'; + } + } + + // The minimum value is replacing the first number into zero. + + targetChar = numString[0]; + int minNum = 0; + for (const auto c : numString) + { + if (c == targetChar) + { + minNum = minNum * 10; + } + else + { + minNum = minNum * 10 + c - '0'; + } + } + + return maxNum - minNum; + } +}; + +// submission codes end + +TEST(P2566, Test1) +{ + ASSERT_EQ(99009, Solution().minMaxDifference(11891)); + ASSERT_EQ(99, Solution().minMaxDifference(90)); +} \ No newline at end of file diff --git a/src/template.cpp b/src/template.cpp index 0abb202..6883b56 100644 --- a/src/template.cpp +++ b/src/template.cpp @@ -10,9 +10,8 @@ using namespace std; __PROBLEM_DEFAULT_CODE__ -// submission codes endo +// submission codes end TEST(__TEST_CASE_NAME__, Test1) { - -} +} \ No newline at end of file