-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path402. Remove K Digits_medium.cpp
52 lines (44 loc) · 1.3 KB
/
402. Remove K Digits_medium.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
50
51
52
#include <vector>
class Solution {
public:
/**
A elemetry question - O(n)
1.to remove elements from the front to make it smaller - like in "1432" ,4 and 3 are to be removed
can be implemented by check the predecessor and remove if greater
2.from back also number may be need to to removed as with smaller examples removing from the solves them - like in "112" we should remove "2" nand not 1
3.skip the leading zeros
**/
string removeKdigits(string num, int k) {
int n= num.length();
vector<int> nums;
for(int i=0;i<n;i++){
nums.push_back(num[i]-'0');
}
if( n==k)return "0";
int i = 0;
while (k > 0 && i < nums.size()) {
if (i > 0 && nums[i] < nums[i - 1]) {
nums.erase(nums.begin() + i - 1); // Erase element at i-1
k--;
i--; // Adjust index after erasing
} else {
i++; // Move to next element
}
}
while (k > 0) {
nums.pop_back();
k--;
}
string m;
for (int digit : nums) {
m += to_string(digit);
}
int j=0;
while(j<m.length() && m[j] == '0'){
j++;
}
string ms=m.substr(j,m.length());
if(ms == "")return "0";
return ms;
}
};