动态规划设计:最长递增子序列 :: labuladong的算法小抄 #998
Replies: 57 comments 20 replies
-
二分杀我 |
Beta Was this translation helpful? Give feedback.
-
咋得到递增子序列呢?这个二分查找最后top数组里面是每堆纸牌最上面的那张牌,牌的堆数就是最长递增子序列长度这个没错,但是好像不太好得到那个最长的递增子序列。求大佬指教。 |
Beta Was this translation helpful? Give feedback.
-
agree that: "正常人基本想不到这种解法" |
Beta Was this translation helpful? Give feedback.
-
二分法没太懂,牌的堆数就是最长递增子序列长度,如果原序列是 [5,4,3,2,1],则牌堆数为1,不等于最长递增子序列长度呀? |
Beta Was this translation helpful? Give feedback.
-
回復@Joycn2018 [5,4,3,2,1] 最长递增子序列长度是1 最长递增子序列的定義是 不能頭尾逆著看。 |
Beta Was this translation helpful? Give feedback.
-
二分法这堆牌让我想起了老windows游戏,“空当接龙”。不过规则略有改变 |
Beta Was this translation helpful? Give feedback.
-
和蜘蛛纸牌也有点像 |
Beta Was this translation helpful? Give feedback.
-
使用数学归纳法的方法, 确定dp代表的含义之后, 在已知dp[:n-1]的情况下, 给出求解dp[n]的方法. 即可迭代的写出动态规划, 这边的action, 貌似并不需要着重考虑 |
Beta Was this translation helpful? Give feedback.
-
for (int i = 0; i < nums.length; i++) { |
Beta Was this translation helpful? Give feedback.
-
东哥,不知道是我个人总有这个问题,还是共性。 |
Beta Was this translation helpful? Give feedback.
-
@MathsGo 感谢你把问题描述地这么清楚,我觉得不用失落,这是一个共有的问题。 首先,自己以为自己会了,并不代表真的会了。所以我反复强调看完我的文章后一定要亲去力扣敲代码,哪怕写的一模一样也得自己敲一遍,而不是仅仅复制粘贴过去提交。更进一步,我在 刷题打卡挑战 里要求读者自己输出一遍解题思路,这种输入 + 输出的方式是更有效的学习方式。 另外,面试不仅仅是技术问题,涉及到很多方面的因素,比如你说的紧张导致大脑空白发挥不好,这些都需要多练习,习惯面试的氛围之后就可以更好地展示自己的实力。 最后,任何问题归根结底还是练的不到位,需要勤加练习,多动脑思考,而不是机械性的重复。祝你早日拿到心仪的 offer。 |
Beta Was this translation helpful? Give feedback.
-
俄罗斯套娃那道题 用DP的解法写LIS过不了 力扣给的测试样例太变态了 超出时间限制了 |
Beta Was this translation helpful? Give feedback.
-
图一dp[2]是不是画错了,按图解应该是dp[4] |
Beta Was this translation helpful? Give feedback.
-
@StrawHat-YYJ 确实画错了,感谢指出,我改下 |
Beta Was this translation helpful? Give feedback.
-
https://leetcode-cn.com/problems/longest-increasing-subsequence/solution/zui-chang-shang-sheng-zi-xu-lie-dong-tai-gui-hua-2/ 这个题解的二分查找方法的理解角度也不错~ |
Beta Was this translation helpful? Give feedback.
-
Hi, thanks for the detailed explanation! This article definitely improves my understanding about dynamic programming (DP) :D
|
Beta Was this translation helpful? Give feedback.
-
为什么信封那个问题,“如果遇到 w 相同的情况,则按照高度 h 降序排序”呢?h 也升序有什么问题吗? |
Beta Was this translation helpful? Give feedback.
-
最长递增自序列 中的 动态规划 解法有一些疑问,希望给位大佬可以帮忙解答一下 按照 明确 base case -> 明确「状态」-> 明确「选择」 -> 定义 dp 数组/函数的含义 这个思维框架来梳理,那么 base case: dp[i] 初始值为1 是这样理解吗? |
Beta Was this translation helpful? Give feedback.
-
最长递增子序列那个二分法中第三个else是为什么,想不明白 |
Beta Was this translation helpful? Give feedback.
-
记录下为什么牌的堆数就是最长递增子序列。 首先,先证明最长递增子序列长度大于等于牌的堆数。每次把牌放到第二堆以后的堆,都一定是因为它在前一堆放不上去,也就是是说大于前一堆的最顶上的牌,因此可以记录个指针,指向放这张牌时的上一堆的最后一张牌,最后从最后一堆的一张牌出发,顺着指针可以一直走到第一堆,把这几张牌挑出来,肯定是按顺序且递增的。因此找到了一个长度为堆数的递增子序列。 假设最长递增子序列的长度大于堆数,那么必有两个牌会在一个牌堆,而且先放下去的底下的牌反而比较小,与规则不符,所以最长递增子序列长度不超过牌堆数。 综上,牌堆数必是最长递增子序列的长度。 |
Beta Was this translation helpful? Give feedback.
-
为什么不可以这样去做呢
|
Beta Was this translation helpful? Give feedback.
-
Top down dp 函数递归解法
|
Beta Was this translation helpful? Give feedback.
-
俄罗斯套娃信封过不了测试用例(c++),原因是排序规则写错了,应该改为: sort(envelopes.begin(), envelopes.end(),
[](vector<int>& a, vector<int>& b) {
return a[0] < b[0] || (a[0] == b[0] && a[1] > b[1]);
}); |
Beta Was this translation helpful? Give feedback.
-
for (int i = 0; i < nums.length; i++) { @labuladong 这里应该是寻找/ 寻找 nums[0..i-1] 中比 nums[i] 小的元素的元素吧。 |
Beta Was this translation helpful? Give feedback.
-
子序列 是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。 |
Beta Was this translation helpful? Give feedback.
-
感觉二分法那里有个地方说的不对:“只能把点数小的牌压到点数比它大的牌上”。事实上可以压在点数相等的牌上,否则俄罗斯套娃的第二个用例三个1会出现三个堆,但东哥的示例代码是正确的,和描述的不一样 |
Beta Was this translation helpful? Give feedback.
-
纯粹用回溯来求解300.最长递增子序列,是不是就没办法优化了?
|
Beta Was this translation helpful? Give feedback.
-
醍醐灌顶,顶礼膜拜,比澳洲某top讲的清楚多了 |
Beta Was this translation helpful? Give feedback.
-
对 300. 最长递增子序列,Youtube 有从“暴力解”开始讲起的题解,分享给其他小伙伴参考: |
Beta Was this translation helpful? Give feedback.
-
二分法那里为什么top[mid]== poker时right = mid呢,还有为什么新建牌堆的时机时left==piles,一直没想明白 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
文章链接点这里:动态规划设计:最长递增子序列
评论礼仪 见这里,违者直接拉黑。
Beta Was this translation helpful? Give feedback.
All reactions