We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 1263e1f commit 1ceba69Copy full SHA for 1ceba69
scripts/algorithms/S/Smallest String With A Given Numeric Value/Smallest String With A Given Numeric Value.cpp
@@ -1,18 +1,26 @@
1
-// Runtime: 70 ms (Top 48.18%) | Memory: 21.1 MB (Top 93.21%)
2
class Solution {
3
public:
4
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;
+ string str="";
+ for(int i=0;i<n;i++){
+ str+='a';
14
}
15
16
- return res;
+ int curr=n;
+ int diff=k-curr;
+ if(diff==0) return str;
+ for(int i=n-1;i>=0 && diff>0;i--){
+ if(diff>25){
+ str[i]='z';
+ diff-=25;
+ }else{
+ str[i]=char('a'+diff);
17
+ return str;
18
+ }
19
20
21
-};
22
+};
23
+// a a a a a
24
+// 5
25
+// diff= 73-5
26
+//
0 commit comments