摘要: 如果相邻两个字符在同一个substring里,那么第一个字符在source的index要小于第二个字符。 解法1: 使用dp[i]记录[x,y]. x: shortest way to form target[0:i]; y: current index of target[i-1] in sour 阅读全文
posted @ 2020-01-07 14:04 热锅上的码裔 阅读(220) 评论(0) 推荐(0)
摘要: 代码如下: class UF(object): def __init__(self,email_lists): self.id={} self.sz={} self.name={} self.emails={} for email,name in email_lists.items(): self. 阅读全文
posted @ 2019-11-14 14:49 热锅上的码裔 阅读(198) 评论(0) 推荐(0)
摘要: Description Find K-th largest element in N arrays. You can swap elements in the array Example Example 1: Input: k=3, [[9,3,2,4,7],[1,2,3,4,8]] Output: 阅读全文
posted @ 2019-11-06 05:34 热锅上的码裔 阅读(145) 评论(0) 推荐(0)
摘要: Description Given an array of integers, how many three numbers can be found in the array, so that we can build an triangle whose three edges length is 阅读全文
posted @ 2019-11-05 13:19 热锅上的码裔 阅读(122) 评论(0) 推荐(0)
摘要: Description Given an array of integers, find how many pairs in the array such that their sum is less than or equal to a specific target number. Please 阅读全文
posted @ 2019-11-05 13:15 热锅上的码裔 阅读(220) 评论(0) 推荐(0)
摘要: 一开始想的是设所求数为x,那么遍历所有可能路径,看最小血量花销。 后来想想对矩阵一顿操作后,返回一个值,而非所有路径的具体值,很有可能是DP。于是往DP的方向想,我们能确定的是救到公主后,骑士的血量至少为1,然后求起始位置是骑士的最低血量可以是多少。这样一想,自然是从右下往左上递推。 1、我们建立d 阅读全文
posted @ 2019-10-26 13:02 热锅上的码裔 阅读(88) 评论(0) 推荐(0)
摘要: 一开始拿到这道题,我就想着递归,思路如下: left=maxPathSum(root.left) right=maxPathSum(root.right) 但是怎么把左右两边递归的值联系起来呢?如果能求出每个从左到右经过root的path的和,那么,再跟左右递归的结果一比较就可以求得最大值。 根据以 阅读全文
posted @ 2019-10-22 06:08 热锅上的码裔 阅读(132) 评论(0) 推荐(0)
摘要: 蠡口116的延续。这道题想了半天想不出递归地做法。于是想到了层次遍历,可以循环地做以下操作: 1)把每层的数据从左到右地放在一个队列里,pop出左边的元素,设为pre; 2)若元素是该层最右非空元素,那不需要做任何操作;否则把pop之后的队首元素作为pre的next; 3)把pre的左右非空孩子依次 阅读全文
posted @ 2019-10-20 02:21 热锅上的码裔 阅读(142) 评论(0) 推荐(0)
摘要: 遇到树形结构,首先想到的就是递归。本题使用递归。要对所有节点添加next指针,我们可以分两步完成:1、左子树每层最右 -> 右子树每层最左;2、左右子树递归地调研函数来添加(如下图所示)。两者顺序可以颠倒,但是两个都是必要的,如果函数里只写left.next=right就进行递归,那么遍历的时候左右 阅读全文
posted @ 2019-10-20 00:26 热锅上的码裔 阅读(218) 评论(0) 推荐(0)
摘要: 又是要对两个字符串一顿操作,返回T or F,典型的dp。 1、建立dp矩阵,确定状态变量:dp[i][j]表示the number of distinct subsequences of s[0:i] which equals t[0:j],就是s[0:i]与t[0:j]带入函数所得值,初值都设为 阅读全文
posted @ 2019-10-19 13:25 热锅上的码裔 阅读(118) 评论(0) 推荐(0)