Posts

Zookeeper的学习笔记

最近在想要optimize一个工作项目里的routing的算法,该项目用到了zookeeper做集群管理,这早已耳熟能详的名字拨开其里却不知其所以,然便下定决心认真学习一番。还记得之前和人讨论问题的时候,被问到了一些metadata和server信息如何同步管理,即使在对方百般的提示之下也没能答其根本,其实现在想来,那只不过是zookeeper的一个很简单的应用罢了。真可谓,知之为知之,不知为不知呀。 这个博客讲述了Zookeeper的特性。 https://blog.csdn.net/king866/article/details/53992653

Pagination

1,28,310.6,SF; 4,5,204.1,SF; 20,7,203.2,Oakland; 6,8,202.2,SF; 7,20,180.1,SF; 6,10,199.1,SF; 1,16,190.4,SF; 2,18,161.2,SF; 3,76,146.2,SF; 6,29,185.2,SF; 6,21,162.1,SF; 2,30,149.1,SF; 2,14,141.1,San Jose 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include <list> #include <iostream> #include <string> #include <unordered_set> using namespace std; namespace airbnb { class Pagination { public: Pagination() {} ~ Pagination() {} void pagination(list < string >& results, int pagesize) { unordered_set < string > visited; list < string > :: iterator itr = results.begin(); int item = 0 ; while ( ! results.empty()) { string id = ( * itr).substr( 0 , ( * itr).find

File System

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 #pragma once #include<iostream> #include<unordered_map> #include<string> #include<sstream> using namespace std; namespace airbnb { struct TrieNode { TrieNode() { this -> value = - 1 ; } unordered_map < string, TrieNode *> children; int value; }; class FileSystem { private: TrieNode * root; void deleteTrie (TrieNode * p) { for ( auto & pchild : p -> children) { deleteTrie(pchild.second); }

Wizard

There are 10 wizards, 0-9, you are given a list that each entry is a list of wizards known by wizard. Define the cost between wizards and wizard as square of different of i and j. To find the min cost between 0 and 9. For example: wizard[0] list: 1, 4, 5 wizard[4] list: 9 wizard 0 to 9 min distance is (0-4)^2+(4-9)^2=41 (wizard[0]->wizard[4]->wizard[9]) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 #pragma once #include <iostream> #include <vector> #include <queue> #include <cmath> using namespace std; namespace airbnb { class Wizard { public: vector < int > GetMinCostPath(vector < vector < int >>& wizards, int start, int end, int n) { vector < int > minPath; vector < int > parents(n, - 1 );

Sliding Game

You're given a 3x3 board of a tile puzzle, with 8 tiles numbered 1 to 8, and an empty spot. You can move any tile adjacent to the empty spot, to the empty spot, creating an empty spot where the tile originally was. The goal is to find a series of moves that will solve the board, i.e. get [ [1, 2, 3], [4, 5, 6], [7, 8, - ]… 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 #pragma once #include <unordered_set> #include <queue> #include <string> #include <algorithm> using namespace std; namespace airbnb { class SlidingGame { public: SlidingGame(); ~ SlidingGame(); bool IsValidStart (string & initState); private: string m_initState; const string finalState = "123456780" ;

reverse int

Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2 31 ,  2 31  − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. class Solution { public: int reverse( int x) { // edge case // 对于integer的操作,要考虑其overflow的情况。 // max, numeric_limits<int>::max() .. min() .. if (x == numeric_limits< int >::max() || x == numeric_limits< int >::min()) { return 0 ; } if (x >= 0 ) { return reverseUtil(x); } else { return 0 - reverseUtil(abs(x)); } } int reverseUtil( int x) { long reverse = 0 ; while