Posts

Showing posts from November, 2018

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" ; ...