Posts

Hanoi Tower

Image
最近学习到了一个很有趣的problem. 汉诺塔。 我认真的想了好几天,终于在今天找IT给我set up machine等着没事干的时候大概想通了。哈哈哈。 解决这个问题的方法让我想到了binary tree traverse 里面的in order的遍历,汉诺塔的solution之一的 recursive 的方式跟二叉树in order就是异曲同工,而且in human mind, 汉诺塔should make more sense to understand compared to binary tree. 在网上看到了 一个印度小男孩对汉诺塔的分析 ,果然是tree. Basically 解决汉诺塔的问题就是一个搬家公司应该怎么搬家的问题,from mission completed to start, which is from bottom to top. Let's define Hanoi move function as: HanoiMove(int leftToSit, int from - 0 , int to - 1, int rest - 2) and we have n pieces in total waiting for the move In theory, to approach the last step nth of move, we just need finish move rest of Hanoi pieces (n-1 in total) from start to rest pile,(简单说来就是,清空路障,直捣虎穴。)and then it should be able to move the last piece from start to To pile. so we just do 1) HanoiMove(n-1,  from, res, to)    ---->>>    left as we planned in theory, we move the nth piece move(from,  to)    after this move, we have...

C++ insights

1. Rule of three Copy assignment and copy  constructor https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three/4172724#4172724 2. best  resources https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list 3. YouTube Video Cherno's Channal https://www.youtube.com/channel/UCQ-W1KE9EYfdxhL6S4twUNw?&ab_channel=TheChernoProject

从文件流中读取第K大的数

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 int getKthLargest (std :: istream & numStream, uint32_t k) { if ( ! numStream.good()) { throw std :: invalid_argument( "numStream" ); } if (k == 0 ) { throw std :: invalid_argument( "k" ); } try { auto initHeap = std :: vector < int > (k, INT_MIN); std :: priority_queue < int , vector < int > , std :: greater < int >> minHeap(initHeap.begin(), initHeap.end()); int num; while ( ! numStream.eof()) { numStream >> num; if (numStream.fail()) { // bad format number continue ; } minHeap.push(num); minHeap.pop(); } // assume input stream doesn't have any number...

Dynamic Programming

https://www.hiredintech.com/classrooms/algorithm-design/lesson/40 what is dynamic programming?  In short, it's a method, which allows you to solve a problem by breaking it down into smaller sub-problems.   --> 以小见大 方法: 1. Breaking down a problem into sub-problems.  2. Two implementations "bottom-up" where we start from the base cases and compute the values until we reach the desired value.  "top-down" in which we recursively compute the answers for smaller problems, on demand, but try to store the computed valued in order not to compute them multiple times. This technique is usually called "memoization". Sometimes, especially for "bottom-up" implementations it is possible to store only one part of the computed values at a time and free the memory for other parts once they have served their job in the computations.

Prime Number相关

Prime Number: 只能被1和它本身整除的的数。1不是Prime number. Ugly Number: 1, 2, 3, 5, 以及能被[2, 3, 5] 整除的数。 直觉上看,除了prime number以外的所有数都能被ugly number整除。 也就是说,两个问题归一了。 无论是find kth prime number或者是kth ugly number. 都属于同一问题,如何有效地在一个int streaming中找到满足条件的数。 进一步分析,如何找到prime number / ugly number 暴力解法就是从1开始每一个数都check, 然后一直check到第kth满足条件的数。这个方法用来找ugly number应该还不错, 原因是从[1. x], 基本上所有的数都属于ugly number. 应该能够在~k 的条件下找到ugly number. 去implement一下看看有没有TLE的问题。 oops. 思路off了一点点。 重新回到ugly number的定义。 ugly number是只能被2, 3, 5整除的数。但事实上,从1开始的数中,除了能够被2, 3, 5整除的数之外,还有的数的factor是prime number同时他们也能被2, 3, 5整除,所以在check数的同时要排除其能被遇见过的prime number整除的可能性。 果然 TLE了。 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 class Solution : """ @param n: An integer @return: the nth prime number as description. """ def nthUglyNumber ( self , n): # write your code here if n == 1 : return 1 order = ...

Design: 如何设计一个web crawler

对于design的总结,就从我最熟悉的crawl 开始。按照我在工作中的学习和了解实际应用一下。 确定design purpose. crawler需要crawl什么,是固定的host, ip还是某一个domain. crawler是为谁服务? crawl 的scale是多少?几百,几千还是几个B? crawl后的内容如何使用? crawl的latency的宽容度是多少? crawl的seed如何选择? 如何filter junk/spam/bad url? 如何保证url的freshness? 如何dedup crawl的次数? 在crawl之后的document需要进行process吗? 假设我们的crawler是一个private crawler, 只是target某一个特定的host, 比如wikipedia. 有一个seed url list, 然后从parent url开始往下衍生找outlinks.

linked list

3 type of linked list Three different kinds of linked lists exist: Singly linked lists have a single pointer pointing to the next element in the list. The last pointer is empty or points to null, signaling the end of the list. Doubly linked lists have two pointers, one pointing to the next element and one pointing to the previous element. The head node's previous pointer points to null and the tail node's next pointer points to null to signal the end of the list. Circular linked lists usually represent buffers. They have no head or tail, and the primary issue is avoiding infinite traversals because of the cycle. These questions rarely come up in interview questions. Arrays are oftentimes a better substitute for a circular linked list, using the modulus operator to wrap around. 小技巧: 1. 固定head 指针 2. 每次加新节点的时候加在head