Skip to content

Commit 1ceba69

Browse files
committed
Runtime 47 ms (Top 45.56%) | Memory 27.0 MB (Top 51.39%)
1 parent 1263e1f commit 1ceba69

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
// Runtime: 70 ms (Top 48.18%) | Memory: 21.1 MB (Top 93.21%)
21
class Solution {
32
public:
43
string getSmallestString(int n, int k) {
5-
string res(n, 'a');
6-
k -= n;
7-
8-
int i = res.size() - 1;
9-
while (k > 0) {
10-
int tmp = min(k, 25);
11-
res[i] += tmp;
12-
k -= tmp;
13-
--i;
4+
string str="";
5+
for(int i=0;i<n;i++){
6+
str+='a';
147
}
15-
16-
return res;
8+
int curr=n;
9+
int diff=k-curr;
10+
if(diff==0) return str;
11+
for(int i=n-1;i>=0 && diff>0;i--){
12+
if(diff>25){
13+
str[i]='z';
14+
diff-=25;
15+
}else{
16+
str[i]=char('a'+diff);
17+
return str;
18+
}
19+
}
20+
return str;
1721
}
18-
};
22+
};
23+
// a a a a a
24+
// 5
25+
// diff= 73-5
26+
//

0 commit comments

Comments
 (0)