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.
defboardCutting(cost_y,cost_x):MOD=10**9+7# Sort costs in descending ordercost_y.sort(reverse=True)cost_x.sort(reverse=True)# Variables to track the number of segmentshorizontal_segments=1vertical_segments=1total_cost=0i,j=0,0whilei<len(cost_y)andj<len(cost_x):ifcost_y[i]>=cost_x[j]:total_cost+=cost_y[i]*vertical_segmentshorizontal_segments+=1i+=1else:total_cost+=cost_x[j]*horizontal_segmentsvertical_segments+=1j+=1total_cost%=MOD# If there are remaining horizontal cutswhilei<len(cost_y):total_cost+=cost_y[i]*vertical_segmentshorizontal_segments+=1i+=1total_cost%=MOD# If there are remaining vertical cutswhilej<len(cost_x):total_cost+=cost_x[j]*horizontal_segmentsvertical_segments+=1j+=1total_cost%=MODreturntotal_cost
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cutting Boards
You are viewing a single comment's thread. Return to all comments →