You are viewing a single comment's thread. Return to all comments →
I uesed Linked List to solve this problem
import sys class Node: def __init__(self, data, next = None): self.data = data self.next = next class Stack: def __init__(self): self.top = None def isempty(self): if self.top is None: return True return False def peek(self): if not self.isempty(): return self.top.data return 0 def push(self, data): node = Node(data) if self.isempty(): self.top = node return node.next = self.top self.top = node def pop(self): if self.isempty(): raise Exception("Stack Overflow") data = self.top.data self.top = self.top.next return data if __name__ == '__main__': g = int(input()) for g_itr in range(g): nmx = input().split() n = int(nmx[0]) m = int(nmx[1]) x = int(nmx[2]) ni = sys.stdin.readline().strip().split() mi = sys.stdin.readline().strip().split() stack1, stack2 = Stack(), Stack() for i in range(n-1, -1, -1): stack1.push(int(ni[i])) for i in range(m-1,-1,-1): stack2.push(int(mi[i])) live_sum, i, j = 0, 0, 0 junk = Stack() while not stack1.isempty() and live_sum + stack1.peek() <= x: live_sum += stack1.peek() junk.push(stack1.pop()) i += 1 count = i while not stack2.isempty(): live_sum += stack2.pop() j += 1 while live_sum>x and not junk.isempty(): i-=1 live_sum -= junk.pop() if live_sum<=x and i+j>count: count = i+j result = count print(result)
Seems like cookies are disabled on this browser, please enable them to open this website
Game of Two Stacks
You are viewing a single comment's thread. Return to all comments →
I uesed Linked List to solve this problem