
算法-DP
sparksnail
这个作者很懒,什么都没留下…
展开
-
POJ 1163 The Triangle
题目总时间限制: 1000ms 内存限制: 65536kB 描述 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5(Figure 1)Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on原创 2017-12-21 14:20:52 · 217 阅读 · 0 评论 -
LintCode 114. Unique Paths
题目思路dp,滚动数组代码class Solution: """ @param m: positive integer (1 <= m <= 100) @param n: positive integer (1 <= n <= 100) @return: An integer """ def uniqu...原创 2018-02-28 20:45:17 · 262 阅读 · 0 评论 -
LintCode 111. Climbing Stairs
题目思路dp代码class Solution: """ @param n: An integer @return: An integer """ def climbStairs(self, n): # write your code here dp = [0, 1, 2]; for i ...原创 2018-02-28 21:04:12 · 184 阅读 · 0 评论 -
LintCode 116. Jump Game
题目思路dp,不断更新数组的状态。代码class Solution: """ @param: A: A list of integers @return: A boolean """ def canJump(self, A): # write your code here dp = [False f...原创 2018-02-28 22:04:45 · 281 阅读 · 0 评论 -
LintCode 76. Longest Increasing Subsequence
题目思路dp[i]表示以下标i结尾的数字,LIS长度。代码class Solution: """ @param nums: An integer array @return: The length of LIS (longest increasing subsequence) """ def longestIncreasingSu...原创 2018-02-28 22:40:17 · 253 阅读 · 0 评论 -
LeetCode 140. Word Break II
题目思路DP+DFS 用dp[i]表示下标为i之前的字符都匹配上了,用来作为剪枝条件。代码class Solution(object): def __init__(self): self.res_list = [] def dfs(self, s, wordDict, startIndex, word_list, dp): if...原创 2018-04-01 01:17:53 · 143 阅读 · 0 评论 -
LeetCode 139. Word Break
题目思路DP。用dp[i]表示下标为i之前的字符可以切分。代码class Solution(object): def wordBreak(self, s, wordDict): """ :type s: str :type wordDict: List[str] :rtype: bool ...原创 2018-04-01 01:20:27 · 176 阅读 · 0 评论 -
LeetCode 120. Triangle
题目思路自底向上遍历,滚动数组。代码class Solution: def minimumTotal(self, triangle): """ :type triangle: List[List[int]] :rtype: int """ dp = [i for i in trian...原创 2018-04-08 23:43:19 · 135 阅读 · 0 评论 -
LeetCode 135. Candy
题目思路双向dp。数组分为两种情况,递增和递减。第一次从前向后遍历,处理递增的情况。第二次从后向前遍历,处理递减的情况。代码class Solution(object): def candy(self, ratings): """ :type ratings: List[int] :rtype: int ...原创 2018-04-01 23:23:58 · 158 阅读 · 0 评论 -
LeetCode 53. Maximum Subarray
题目Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] ...原创 2018-04-02 09:13:36 · 123 阅读 · 0 评论 -
LeetCode 131. Palindrome Partitioning
题目思路dfs+dp。 判断一个字符串的所有子串是否为回文,可以用dp来加快计算。如果s[i]==s[j],dp[i][j]=dp[i+1][j−1]s[i]==s[j],dp[i][j]=dp[i+1][j−1]s[i]==s[j],dp[i][j]=dp[i+1][j-1]代码class Solution(object): def __init__(sel...原创 2018-04-02 15:09:10 · 134 阅读 · 0 评论 -
LeetCode 5. Longest Palindromic Substring
题目思路dp,构造上三角矩阵。代码class Solution: def longestPalindrome(self, s): """ :type s: str :rtype: str """ if not s: return null length = len(s...原创 2018-04-15 02:01:12 · 135 阅读 · 0 评论 -
剑指offer 变态跳台阶
题目一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。思路dp代码# -*- coding:utf-8 -*-class Solution: def jumpFloorII(self, number): # write code here dp = [1 for i i...原创 2018-06-13 21:03:29 · 120 阅读 · 0 评论 -
LintCode 110. Minimum Path Sum
题目思路dp,滚动数组代码class Solution: """ @param grid: a list of lists of integers @return: An integer, minimizes the sum of all numbers along its path """ def minPathSum(self, ...原创 2018-02-28 20:37:43 · 241 阅读 · 0 评论 -
LintCode 109. Triangle
题目思路滚动数组代码class Solution: """ @param triangle: a list of lists of integers @return: An integer, minimum path sum """ def minimumTotal(self, triangle): # write...原创 2018-02-28 19:16:29 · 229 阅读 · 0 评论 -
nowcoder 01背包
题目一个背包有一定的承重cap,有N件物品,每件都有自己的价值,记录在数组v中,也都有自己的重量,记录在数组w中,每件物品只能选择要装入背包还是不装入背包,要求在不超过背包承重的前提下,选出物品的总价值最大。 给定物品的重量w价值v及物品数n和承重cap。请返回最大总价值。 测试样例: [1,2,3],[1,2,3],3,6 返回:6思路f(i,j)=max{f(i−1,j−v[i])+p[原创 2017-12-28 13:38:13 · 255 阅读 · 0 评论 -
动态规划
定义将一个问题分解为子问题递归求解,并将中间结果保存以避免重复计算的办法,可以称为“动态规划”。性质原创 2017-12-21 14:30:49 · 461 阅读 · 0 评论 -
POJ 2533 Longest Ordered Subsequence
题目总时间限制: 2000ms 内存限制: 65536kB 描述 A numeric sequence of ai is ordered if a1 < a2 < … < aN. Let the subsequence of the given numeric sequence (a1, a2, …, aN) be any sequence (ai1, ai2, …, aiK), where原创 2017-12-22 11:14:25 · 221 阅读 · 0 评论 -
POJ 1458 Common Subsequence
题目总时间限制: 1000ms 内存限制: 65536kB 描述 A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, …, xm > another sequence Z = < z1原创 2017-12-22 12:06:20 · 246 阅读 · 0 评论 -
POJ 4131 Charm Bracelet
题目总时间限制: 1000ms 内存限制: 65536kB 描述 Bessie has gone to the mall’s jewelry store and spies a charm bracelet. Of course, she’d like to fill it with the best charms possible from the N(1 ≤ N≤ 3,402) avail原创 2017-12-23 14:33:31 · 380 阅读 · 0 评论 -
POJ 1088 滑雪
题目总时间限制: 1000ms 内存限制: 65536kB 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长的滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子 1 2 3 4 5 16 17 18 19 6原创 2017-12-23 15:55:39 · 205 阅读 · 0 评论 -
POJ 4117 简单的整数划分问题
题目总时间限制: 100ms 内存限制: 65536kB 描述 将正整数n 表示成一系列正整数之和,n=n1+n2+…+nk, 其中n1>=n2>=…>=nk>=1 ,k>=1 。 正整数n 的这种表示称为正整数n 的划分。正整数n 的不同的划分个数称为正整数n 的划分数。 输入 标准的输入包含若干组测试数据。每组测试数据是一个整数N(0 < N <= 50)。 输出 对于每组测试数原创 2017-12-27 20:22:17 · 1210 阅读 · 0 评论 -
POJ 4118 开餐馆
题目总时间限制: 1000ms 内存限制: 65536kB 描述 北大信息学院的同学小明毕业之后打算创业开餐馆.现在共有n 个地点可供选择。小明打算从中选择合适的位置开设一些餐馆。这 n 个地点排列在同一条直线上。我们用一个整数序列m1, m2, … mn 来表示他们的相对位置。由于地段关系,开餐馆的利润会有所不同。我们用pi 表示在mi 处开餐馆的利润。为了避免自己的餐馆的内部竞争,餐馆之间原创 2017-12-27 21:13:39 · 782 阅读 · 0 评论 -
nowcoder 找零钱
问题有数组penny,penny中所有的值都为正数且不重复。每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个整数aim(小于等于1000)代表要找的钱数,求换钱有多少种方法。 给定数组penny及它的大小(小于等于50),同时给定一个整数aim,请返回有多少种方法可以凑成aim。 测试样例: [1,2,4],3,3 返回:2思路dp[i][j]dp[i][j]表示使用前ii原创 2017-12-27 22:56:25 · 185 阅读 · 0 评论 -
nowcoder 台阶问题
问题有n级台阶,一个人每次上一级或者两级,问有多少种走完n级台阶的方法。为了防止溢出,请将结果Mod 1000000007 给定一个正整数int n,请返回一个数,代表上楼的方式数。保证n小于等于100000。 测试样例: 1 返回:1思路水题。代码class GoUpstairs: def countWays(self, n): # write code here原创 2017-12-27 23:29:22 · 174 阅读 · 0 评论 -
nowcoder 矩阵最小路径和
题目有一个矩阵map,它每个格子有一个权值。从左上角的格子开始每次只能向右或者向下走,最后到达右下角的位置,路径上所有的数字累加起来就是路径和,返回所有的路径中最小的路径和。 给定一个矩阵map及它的行数n和列数m,请返回最小路径和。保证行列数均小于等于100. 测试样例: [[1,2,3],[1,1,1]],2,3 返回:4思路动态规划。滚动数组。水题。代码class MinimumPat原创 2017-12-27 23:55:38 · 216 阅读 · 0 评论 -
nowcoder LIS
题目这是一个经典的LIS(即最长上升子序列)问题,请设计一个尽量优的解法求出序列的最长上升子序列的长度。 给定一个序列A及它的长度n(长度小于等于500),请返回LIS的长度。 测试样例: [1,4,2,5,3],5 返回:3思路dp[i]=max{dp[j]+1},j=1,2,3...idp[i]=max\{dp[j]+1\},j=1,2,3...i代码class LongestIncre原创 2017-12-28 00:03:11 · 205 阅读 · 0 评论 -
nowcoder LCS
题目给定两个字符串A和B,返回两个字符串的最长公共子序列的长度。例如,A=”1A2C3D4B56”,B=”B1D23CA45B6A”,”123456”或者”12C4B6”都是最长公共子序列。 给定两个字符串A和B,同时给定两个串的长度n和m,请返回最长公共子序列的长度。保证两串长度均小于等于300。 测试样例: “1A2C3D4B56”,10,”B1D23CA45B6A”,12 返回:6思路原创 2017-12-28 00:23:46 · 172 阅读 · 0 评论 -
LeetCode 91. Decode Ways
题目思路DP代码class Solution: def numDecodings(self, s): """ :type s: str :rtype: int """ dp = [0 for i in range(len(s) + 1)] dp[0] = 1 ...原创 2018-06-21 22:01:16 · 123 阅读 · 0 评论