-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
题解 #16
Comments
|
|
|
思路没有什么好说的就是最短路径算法加上边,
|
|
因为是中序序列,方法是只要加进来的比上一个大,就需要把上次小的pop出来因为他已经不参与之后的计算了,同理如果一直都大都不参与计算了,然后pop的时候要计算pop的cost是跟左边乘还是跟右边
|
https://docs.python.org/zh-cn/3/library/collections.html
|
快慢指针
最后一次循环
设在环上走了x |
39 -> 40 -> 77 -> 78 -> 90 -> 216,
77 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
78.Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
216.dfs
|
|
0-1背包问题 |
已知前一步计算时最大相同的前后缀长度为k(k>0),即P[0]···P[k-1]; |
动态规划
983. Minimum Cost For Tickets
935. Knight Dialer
303. Range Sum Query - Immutable
这就是个数学题出的不好,但是以后记得求中间和可以用前面的和减后面和
1021. Best Sightseeing Pair
注意求解问题,可以先转化为部分问题求解,就像求数学题一样
1023.
注意这里iter的迭代情况
873. Length of Longest Fibonacci Subsequence
动态规划双向考虑一下
300 最长递增子序列
我发现上题不用反过来也可以解。
这个可以用一个o(nlogn)的算法,维护一个数组,遍历原数组
2019.4.24日添加:
这类题目是个特点
寻找子数组,子数组满足一些关系就可以抽象成这种题目的思想,如果这种关系是可以传递的** 求长度 ** 比 求数组要简单的多 如果关系不是传递的求长度则要麻烦很多,也是一道典型的dp题,维护一个数组不断求解
同理求解 369
这题也可以归结为300题,不过这题只是其中一个性质才是对的,而且第二个循环正向遍历,不能反向遍历因为这个子序列其中一个一定包括第一个元素,正向一定是对的。并且每个dp元素持有最大的一定是对的。
304. Range Sum Query 2D - Immutable
对于有sqare的题目,使用数组缓存需要+1,一维数组可以用后缀数组求和
二维同样也可以
309. Best Time to Buy and Sell Stock with Cooldown
这题用动态规划是o(n^2)如果不加if条件就会超时,还有种n的方法用的广度优先搜索
357. Count Numbers with Unique Digits
遇到 这种题真的……就是个排列组合的问题
264. Ugly Number II
跟 309的思想差不多,在动态规划中维护了前几个好几个状态
Partition Equal Subset Sum
注意转化问题,subsetSum为整个 array的一半
然后最大的和的dp数组,dp[0]=1 遍历每个数,逆序遍历 dp如果存在 dp[sum+num]=1,为什么要逆序,就是防止这次更改了dp的影响
Can I Win
就是搜索,加memo
Unique Substrings in Wraparound String
像413. Arithmetic Slices
这种dp中安某种顺序序列增长的个数dp问题,一般都为O(n)解法,并且dp[i]= dp[i-1]+1 if some true else 1
所以k一定是在外层循环
同时这是个多源的遍历一遍,把所有节点之间的最短路径都得到了,而diksj那种算法不行。
The text was updated successfully, but these errors were encountered: