LeetCode 514. Freedom Trail – Ysing's blog



LeetCode 514. Freedom Trail – Ysing's blog

由于做出来的时候posted在discuss了,所以我直接复制里面的内容

  • 看Tag上dp上打了个冒号,这似乎不完全算是一个dp solution
  • 基本思路:
  1. 对于解一个key,我们可以将这个key的长度作为我们接这道题的step,每一步step接一个char,解到最后一个字符最小step就是我们需要求的答案
  2. 所以算法开始,我们需要知道,key对应的每一个char在string ring中的哪一个位置,需要对ring for一遍,将对应的char记录在相应的mp中,这样可以节省很多的搜索时间。
  3. 开始搜索key的循环,算法第一步开始的index为 ring[0],需要求转动ring,使达到ring[index]==key[0](第一步)的步数最小,因为可能ring中不止有一个key[0],所以需要对每一个符合的进行选择,(每一个dp[i][j],记录的实际就是在第i步时,通过起点到达j的最短step,主要是考虑rotate left or right)
  4. 依次循环key.size()步后结束循环
  5. 对最后一个dp数组for出最小的值,return minStep + key.size()即是最小的步数

Simple dp idea

  1. recording each index of characters in ring,beacuse each characters we have search in this time would be starting index in the next search
  2. How could we solve the problem that rotate the ring to the left or right ? My idea is min((tmp[j] + size -it)%size,(it + size - tmp[j])%size)
  • Suppose you want to rotate the ring to the right and search 'k', and the size is 5. We could calculate it by this + size -k(index)%size
this -  -  -  -  k 
  • If we want to rotate the ring to the left,what should we do? It is the same problem with above problem,move this to it 's right,and reach k
k -  -  -  -   this 
  • So we could calculate it by k(index) + size -this%size
  • There are many people use abs() instead of %size,I think it's faster than mine :)


Read full article from LeetCode 514. Freedom Trail – Ysing's blog


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts