摘要: Recursive (Backtracking)This is a typical problem that can be tackled by backtracking.Since backtracking has a more-or-less similar template, so I do ... 阅读全文
posted @ 2015-06-03 00:19 jianchao-li 阅读(218) 评论(0) 推荐(0) 编辑
摘要: This problem is similar toBest Time to Buy and Sell Stock. Givenprices, we find the day (denoted asbuy) of the first local minimum and the day (denote... 阅读全文
posted @ 2015-06-03 00:16 jianchao-li 阅读(179) 评论(0) 推荐(0) 编辑
摘要: This post shares a very simple solution, whose code is rewritten below, just 5 lines :-) 1 class Solution { 2 public: 3 int maxProfit(vector& pric... 阅读全文
posted @ 2015-06-03 00:14 jianchao-li 阅读(145) 评论(0) 推荐(0) 编辑
摘要: This problem seems to be tricky at first glance. However, if you know Morris traversal, it is just the preorder case of Morris traversal and the code ... 阅读全文
posted @ 2015-06-02 23:53 jianchao-li 阅读(185) 评论(0) 推荐(0) 编辑
摘要: There are many merge-sort solutions at the forum, but very few quicksort solutions. So I post my accepted quicksort solution here.Well, after reading ... 阅读全文
posted @ 2015-06-02 23:47 jianchao-li 阅读(201) 评论(0) 推荐(0) 编辑
摘要: This is a typical DP problem. Suppose the minimum path sum of arriving at point(i, j)isS[i][j], then the state equation isS[i][j] = min(S[i - 1][j], S... 阅读全文
posted @ 2015-06-02 23:42 jianchao-li 阅读(223) 评论(0) 推荐(0) 编辑
摘要: Well, this problem is similar toUnique Paths. The introduction of obstacles only changes the boundary conditions and make some points unreachable (sim... 阅读全文
posted @ 2015-06-02 23:41 jianchao-li 阅读(153) 评论(0) 推荐(0) 编辑
摘要: This is a fundamental DP problem. First of all, let's make some observations.Since the robot can only move right and down, when it arrives at a point,... 阅读全文
posted @ 2015-06-02 23:40 jianchao-li 阅读(203) 评论(0) 推荐(0) 编辑
摘要: Well, this problem has a naive solution, which is to sort the array in descending order and return thek-1-th element. However, sorting algorithm gives... 阅读全文
posted @ 2015-06-02 23:39 jianchao-li 阅读(366) 评论(0) 推荐(0) 编辑
摘要: To be honest, this problem is designed to let you use stacks. However, I don't. In fact, you only need to keep a flagand switch it between falseandtru... 阅读全文
posted @ 2015-06-02 23:36 jianchao-li 阅读(173) 评论(0) 推荐(0) 编辑
摘要: Well, I do not see what this problem is for. The same code of Binary Tree Level Order Traversal can be used here. The only difference is that we shoul... 阅读全文
posted @ 2015-06-02 23:31 jianchao-li 阅读(174) 评论(0) 推荐(0) 编辑
摘要: A classic tree traversal problem. I share my two solutions here: BFS and DFS.BFS: 1 vector> levelOrder(TreeNode *root) { 2 vector> levels;... 阅读全文
posted @ 2015-06-02 23:29 jianchao-li 阅读(175) 评论(0) 推荐(0) 编辑
摘要: This is a fundamental and yet classic problem. I share my three solutions here:Iterative solution using stack ---O(n)time andO(n)space;Recursive solut... 阅读全文
posted @ 2015-06-02 23:27 jianchao-li 阅读(248) 评论(0) 推荐(0) 编辑
摘要: This is a fundamental and yet classic problem. I share my three solutions here:Iterative solution using stack ---O(n)time andO(n)space;Recursive solut... 阅读全文
posted @ 2015-06-02 23:22 jianchao-li 阅读(256) 评论(0) 推荐(0) 编辑
摘要: This is a fundamental and yet classic problem. I share my three solutions here:Iterative solution using stack ---O(n)time andO(n)space;Recursive solut... 阅读全文
posted @ 2015-06-02 23:20 jianchao-li 阅读(248) 评论(0) 推荐(0) 编辑