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