【LEETCODE】6九、動態規劃,easy,medium級別,題目:19八、13九、221

package y2019.Algorithm.dynamicprogramming.easy;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.dynamicprogramming.easy
 * @ClassName: Rob
 * @Author: xiaof
 * @Description: 198. House Robber
 * You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
 *
 * Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
 *
 * Example 1:
 *
 * Input: [1,2,3,1]
 * Output: 4
 * Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
 *              Total amount you can rob = 1 + 3 = 4.
 * Example 2:
 *
 * Input: [2,7,9,3,1]
 * Output: 12
 * Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
 *              Total amount you can rob = 2 + 9 + 1 = 12.
 * @Date: 2019/8/16 8:45
 * @Version: 1.0
 */
public class Rob {

    public int solution(int[] nums) {
        if (nums == null || nums.length <= 0) {
            return 0;
        }
        //這題主要就是要發現規律,那就是選擇盜竊的時候,當前室是否要搶劫進入
        //那麼區別就是rob(i) = max{rob(i-2) + curhouse, rob(i-1)}
        int[] dp = new int[nums.length + 1];
        dp[1] = nums[0];
        for (int i = 2; i < dp.length; ++i) {
            dp[i] = Math.max(dp[i - 2] + nums[i - 1], dp[i - 1]);
        }

        return dp[nums.length];
    }
}

 

 

package y2019.Algorithm.dynamicprogramming.medium;

import java.util.List;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.dynamicprogramming.medium
 * @ClassName: WordBreak
 * @Author: xiaof
 * @Description: 139. Word Break
 * Given a non-empty string s and a dictionary wordDict containing a list of non-empty words,
 * determine if s can be segmented into a space-separated sequence of one or more dictionary words.
 *
 * Note:
 *
 * The same word in the dictionary may be reused multiple times in the segmentation.
 * You may assume the dictionary does not contain duplicate words.
 * Example 1:
 *
 * Input: s = "leetcode", wordDict = ["leet", "code"]
 * Output: true
 * Explanation: Return true because "leetcode" can be segmented as "leet code".
 * Example 2:
 *
 * Input: s = "applepenapple", wordDict = ["apple", "pen"]
 * Output: true
 * Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
 *              Note that you are allowed to reuse a dictionary word.
 * Example 3:
 *
 * Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
 * Output: false
 * @Date: 2019/8/16 8:46
 * @Version: 1.0
 */
public class WordBreak {

    public boolean solution(String s, List<String> wordDict) {
        //咱們把s當作一個地址的字符數組,每次獲取到一個新的字符的時候
        //判斷是否能夠和字典匹配成功,若是成功那麼當前位置的值就是true(任意一個字符串)
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true; //若是字符串長度爲0,那麼默認爲true

        for (int i = 1; i < dp.length; ++i) {
            //遍歷全部的字符,進行比較
            for (String curs : wordDict) {
                //比較當前字符位置
                if (i >= curs.length()) {
                    //前面的字符也要比較確認沒問題才能繼續比較
                    if (dp[i - curs.length()]) {
                        //獲取相同長度的字符進行比較,而後把前面剩下的字符再進行比較
                        String compare = s.substring(i - curs.length(), i);
                        if (compare.equals(curs)) {
                            dp[i] = true;
                            break;
                        }
                    }
                }
            }

        }

        return dp[s.length()];
    }
}

 

package y2019.Algorithm.dynamicprogramming.medium;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.dynamicprogramming.medium
 * @ClassName: MaximalSquare
 * @Author: xiaof
 * @Description: 221. Maximal Square
 * Given a 2D binary matrix filled with 0's and 1's, find the largest square(正方形) containing only 1's and return its area.
 *
 * Example:
 *
 * Input:
 *
 * 1 0 1 0 0
 * 1 0 1 1 1
 * 1 1 1 1 1
 * 1 0 0 1 0
 *
 * Output: 4
 * @Date: 2019/8/16 8:46
 * @Version: 1.0
 */
public class MaximalSquare {

    public int solution(char[][] matrix) {
        if (matrix == null || matrix.length == 0) {
            return 0;
        }
        //這題能夠轉換爲求邊長,只有一個點的上面,左邊,和左上角都是1的時候,才進行長度加1,若是有一個反向的值不爲1,那麼就沒法進行加一
        //這個二維數組用來求邊長
        int[][] dp = new int[matrix.length + 1][matrix[0].length + 1];
        int maxlen = 0;

        for (int i = 1; i < dp.length; ++i) {
            for (int j = 1; j < dp[i].length; ++j) {
                //首先判斷當前位置是否爲空,若是是那麼就判斷邊長可否添加
                if (matrix[i - 1][j - 1] == '1') {
                    dp[i][j] = Math.min(dp[i-1][j], Math.min(dp[i][j-1], dp[i-1][j-1])) + 1;
                    maxlen = Math.max(maxlen, dp[i][j]);
                }
            }
        }

        return maxlen * maxlen;
    }
}
相關文章
相關標籤/搜索