![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode Dynamic Programming
Tech In Pieces
这个作者很懒,什么都没留下…
展开
-
动态规划如何输出路径?
动态规划的大部分题目 都是不需要输出具体的路径的,但是万一要输出路径呢?下面来看一下动态规划如何输出路径,以背包问题为例:一般而言,背包问题是要求一个最优值,如果要求输出这个最优值的方案(路径),可以参照一般动态规划问题输出方案的方法:记录下每个状态的最优值是由状态转移方程的哪一项推出来的,换句话说,记录下它是由哪一个策略推出来的。便可根据这条策略找到上一个状态,从上一个状态接着向前推即可。这也就意味着 我们必须要维护一个状态数组 这个数组和DP数组一样,每个元素必须记录着这个状态是由哪个元素转移过原创 2020-12-19 05:22:37 · 2378 阅读 · 1 评论 -
Classic DP Model(经典DP问题)
Linear DP:Number TriangleLIS:LCS/Edit DistanceCondation Machine: stock market 4,5BackpackTree-shape dp: house robberyinterval dp: leetcode1000 minimum cost to merge stonescondition compression dp:原创 2020-12-14 00:21:47 · 623 阅读 · 0 评论 -
LeetCode Coin Change Series
classic dp problem322You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combinat原创 2020-11-26 08:55:36 · 149 阅读 · 0 评论 -
LeetCode 72. Edit Distance
really classic DP:Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2.You have the following three operations permitted on a word:Insert a characterDelete a characterReplace a characterremembe原创 2020-11-25 12:47:16 · 111 阅读 · 0 评论 -
LeetCode 338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.give a num, starting from every number from 0 to num, counting how many 1s in a nu原创 2020-11-25 12:13:14 · 107 阅读 · 0 评论 -
LeetCode 1269. Number of Ways to Stay in the Same Place After Some Steps
You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 position to the left, 1 position to the right in the array or stay in the same place (The pointer should not be placed outside the array at any time).Given two integer原创 2020-11-25 11:50:57 · 184 阅读 · 0 评论 -
LeetCode 1277. Count Square Submatrices with All Ones?
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.which is count the number of square submatrix that are all one.remember, square and all 1s.so let’s say for each 1, it can belongs to multiple submatrix. so we can;原创 2020-11-25 07:24:18 · 153 阅读 · 0 评论 -
LeetCode 1301. Number of Paths with Max Score
[“E23”," 2X2",“12S”]given this 2D array, we will start from letter S, and we need to end in the letter E, and we can only move top, left, and top-left, and there might be some obstacles labeled with X,we need to return two integers:the first will be t原创 2020-11-25 06:10:54 · 113 阅读 · 0 评论 -
LeetCode 1340. Jump Game V
Given an array of integers arr and an integer d. In one step you can jump from index i to index:i + x where: i + x < arr.length and 0 < x <= d.i - x where: i - x >= 0 and 0 < x <= d.In addition, you can only jump from index i to index原创 2020-11-24 12:01:38 · 120 阅读 · 0 评论 -
LeetCode 221. Maximal Square
Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area.idea:since we know that this problem is solved in DP,now let’s try to do that in DP.dp[i][j] =原创 2020-11-24 11:51:58 · 65 阅读 · 0 评论 -
LeetCode 1312. Minimum Insertion Steps to Make a String Palindrome
Given a string s. In one step you can insert any character at any index of the string.Return the minimum number of steps to make s palindrome.原创 2020-11-24 05:23:18 · 207 阅读 · 0 评论 -
LeetCode 712. Minimum ASCII Delete Sum for Two Strings
Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal.kind of like edit distance and the LC583.now, we are not asking to delete as less characters as possible to make them equal. as a matter of fact, consider原创 2020-11-23 06:00:35 · 127 阅读 · 0 评论 -
LeetCode 583. Delete Operation for Two Strings
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.idea:this is the edit distance problem with only delete operation allowed.2D ar原创 2020-11-23 05:15:55 · 105 阅读 · 0 评论 -
LeetCode 1143. Longest Common Subsequence
classic DP problem, LCSGiven two strings text1 and text2, return the length of their longest common subsequence.虽然不记得如何写的了 但是知道是DP 也知道应该用2D dp去记录dp[i][j] represents for the LCS if text1 has a length of i and text2 has a length of j, the LCS of those two原创 2020-11-23 04:51:52 · 84 阅读 · 0 评论 -
LeetCode 1048. Longest String Chain?
we define two words can form a chain: word1 word2, if we can only add one letter to word1 that makes it word2 then they are a chain.so we are given a list of words, Return the longest possible length of a word chain with words chosen from the given list o原创 2020-11-23 04:33:53 · 165 阅读 · 0 评论 -
LeetCode 1024. Video Stitching
given a 2D array and a time Tclips[i][0] represents for the starting time of clip iclips[i][1] represents for the ending time of clip iEach video clip clips[i] is an interval: it starts at time clips[i][0] and ends at time clips[i][1]. We can cut these原创 2020-11-23 02:10:13 · 162 阅读 · 0 评论 -
LeetCode 动态规划类题目以及要注意的点
所谓的动态规划 是一种手段 不像树或者图或者Math之类的题目一样 看到题目就知道这个是属于什么题目 应该用什么方式去做。比如说 树的话 就是递归或者各种遍历图的话就是BFS和DFS方式而动态规划的题目 大多数是给一些数组(一维或者二维)然后让你去求一些最大最小值 或者是能否的问题。什么情况下会考虑用这种方法?当这道题不是一些典型的树 图 链表 Math 之类的题目 也不是一些需要用特殊数据结构比如并查集 PQ, Min Stack去做,而且用馋鬼的一些小技巧也挣不出来的时候 就要考虑这道题目是不原创 2020-11-23 00:27:41 · 125 阅读 · 0 评论 -
LeetCode 5. Longest Palindromic Substring?
Given a string s, return the longest palindromic substring in s.return that longest palindrome.this is not some regular dp problems, if i first saw this problem, there is no way I will think of this a dp problem. because we need to return the actual pali原创 2020-11-22 11:39:34 · 137 阅读 · 0 评论 -
LeetCode 279. Perfect Squares
Given a positive integer n, find the **least number of perfect square numbers (**for example, 1, 4, 9, 16, …) which sum to n.first time you met with this, there is no way that i’m thinking that as something can be solved in DP.dp[i]: least number of squa原创 2020-11-22 10:25:22 · 93 阅读 · 0 评论 -
LeetCode 931/1289. Minimum Falling Path Sum Series
931 还是min path sum的问题 这种问题首先想到DP.首先看一下规则:Given a square array of integers A, we want the minimum sum of a falling path through A.A falling path starts at any element in the first row, and chooses one element from each row. The next row’s choice must be原创 2020-11-22 09:13:30 · 143 阅读 · 0 评论 -
LeetCode 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.input:[[2],[3,4],[6,5,7],[4,1,8,3]]这道题解法比较巧妙 从下往上走 直到汇聚到一个点而且也要明白这个三角形class Solution { public int minimumTotal(List&l原创 2020-11-22 05:05:31 · 100 阅读 · 0 评论 -
LeetCode 62/63. Unique Paths Series
another classic DP problem.2D grid, m*n, each time can only move right or down, how many unique paths from left top to right bottom?dp[i] [j] represents for the number of ways from 0,0 to i,j当然 下面的code的空间复杂度可以优化到O(min(m,n))class Solution { public i原创 2020-11-22 04:34:15 · 104 阅读 · 0 评论 -
LeetCode 91. Decode Ways
Another classic DP problems.Given a non-empty string containing only digits, determine the total number of ways to decode it.1->A2->B…How many ways can we decode a given string?for problem that asked us: how many ways…, instead of print out all原创 2020-11-22 04:00:39 · 73 阅读 · 0 评论 -
LeetCode 303. Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.classic DP problem.dp[i] is the sum from 0 to i exclusivelyvery easy, just be more careful.class NumArray { private int[] sum; p原创 2020-11-22 03:10:06 · 53 阅读 · 0 评论 -
LeetCode 198/213. House Robber Series
1原创 2020-11-22 02:58:03 · 104 阅读 · 0 评论 -
LeetCode 121/122/123/188/309 Best Time to Buy and Sell Stock Series
给出一个数组 代表股票价格1 只允许交易一次2 允许交易任意多次3 最多交易两次4 最多交易k次5 允许交易任意多次 但是每次交易的时间有至少一天的cooldown期1 我们只需要找到一前一后的ij指针 然后j指针减i指针差值最大即可。从前往后遍历 维护一个截止到目前最小的price 维护另一个当前最大的profit/核心代码:for(int price: prices){ min = Math.min(min, price); profit原创 2020-11-21 10:12:01 · 186 阅读 · 0 评论 -
LeetCode 309. Best Time to Buy and Sell Stock with Cooldown
cool down time: at least one day//we use dp, and the following explaination is crystal clear// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/75931/Easiest-JAVA-solution-with-explanationsclass Solution { public i原创 2020-11-21 10:09:33 · 67 阅读 · 0 评论 -
LeetCode 188. Best Time to Buy and Sell Stock IV
最多转k次 求最大利润毫无思路 不妨来看之前的笔记:利用二维DP去做dp[i][j] is defined as maximum profit from at most i transactions using prices[0…j]so what’s the transaction formula?so on day j, we have two options: do nothing, that makes dp[i][j] = dp[i][j-1]or we choose to sell原创 2020-11-21 09:36:54 · 150 阅读 · 0 评论 -
LeetCode 123. Best Time to Buy and Sell Stock III
最多只能交易两次 求最大可以获得利润class Solution { public int maxProfit(int[] prices) { int t1Cost = Integer.MAX_VALUE, t2Cost = Integer.MAX_VALUE; int t1Profit = 0, t2Profit = 0; for (int price : prices) { // the maximum profit if原创 2020-11-21 09:09:06 · 107 阅读 · 0 评论 -
LeetCode 70. Climbing Stairs/746. Min Cost Climbing Stairs
70 You are climbing a staircase. It takes n steps to reach the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?very easy, using recursion:the state transaction forumla is simple:climbStairs(n) = clim原创 2020-11-21 06:39:31 · 132 阅读 · 0 评论