LeetCode-动态规划

动态规划不在于记住dp table里填什么,而在于找到subproblems。

53. Maximum Subarray 最大子序列和

https://leetcode.com/problems/maximum-subarray/

题目:给定整数数组nums,查找具有最大和的连续子数组(至少包含一个数字)并返回其和。

思路:在[-2,1,-3,4,-1,2,1,-5,4]对应的dp table中,index=3对应的subproblem是从index=0到index=3这个子列的最大子序列和是多少。对于每个subproblem,有两种选择,一种仅选择当前子列的最后一位,一种是选择之前的最大值加上当前值,选择二者中较大的那个。在dp table中表示就是[-2,1,-2,4,3,5,6,1,5],最大值为6。

class Solution {
    public int maxSubArray(int[] nums) {
        int res = Integer.MIN_VALUE;
        int cur = 0;
        for(int num : nums){
            cur = Math.max(cur+num, num);
            res = Math.max(res, cur);
        }
        return res;
    }
}

72. Edit Distance 最短编辑距离

https://leetcode.com/problems/edit-distance/

题目:给定两个单词Word 1和Word2,找到将word 1转换为Word2所需的最小操作数。允许对一个单词执行以下3种操作:插入字符、删除字符、替换字符。

思路:

class Solution {
    public int minDistance(String word1, String word2) {
        int a = word1.length();
        int b = word2.length();
        int[][] dp = new int[b+1][a+1];
        dp[0][0] = 0;
        for(int i = 1; i < a+1; i++) {
            dp[0][i] = i;
        }
        for(int i = 1; i < b+1; i++) {
            dp[i][0] = i;
        }
        for(int j = 1; j < b+1; j++) {
            for(int i = 1; i < a+1; i++) {
                if(word1.charAt(i-1) == word2.charAt(j-1)){
                    dp[j][i] = dp[j-1][i-1];
                } else {
                    int temp = Math.min(dp[j-1][i], dp[j-1][i-1]);
                    dp[j][i] = Math.min(temp, dp[j][i-1]) + 1;
                }
            }
        }
        return dp[b][a];
    }
}

322. Coin Change 换硬币

https://leetcode.com/problems/coin-change/

题目:你会得到不同面额的硬币和总金额。编写一个函数来计算弥补这个数量所需的最少硬币数。如果这个数额的钱不能由任何组合的硬币,返回-1。

思路:

class Solution {
    public int coinChange(int[] coins, int amount) {
        int n = coins.length;
        int[] dp = new int[amount+1];
        for(int i = 1; i < amount+1; i++) {
            dp[i] = amount+1;
        }
        dp[0] = 0;
        for (int i = 1; i <= amount; i++) {
            int temp = i;
            int cur = 0;
            for(int j = 0; j < n; j++) {
                if(coins[j] > temp) continue;
                cur = 1 + dp[temp-coins[j]];
                dp[i] = Math.min(dp[i], cur);
            }
        }
        if(dp[amount] < amount+1) {
            return dp[amount];
        } else {
            return -1;
        }
    }
}

416. Partition Equal Subset Sum 分成和相等的两个子集

https://leetcode.com/problems/partition-equal-subset-sum/

题目:给定一个只包含正整数的非空数组,请查找该数组是否可以划分为两个子集,以便两个子集中的元素之和相等。

思路:

class Solution {
    public boolean canPartition(int[] nums) {
        int n = nums.length;
        int total = 0, target = 0;
        for(int i = 0; i < n; i++) {
            total += nums[i];
        }
        if(total % 2 == 1) return false;
        target = total / 2;
        boolean[] dp = new boolean[target+1];
        for(int i = 0; i < target+1; i++) {
            dp[i] = false;
        }
        dp[0] = true;
        for(int i = 0; i < n; i++) {
            for(int j = target; j >= nums[i]; j--) {
                dp[j] = dp[j] | dp[j-nums[i]];
            }
        }
        return dp[target];
    }
}

771. Jewels and Stones 宝石和石头

https://leetcode.com/problems/jewels-and-stones/

题目:字符串J代表宝石的类型,字符串S代表你拥有的石头。S中的每一个字符都是你所拥有的一种石头。你想知道你有多少石头属于宝石。J中的字母是独立的,而J和S中的所有字符都是字母。字母区分大小写,因此“a”被认为是一种不同于“A”的石头。

思路:

class Solution {
    public int numJewelsInStones(String J, String S) {
        int jl = J.length();
        int sl = S.length();
        int[][] dp = new int[jl+1][sl+1];
        for(int i = 0; i < jl+1; i++) {
            dp[i][0] = 0;
        }
        for(int i = 0; i < sl+1; i++) {
            dp[0][i] = 0;
        }
        for(int i = 1; i < jl+1; i++) {
            int temp = 0;
            for(int j = 1; j < sl+1; j++) {
                if(S.charAt(j-1)==J.charAt(i-1)) temp++;
                dp[i][j] = temp + dp[i-1][j];
            }
        }
        return dp[jl][sl];
    }
}

动态规划:
http://oj.leetcode.com/problems/triangle/ (最短路径)
http://oj.leetcode.com/problems/subsets/ (另一种形式)
http://oj.leetcode.com/problems/subsets-ii/
// http://oj.leetcode.com/problems/edit-distance/ (经典)
http://oj.leetcode.com/problems/word-break/
http://oj.leetcode.com/problems/word-break-ii/
http://oj.leetcode.com/problems/unique-binary-search-trees/ (动态规划避免递归)
http://oj.leetcode.com/problems/unique-paths-ii/
http://oj.leetcode.com/problems/scramble-string/
http://oj.leetcode.com/problems/palindrome-partitioning/
http://oj.leetcode.com/problems/palindrome-partitioning-ii/
http://oj.leetcode.com/problems/interleaving-string/
http://oj.leetcode.com/problems/distinct-subsequences/
http://oj.leetcode.com/problems/decode-ways/
http://oj.leetcode.com/problems/gray-code/
http://oj.leetcode.com/problems/minimum-path-sum/

猜你喜欢

转载自www.cnblogs.com/nomad1c/p/11577086.html