Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created October 21, 2019 02:44
Show Gist options
  • Save surinoel/702c2e057a39b5b3c54a9f7314230d65 to your computer and use it in GitHub Desktop.
Save surinoel/702c2e057a39b5b3c54a9f7314230d65 to your computer and use it in GitHub Desktop.
#include <stack>
#include <string>
#include <vector>
using namespace std;
string solution(string number, int k) {
string answer = "";
int len = number.length();
stack<char> st;
for (int i = 0; i < len; i++) {
char num = number[i];
while (!st.empty() && k > 0) {
char tp = st.top();
if (tp < num) {
st.pop();
k--;
}
else {
break;
}
}
st.push(num);
}
while (k > 0) {
st.pop();
k--;
}
while (!st.empty()) {
answer = st.top() + answer;
st.pop();
}
return answer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment