Skip to content

Commit 3a2a0ba

Browse files
authored
Create 2999-count-the-number-of-powerful-integers.js
1 parent d8606e1 commit 3a2a0ba

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @param {number} num
3+
* @param {number} limit
4+
* @param {string} s
5+
*/
6+
function adjust(num, limit, s) {
7+
let sn = parseInt(s);
8+
let sufMod = 10 ** s.length;
9+
10+
let suf = num % sufMod;
11+
num = Math.floor(num / sufMod);
12+
if (suf < sn) --num;
13+
14+
if (num <= 0) return num + 1;
15+
16+
let sNum = num.toString();
17+
let res = sNum.charCodeAt(0) - 48;
18+
let tight = 1;
19+
if (res > limit) {
20+
return (limit + 1) ** sNum.length;
21+
}
22+
23+
for (let i = 1; i < sNum.length; ++i) {
24+
res *= (limit + 1);
25+
26+
if (tight) {
27+
let c = sNum.charCodeAt(i) - 48;
28+
if (c > limit) {
29+
tight = 0;
30+
res += limit + 1;
31+
}
32+
else {
33+
res += c;
34+
}
35+
}
36+
}
37+
38+
return res + tight;
39+
}
40+
/**
41+
* @param {number} start
42+
* @param {number} finish
43+
* @param {number} limit
44+
* @param {string} s
45+
* @return {number}
46+
*/
47+
var numberOfPowerfulInt = function (start, finish, limit, s) {
48+
--start;
49+
50+
let ss = adjust(start, limit, s);
51+
let ff = adjust(finish, limit, s);
52+
53+
return ff - ss;
54+
};

0 commit comments

Comments
 (0)