Proper Locking
def get_event_info(event, i):
words = event.split()
return [i, words[0], int(words[1])]
def check_log_history(events):
stack = []
acquired = set()
for i, event in enumerate(events):
i, type, id = get_event_info(event, i)
if type == "ACQUIRE" and acquired.__contains__(id):
return i+1
elif type == "ACQUIRE":
stack.append({"time": i, "action": type, "id": id})
acquired.add(id)
elif type == "RELEASE":
if not stack or stack[-1]["id"] != id or stack[-1]["action"] != "ACQUIRE":
return i + 1
else:
stack.pop()
acquired.remove(id)
if stack:
return len(events) + 1
else:
return 0Last updated