list_of_nums = list(map(int, input().split()))
list_ans = [-1 for element in (list_of_nums)]
stack = []
for i in range(len(list_of_nums)):
while ((len(stack) > 0) and (list_of_nums[stack[-1]] < list_of_nums[i])):
j = stack.pop()
list_ans[j] = list_of_nums[i]
stack.append(i)
# implement the code below btw
stack = []
for i in range(len(list_of_nums) - 1 , -1, -1):
while ((len(stack) > 0) and (list_of_nums[stack[-1]] < list_of_nums[i])):
j = stack.pop()
list_ans[j] = list_of_nums[i]
stack.append(i)
print(list_ans)
#How would I implement this without overwriting the first pass?
12 messages · Page 1 of 1 (latest)
You'd need a second list probably. What's the goal here?
Storing the indexes of values?
list_of_nums = list(map(int, input().split()))
list_ans = [-1 for element in (list_of_nums)]
stack = []
indexes_of_biggernums = []
print(list_of_nums)
for i in range(len(list_of_nums)):
while ((len(stack) > 0) and (list_of_nums[stack[-1]] < list_of_nums[i])):
j = stack.pop()
list_ans[j] = list_of_nums[i]
indexes_of_biggernums.append(j)
stack.append(i)
stack = []
for i in range(len(list_of_nums) - 1 , -1, -1):
while ((len(stack) > 0) and (list_of_nums[stack[-1]] < list_of_nums[i])):
j = stack.pop()
index_of_bigger_num = indexes_of_biggernums[i]
if i-index_of_bigger_num > i-j:
list_ans[j] = list_of_nums[i]
stack.append(i)
print(list_ans)
thus us what I have now
but its always one thing giving problems
i'm 99% sure there's just one small flaw in my logic but I just can't pinpoint it
Where is the array of Values?...
wdym
It's just a string of values separated by whitespace. So it can be any array of strings.
I figured it out btw