The Travels of Marco Cai

  • Home

  • About

  • Tags

  • Archives

  • Search

[LeetCode] Challenge log 47

Posted on 2018-07-17 |
47. Permutations II

DFS problem. Same as 46, just to make sure at one level do not repeat duplicates.

Read more »

[LeetCode] Challenge log 46

Posted on 2018-07-17 |
46. Permutations

DFS practice.

Algorithm1:

  1. use a stack to store current set, if len(stack) == len(nums), append stack to res
  2. choose a number from nums, append it to stack and mark it as used. DFS to the next level.

Algorithms2:

  1. use recursion to build all the possible set simultaneously.
Read more »

[LeetCode] Challenge log 17

Posted on 2018-07-17 |
17. Letter Combinations of a Phone Number

It is a full permutation problem. DFS or BFS both ok.

Read more »

Robot Sketch: ZOK

Posted on 2018-07-15 |

ZOK

  • media: IPAD pro
  • app: Procreate

[LeetCode] Challenge log 148

Posted on 2018-07-13 |
148. Sort List

Merge sort:

  1. if head or head.next is none, return head.
  2. split the list from middle into A and B
  3. recursively merge sort A and B
  4. merge sorted A and sorted B

Complexity:

  • time: Nlog(N)
  • space: recursion call is at most log(N) deep, hence log(N)
Read more »

[LeetCode] Challenge log 146

Posted on 2018-07-12 | Edited on 2018-07-13 |
146. LRU Cache

Analysis:

Although the problem only asks for implementing two operaions. It actually requires more than that. The two obvious and a few hidden required operations are:

  1. get: access value by key in O(1) and update its “freshness”
  2. put: add (key, value) pair in data structure and set it to be the freshest
  3. delete: delete the least recently used pair
  4. update: update pair’s value and “freshness” = delete and move to end

First, if we want “get” to be done in O(1), hashing is a must. And we need to keep track of the “least recently used” element, hence a queue (FIFO) is considered. But if queue is used, updating the freshness of the pair will cost linear time, which is why a linklist should be used. So we can use hashing to map key to a node, where the value is saved. To update a node’s freshness from the linklist, we need to delete it from its original position, for which we need to not only know its next but also its pre — so we need to used double linklist! Finally, to delete the LRU from the hash table, we also need the key stored in the coresponding node. So now we have somthing like:

hash key 1 key 2 key 3 …
double linklist (k1, v1) (k2, v2) (k3, v3) …

Trick: Python has a class collections.OrderedDict(), which is a dict that keeps track of the insertion order…

Read more »

[LeetCode] Challenge log 142

Posted on 2018-07-12 | Edited on 2018-07-13 |

142. Linked List Cycle II

Algorithm:

  1. find the loop and record where slow and fast hit
  2. set slow = head
  3. start traverse again, until the pointers hit again

Analysis:

  1. When they first hit, fast is x steps ahead of slow, where x = k * len(loop).
  2. So in the second traverse, when slow first enter the loop, fast is exatcly k circles ahead and hit slow again.
Read more »

[LeetCode] Challenge log 147

Posted on 2018-07-12 | Edited on 2018-07-13 |
147. Insertion Sort List

Algorithm: Insertion sort

Trick: If a node is greater than the sorted tail, make it the new tail and move on to next.

Read more »

[LeetCode] Challenge log 160

Posted on 2018-07-11 | Edited on 2018-07-12 |

160. Intersection of Two Linked Lists

Algorithm1:

  1. traverse both lists and count len(a) and len(b)
  2. chop |len(b) - len(a)| nodes from the head of the longer list
  3. travese both lists simutaneously until two pointers hit

Algorithm2:

  1. let A = A+B, B = B+A such that two lists are alighn by their tails
  2. travese both lists simutaneously until two pointers hit

Analysis:

Both algs align the two lists by their tails.

Read more »

[LeetCode] Challenge log 141

Posted on 2018-07-11 | Edited on 2018-07-12 |

141. Linked List Cycle

Classic application of two pointers: slow and fast.

Algorithm: when fast catches up with slow, there is cycle.

Read more »
123…13
Mingxiang Cai

Mingxiang Cai

A learner, a hobbyist.

128 posts
56 tags
© 2019 Mingxiang Cai
0%