摘要: Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than 1./** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left;... 阅读全文
posted @ 2013-02-28 22:06 一只会思考的猪 阅读(129) 评论(0) 推荐(0) 编辑
摘要: Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at 阅读全文
posted @ 2013-02-28 21:25 一只会思考的猪 阅读(116) 评论(0) 推荐(0) 编辑
摘要: Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.这个问题挺有意思,刚开始想到了DP,和最大递增子列有点像 C[i] = max{0 < j < i C[j 阅读全文
posted @ 2013-02-28 20:43 一只会思考的猪 阅读(119) 评论(0) 推荐(0) 编辑
摘要: LCS问题的递推公式是:C[i,j]->A的前i和元素和B的前j个元素的最大公共子列的长度。C[i,j] = max{C[i-1,j-1] + same{A[i],B[j]}, C[i -1,j], C[i,j-1]}.实际求解的时候,可以用递推的方法,因为子问题大量重合的情况出现[这才是DP的真正含义]。算法复杂度O(MN).回溯输出最长公共子序列过程:算法分析:由于每次调用至少向上或向左(或向上向左同时)移动一步,故最多调用(m + n)次就会遇到i = 0或j = 0的情况,此时开始返回。返回时与递归调用时方向相反,步数相同,故算法时间复杂度为Θ(m + n)。void FindL 阅读全文
posted @ 2013-02-28 00:40 一只会思考的猪 阅读(195) 评论(0) 推荐(0) 编辑