在JS中将纸张切成最小平方数

时间:2019-10-14 09:20:41

标签: javascript algorithm

我正在尝试从https://www.geeksforgeeks.org/paper-cut-minimum-number-squares-set-2/转换算法 在javascript中。

我能够轻松翻译js(https://www.geeksforgeeks.org/paper-cut-minimum-number-squares/)中的第一个算法,但是贪婪的方法不够精确。

我在js和递归编程方面遇到一些问题,在我的第一次尝试中,出现了最大调用堆栈大小超出错误,因此我尝试使用node --stack-size=10000 <app>来提高它,但是在这种情况下,脚本没有什么都不输出。

这就是我现在拥有的:

// JS program to find minimum  
// number of squares  
// to cut a paper using Dynamic Programming  

const MAX = 300


let dp = new Array(MAX);

for (let i = 0; i < dp.length; i++) {
  dp[i] = new Array(MAX).fill(0);
}

// Returns min number of squares needed  
function minimumSquare(m, n) {

    // Initializing max values to  
    // vertical_min  
    // and horizontal_min 
    let vertical_min = 10000000000
    let horizontal_min = 10000000000


    // If the given rectangle is  
    // already a square  
    if (m === n) {
        return 1
    }

    // If the answer for the given rectangle is  
    // previously calculated return that answer  
    if (dp[m][n] !== 0) {
        return dp[m][n]
    }

    // The rectangle is cut horizontally and  
    // vertically into two parts and the cut  
    // with minimum value is found for every  
    // recursive call.  
    for (let i=1; i<m/2+1; i++) {
        // Calculating the minimum answer for the  
        // rectangles with width equal to n and length  
        // less than m for finding the cut point for  
        // the minimum answer 
        horizontal_min = Math.min(minimumSquare(i, n) + minimumSquare(m-i, n), horizontal_min)

    }

    for (let j=1; j<n/2+1; j++) {

        // Calculating the minimum answer for the  
        // rectangles with width equal to n and length  
        // less than m for finding the cut point for  
        // the minimum answer 
        vertical_min = Math.min(minimumSquare(m, j) + minimumSquare(m, n-j), vertical_min)

    }

    // Minimum of the vertical cut or horizontal 
    // cut to form a square is the answer 
    dp[m][n] = Math.min(vertical_min, horizontal_min) 
    return dp[m][n]

}

// Driver code 
    let m = 30
    let n = 35
    console.log(minimumSquare(m, n))

//This code is contributed by sahilshelangia 

预期结果将是m,n尺寸的矩形中可能的最小正方形数。

例如,对于30x15的矩形,脚本将输出2。

1 个答案:

答案 0 :(得分:1)

我添加了2条新条件来解决此问题。那些是

  • 如果m可以被n整除,则返回值m / n
  • 如果n可被m整除,则返回值n / m

const MAX = 300


let dp = new Array(MAX);

for (let i = 0; i < dp.length; i++) {
  dp[i] = new Array(MAX).fill(0);
}

// Returns min number of squares needed  
function minimumSquare(m, n) {
    // Initializing max values to  
    // vertical_min  
    // and horizontal_min 
    let vertical_min = 10000000000
    let horizontal_min = 10000000000


    // If the given rectangle is  
    // already a square  
    if (m === n) {
        return 1
    }

    // If the answer for the given rectangle is  
    // previously calculated return that answer  
    if (dp[m][n] !== 0) {
        return dp[m][n]
    }
    
    if(m%n==0){//if m is exactly divisible by n
      dp[m][n] = m/n
      return m/n;
    }
    
    if(n%m==0){//if n is exactly divisible by m
      dp[m][n] = n/m
      return n/m;
    }

    // The rectangle is cut horizontally and  
    // vertically into two parts and the cut  
    // with minimum value is found for every  
    // recursive call.  
    for (let i=1; i<m/2+1; i++) {
        // Calculating the minimum answer for the  
        // rectangles with width equal to n and length  
        // less than m for finding the cut point for  
        // the minimum answer 
        horizontal_min = Math.min(minimumSquare(i, n) + minimumSquare(m-i, n), horizontal_min)

    }

    for (let j=1; j<n/2+1; j++) {

        // Calculating the minimum answer for the  
        // rectangles with width equal to n and length  
        // less than m for finding the cut point for  
        // the minimum answer 
        vertical_min = Math.min(minimumSquare(m, j) + minimumSquare(m, n-j), vertical_min)

    }

    // Minimum of the vertical cut or horizontal 
    // cut to form a square is the answer 
    dp[m][n] = Math.min(vertical_min, horizontal_min) 
    return dp[m][n]

}

// Driver code 
    let m = 30
    let n = 35
    console.log(minimumSquare(m, n))