JavaScript solution


  • 0
    L
    function numSquares(n) {
      var map = [0]
      for (var i = 1; i <= n; i++) {
        var j = 1
        var min = i
        while (j * j <= i) {
          min = Math.min(min, 1 + map[i - j * j])
          j++
        }
        map[i] = min
      }
      return map[n]
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.