Tony's Log

Algorithms, Distributed System, Machine Learning

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

10 2015 档案

摘要:Great great DP learning experience:http://www.cnblogs.com/yuzhangcmu/p/4279676.htmlRemember 2 steps of DP: a) setup state transfer equation; b) setup ... 阅读全文
posted @ 2015-10-30 01:22 Tonix 阅读(181) 评论(0) 推荐(0)

摘要:A simple application to binary tree traversal.class Solution { typedef pair Rec; // last val - length unsigned ret; void go(TreeNode *p, Rec ... 阅读全文
posted @ 2015-10-28 11:47 Tonix 阅读(164) 评论(0) 推荐(0)

摘要:Based on Bucketing and "Majority Number I".class Solution { pair majorityNumber0(vector &num) { int count = 0; int ret = 0; fo... 阅读全文
posted @ 2015-10-28 06:10 Tonix 阅读(199) 评论(0) 推荐(0)

摘要:A reverse version of the Dictionary algorithm :) If you AC-ed "Next Permutation II", copy it over and just reverse the conditions.class Solution {publ... 阅读全文
posted @ 2015-10-28 05:25 Tonix 阅读(166) 评论(0) 推荐(0)

摘要:Such a classic DP.. Thought flow:1. find out the 2 vars in the problem statement: n and k2. setup dp equation - my first thought was by n which was no... 阅读全文
posted @ 2015-10-28 04:27 Tonix 阅读(174) 评论(0) 推荐(0)

摘要:4 Pointer solution. Key: when moving pointers, we skip duplicated ones.Ref: https://github.com/xbz/lintcode/blob/master/4_sum/4sum.cppclass Solution {... 阅读全文
posted @ 2015-10-24 09:17 Tonix 阅读(150) 评论(0) 推荐(0)

摘要:Interesting one.. I can feel sparkle in mind in the proposed solution - just pick the mid-point! (yes some boiler-plate code below)class Solution { ... 阅读全文
posted @ 2015-10-23 13:19 Tonix 阅读(191) 评论(0) 推荐(0)

摘要:A typical Union-Find one. I'm using a kinda Union-Find solution here. Some boiler-plate code - yeah I know.class Solution { unordered_set hs; // st... 阅读全文
posted @ 2015-10-22 13:15 Tonix 阅读(219) 评论(0) 推荐(0)

摘要:New stuff learnt - Union-Find. Simpler and more elegant than I thought.class Solution { unordered_map father; int find(int val) { if(!... 阅读全文
posted @ 2015-10-21 13:09 Tonix 阅读(197) 评论(0) 推荐(0)

摘要:Should be "Medium" or even "Easy".. Just with a little Greedy.class Solution {public: /** * @param S: A list of integers * @return: An inte... 阅读全文
posted @ 2015-10-21 07:53 Tonix 阅读(258) 评论(0) 推荐(0)

摘要:Regular segment tree usage. Should be medium though.struct Node{ Node(int rs, int re) : s(rs), e(re), left(nullptr), right(nullptr){} int s;... 阅读全文
posted @ 2015-10-20 14:40 Tonix 阅读(193) 评论(0) 推荐(0)

摘要:Similar one as LintCode "Data Stream Median".class MedianFinder { priority_queue hl; // max-heap priority_queue, std::greater > hr; // min-heapp... 阅读全文
posted @ 2015-10-20 14:05 Tonix 阅读(152) 评论(0) 推荐(0)

摘要:Lesson learnt: for any calculator problems, keep 2 stacks: 1 for operators and 1 for operands.class Solution { stack op; stack data; void... 阅读全文
posted @ 2015-10-19 14:28 Tonix 阅读(181) 评论(0) 推荐(0)

摘要:Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you use a balanced binary search tree, you can get O(n... 阅读全文
posted @ 2015-10-19 04:37 Tonix 阅读(129) 评论(0) 推荐(0)

摘要:A variantion to "largest\smallest consecutive subarray". Idea is, at position i, the current max diff is max(max_left[i] - min_right[i+1], max_right[i... 阅读全文
posted @ 2015-10-18 12:57 Tonix 阅读(151) 评论(0) 推荐(0)

摘要:A natural recursion thought.. Please note we can cache intermediate results. class Solution { unordered_map<string, bool> hs; public: bool canWin(stri 阅读全文
posted @ 2015-10-16 11:23 Tonix 阅读(151) 评论(0) 推荐(0)

摘要:Besides heap, multiset can also be used:class Solution { void removeOnly1(multiset &ms, int v) { auto pr = ms.equal_range(v); ms.e... 阅读全文
posted @ 2015-10-15 14:37 Tonix 阅读(138) 评论(0) 推荐(0)

摘要:Recursive counting..class Solution { long long w(int n) { long long ret = 1; while(n > 1) { ret *= n; ... 阅读全文
posted @ 2015-10-14 06:55 Tonix 阅读(239) 评论(0) 推荐(0)

摘要:Warning: input could be > 10000...Solution by segment tree:struct Node{ Node(int s, int e) : start(s), end(e), cnt(0), left(nullptr), right(nullptr... 阅读全文
posted @ 2015-10-13 13:57 Tonix 阅读(155) 评论(0) 推荐(0)

摘要:Same as LintCode 'Coins in a line'. Here is mind-flow: 4 is a definite lose, for (4 + 1), (4 + 2), (4 + 3), we can put the other player into slot 4 - ... 阅读全文
posted @ 2015-10-13 05:34 Tonix 阅读(183) 评论(0) 推荐(0)

摘要:O(nlgn) - my first thought is, keep maintaining a balanced binary search tree, and then use "Binary Search Tree Closest" one to solve it. But unfortun... 阅读全文
posted @ 2015-10-12 02:32 Tonix 阅读(161) 评论(0) 推荐(0)

摘要:Here is the mind flow: we don't know 1st token right? then we try it one by one - recursively.typedef unordered_map HMap;class Solution { bool go(s... 阅读全文
posted @ 2015-10-10 14:42 Tonix 阅读(191) 评论(0) 推荐(0)

摘要:2D DP. DP is all about choices - current choice with previous choice :) A natural solution as below, and of course we can simply use rolling array for... 阅读全文
posted @ 2015-10-10 07:34 Tonix 阅读(163) 评论(0) 推荐(0)

摘要:My first try was, using partial sort to figure out numbers layer by layer in the heap.. it only failed with TLE with the last test case. The problem i... 阅读全文
posted @ 2015-10-09 01:50 Tonix 阅读(222) 评论(0) 推荐(0)

摘要:Classic DP. The initial intuitive O(k*n^2) solution is like this: class Solution { public: /** * @param pages: a vector of integers * @param k: an int 阅读全文
posted @ 2015-10-07 15:12 Tonix 阅读(869) 评论(0) 推荐(0)

摘要:Bucketing! A lot of details to take care.struct Bucket{ Bucket() :l(-1), r(-1), bValid(false){};int l, r; bool bValid;};class Solution {public: ... 阅读全文
posted @ 2015-10-07 13:50 Tonix 阅读(176) 评论(0) 推荐(0)

摘要:Binary search. Please not data types and some details.class Solution {public: /** *@param L: Given n pieces of wood with length L[i] *@para... 阅读全文
posted @ 2015-10-07 11:47 Tonix 阅读(229) 评论(0) 推荐(0)

摘要:This is sth. for me to learn..https://github.com/kamyu104/LintCode/blob/master/C++/expression-evaluation.cpp 阅读全文
posted @ 2015-10-06 15:08 Tonix 阅读(149) 评论(0) 推荐(0)

摘要:Idea is the same: climbing up the hill along one edge (Greedy)! Visualize it in your mind!class Solution {public: /** * @param A: An integer ma... 阅读全文
posted @ 2015-10-06 07:45 Tonix 阅读(164) 评论(0) 推荐(0)

摘要:Not hard to find a solution, but there are several corner cases.class Solution {public: /** * @param root: The root of the binary search tree. ... 阅读全文
posted @ 2015-10-05 06:35 Tonix 阅读(189) 评论(0) 推荐(0)

摘要:Greedy: remove earliest down-edge: like "54", "97".class Solution {public: /** *@param A: A positive integer which has N digits, A is a string.... 阅读全文
posted @ 2015-10-05 01:14 Tonix 阅读(162) 评论(0) 推荐(0)

摘要:Not hard to think of a solution. But the key is all details.class Solution {public: /** *@param n: Given a decimal number that is passed in as ... 阅读全文
posted @ 2015-10-04 11:54 Tonix 阅读(158) 评论(0) 推荐(0)

摘要:A coding practice.class Solution {public: // encoding: 2 - 1 to 0; -1 - 0 to live void gameOfLife(vector>& board) { int h = board.size();... 阅读全文
posted @ 2015-10-04 10:31 Tonix 阅读(182) 评论(0) 推荐(0)

摘要:Recursion + Memorized Search(DP). And apparently, the code below can be iterative with only 3 vars - DP.class Solution { unordered_map cache;public... 阅读全文
posted @ 2015-10-03 12:34 Tonix 阅读(138) 评论(0) 推荐(0)

摘要:LeetCode AC code failed last case with TLE. We need further pruning - we only enumerate all possible lengths of all dict words.class Solution { vec... 阅读全文
posted @ 2015-10-03 07:02 Tonix 阅读(214) 评论(0) 推荐(0)

摘要:Something new I learnt from it: what is Treap and a O(n) constructionhttps://en.wikipedia.org/wiki/Cartesian_tree#Efficient_constructionclass Solution... 阅读全文
posted @ 2015-10-03 06:23 Tonix 阅读(183) 评论(0) 推荐(0)

摘要:Binary search.class Solution { int _findClosest(vector &A, int v) { int s = 0, e = A.size() - 1; int ret = INT_MAX; while(s... 阅读全文
posted @ 2015-10-02 13:02 Tonix 阅读(273) 评论(0) 推荐(0)

摘要:Flip over your mind: in rotated subarray case, we can simply cut the continuous smallest subarray.class Solution {public: /** * @param A an int... 阅读全文
posted @ 2015-10-02 00:19 Tonix 阅读(155) 评论(0) 推荐(0)

摘要:Partition and swapping. A lot details to take care.class Solution {public: /** * @param A: An integer array. * @return: void */ void... 阅读全文
posted @ 2015-10-01 15:27 Tonix 阅读(163) 评论(0) 推荐(0)

摘要:Fenwick Tree is perfect for this problem, though space complexity is not quite efficient.class Solution { ////////////////// // Fenwick Tree // ... 阅读全文
posted @ 2015-10-01 11:14 Tonix 阅读(203) 评论(0) 推荐(0)

摘要:Typical Trie usage. But please note that it could be any order of input strings.#include #include #include #include #include #include #include using n... 阅读全文
posted @ 2015-10-01 01:07 Tonix 阅读(353) 评论(0) 推荐(0)