279. Perfect Squares

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.
For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.

直接binary search 不对是因为
12 = 4 + 4 + 4
而不等于
12 = 9 + 1 + 1 + 1

Solution1:DP

思路: num最小的一定是历来的结果上再加一个perfect square number(1, 4, 9..),所以dp历来的结果,dp[n] = Min{ dp[n - ii] + 1 }, n - ii >=0 && i >= 1 for all i,找除最小。
Demonstration:

dp[0] = 0 
dp[1] = dp[0]+1 = 1
dp[2] = dp[1]+1 = 2
dp[3] = dp[2]+1 = 3
dp[4] = Min{ dp[4-1*1]+1, dp[4-2*2]+1 } 
      = Min{ dp[3]+1, dp[0]+1 } 
      = 1               
dp[5] = Min{ dp[5-1*1]+1, dp[5-2*2]+1 } 
      = Min{ dp[4]+1, dp[1]+1 } 
      = 2
                        .
                        .
                        .
dp[13] = Min{ dp[13-1*1]+1, dp[13-2*2]+1, dp[13-3*3]+1 } 
       = Min{ dp[12]+1, dp[9]+1, dp[4]+1 } 
       = 2
                        .
                        .
                        .
dp[n] = Min{ dp[n - i*i] + 1 },  n - i*i >=0 && i >= 1

Time Complexity: O(n^1.5) Space Complexity: O(N)

Solution1.5:DP Round1

Time Complexity: O(n^1.5) Space Complexity: O(N)

Solution2:BFS

思路: 在perfect_numbers的基础上 bfsly加上perfect_numbers去找是否等于N,第一个level的结果放入queue继续一下个level找,因为是bfs所以找的话就是least number。建立visited数组避免重复入queue

279. Perfect Squares_第1张图片
屏幕快照 2017-09-23 上午11.50.18.png

Time Complexity: O((N1/2)2 = N) Space Complexity: O(N)

Solution3:Math

思路:
// Based on Lagrange's Four Square theorem, there
// are only 4 possible results: 1, 2, 3, 4.
...
reference: https://leetcode.com/problems/perfect-squares/discuss/
(Temporally skip)
Time Complexity: O(1) Space Complexity: O(1)

Solution1 Code:

class Solution {
    public int numSquares(int n) {
        int[] dp = new int[n + 1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for(int i = 1; i <= n; ++i) {
            int min = Integer.MAX_VALUE;
            int j = 1;
            while(i - j * j >= 0) {
                min = Math.min(min, dp[i - j * j] + 1); // 1 for add one j * j
                ++j;
            }
            dp[i] = min;
        }       
        return dp[n];
    }
}

Solution1.5 Round1 Code:

class Solution {
    public int numSquares(int n) {
        List square_list = new ArrayList<>();
        
        int[] dp = new int[n + 1];
        dp[0] = 0;
        dp[1] = 1;
        square_list.add(1);
        
        for(int i = 2; i <= n; i++) {
            if(isSquareNum(i)) {
                square_list.add(i);
            }
            
            int min_count = Integer.MAX_VALUE;
            for(int square: square_list) {
                int cur_count = 1 + dp[i - square];
                min_count = Math.min(min_count, cur_count);
            }
            dp[i] = min_count; 
        }
        return dp[n];
    }
    
    
    private boolean isSquareNum(int n) {
        int root = (int)Math.sqrt(n);
        if(root * root == n) {
            return true;
        }
        else {
            return false;
        }
    }
}

Solution2 Code:

class Solution {
    public int numSquares(int n) {
        List perfect_squares = new ArrayList<>();
        Queue queue = new LinkedList<>();
        boolean[] visited = new boolean[n];
        int level = 1;
        
        
        for(int i = 1; i * i <= n; i++) {
            if(i * i == n) { // direct case
                return level;
            }
            perfect_squares.add(i * i);
            queue.offer(i * i);
        }
        
        
        while(!queue.isEmpty()) {
            level++;
            int num_on_level = queue.size();
            for(int i = 0; i < num_on_level; i++) {
                int num = queue.poll();
                for(int ps: perfect_squares) {
                    int new_num = num + ps;
                    if(new_num == n) {
                        return level;
                    }
                    else if(new_num < n && !visited[new_num - 1]) {
                        queue.offer(new_num);
                        visited[new_num - 1] = true;
                    }
                    else if(new_num > n){
                        // no need to consider the nodes which are greater than n.
                        break;
                    }
                }
            }
        }
        
        return level;
    }
}

你可能感兴趣的:(279. Perfect Squares)