Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

07 2015 档案

摘要:A trickier binary search one.//////////////class Solution { int lower_bound_vert(vector>& m, int col, int i, int j, int target) { int... 阅读全文
posted @ 2015-07-23 15:06 Tonix 阅读(176) 评论(0) 推荐(0)

摘要:Classic.. just classic. It is a mix of Greedy and DP. The naive DP is, iterate over all [1..Bi] which is O(n^3). However, deeper thought into the prob... 阅读全文
posted @ 2015-07-21 15:40 Tonix 阅读(692) 评论(0) 推荐(0)

摘要:Intuition: 2D DP. Basic idea: compose square at dp[i][j] from dp[i-1][j-1]. You need 2 facility 2D matrix: accumulated horizontal\vertical number of 1... 阅读全文
posted @ 2015-07-21 13:18 Tonix 阅读(170) 评论(0) 推荐(0)

摘要:Interesting one.. It is more about data structure design actually. After you figure out how to represent cells, the DP formula will be very intuitive ... 阅读全文
posted @ 2015-07-20 12:15 Tonix 阅读(353) 评论(0) 推荐(0)

摘要:Monotonic Queue is getting more popular, and finally LeetCode put it on.Typical Solution: element in array will be pushed\popped in\from a sorted data... 阅读全文
posted @ 2015-07-20 11:08 Tonix 阅读(437) 评论(0) 推荐(0)

摘要:Actually I think it should belong to category of 'Bits Manipulation'.. but still, a really good one.My first reaction was, how to derive from a ^ (b #... 阅读全文
posted @ 2015-07-17 14:17 Tonix 阅读(354) 评论(0) 推荐(0)

摘要:Question 1: without division. We can simply compose left\right accumulated product arrays:typedef long long LL;class Solution {public: vector produ... 阅读全文
posted @ 2015-07-16 13:21 Tonix 阅读(185) 评论(0) 推荐(0)

摘要:Brutal-force solution is not hard to think about. But linear space input usually indicates O(n) DP solution.State design: dp[i]: total sum until index... 阅读全文
posted @ 2015-07-15 13:14 Tonix 阅读(391) 评论(0) 推荐(0)

摘要:An interview problem I encountered years ago.. 1ACclass Solution {public: void deleteNode(ListNode* node) { if(!node) return; Lis... 阅读全文
posted @ 2015-07-15 09:10 Tonix 阅读(140) 评论(0) 推荐(0)

摘要:My first thought made it unnecessarily difficult. Well, the correct solution falls into a typical pattern: use DP to calc cumulative results of each c... 阅读全文
posted @ 2015-07-14 05:02 Tonix 阅读(514) 评论(0) 推荐(0)

摘要:First please note, it is BST. So it is about value compare.class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, Tree... 阅读全文
posted @ 2015-07-11 06:27 Tonix 阅读(192) 评论(0) 推荐(0)

摘要:A typical bucketing strategy, but it is probably the most complex one..Basically I refer tothissubmission. 阅读全文
posted @ 2015-07-10 13:16 Tonix 阅读(297) 评论(0) 推荐(0)

摘要:O(n) time O(n) space solution using stack:class Solution {public: bool isPalindrome(ListNode* head) { if (!head || !head->next) return t... 阅读全文
posted @ 2015-07-10 10:13 Tonix 阅读(134) 评论(0) 推荐(0)

摘要:Marked as mathematics problem, but if you are able to divide 1s into several cases, the AC code becomes natural.class Solution { unordered_map memo... 阅读全文
posted @ 2015-07-08 14:45 Tonix 阅读(190) 评论(0) 推荐(0)

摘要:First I was stuck at how to represent state of "add all except i".. but after checking editorial, it is simply inverted Coin Change problem..#include ... 阅读全文
posted @ 2015-07-07 05:38 Tonix 阅读(375) 评论(0) 推荐(0)

摘要:Very similar as "Implement Stack using Queues".class Queue { stack _s0; stack _s1; int _front; public: // Push element x to the back of... 阅读全文
posted @ 2015-07-07 04:45 Tonix 阅读(179) 评论(0) 推荐(0)

摘要:Just one line. Bit-wise ops:class Solution {public: bool isPowerOfTwo(int n) { return (n > 0) && (n & (n - 1)) == 0; }}; 阅读全文
posted @ 2015-07-06 07:35 Tonix 阅读(136) 评论(0) 推荐(0)

摘要:1AC. 1D DP + Sieving#include #include #include #include #include #include #include #include #include #include #include using namespace std;int ways(in... 阅读全文
posted @ 2015-07-03 06:58 Tonix 阅读(209) 评论(0) 推荐(0)

摘要:Good one.. marked as DP, so I was striking to work out a DP formula... well, not all DP have a formula - one type of DP is called memorized search: th... 阅读全文
posted @ 2015-07-02 14:08 Tonix 阅读(829) 评论(0) 推荐(0)

摘要:Simply to utilize BST's property. The two-pass code below can be extended to support frequent insert\delete.struct SNode{ SNode(int rcnt) : cnt(rcn... 阅读全文
posted @ 2015-07-02 09:45 Tonix 阅读(150) 评论(0) 推荐(0)

摘要:First I thought it should be solved using DP, and I gave a standard O(n^2) solution:#include #include #include #include using namespace std;#define RE... 阅读全文
posted @ 2015-07-01 07:10 Tonix 阅读(323) 评论(0) 推荐(0)