Tried a medium challenge on HackerRank called Piling Up
My solution isn't as elegant as others but it works and constantly reduces the size of the cube size list that need to be rearranged from horizontal to vertical where the block size needs to be equal or smaller than the block below it.
for tests in range(int(input())):
cube_length = int(input())
list_of_cubes = list(map(int, input().split()))
last_value_placed_on_stack = 0
all_items_on_placed_on_stack = False
cannot_stack_anymore = False
a = 0
z = int(len(list_of_cubes)) - 1
while a < int(len(list_of_cubes)):
if cannot_stack_anymore:
break
if int(len(list_of_cubes)) == 1:
if int(list_of_cubes[a]) <= last_value_placed_on_stack:
all_items_on_placed_on_stack = True
break
else:
break
while z > 0:
if list_of_cubes[a] >= int(list_of_cubes[z]):
if list_of_cubes[a] <= last_value_placed_on_stack or last_value_placed_on_stack == 0:
last_value_placed_on_stack = list_of_cubes[a]
list_of_cubes.pop(a)
z = z - 1
break
else:
cannot_stack_anymore = True
break
if list_of_cubes[z] >= int(list_of_cubes[a]):
if list_of_cubes[z] <= last_value_placed_on_stack or last_value_placed_on_stack == 0:
last_value_placed_on_stack = list_of_cubes[z]
list_of_cubes.pop(z)
z = z - 1
break
else:
cannot_stack_anymore = True
break
if all_items_on_placed_on_stack:
print("Yes")
else:
print("No")
No comments:
Post a Comment