-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1323-maximum-69-number.js
257 lines (212 loc) · 9.13 KB
/
1323-maximum-69-number.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
# 1323. Maximum 69 Number
## https://leetcode.com/problems/maximum-69-number/
Given a positive integer num consisting only of digits 6 and 9.
Return the maximum number you can get by changing at most one digit (6 becomes
9, and 9 becomes 6).
## Constraints
- 1 <= num <= 10^4
- num's digits are 6 or 9.
*/
// 〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰
// Runtime: 108 ms, faster than 5.15% of JavaScript online submissions
// Memory Usage: 33.5 MB, less than 100.00% of JavaScript online submissions
// /**
// * @param {number} num
// * @return {number}
// */
// const maximum69Number = num => {
// const s = '' + num;
// const first6 = s.indexOf(6);
// // console.log(first6);
// if (first6 < 0) return num;
// // console.log(num + 3 * 10 ** (s.length - 1 - first6));
// return num + 3 * 10 ** (s.length - 1 - first6);
// };
// 〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰
// Runtime: 96 ms, faster than 11.86% of JavaScript online submissions
// Memory Usage: 33.4 MB, less than 100.00% of JavaScript online submissions
// /**
// * @param {number} num
// * @return {number}
// */
// const maximum69Number = num => {
// let x = 10 ** Math.trunc(Math.log10(Math.abs(num)));
// while (0 < x) {
// if (num % (x * 10) < 9 * x) return num + 3 * x;
// x = Math.trunc(x / 10);
// }
// return num;
// };
// 〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰
// Runtime: 76 ms, faster than 20.04% of JavaScript online submissions
// Memory Usage: 33.7 MB, less than 100.00% of JavaScript online submissions
// /**
// * @param {number} num
// * @return {number}
// */
// const maximum69Number = num => +('' + num).replace('6', '9');
// 〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰
// Runtime: 72 ms, faster than 22.70% of JavaScript online submissions
// Memory Usage: 33.5 MB, less than 100.00% of JavaScript online submissions
// /**
// * @param {number} num
// * @return {number}
// */
// const maximum69Number = num => +('' + num).replace(6, 9);
// 〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰
// Runtime: 144 ms, faster than 5.01% of JavaScript online submissions
// Memory Usage: 38.5 MB, less than 62.87% of JavaScript online submissions
// /**
// * @param {number} num
// * @return {number}
// */
// const maximum69Number = num => Number(`${num}`.replace(6, 9));
// 〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰
// Runtime: 76 ms, faster than 84.11% of JavaScript online submissions
// Memory Usage: 38.8 MB, less than 16.93% of JavaScript online submissions
// /**
// * @param {number} num
// * @return {number}
// */
// const maximum69Number = num => {
// let tmp = num;
// let tmp2 = 3;
// let addend = 0;
// while (0 < tmp) {
// // console.log(tmp % 10);
// if (6 === tmp % 10) addend = tmp2;
// tmp = Math.trunc(tmp / 10);
// tmp2 *= 10;
// }
// return num + addend;
// };
// 〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰
// Runtime: 108 ms, faster than 5.01% of JavaScript online submissions
// Memory Usage: 38.3 MB, less than 93.96% of JavaScript online submissions
// /**
// * @param {number} num
// * @return {number}
// */
// const maximum69Number = num => {
// let [cntDown, cntUp, addend] = [num, 3, 0];
// while (0 < cntDown) {
// if (6 === cntDown % 10) addend = cntUp;
// [cntDown, cntUp] = [Math.trunc(cntDown / 10), cntUp * 10];
// }
// return num + addend;
// };
// 〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰
// Runtime: 72 ms, faster than 95.34% of JavaScript online submissions
// Memory Usage: 38.6 MB, less than 62.87% of JavaScript online submissions
// /**
// * @param {number} num
// * @return {number}
// */
// const maximum69Number = num => {
// // Count down by "chopping off" the last digit from a copy of `num`
// // Count up by by powers of ten (10 ** n) times three (3, 30, 300, etc.)
// // Addend will be added to `num` (as the augend) as return value
// let [cntDown, cntUp, addend] = [num, 3, 0];
// // Keep chopping numbers off the end of `cntDown`
// while (0 < cntDown) {
// // If the digit about to be chopped off is a "6",
// // then remember where we are in counting up by powers of ten times three
// if (6 === cntDown % 10) addend = cntUp;
// // Count down by chopping off the last digit
// // Count up by power of ten times three
// [cntDown, cntUp] = [Math.trunc(cntDown / 10), cntUp * 10];
// }
// // Add that last power of ten times three to the original `num`
// return num + addend;
// };
// 〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰
// Runtime: 80 ms, faster than 61.83% of JavaScript online submissions
// Memory Usage: 38.4 MB, less than 91.02% of JavaScript online submissions
// /**
// * @param {number} decreasing
// * @return {number}
// */
// const maximum69Number = decreasing => {
// // Move the most significant digit from one number to another
// // The number passed in will be `decreasing` and this will be `increasing`
// let increasing = 0;
// // While the decreasing number still has significant digits to be moved
// while (0 < decreasing) {
// // Use a combo of decimal and binary math to get the most significant digit
// // (`x | 0` is like `Math.trunc(x)` but shorter, and binary)
// const sigDigit = (decreasing / 10 ** (Math.log10(decreasing) | 0)) | 0;
// // Use a combo of decimal and binary math to get the power of ten of the
// // most significant digit
// // (Really, `x | 0` is just shorter; that's the only reason it's used)
// const powerOfTen = 10 ** (Math.log10(decreasing) | 0);
// // If the most significant digit is a 6, return the answer
// if (6 === sigDigit) return increasing + decreasing + 3 * powerOfTen;
// // The `decreasing` number loses it's most significant digit
// decreasing -= sigDigit * powerOfTen;
// // And the `increasing` number gets a new least significant non-zero digit
// increasing += sigDigit * powerOfTen;
// }
// // If we got here, no `6`s were found and all digits moved from
// // `decreasing` to `increasing` so we have to return `increasing`
// // to return the original `decreasing` number
// return increasing;
// };
// 〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰
// /**
// * @param {number} decreasing
// * @return {number}
// */
// const maximum69Number = decreasing => {
// let increasing = 0;
// while (0 < decreasing) {
// const sigDigit = (decreasing / 10 ** (Math.log10(decreasing) | 0)) | 0;
// const powerOfTen = 10 ** (Math.log10(decreasing) | 0);
// if (6 === sigDigit) return increasing + decreasing + 3 * powerOfTen;
// decreasing -= sigDigit * powerOfTen;
// increasing += sigDigit * powerOfTen;
// }
// return increasing;
// };
// 〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰
// Runtime: 88 ms, faster than 14.24% of JavaScript online submissions
// Memory Usage: 38.4 MB, less than 80.23% of JavaScript online submissions
// /**
// * @param {number} num
// * @return {number}
// */
// const maximum69Number = num => {
// for (let pow = Math.log10(num) | 0; 0 <= pow; pow--) {
// const digit = ((num / 10 ** pow) | 0) % 10;
// if (6 === digit) return num + 3 * 10 ** pow;
// }
// return num;
// };
// 〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰
// Runtime: 88 ms, faster than 14.24% of JavaScript online submissions
// Memory Usage: 38.6 MB, less than 63.99% of JavaScript online submissions
/**
* @param {number} num
* @return {number}
*/
const maximum69Number = num => {
for (let pow = Math.log10(num) | 0; 0 <= pow; pow--)
if (6 === ((num / 10 ** pow) | 0) % 10) return num + 3 * 10 ** pow;
return num;
};
// 〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰
import { strictEqual } from 'assert';
// Example 1
strictEqual(maximum69Number(9669), 9969);
// Explanation:
// Changing the first digit results in 6669.
// Changing the second digit results in 9969.
// Changing the third digit results in 9699.
// Changing the fourth digit results in 9666.
// The maximum number is 9969.
// Example 2
strictEqual(maximum69Number(9996), 9999);
// Explanation: Changing the last digit 6 to 9 results in the maximum number.
// Example 3
strictEqual(maximum69Number(9999), 9999);
// Explanation: It is better not to apply any change.