Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. int twoStacks(int x, stack<int> a, stack<int> b) {
  2.  
  3. if(x < 0) {
  4. return -1;
  5. }
  6. int a_top = -1, b_top = -1, m1 = 0, m2 = 0;
  7. if(!a.empty()) { // Pop A
  8. a_top = a.top();
  9. a.pop();
  10. m1 = 1 + twoStacks(x - a_top, a, b);
  11. }
  12. if(!b.empty()) { // Pop B
  13. b_top = b.top();
  14. b.pop();
  15. if(a_top != -1) {
  16. a.push(a_top);
  17. }
  18. m2 = 1 + twoStacks(x - b_top, a, b);
  19. }
  20. return max(m1, m2);
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement