General methods of design related problem:
three types: data structure/algorithm(method)/iterator
really understand what this problem means. but not lead by it every second.
think about some fancy data structure that has some 「relations」 with this problem. not just think in a shallow way.
theorically, every data strucuture is connected, so any average data structre will do. we can use more than one type of data strucutre and more than one for each kind. sometimes, we don’t even needs anyfancy data strucutures.
146 LRU cache: HashMap and self defined Node(double linkedList node with k-v pair as the data)
we use hashMap because we have to implement those method in O(1), we use double linkedlist, becasue we need to know which one is 「least recent used」
155.minStack: maintaining two stacks, they are both stack but we are gonna use the first as regular stack, and the other stack will only stores the min element so far being pushed.
170.two sum 3:use hashmap
173.binary search tree iterator. this problem requires us to run each method in O(1) time and O(h) of space, just using a stack wisely
208 implement trie: a high level data structure, impelement its insert and search methods
211 add and search word. implement a data structure which supports two operations: addWord(), search(). this problem needs us to implement trie. addWord is insert and seach is search a word in trie;
225 implement stack using queues: queue is an abstract API, it can be implemented by LinkedList or arrayList. in such case we will use linkedLis(add remove peek), this problem can be done using only one queue
232 implement queue using stacks:this problem have to done by two stacks
244 shortest word distance: it’s very easy, just use hashmap
251 flatten 2D vector: this probelm needs us to implement an iterator to flatten a 2d matrix wo we do not need any fanct data structures, just use two extra pointers;
281 zigzag iterator: given 2 1d vector, implement an iterator to return their elements alternately. like last problem, we only need to implement the constructor and next() hasNext() method. the difficult for this problem is, how do we know when we call next(), which one need we extract? this is actually tricky, we use mainly two iterator, and we swap them every time we call that next() method.
284 peek interators:except next() hasNext() we also have a peek() method. this key point for this problem is that next varible, it is actually not next, it’s next’s next. that the reason why you can peek it without without using next() method. and you can peek as many times you want without change.
288 unique word abbreviation: this is a kind of strange problem, give principles of word abbreviation. and give a array of unabbrev words, implement a method called inUnqiue(word) to check if current word’s abbrev is unique or not. use hashmap and implement another 輔助methods
295 find median from data stream. using two pqs(minheap and maxheap), real classic
297:serialize and deserialzie binary tree: you need to implement an algrithm that can serialize and deserialize a binary tree. so we use level order tranverse idea for both methods.use queue
341: faltten nested list interator: still, like all other iterator problems, we needs to implement next() and hasNext() method but unlike other problems, we have a new type of data in this problem: NestedInteger. this class is implemented already and provided with nestedinteger interface: IsInteger() getInteger() and getList(). so you have to stay clearly. this is nothing difference with unsing other classes like arraylist or sth.
346: moving average from data stream:calculate the averge of a moving fixed length data stream. the fixed length is defined by the user using constructor. and you have to implement the next(value) method to insert and calculate the average at the same time. this problem can be easily solve in queue.
348 design tic-tac-toe: given a set of rules: read carefully about the rules and implement them smartly. the solution of my leetcode is so smart. I am ashamed.
353 design snake game: this is kind of complicated, it uses set and deque. we jusr need to know that in this circumstance, deque will be used.
355 design twitter: implement class twiiter and implement its for methods: postTweet, getNewsFeed, follow and unfollow. this problem is a completely a OOD problem.besides the methods we need to implemented, we also need to implement the actors in this system: user and tweet. this is really classic. it’s an implement problem
359 logger rate limiter: design a class that can limit user log. using a hashmap, the key is the messgae and the value is time stamp. because every message we enter has a timestamp and string message. very imple
362 design hit counter: Design a hit counter which counts the number of hits received in the past 5 minutes.using queue, which store the timestamp.
379 design phone directory:Design a Phone Directory which supports the following operations: get check and release, using a hashset and a queue. we use queue to store every posible number, set will store every number that’s already been use.
380 Insert delete getrandom O(1): desgin a data structure that support insert remove and getRandom three methods in O(1). when saw O(1) the moment we think is using hashmap, but how can we get a random from hashmap? we don’t use any index in hashmap, so we add another data structure which is a list. list will get given random index element in O(1) time. and we need to maintain both list and hashmap in insert and remove method;
381: insert delete getrandom O(1) duplicate allowed, this is the follow up of 380, we still need to implement that three methods but this time those values can be duplicate, so hashmap<Integer, Integer> has to be changed into hashmap<Integer, Set>. basically speaking, their is not much of difference from lc380.
432 all O one data strucure: implement a data structure with 4 methods: inc, dec, getMaxKey, getMinKey. the problem is kind of strange. this is some kind of problem that can really shows someone have problem solving skills. because you have to construt you own data structure to solve this specific question.
460 LFU cache: really classic problem: implement two methods: get and put.
588 design in memory file system: implements methods like ls/mkdir/addContentToFile/readContentFromFile. we can see from this problem that fileSystem is also a class, a object. and all those cmd are methods. we need to create a class file in this. because that’s the object lives in this system. and the general idea for this is like a tree. files are like the node in tree.
635 design log storage system: this is a log create and read system, we need to implement put and retrieve() for this. used treemap and subtree here
642 Design Search Autocomplete System: it is a autocomplete system. which is also widely used. use trie to solve this problem is better.
716 max Stack: this can also using two stacks to solve this problem. if you take a look at the solution, you will find that is genius. I’m so stupid.
Design DS we already known
//just implement them as you do in CS1332
705 Design HashSet
706 Design HashMap
707 Design linkedList
641 design circular deque
622 design circular queue
1206 design skiplistnode