
dp
文章平均质量分 62
markpen
这个作者很懒,什么都没留下…
展开
-
poj 3186 Treats for the Cows
dp dp[i][j]=max(dp[i-1][j]+val[i]*(i+原创 2014-06-12 16:55:21 · 432 阅读 · 0 评论 -
Leetcode Maximal Square
题意:找给定矩形中最大的正方形。 思路:DP,转态方程为:dp[i][j] = min{dp[i-1][j-1], dp[i -1][j], dp[i][j-1]} + 1 class Solution { public: int maximalSquare(vector>& matrix) { vector > maxsq; for(int i = 0原创 2016-12-20 10:38:42 · 211 阅读 · 0 评论 -
Leetcode Decode Ways
题意:A-Z分别与1-26对应,给出一串数字求有多少中匹配的可能性。 思路:DP,记录到目前位置,最多的匹配数。 class Solution { public: int numDecodings(string s) { if(s.length() == 0) return 0; map mm; for(int i = 1; i <= 2原创 2016-12-28 15:25:57 · 244 阅读 · 0 评论 -
Leetcode Unique Paths II
题意:Unique Path的衍生题,加入了障碍物。 思路:DP,将障碍物的dp值置为0。 class Solution { public: int uniquePathsWithObstacles(vector>& obstacleGrid) { vector > dp = obstacleGrid; if(obstacleGrid[原创 2016-12-26 13:19:00 · 221 阅读 · 0 评论 -
Leetcode Edit Distance
题意:求从字符串A到字符串B最小需要的步骤。 思路:经典DP,dp[i][j] 表示A的前i个字符到B的前j个字符所需的最小步骤。 class Solution { public: int minDistance(string word1, string word2) { vector ini(word1.length() + 1, INT_MAX);原创 2016-12-26 09:05:15 · 183 阅读 · 0 评论 -
Leetcode House Robber III
题意:House Robber 的衍生题,将房子的排列由直线改成了二叉树。 思路:DFS,在此基础上运用DP。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(原创 2016-12-26 04:35:06 · 219 阅读 · 0 评论 -
Leetcode Largest Divisible Subset
题意:给定一个集合,找到该集合的最大子集,使得该子集内的所有元素都可以相互整除。 思路:将元素排序,从最大的开始DFS。适当剪枝,当当前的集合已经大于剩下的集合就停止搜索。 class Solution { public: vector tempnums; vector largestDivisibleSubset(vector& nums) { vector原创 2016-12-24 14:50:45 · 238 阅读 · 0 评论 -
Leetcode Triangle
题意:从三角形顶端沿路径向下到底部,求沿途结点之和最小。 思路:简单DP, 取两肩较小者。 class Solution { public: int minimumTotal(vector>& triangle) { if(triangle.size() == 1) return triangle[0][0]; vector > re;原创 2016-12-18 13:00:12 · 176 阅读 · 0 评论 -
Leetcode House Robber II
题意:House Robber 的衍生题,将直线改成环。 思路:遍历两次,一次包含头,一次不包含头。包含头的需要记录是否最终结果包含头。 class Solution { public: int rob(vector& nums) { if(nums.size() == 0) return 0; if(nums.size() == 1) return n原创 2016-12-23 02:38:28 · 198 阅读 · 0 评论 -
Leetcode Longest Palindromic Substring
题意:求最长回文子串。 思路:记录到目前为止,以该字母结尾的所有字串的起始位置,之后的字符遍历这些长度,记录更新后的起始位置。 class Solution { public: string longestPalindrome(string s) { vector > pal; vector temp(1, 0); pal.push_ba原创 2016-12-22 14:25:11 · 201 阅读 · 0 评论 -
Leetcode Maximum Subarray
Leetcode 53 Maximum Subarray原创 2016-08-13 11:35:10 · 209 阅读 · 0 评论 -
Leetcode Best Time to Buy and Sell Stock with Cooldown
题意:给出每个时刻股票的价格,每天只能买入或卖出,买入或卖出后第二天不能交易。 思路:DP,dp[i][j] 表示在第i天买入,第j天卖出的最大利润,转态方程为:dp[i][j] = max(dp[i][j - 1], max(dp[i - 2][k])。 class Solution { public: int maxProfit(vector& prices) {原创 2017-03-02 11:30:07 · 267 阅读 · 0 评论