Tuesday, March 22, 2016

MJ [56] Longest String Chain

Given a set of strings, find the longest string chain. Two strings are chained if 
    1. both strings belong to the given set
    2. the second string is generated  by remove one letter in the first string
For example:
Given vector<string> w = {a,ba,bca,bda,bdca}, the longest string chain is bdca->bda->ba->a.

Solution:
Represent the string chain by a Trie like structure. Record the depth while building Trie. 

    bdca 
   |      |
 bda    bca
   |        | 
  ba      ba
   |
   a

=============
===============

Ref
[1] http://www.1point3acres.com/bbs/thread-131978-1-1.html

MJ [55] Best Merging Point

problem: robot merge point
input:
robot: 1
obstacle: X
[
    0   0   0   M   1
    0   1   X   0   0
    0   X   0   0   0
    0   0   0   1   0
    0   0   0   0   0
]
output:
best merge point: M
3 + 1 + 3 = 7

======
For every robot, compute its shortest distance to every point. The best merge point is the point with the smallest distance sum over all the robots.



Saturday, March 12, 2016

LeetCode [336] Palindrome Pairs


Thursday, March 10, 2016

QuickSort & QuickSelect