Skip to content

Commit c28c4fa

Browse files
authored
Create 2999. Count the Number of Powerful Integers1 (#763)
2 parents a4c0461 + dd1446a commit c28c4fa

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
long long dp[17][2];
3+
private:
4+
long long getAns(string& s,int n,bool tight,int limit,int m,long long t){
5+
if(n == m) {
6+
if(!tight) return 1ll;
7+
else{
8+
string temp=s.substr(s.length()-m);
9+
long long ele=stoll(temp);
10+
if(ele >= t) return 1ll;
11+
return 0ll;
12+
}
13+
}
14+
if(n < m) return 0ll;
15+
if(dp[n][tight] != -1) return dp[n][tight];
16+
int ub=tight ? s[s.length()-n]-'0' : 9;
17+
long long ans=0;
18+
19+
for(int i=0;i<=min(ub,limit) ;i++){
20+
ans+=getAns(s,n-1,tight&(ub==i),limit,m,t);
21+
}
22+
23+
return dp[n][tight]=ans;
24+
}
25+
public:
26+
long long numberOfPowerfulInt(long long start, long long finish, int limit, string s) {
27+
start--;
28+
29+
string st=to_string(start);
30+
31+
string f=to_string(finish);
32+
long long t=stoll(s);
33+
memset(dp,-1,sizeof(dp));
34+
long long left=getAns(st,st.length(),1,limit,s.length(),t);
35+
memset(dp,-1,sizeof(dp));
36+
long long right=getAns(f,f.length(),1,limit,s.length(),t);
37+
38+
return (right-left);
39+
}
40+
};

0 commit comments

Comments
 (0)