![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
动态规划
jiyanfeng1
喜欢算法和编程的工科男
展开
-
抵达终点的最小跳数
给定一个非负整数数组,最初你被放在数组的第一个元素上,数组的每个元素代表你能跳的最大的步数。你的目标是用最少的跳数抵达最后一个元素。例如,给定A = [2,3,1,1,4],抵达终点的最小跳数是2(下标0->下标1->下标4)。思路一:table[i]表示从数组下标i的位置抵达最后一个元素的最少跳数。对于在下标i用1跳抵达的地方(最远跳arr[i],最近跳1跳),可以写出状态转原创 2012-10-14 05:21:28 · 2305 阅读 · 0 评论 -
[LeetCode] word break 字符串的划分
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, givens = "leetcode",dict = ["leet"原创 2014-12-02 13:52:53 · 651 阅读 · 0 评论 -
数字三角形III
本问题的相关问题:数字三角形II有一个由正整数组成的三角形,第一行只有一个数,除了最下行之 外每个数的左下方和右下方各有一个数,如下图所示。从第一行的数开始,每次都只能左下或右下走一格,直到走到最下行,把沿途经过的数全部加起来。如何走,使得这个和的个位数尽量大?在本例中,记d[i,j,k]表示以格子(i,j)为根的子三角形是否存在所有数之和个位为k的路径,则d[i,j,k]原创 2013-01-26 12:39:15 · 1137 阅读 · 0 评论 -
[LeetCode] distinct subsequence
Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be non原创 2014-12-05 04:43:54 · 764 阅读 · 0 评论 -
[LeetCode] Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great / \ gr原创 2013-02-28 04:38:33 · 1548 阅读 · 0 评论 -
[LeetCode] Interleaving String 交叉字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return原创 2014-12-07 10:16:21 · 839 阅读 · 0 评论 -
[LeetCode] Number of decoding 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 numb原创 2014-12-08 04:10:38 · 544 阅读 · 0 评论 -
[LeetCode] Minimum Path Sum in Matrix
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 either down or right at原创 2014-12-09 11:07:42 · 523 阅读 · 0 评论 -
Egg Drop 扔鸡蛋
假设有 n=2 个完全一样的鸡蛋, 有 k=36 层楼。 存在一个楼层 m,把鸡蛋从低于m的楼层扔下去的时候,鸡蛋不会摔破;把鸡蛋从高于等于m的楼层扔下去的时候,鸡蛋会摔破。题目要求,找出临界楼层m,并且扔鸡蛋的次数要做到最少。思路:最简单的方法是,从第 1 层往上,一层一层 的扔鸡蛋,直到鸡蛋摔碎。这样需要扔鸡蛋 m 次。下面用递归的方法解决该问题。假设鸡蛋从 x 层扔下去。(1)如原创 2015-08-01 23:44:20 · 2633 阅读 · 1 评论 -
[LeetCode] Best Time to Buy and Sell Stock VI
相关问题:给你一个数组,数组的每个元素表示每天的stock价格,你最多可以进行k次交易,两次交易不能在时间上重叠,也就是说,你必须先卖掉股票,才能再买入股票。求可以获得的最大利润。 思路:利用动态规划。这里我们需要两个递推公式来分别更新两个变量local和global,参见网友Code Ganker的博客,我们其实可以求至少k次交易的最大利润。我们定义local[i][j]为在到达第i原创 2015-08-03 05:16:40 · 558 阅读 · 0 评论 -
找零钱-动态规划
相关问题1:https://blog.csdn.net/jiyanfeng1/article/details/8079530问题:假设有m种面值不同的硬币,个个面值存于数组S ={S1,S2,… Sm}中,现在用这些硬币来找钱,各种硬币的使用个数不限。 求对于给定的钱数N,我们最多有几种不同的找钱方式。硬币的顺序并不重要。例如,对于N = 4,S = {1,2,3},有四种方案:{1,1,...转载 2014-10-28 22:27:15 · 6917 阅读 · 2 评论 -
Closest 3 Sum
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly原创 2013-02-28 04:12:05 · 632 阅读 · 0 评论 -
均匀分割问题 负载均衡问题
给定一个非负正整数数组S={s1, s2, s3, ..., sn},和一个正整数k。要求把S分割成k个部分,使得每个部分的和的最大值是最小的。例如,S={2,2,3,4},k=3. 那么应该分成{2,2},{3},{4}.这个问题转自:http://www8.cs.umu.se/kurser/TDBAfl/VT06/algorithms/BOOK/BOOK2/NODE45.HTM翻译 2012-10-08 13:29:00 · 1126 阅读 · 0 评论 -
最大子段和问题
最大子段和问题(Maximum Interval Sum) 经典的动态规划问题,几乎所有的算法教材都会提到.本文将分析最大子段和问题的几种不同效率的解法,以及最大子段和问题的扩展和运用. 一.问题描述 给定长度为n的整数序列,a[1...n], 求[1,n]某个子区间[i , j]使得a[i]+…+a[j]和最大.或者求出最大的这个和.转载 2012-10-11 00:10:14 · 5470 阅读 · 0 评论 -
最大乘积子序列的值
给一个浮点数序列,取最大乘积子序列的值,例如 -2.5,4,0,3,0.5,8,-1,则取出的最大乘积子序列为3,0.5,8。解法一:用动态规划来做。 假设数组为a[],直接利用动归来求解,考虑到可能存在负数的情况,我们用Max[i]来表示以a[i]结尾的最大连续子序列的乘积值,用Min[i]表示以a[i]结尾的最小的连续子序列的乘积值,那么状态转移方程为: Ma原创 2012-10-17 11:31:53 · 1279 阅读 · 0 评论 -
Optimal BST
Optimal BSTAssume:We have a list of n items: a1, a2, ..., anKey(ak) = akProbability of accessing item ak is known in advance and is P(ak)The list is ordered by keys, a1转载 2012-10-18 06:14:10 · 3503 阅读 · 0 评论 -
找出最长的等差数列
这篇文章的来源:Finding Longest Arithmetic Progressions - by Jeff Erickson - je ffe@uiuc.edu - http://www.uiuc.edu/~je e1 IntroductionThis paper describes efficient algorithms for finding the longest ari翻译 2012-12-17 07:25:16 · 959 阅读 · 0 评论 -
数字三角形II
有一个由正整数组成的三角形,第一行只有一个数,除了最下行之外每个数的左下方和右下方各有一个数,如下图所示。从第一行的数开始,除了某一次可以走到下一行的任意位置外,每次都只能左下或右下走一格,直到走到最下行,把沿途经过的数全部加起来。如何走,使得这个和尽量大?注意:此题说到,除了某一次,意思是,有可能是第一次,还可能是第二次,第三次...。如果没有“除了某一次可以走到下一行的任意原创 2013-01-27 02:01:41 · 1400 阅读 · 0 评论 -
一个数组由2n个整数组成,把这个数组分成两半,各有n个整数,求一个分法,使这两个子数组和的差最小
有两个序列a,b,大小都为n,序列元素的值任意整数,无序;要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小。例如: var a=[100,99,98,1,2, 3];var b=[1, 2, 3, 4,5,40];可以用动态规划来解决这个问题。代码如下:#include #include #include #inclu原创 2013-02-02 06:44:13 · 5701 阅读 · 0 评论 -
最长递增子序列
最长连续递增子序列,请看这里。求最长递增子序列。例如,{1,-1,2,-3,4,-5,6,-7}中,最长的递增子序列为1,2,4,6.利用动态规划的方法,复杂度是O(n^2)。代码如下:#include #include using namespace std;int LIS(int * arr, int len){ int* lis = new int[l原创 2013-01-20 02:44:21 · 678 阅读 · 0 评论 -
寻找最长递增子序列
最长递增子序列(不一定连续),请看这里。给定一个长度为n的数组,求它的最长连续递增子序列。思路:用bruteforce的方法,复杂度是O(n).可是我们可以观察到,如果A[i-1]>A[i],而且到目前为止最长递增子序列长度是L,那么我们可以从A[i+L]向A[i]的方向查找,看是否是递减数列,如果不是那么我们就可以停止查找。这个方法的最差复杂度仍是O(n),但是最好情原创 2012-10-11 00:05:20 · 1018 阅读 · 0 评论 -
[LeetCode] 01矩阵中最大正方形 Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Re原创 2015-08-04 03:48:44 · 8972 阅读 · 0 评论