We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This is really nice looking code man, I really appreciated being able to work through your logic. This was my code by comparison...
importmathdefis_stackable(cubes,bottom):whilelen(cubes)>0:left=0right=len(cubes)-1# If we can't stack the cubesifcubes[left]>bottomorcubes[right]>bottom:returnFalse# Check which cube to removeifcubes[left]>cubes[right]:bottom=cubes.pop(left)elifcubes[right]>cubes[left]:bottom=cubes.pop(right)# If we're on the last cube, check if it is stackableiflen(cubes)==1andcubes[0]<bottom:returnTrueeliflen(cubes)==1andcubes[0]>bottom:returnFalse# If the leftmost and rightmost cubes are the sameifcubes[left]==cubes[right]andlen(cubes)!=1:next_left=leftnext_right=rightcube_to_remove=0whilecubes[next_left]==cubes[next_right]:next_left+=1next_right-=1# If the cubes are all equal up to the middle, then check if the# middle is greater than any cube to the right or leftifnext_left==next_rightornext_right-next_left==1:foriinrange(next_right,len(cubes)-1):ifcubes[next_right]>cubes[i]:returnFalseforiinrange(0,next_left):ifcubes[next_left]>cubes[i]:returnFalse# If the middle cubes aren't bigger than any of the outer cubes, return TruereturnTrue# If the inner cubes are also the same, skip to the next iterationifcubes[next_left]==cubes[next_right]:continue# Identify which side we will draw fromifcubes[next_left]>cubes[next_right]:cube_to_remove=0elifcubes[next_right]>cubes[next_left]:cube_to_remove=len(cubes)-1# Remove the cube from the rowbottom=cubes.pop(cube_to_remove)continuenum_test_cases=int(input())test_cases=list()for_inrange(num_test_cases):case=list()case.append(int(input()))case.append(list(map(int,input().split())))test_cases.append(case)forcaseintest_cases:bottom=math.infnum_cubes=case[0]cubes=case[1]ifis_stackable(cubes,bottom):print('Yes')else:print('No')
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Piling Up!
You are viewing a single comment's thread. Return to all comments →
This is really nice looking code man, I really appreciated being able to work through your logic. This was my code by comparison...