Tony's Log

Algorithms, Distributed System, Machine Learning

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

08 2015 档案

摘要:One pass in-place solution: all swaps.class Solution {public: /** * @param nums: a vector of integers * @return: nothing */ void partit... 阅读全文
posted @ 2015-08-29 14:38 Tonix 阅读(203) 评论(0) 推荐(0)

摘要:A variation to a classical DP: LCS.class Solution {public: /** * @param A an integer array * @return A list of integers includes the index o... 阅读全文
posted @ 2015-08-29 11:43 Tonix 阅读(207) 评论(0) 推荐(0)

摘要:It can be solved based on the code from "Strobogrammatic Number II". The idea is pretty straight forward - binary search the boundaries.class Solution... 阅读全文
posted @ 2015-08-29 06:59 Tonix 阅读(170) 评论(0) 推荐(0)

摘要:This is abouthttps://en.wikipedia.org/wiki/Run-length_encoding. The trick is, for a valid char, we only compress up to 254 occurences - count 255 mean... 阅读全文
posted @ 2015-08-29 01:34 Tonix 阅读(315) 评论(0) 推荐(0)

摘要:Key: each of 2\3\5 is trying to multiply with the least number it has not been multiplied.class Solution {public: int nthUglyNumber(int n) { ... 阅读全文
posted @ 2015-08-25 13:15 Tonix 阅读(124) 评论(0) 推荐(0)

摘要:Another topological sorting problem. Note: the DFS one is like a 'post-order' traversal.class Solution { unordered_map> g; unordered_set visited... 阅读全文
posted @ 2015-08-25 12:44 Tonix 阅读(239) 评论(0) 推荐(0)

摘要:A variation of 3Sum. And the trick is, (i& nums, int target) { size_t len = nums.size(); if(len = target) k--; ... 阅读全文
posted @ 2015-08-24 14:38 Tonix 阅读(150) 评论(0) 推荐(0)

摘要:Use constructive strategy: we compose each of the 26 variations of one word.class Solution {public: vector> groupStrings(vector& strings) { ... 阅读全文
posted @ 2015-08-24 12:26 Tonix 阅读(217) 评论(0) 推荐(0)

摘要:I learnt a clean idea: all read is from the 4-char buffer.int read4(char *buf);class Solution { int offset; int validLen; char _buf[4]; ... 阅读全文
posted @ 2015-08-24 11:42 Tonix 阅读(157) 评论(0) 推荐(0)

摘要:An interesting DFS with a lot of details to take care of.class Solution { vector> ret; void go(int n, vector sofar, int minF) { int st... 阅读全文
posted @ 2015-08-23 12:43 Tonix 阅读(258) 评论(0) 推荐(0)

摘要:Classic DP! For house[i] to pick a min-color from dp[i-1], we only need to check if color j is the min cost color index of dp[i - 1]; if yes, then we ... 阅读全文
posted @ 2015-08-23 12:21 Tonix 阅读(233) 评论(0) 推荐(0)

摘要:Simply store indices in ctor.class WordDistance { unordered_map> hm;public: WordDistance(vector& words) { for (int i = 0; i &vec1 = hm[w... 阅读全文
posted @ 2015-08-22 11:18 Tonix 阅读(179) 评论(0) 推荐(0)

摘要:Just take care of corner cases..class Vector2D { vector> &r; int i, i0;public: Vector2D(vector>& vec2d) : r(vec2d), i(0), i0(0) { i... 阅读全文
posted @ 2015-08-22 10:59 Tonix 阅读(180) 评论(0) 推荐(0)

摘要:Linear scan after sorting. Please note sorting detail.typedef std::pair Rec;class Solution {public: int minMeetingRooms(vector& intervals) { ... 阅读全文
posted @ 2015-08-22 10:37 Tonix 阅读(182) 评论(0) 推荐(0)

摘要:A typical DPclass Solution {public: int minCost(vector>& costs) { size_t len = costs.size(); if(len == 0) return 0; ve... 阅读全文
posted @ 2015-08-22 00:46 Tonix 阅读(176) 评论(0) 推荐(0)

摘要:A typical sliding window solution.class Solution { int rec[26]; // for a-z int rec1[26]; // for A-Z int cnt; int &getCnt(char c) ... 阅读全文
posted @ 2015-08-21 13:16 Tonix 阅读(125) 评论(0) 推荐(0)

摘要:DFS to check cyclic. Please note some details.class Solution{ unordered_map> g; unordered_set visited; bool go(int i, int p) // true - cy... 阅读全文
posted @ 2015-08-21 12:41 Tonix 阅读(260) 评论(0) 推荐(0)

摘要:A variation of linear scan..typedef pair Rec;class Solution {public: int shortestDistance(vector& words, string word1, string word2) { v... 阅读全文
posted @ 2015-08-20 15:32 Tonix 阅读(133) 评论(0) 推荐(0)

摘要:A typical recursion one.class Solution { bool _verify(vector &in, int i, int j, int low, int high) { if (i > j) return tr... 阅读全文
posted @ 2015-08-20 14:59 Tonix 阅读(247) 评论(0) 推荐(0)

摘要:Recursion, a natural thought:class Solution { // Top : BottomRight std::pair rotate(TreeNode *p) { if (!p->left && !p->right) ... 阅读全文
posted @ 2015-08-20 13:23 Tonix 阅读(169) 评论(0) 推荐(0)

摘要:It should be categorized as 'Advanced' I think ... Anyway, Fenwick tree is the key.Editorial:https://www.hackerrank.com/challenges/candles-2/editorial... 阅读全文
posted @ 2015-08-10 13:07 Tonix 阅读(658) 评论(0) 推荐(0)

摘要:A trickier Dijkstra.#include #include #include #include #include #include #include #include #include using namespace std;const int INF = std::numeric_... 阅读全文
posted @ 2015-08-03 07:41 Tonix 阅读(393) 评论(0) 推荐(0)

摘要:Typical Floyd-Walshall Algorithm.#include #include #include #include #include #include #include #include #include using namespace std;const long DIST_... 阅读全文
posted @ 2015-08-03 01:52 Tonix 阅读(331) 评论(0) 推荐(0)

摘要:No big difference with "Breadth First Search: Shortest Reach", but this statement is crucial:If there are edges between the same pair of nodes with di... 阅读全文
posted @ 2015-08-02 15:18 Tonix 阅读(1175) 评论(0) 推荐(0)

摘要:Typical Dijstra algorithm impl. one.#include #include #include #include #include #include #include #include #include using namespace std;const long DI... 阅读全文
posted @ 2015-08-02 14:43 Tonix 阅读(1189) 评论(0) 推荐(0)

摘要:A combination of different DP passes.https://www.hackerrank.com/challenges/lego-blocks/editorial#include #include #include using namespace std;typedef... 阅读全文
posted @ 2015-08-02 05:31 Tonix 阅读(866) 评论(0) 推荐(0)

摘要:My initial thought was recursive (a op b) evaluation, which doesn't work quite well.The correct way is to split by op. And, we can cache calculaton re... 阅读全文
posted @ 2015-08-01 13:45 Tonix 阅读(147) 评论(0) 推荐(0)