File tree Expand file tree Collapse file tree 4 files changed +94
-1
lines changed Expand file tree Collapse file tree 4 files changed +94
-1
lines changed Original file line number Diff line number Diff line change @@ -5,4 +5,5 @@ This repository contains the coding interview problems along with solutions.
5
5
| ---------------| ----------| ------| ----------| ------------|
6
6
| [ Island Count] ( Island%20Count ) | | [ CPP] ( Island%20Count/solution.cpp ) | Graph Traversal | Medium |
7
7
| [ Minimum Falling Path Sum] ( Minimum%20Falling%20Path%20Sum ) | [ text] ( Minimum%20Falling%20Path%20Sum/solution.txt ) | [ CPP] ( Minimum%20Falling%20Path%20Sum/solution.cpp ) | Dynamic Programming | Medium |
8
- | [ Word Break] ( Word%20Break ) | [ text] ( Word%20Break/solution.txt ) | [ CPP] ( Word%20Break/solution.cpp ) | Dynamic Programming | Medium |
8
+ | [ Word Break] ( Word%20Break ) | [ text] ( Word%20Break/solution.txt ) | [ CPP] ( Word%20Break/solution.cpp ) | Dynamic Programming | Medium |
9
+ | [ Remove K Digits] ( Remove%20K%20Digits ) | [ text] ( Remove%20K%20Digits/solution.txt ) | [ CPP] ( Remove%20K%20Digits/solution.cpp ) | Greedy, Stack | Medium |
Original file line number Diff line number Diff line change
1
+ # Remove K Digits
2
+ [ Try the problem] ( https://leetcode.com/problems/remove-k-digits/ )
3
+
4
+ Given a non-negative integer ` num ` represented as a string, remove * k* digits from the number so that the new number is the smallest possible.
5
+
6
+ ** Note** :
7
+ - The given ` num ` does not contain any leading zero.
8
+
9
+ ### Example 1
10
+
11
+ ```
12
+ Input: num = "1432219", k = 3
13
+ Output: "1219"
14
+ Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
15
+ ```
16
+
17
+ ### Example 2
18
+
19
+ ```
20
+ Input: num = "10200", k = 1
21
+ Output: "200"
22
+ Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
23
+ ```
24
+
25
+ ### Example 3
26
+
27
+ ```
28
+ Input: num = "10", k = 2
29
+ Output: "0"
30
+ Explanation: Remove all the digits from the number and it is left with nothing which is 0.
31
+ ```
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < string>
3
+ #include < stack>
4
+ using namespace std ;
5
+ class Solution {
6
+ public:
7
+ string removeKdigits (string num, int k) {
8
+ int n = num.length ();
9
+ stack<char > st;
10
+ for (int i = 0 ; i < n; ++i) {
11
+ if (st.empty ()) {
12
+ st.push (num[i]);
13
+ } else if (st.top () > num[i] && k > 0 ) {
14
+ while (!st.empty () && st.top () > num[i] && k > 0 ) {
15
+ st.pop ();
16
+ --k;
17
+ }
18
+ st.push (num[i]);
19
+ } else {
20
+ st.push (num[i]);
21
+ }
22
+ }
23
+ while (!st.empty () && k > 0 ) {
24
+ st.pop ();
25
+ --k;
26
+ }
27
+ string res;
28
+ if (st.empty ()) {
29
+ res = ' 0' ;
30
+ }
31
+ while (!st.empty ()) {
32
+ res += st.top ();
33
+ st.pop ();
34
+ }
35
+ reverse (res.begin (), res.end ());
36
+ // remove leading zeroes
37
+ string finalRes;
38
+ int firstIdx = 0 ;
39
+ int finalLen = res.length ();
40
+ while (firstIdx < finalLen && res[firstIdx] == ' 0' ) {
41
+ ++firstIdx;
42
+ }
43
+ if (firstIdx == finalLen) {
44
+ finalRes = ' 0' ;
45
+ } else {
46
+ while (firstIdx < finalLen) {
47
+ finalRes += res[firstIdx];
48
+ ++firstIdx;
49
+ }
50
+ }
51
+ return finalRes;
52
+ }
53
+ };
54
+
55
+ int main () {
56
+ Solution solver;
57
+
58
+ cout << solver.removeKdigits (" 1234567890" , 9 ) << endl;
59
+ }
Original file line number Diff line number Diff line change
1
+ The problem is quite tricky and revolved around the idea of comparing the numbers,
2
+ observing some kind of properties, leading to a greedy solution.
You can’t perform that action at this time.
0 commit comments