-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path402.c
More file actions
68 lines (52 loc) · 1.59 KB
/
Copy path402.c
File metadata and controls
68 lines (52 loc) · 1.59 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <string.h>
int main() {
long long num = 0;
int k = 0;
long long temp = num;
int num_digits = 0;
while (temp > 0) {
num_digits++;
temp /= 10;
}
int digits_arr[num_digits];
for (int i = num_digits - 1; i >= 0; i--) {
digits_arr[i] = num % 10;
num /= 10;
}
if(k == num_digits) { printf("%d", 0); return 0; }
int stack[num_digits];
int stack_top = 0;
memset(stack, 0, num_digits * sizeof(int));
int last_index = 0;
for (int i = 0; i < num_digits; i++) {
if (stack[stack_top] <= digits_arr[i]) {
stack_top++;
stack[stack_top] = digits_arr[i];
} else {
while (stack[stack_top] > digits_arr[i] && k != 0) {
stack[stack_top] = 0;
stack_top--;
k--;
} // remove all the elements that are bigger than the digit at i
// because we need to have a monotonically increasing stack
stack_top++;
stack[stack_top] = digits_arr[i];
}
last_index = i+1;
if(k == 0) break;
}
int printed = 0;
for (int i = 0; i <= stack_top - k; i++) { // decrease by k because the last elements of the stack are the biggest
if(stack[i] == 0 && !printed) continue;
printf("%d", stack[i]);
printed = 1;
}
for (int i = last_index; i < num_digits; i++) {
if(digits_arr[i] == 0 && !printed) continue;
printf("%d", digits_arr[i]);
printed = 1;
}
if(!printed) printf("%d", 0);
return 0;
}