Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int twoStacks(int x, stack<int> a, stack<int> b) {
- if(x < 0) {
- return -1;
- }
- int a_top = -1, b_top = -1, m1 = 0, m2 = 0;
- if(!a.empty()) { // Pop A
- a_top = a.top();
- a.pop();
- m1 = 1 + twoStacks(x - a_top, a, b);
- }
- if(!b.empty()) { // Pop B
- b_top = b.top();
- b.pop();
- if(a_top != -1) {
- a.push(a_top);
- }
- m2 = 1 + twoStacks(x - b_top, a, b);
- }
- return max(m1, m2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement