动态规划算法
ojshilu
https://github.com/lucky521
展开
-
无重复货币面值组合所不能表示的最小钱数 DP思想
9OJ编号1531。题目:给出若干种货币的面值,问这些面值组合(不重复)起来所不能表示的最小钱数值(自然数)。例如:输入:1 2 3 9 100输出:7这个题隐含的,如果这些面值里没有1元钱,那最小钱数肯定是1。思路:这种题目一般有两个方向的思路。一是,从目标出发,从小到大判断每个自然数是否满足条件(可以被这些面值组合成)。二者,从已知出发,逐步扩大规模,求出结果。原创 2013-10-04 18:16:33 · 1499 阅读 · 0 评论 -
两个字符串的最小编辑距离 Edit Distance
问题:Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word原创 2014-03-05 14:40:40 · 5547 阅读 · 0 评论 -
通配符匹配字符串 Wildcard Matching
问题:实现支持?和*两个通配符的字符串匹配函数。Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).原创 2014-03-31 22:09:34 · 2384 阅读 · 2 评论 -
Triangle Path 寻找最小和路径
问题:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4],原创 2014-02-26 20:14:01 · 1048 阅读 · 0 评论 -
寻找最少次跳数 - 贪心VS动态规划 Jump Game 2
非常好的问题:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your原创 2014-03-14 12:35:44 · 4383 阅读 · 0 评论 -
交错字符串 Interleaving String
题目源自待字闺中的微信。判断字符串c是否是字符串a和字符串b按顺序的交错(interleave)。关键问题:判断一个字符串是否是另一个字符串的子序列。需要用O(n)时间来解决。int issubsequence(char *m, char *s){ if(m == NULL || s == NULL) return NULL; int i=0,j=0; while(原创 2013-09-17 13:46:10 · 1935 阅读 · 0 评论 -
最小回文分割数 Palindrome Partitioning II
问题:Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab",原创 2014-04-01 21:35:09 · 3425 阅读 · 0 评论 -
有障碍物的矩阵格路径的个数 Unique Paths II
在《矩形格路径的个数 Unique Paths》中,介绍了求解矩阵格路径个数的两种方法,这种属于没有阻碍的路径个数。本文要处理的问题是,如果人为的在矩阵的某些格子中布置障碍物,意味着不能通过该格子。那么格路径的个数如何求解。思路:该问题仍然具有子问题。还是可以采用动态规划方法。设置状态量H[][],H[i][j]表示从A[0][0]格子到A[i][j]格子的路径个数。递推关系:原创 2014-03-20 13:14:01 · 2991 阅读 · 0 评论 -
字符串单词拆分 Word Break
问题:给出一个字符串,问能否将其拆分为1或多个单词。单词表已给出。Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, given原创 2014-03-17 10:07:28 · 3426 阅读 · 0 评论 -
解码方法数 Decode Ways
问题:A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total原创 2014-03-17 21:10:07 · 1760 阅读 · 0 评论 -
字符串中某子序列的个数 Distinct Subsequences
问题:Given a string S and a string T, count the number of distinct subsequences of T in S.要求出S中所有的为T的子序列。思路:一、直观想法:递归试探。指数级复杂度。class Solution {public: int numDistinct(string S, string T)原创 2014-03-11 19:36:55 · 2311 阅读 · 0 评论 -
丑数 Ugly Numbers
问题:Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …shows the first 11 ugly numbers. By convention, 1 is included. Write a program to原创 2013-12-29 16:22:04 · 1584 阅读 · 0 评论 -
矩阵上寻找最短路径 Minimum Path Sum
题目源自于leetcode。图问题。题目:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move ei原创 2013-11-14 12:01:28 · 4704 阅读 · 0 评论 -
寻找最长回文子串 Longest Palindrome DP解法
Longest Palindrome回文基础:如果子串P是回文,那么xPx是回文。如果子串P不是回文,那么xPx不是回文。根据回文基础,我们可以得到动态规划的状态转移函数。设置状态量LP[i][j]来表示任意某个子串a[i]...a[j]是否是回文。那么,1. 初始条件:空串 看作是回文的最初始条件,LP[i][i-1]=1。这作为初始状态,并不认为原创 2013-09-29 11:01:33 · 1501 阅读 · 0 评论