[Naive Approach] Try All Permutations - O((n+m)!×(n+m)) Time and O(n+m) Space
The idea is to generate all possible permutations of the given cuts and then calculate the cost for each permutation. Finally, return the minimum cost among them.
Note: This approach is not feasible for larger inputs because the number of permutations grows factorially as (m+n-2)!. For each permutation, we must calculate the cost in O(m+n) time. Hence, the overall time complexity becomes O((m+n−2)!×(m+n)).
[Expected Approach] Using Greedy Technique - O( n (log n)+m (log m)) Time and O(1) Space
Since every cut increases the number of board pieces, performing an expensive cut later would make it cross more pieces and increase its contribution to the total cost.
At each step, choose the cut with the maximum cost among the remaining horizontal and vertical cuts.
Sort both x[] and y[] in ascending order.
Initialize hCount = 1 and vCount = 1.
Set two pointers at the end of both arrays to process the largest cut costs first.
While both arrays have remaining cuts:
If x[i] >= y[j], add x[i] × hCount to the answer, increment vCount, and decrement i.
Otherwise, add y[j] × vCount to the answer, increment hCount, and decrement j.
Process the remaining cuts in x[] using hCount as the multiplier.
Process the remaining cuts in y[] using vCount as the multiplier.
Return the accumulated cost.
C++
#include<iostream>#include<vector>#include<algorithm>usingnamespacestd;intminCost(intn,intm,vector<int>&x,vector<int>&y){// Sort the cutting costs in ascending ordersort(x.begin(),x.end());sort(y.begin(),y.end());inthCount=1,vCount=1;inti=x.size()-1,j=y.size()-1;inttotalCost=0;while(i>=0&&j>=0){// Choose the larger cost cut to // minimize future costsif(x[i]>=y[j]){totalCost+=x[i]*hCount;vCount++;i--;}else{totalCost+=y[j]*vCount;hCount++;j--;}}// Process remaining vertical cutswhile(i>=0){totalCost+=x[i]*hCount;vCount++;i--;}// Process remaining horizontal cutswhile(j>=0){totalCost+=y[j]*vCount;hCount++;j--;}returntotalCost;}intmain(){intn=3,m=3;vector<int>x={2,1};vector<int>y={4,3};cout<<minCost(n,m,x,y)<<endl;return0;}
Java
importjava.util.Arrays;classGfG{publicstaticintminCost(intn,intm,int[]x,int[]y){// Sort the cutting costs in ascending orderArrays.sort(x);Arrays.sort(y);inthCount=1,vCount=1;inti=x.length-1,j=y.length-1;inttotalCost=0;while(i>=0&&j>=0){// Choose the larger cost cut to // minimize future costsif(x[i]>=y[j]){totalCost+=x[i]*hCount;vCount++;i--;}else{totalCost+=y[j]*vCount;hCount++;j--;}}// Process remaining vertical cutswhile(i>=0){totalCost+=x[i]*hCount;vCount++;i--;}// Process remaining horizontal cutswhile(j>=0){totalCost+=y[j]*vCount;hCount++;j--;}returntotalCost;}publicstaticvoidmain(String[]args){intn=3,m=3;int[]x={2,1};int[]y={4,3};System.out.println(minCost(n,m,x,y));}}
Python
defminCost(n,m,x,y):# Sort the cutting costs in ascending orderx.sort()y.sort()hCount,vCount=1,1i,j=len(x)-1,len(y)-1totalCost=0whilei>=0andj>=0:# Choose the larger cost cut to # minimize future costsifx[i]>=y[j]:totalCost+=x[i]*hCountvCount+=1i-=1else:totalCost+=y[j]*vCounthCount+=1j-=1# Process remaining vertical cutswhilei>=0:totalCost+=x[i]*hCountvCount+=1i-=1# Process remaining horizontal cutswhilej>=0:totalCost+=y[j]*vCounthCount+=1j-=1returntotalCostif__name__=="__main__":n,m=3,3x=[2,1]y=[4,3]print(minCost(n,m,x,y))
C#
usingSystem;classGfG{publicstaticintminCost(intn,intm,int[]x,int[]y){// Sort the cutting costs in ascending orderArray.Sort(x);Array.Sort(y);inthCount=1,vCount=1;inti=x.Length-1,j=y.Length-1;inttotalCost=0;// Process the cuts in greedy mannerwhile(i>=0&&j>=0){// Choose the larger cost cut to // minimize future costsif(x[i]>=y[j]){totalCost+=x[i]*hCount;vCount++;i--;}else{totalCost+=y[j]*vCount;hCount++;j--;}}// Process remaining vertical cutswhile(i>=0){totalCost+=x[i]*hCount;vCount++;i--;}// Process remaining horizontal cutswhile(j>=0){totalCost+=y[j]*vCount;hCount++;j--;}returntotalCost;}publicstaticvoidMain(){intn=3,m=3;int[]x={2,1};int[]y={4,3};Console.WriteLine(minCost(n,m,x,y));}}
JavaScript
functionminCost(n,m,x,y){// Sort the cutting costs in ascending orderx.sort((a,b)=>a-b);y.sort((a,b)=>a-b);lethCount=1,vCount=1;leti=x.length-1,j=y.length-1;lettotalCost=0;while(i>=0&&j>=0){// Choose the larger cost cut to // minimize future costsif(x[i]>=y[j]){totalCost+=x[i]*hCount;vCount++;i--;}else{totalCost+=y[j]*vCount;hCount++;j--;}}// Process remaining vertical cutswhile(i>=0){totalCost+=x[i]*hCount;vCount++;i--;}// Process remaining horizontal cutswhile(j>=0){totalCost+=y[j]*vCount;hCount++;j--;}returntotalCost;}// Driver Codeletn=3,m=3;letx=[2,1];lety=[4,3];console.log(minCost(n,m,x,y));