# LeetCode Summary Summary of all Leet Code practice problems. Accepted: 54 problems Last updated: Saturday, August 15, 2020. Link all codes: https://github.com/fool1280/Algorithms-Practice ## LeetCode 425 Problem statement: https://leetcode.com/problems/word-squares/ Difficulty: Hard Time: 1 hour and 30 minutes Two approaches: 1) Brute force: the result has to consist of K words (with K is the length of one single word; every word has the same length). Time complexity: O(N^K) -> At every position, we have N options to choose with N is the number of total words and K is length of one word. 2) We realize that the next word have to match with the prefix of all previous word. For example, the first word is "wall", then the second word has to start with "a". If the second word is "area" then the third word has to stat with "le" and so on. I really don't know how to calculate this time complexity | w | a | l | l | | - | - | - | - | | a | r | e | a | | l | e | a | d | | l | a | d | y | ## LeetCode 138 Problem statement: https://leetcode.com/problems/copy-list-with-random-pointer/ Difficulty: Medium Time: 30 minutes Idea: using hashmap to store what node already traveled. Time complexity O(N) ## LeetCode 141 Problem statement: https://leetcode.com/problems/linked-list-cycle/ Difficulty: Easy Time: 10 minutes Idea: travel using one step (slow) and two steps (fast) pointers. When they meet -> there is cycle. Time complexity: around O(n) ## LeetCode 48 Problem statement: https://leetcode.com/problems/rotate-image/ Difficulty: Medium Time: 45 minutes Idea: Swap elements spirally from outside to inside What can I do better? Perform a run with a sample test rather than try to figure out which edges cases. Time complexity: O(N^2) ## LeetCode 163 Problem statement: https://leetcode.com/problems/missing-ranges/ Difficulty: Medium Time: 30 minutes Idea: ad-hoc. Simple process. Take too much time for edge cases. What can I do better? I added lower, and upper into the arrays without realizing it will interfere with the rest. Problem states "inclusive". Did not notice. Time complexity: O(n) ## LeetCode 159 Problem statement: https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ Difficulty: Medium Time: 15 minutes Idea: hash table. Using collections.Counter() to get all the keys fast. Time complexity: O(n) ## LeetCode 833 Problem statement: https://leetcode.com/problems/find-and-replace-in-string/ Difficulty: Medium Time: 15 minutes Idea: process one by one Time complexity: O(n) ## LeetCode 247 Problem statement: https://leetcode.com/problems/strobogrammatic-number-ii/ Difficulty: Medium Time: 45 minutes Idea: recursion. The suitable numbers are 0, 1, 8, 9, 6. Mirror of 1 is 1, 0 is 0, 8 is 8, 6 is 9 and vice versa. Recursion for one part left, right has to be its mirror. If the length of number is odd, the middle number has to be 1, 0, or 8. Time complexity: O(5^(N/2)) ## LeetCode 253 Problem statement: https://leetcode.com/problems/meeting-rooms-ii/ Difficulty: Medium Time: 2 hour. Idea: Thinking about doing an interval segment tree, with each task increase value in range [a, b] to 1. The max will be answer. Think about priority queue. Make more sense. What I learn? Thinking about what information matter and what's not. Solution using two pointers are really clever. Time complexity: O(NlogN)