LeetCode- dynamic programming

Dynamic programming is not to remember what dp table was filled, but rather to find subproblems.

53. Maximum Subarray maximum subsequence and

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

Title: Given integer array nums, find the maximum and having a continuous subarray (comprising at least one number) and returns it and the.

Ideas: In the [-2,1, -3,4, -1,2,1, -5,4] of the corresponding dp table, index = 3 corresponding to subproblem from index = 0 to the sub-column index = 3 the largest sub-sequences and how much. For each subproblem, there are two options, one child only select the current last column, one is the maximum value plus the current value before the selection, choose the larger of the two. Indicates that [-2,1, -2,4,3,5,6,1,5] in dp table, the maximum value of 6.

class solution {
     public  int maxSubArray ( int [] nums) {
         int a = Integer.MIN_VALUE;
        int why = 0 ;
        for ( int num núms) { 
            why = Math.max (why + num, num); 
            a = Math.max (the reason); 
        } 
        Return res; 
    } 
}

72. Edit Distance shortest edit distance

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

Title: Given two words Word 1 and Word2, find the word 1 is converted to the minimum number of operations required Word2. Allow for a word in the following three operations: insert characters, delete characters, replace characters.

Ideas:

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 change coin

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

Title: You'll get the total amount of coins and different denominations. Write a function to calculate the minimum number of coins needed to make up this amount of. If this amount of money can not be made any combination of coins, returns -1.

Ideas:

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 and divided into two equal subsets

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

Title: Given an array containing only non-null positive integer, find whether the array may be divided into two subsets, so that the two subsets of elements equal to the sum.

Ideas:

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 gems and stones

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

Title: J represents the type string of gems, string S represents the stone you have. S each character is a kind of stone you have. You want to know how much you belong gem stones. J letters are independent, and J and S of all the characters are letters. A case-sensitive, so "a" is considered different from an "A" stone.

Ideas:

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];
    }
}

Dynamic Programming:
http://oj.leetcode.com/problems/triangle/ (shortest path)
http://oj.leetcode.com/problems/subsets/ (another form)
http://oj.leetcode.com / Problems / Subsets-ii /
//  http://oj.leetcode.com/problems/edit-distance/ (classic)
http://oj.leetcode.com/problems/word-break/
HTTP: // OJ. leetcode.com/problems/word-break-ii/
http://oj.leetcode.com/problems/unique-binary-search-trees/ (dynamic programming to avoid recursion)
http://oj.leetcode.com/problems/ Paths-II-UNIQUE /
http://oj.leetcode.com/problems/scramble-string/
http://oj.leetcode.com/problems/palindrome-partitioning/
http://oj.leetcode.com/problems/ Partitioning-II-Palindrome /
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/

Guess you like

Origin www.cnblogs.com/nomad1c/p/11577086.html