-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1432-max-difference-you-can-get-from-changing-an-integer.js
61 lines (57 loc) · 1.67 KB
/
1432-max-difference-you-can-get-from-changing-an-integer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* 1432. Max Difference You Can Get From Changing an Integer
* https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/
* Difficulty: Medium
*
* You are given an integer num. You will apply the following steps exactly two times:
* - Pick a digit x (0 <= x <= 9).
* - Pick another digit y (0 <= y <= 9). The digit y can be equal to x.
* - Replace all the occurrences of x in the decimal representation of num by y.
* - The new integer cannot have any leading zeros, also the new integer cannot be 0.
*
* Let a and b be the results of applying the operations to num the first and second times,
* respectively.
*
* Return the max difference between a and b.
*/
/**
* @param {number} num
* @return {number}
*/
var maxDiff = function(num) {
const digits = num.toString().split('');
const maxNum = digits.slice();
for (let i = 0; i < maxNum.length; i++) {
if (maxNum[i] !== '9') {
const target = maxNum[i];
for (let j = i; j < maxNum.length; j++) {
if (maxNum[j] === target) {
maxNum[j] = '9';
}
}
break;
}
}
const minNum = digits.slice();
if (minNum[0] !== '1') {
const target = minNum[0];
for (let j = 0; j < minNum.length; j++) {
if (minNum[j] === target) {
minNum[j] = '1';
}
}
} else {
for (let i = 1; i < minNum.length; i++) {
if (minNum[i] !== '0' && minNum[i] !== '1') {
const target = minNum[i];
for (let j = i; j < minNum.length; j++) {
if (minNum[j] === target) {
minNum[j] = '0';
}
}
break;
}
}
}
return parseInt(maxNum.join('')) - parseInt(minNum.join(''));
};