博文

目前显示的是 三月, 2016的博文

WordNet BFS Shortest Path

图片
WordNet is a graph which represents a set of words,  Synset can be interpreted as a set of words, which have same/similar meaning,  For example AND_circuit AND_gate, have similar meaning "a circuit in a computer that fires only when all of its inputs fire" The Hypernym of a synset is a more general form of this synset, for example, "gate" is the hypernym of synset "AND_circuit AND_gate" and "OR_circuit OR_gate" (from priceton wordnet digraph) To find the distance between two synsets, we can use BFS,  First of all we need to build graph, after that we can run BFS 1. run BFS on synset1, Complexity (V+E), we can get distance from synset1 to each synset 2. run BFS on synset2, Complexity (V+E), we can get distance from synset2 to each synset 3. Iterate through all synset and calculate the minimum of distance_to_synset1 + distance_to_synset2 Complexity (V) Thus the total complexity is (V+E) In reality a word ma

Increasing Triplet Subsequence LeetCode

For the three increasing number, it’s quite easy, we just need to find a number which has one number smaller than it, and one number greater than it, so I used to arrays, one is used to keep the maximum number on the right, the other is used to keep the minum on the left, then loop the array again to find a number which is larger than left minum and smaller than right maxium class Solution(object): def increasingTriplet(self, nums): """ :type nums: List[int] :rtype: bool """ l = len(nums) if l < 3: return False less = [0] * l great = [0] * l minimum = nums[0] maximum = nums[-1] i = 0 while i < l: minimum = min(nums[i],minimum) less[i] = minimum i += 1 i = l - 1 while i > 0: maximum = max(nums[i],maximum) great[i] = maximum i -= 1 i = 0 while i < l: if nums[i] > less[i] and nums[i] < great[i]: return True i += 1 return False

Counting Bits Leetcode

Runtime:  791 ms We should divide it into even number and odd number, if the number is odd, then number of ones = ones of previous number + 1 if it's even number then the number of ones = number of ones of this number shift to right by 1, which is already known

Palindrome Pairs Leetcode

I used python hash table, first add all words to dictionary, then for each word partition it check if the reverse of first half/second half is in dictionary, if it is, add it to result. One tricky input is when word is empty, we have to consider it. The complexity is O(n*m) where m is the word length, and n is total number of words The method is not very efficient 134 / 134  test cases passed. Status: Accepted Runtime:  872 ms Submitted:  31 minutes ago class Solution(object): def isPalindrome(self,word): return word == word[::-1] def generatePairs(self,index1,index2): s = "{},{}".format(str(index1),str(index2)) return s def palindromePairs(self, words): """ :type words: List[str] :rtype: List[List[int]] """ ret = [] d = {} pairs = {} for i,word in enumerate(words): d[word] = i for word in words: for i in xrange(0,len(word)+1): first_rev = word[:i][::-1] if first_

San Diego Real Estate Risk&Profit

I used the data from Federal Housing Financing Agency , and analyzed the house price index of San Diego Carlsbad Area over 25 years, from 1991 to the end of 2015 Using the first quarter of every year The standard deviation is about 7% and annualized return rate is about 4%, compared to S&P 500 standard deviation about 15% and 10% return (about 12% for large cap,  and 15% for small cap ) Considering this value, the sharp ratio is lower than investing the stock market, But it still outperforms some fund, for example this one ONECX , it has 11% stdev and 4% return over 15 years. However, if the home is bought as an investment, it will be lucrative. For example, if a house is 230K, we put 200K as downpayment we rent it for 1K per month, after three years, the house will worth about 260K,  we will make a profit of 380K, in three years, even if we want to sell it, and we pay the commission which costs 150K, we still make a profit of 230K, that's about 15% annual return ,