One Character Of Difference
This is the USACO Guide solution for nearest smaller element. It reports, for each position, the one-based index of the nearest strictly smaller element to the left, or 0 when there is none.
for idx, num in enumerate(nums):
while stack and stack[-1][0] >= num:
stack.pop()
ans.append(0 if not stack else stack[-1][1] + 1)
stack.append((num, idx))
A reviewer changes >= to > and claims it is equivalent. Run both on nums = [2, 5, 5, 3]. What actually happens?
Sign in to answer questions and track your progress
Sign In