Skip to content

Commit 84eb44c

Browse files
authored
Add files via upload
1 parent 8ee94f8 commit 84eb44c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Day13.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Author: Aryan Yadav
3+
Remove K Digits
4+
5+
Complexity: O(n)
6+
Algorithm: NA
7+
Difficulty: Medium
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
class Solution
13+
{
14+
public:
15+
string removeKdigits(string num, int k)
16+
{
17+
string result = "";
18+
19+
minnum(num, k, result);
20+
result.erase(0, min(result.find_first_not_of('0'), result.size() - 1));
21+
if (result.length() == 0)
22+
result.push_back('0');
23+
return result;
24+
}
25+
26+
void minnum(string num, int k, string &result)
27+
{
28+
if (k == 0)
29+
{
30+
result.append(num);
31+
return;
32+
}
33+
int len = num.length();
34+
if (len <= k)
35+
{
36+
return;
37+
}
38+
39+
int min = 0;
40+
for (int i = 1; i <= k; i++)
41+
{
42+
if (num[i] < num[min])
43+
min = i;
44+
}
45+
result.push_back(num[min]);
46+
string new_str = num.substr(min + 1, len - min);
47+
minnum(new_str, k - min, result);
48+
}
49+
};

0 commit comments

Comments
 (0)