{"id":20591791,"url":"https://github.com/mincong-h/algorithm","last_synced_at":"2025-03-06T13:46:45.644Z","repository":{"id":72841319,"uuid":"42199240","full_name":"mincong-h/algorithm","owner":"mincong-h","description":"Learning Algorithm with LeetCode \u0026 HackerRank","archived":false,"fork":false,"pushed_at":"2019-07-01T06:45:11.000Z","size":1220,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-17T00:11:26.743Z","etag":null,"topics":["algorithms","algorithms-datastructures","data-structures","interview","java","leetcode","leetcode-java","leetcode-solutions"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mincong-h.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-09-09T19:14:50.000Z","updated_at":"2024-03-15T11:49:03.000Z","dependencies_parsed_at":"2023-03-09T22:15:16.397Z","dependency_job_id":null,"html_url":"https://github.com/mincong-h/algorithm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mincong-h%2Falgorithm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mincong-h%2Falgorithm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mincong-h%2Falgorithm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mincong-h%2Falgorithm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mincong-h","download_url":"https://codeload.github.com/mincong-h/algorithm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242223023,"owners_count":20092173,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithms","algorithms-datastructures","data-structures","interview","java","leetcode","leetcode-java","leetcode-solutions"],"created_at":"2024-11-16T07:41:41.609Z","updated_at":"2025-03-06T13:46:45.635Z","avatar_url":"https://github.com/mincong-h.png","language":"Java","readme":"# Algorithm\n\nI'm learning algorithm at [LeetCode](https://leetcode.com) and [HackerRank](https://hackerrank.com).\nThis repo contains some techniques and the list of solutions. Solutions are\nwritten as markdown files following the naming convention:\n\n    leetcode/${id}.${name}.md\n\n## CheckList\n\n- Do you understand the question? Can you rephrase it?\n- What is your strategy for the resolution?\n- The estimation of time complexity and space complexity?\n- Illustrate the solution steps using a simple example?\n- Corner cases, are they being considered?\n- Any ideas of further optimization? Saying it rather than writing it, because\n  of time constraint, readability, or other reasons.\n\n## Array\n\n**Boyer–Moore majority vote algorithm.** Finding a majority element that occurs\n≥ N/2 times. Done in O(N) runtime and O(1) space. See\n\u003chttps://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm\u003e.\n\n**Hash code of the array.** Returns a hash code based on the contents of the\nspecified array (overloaded methods: `boolean[]`, `byte[]`, `char[]`,\n`double[]`, `float[]`, `int[]`, `long[]`, `Object[]`, `short[]`):\n\n```java\njava.util.Arrays.hashCode(int[] a);\n```\n\n**2 Pointers (Fast \u0026 Slow).** One slow pointer and one fast pointer. They both move forwards\nat same speed unless in some cases, slow pointer needs to stop. This strategy\ncan be used to manipulate the array in place. Example: [LeetCode 27 Remove\nElement](https://leetcode.com/problems/remove-element/).\n\n**2 Pointers (Left \u0026 Right).** Left pointer moves right, right pointer moves\nleft. :warning: Make sure they stop at the right moment to avoid collapse.\n\n**Find Max (Min).** Do not use sorting to find max or min values from an array.\nThis can be done using variables and in one iteration. Time complexity: O(N).\n\n**Sorting.** Sometimes sorting is useful before addressing the real problem. It\nenables the relationship between neighbour elements. The time complexity is O(N\nlog N).\n\n**Permutation.** The next permutation in lexicographic order can be generated\nusing the method created by Narayana Pandita, see [Wikipedia: Permutation -\nGeneration in lexicographic order](https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order)\n\nRelated problems:\n\n- [31. Next Permuation](https://leetcode.com/problems/next-permutation/)\n\n**Prefix Sum.** In computer science, the prefix sum, cumulative sum, inclusive\nscan, or simply scan of a sequence of numbers x0, x1, x2, ... is a second\nsequence of numbers y0, y1, y2, ..., the sums of prefixes (running totals) of\nthe input sequence. See \u003chttps://en.wikipedia.org/wiki/Prefix_sum\u003e.\n\nRelated problems:\n\n- [974: Subarray Sums Divisible by K](https://leetcode.com/problems/subarray-sums-divisible-by-k/)\n\n## Priority Queue\n\n**Kth Element.** Find the Kth largest or smallest element in a collection or\narray. This can be done using a priority queue with K elements. If looking for\nthe Kth largest element, use a min heap, so that peek (poll) from the priority\nqueue returns the Kth element. If looking for the Kth smallest, use a max heap,\nso that peek (poll) from the priority queue returns the Kth element.\n\nRelated problems:\n\n- [215: Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)\n- [378: Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/)\n- [703. Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/)\n- [973: K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/)\n\n## Hash Table\n\n**Use array as hash table.** If possible, use an array as the reference table\nfor finding the right element. Compared to any `Map` implementation, using array\nis faster and consumes less memory. This is typically possible when the keys are\nASCII characters. You can create an array with 128 elements, for boolean status\n(`boolean[]`), char count (`int[]`), etc.\n\n```java\nint[] counts = new int[128];\nfor (char c : word.toCharArray()) {\n    counts[c]++;\n}\n```\n\nRelated problems:\n\n- [771: Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)\n\n**Use Hashtable to Store References.** Use hashtable to store the target\nreferences. It helps to reduce cost for lookup operation, compared to double\nfor-loop.\n\nRelated problems:\n\n- [1: Two Sum](https://leetcode.com/problems/two-sum/)\n- [560: Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/)\n\n## Linked List\n\n**2 pointers.** One slow pointer (1x speed) and one fast pointer (2x speed). It\nallows to resolve circular issue, because the faster one will be 1 round faster\nafter some moves. 2 pointers strategy can also be used for keeping a distance\nbetween two pointers.\n\nRelated problems:\n\n- [19. Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)\n\n**Changing \"next\" reference.** In sinlge linked list, changing the reference of the\n\"next\" node can be useful for some operations, such as reversing the list.\nHere is an illustration:\n\n```\nInput:   1 -\u003e 2 -\u003e 3 -\u003e 4 -\u003e 5\nOutput:  1 \u003c- 2 \u003c- 3 \u003c- 4 \u003c- 5\n```\n\nRelated problems:\n\n- [92: Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/)\n- [206: Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)\n\n## String\n\n**Two Pointers.** Use two pointers L, R for switching characters.\n\nRelated problems:\n\n- [344: Reverse String](https://leetcode.com/problems/reverse-string/)\n\n**Permutation in String.** Maintain a sliding window `[L, R]` in the string, use\ntwo variables: `int[] stock` and `int todo` to record respectively what are the\nremaining characters available for permutation as ASCII table, and the remaining\nnumber of characters to do to have a match.\n\n- When R (right pointer) moves forward, it consumes more chars from the stock\n  table.\n- When L (left pointer) moves forward, it recover the consumed chars from the\n  stock table.\n\nRelated problems:\n\n- [3: Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)\n- [76: Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)\n- [438: Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/)\n- [567: Permutation in String](https://leetcode.com/problems/permutation-in-string/)\n\n**String construction.** When resolving exercises, it's better to use\n[`java.lang.StringBuilder`](https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html)\nrather than string concatenation using `+`. Even better, use char array `char[]`\nand fill it manually, which is efficient and allows navigation.\n\nConstructor | Description\n:--- | :---\n`StringBuilder()` | Constructor without additional arguments\n`StringBuilder(String)` | Constructor with an initial string\n`StringBuilder(int)` | Constructor with initial capacity\n`String(char[])` | Constructor with a char array\n`String(char[], int, int)` | Constructor with offset and length\n\n## Bit Manipulation\n\n**Power of Two.** An integer is power of two if it is positive and has only one\nbit.\n\n   N | Binary\n---: | :---------------------:\n   1 | ... 0000 0000 0000 0001\n   2 | ... 0000 0000 0000 0010\n   4 | ... 0000 0000 0000 0100\n   8 | ... 0000 0000 0000 1000\n  16 | ... 0000 0000 0001 0000\n\n## Tree\n\nThere are two types traverals: depth-first search (DFS) and breath-first search\n(BFS). For depth-first search, the traversal can be pre-order (NLR), in-order\n(LNR), out-order (RNL), and post-order (LRN).\n\nDepth-first search: pre-order\n\n![preorder traversal](img/220px-Sorted_binary_tree_preorder.svg.png)\n\nDepth-first search: in-order\n\n![inorder traversal](img/220px-Sorted_binary_tree_inorder.svg.png)\n\nDepth-first search: post-order\n\n![postorder traversal](img/220px-Sorted_binary_tree_postorder.svg.png)\n\nBreath-first search:\n\n![breath first search traversal](img/220px-Sorted_binary_tree_breadth-first_traversal.svg.png)\n\n## Graph\n\nIn mathematics, and more specifically in graph theory, a graph is a structure\namounting to a set of objects in which some pairs of the objects are in some\nsense \"related\". Graph can be undirected or directed. The figure below shows an\nexample network with 8 vertices and 10 edges.\n\n![Small Network](img/Small_Network.png)\n\nGraph (undirected graph):\n\n![Undirected](img/220px-Undirected.svg.png)\n\nDirected Graph:\n\n![Directed](img/220px-Directed.svg.png)\n\n## Dynamic Programming\n\nDynamic programming (DP) is a method for solving complex problems by breaking\nthem down into simpler subproblems. Remember the three steps:\n\n1. Defining sub-problems\n2. Finding recurrences\n3. Solving the base cases\n\nFor more detail, see Stanford University Online Lecture CS 97SI:\n\u003chttps://web.stanford.edu/class/cs97si/04-dynamic-programming.pdf\u003e.\n\n## Number\n\n**Enrich Integer Via Bits.** You can provide additional information to an\ninteger by using the unused bits. This is possible when integer is served as an\nemumerate value, e.g. only using 0 as false and 1 as true. This strategy is\nuseful when you need to do something in-place, or you are not allowed to use\nmore complex data structure.\n\nRelated problems:\n\n- [289: Game of Life](https://leetcode.com/problems/game-of-life/)\n\n## Search and Sort\n\n**Custom Binary Search.** In some cases, the problem can be solved by using a\nbinary-search-like strategy to find the target item by eliminating half of the\nitems.\n\nRelated problems:\n\n- [378. Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/)\n\n## Custom Data Structure\n\n**[LRU Cache](https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU).**\nDiscards the least recently used items first. See YouTube video [Implement An\nLRU Cache - The LRU Cache Eviction Policy (\"LRU Cache\" on\nLeetCode)](https://www.youtube.com/watch?v=S6IfqDXWa10) for more detail\nexplanation.\n\nRelated problems:\n\n- [146: LRU Cache](https://leetcode.com/problems/lru-cache/)\n\n## Useful APIs\n\nUseful APIs in Java.\n\nMethod | Description\n:----- | :----------\n[`String(char[] value, int offset, int count): String`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#String-char:A-int-int-) | String constructor, useful for creating a string from a char array.\n[`String#substring(int beginIndex, int endIndex): String`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#substring-int-int-) | Extracts a substring from string.\n\n## Corner Cases\n\nTreeNode:\n\n- Can `root` be `null`?\n\nInteger:\n\n- Boundaries: `Integer.MIN_VALUE` and `Integer.MAX_VALUE` good boundaries?\n  Comparison will fail when the integer itself is one of these values.\n- Risk of overflow?\n- Average for sum of two integers: prefer `start + (end - start) / 2` rather\n  than `(start + end) / 2`, so that the overflow problem can be avoided.\n- Integer division: when both dividend and divisor are integer, the\n  quotient will be an integer too. For example, `3 / 2 = 1`. In order to have\n  an accurate result, you need to ensure either divident or divisor to be a\n  float or a double.\n\nArray:\n\n- Can array be null?\n- The length of the array?\n- Subarray: how to define subarray, at least 1 item or 2 items?\n\n2D Array:\n\n- Coordinate (x, y) or (i, j): which position is (0, 0), what direction is the\n  axes? It's easy to draw diagram on whitebroad using (x, y) like in Math. But\n  in the program, it's easier to use (i, j).\n\nComparator:\n\n- Compare one field after another, if one field is different, compute the diff\n  and return the result. Only when this field is equal on both instances, you\n  can pass to the next field. Be careful about problem\n  [\"Comparison method violates its general contract!\"](https://stackoverflow.com/questions/8327514/)\n\n## HackerRank\n\nToo lazy to add :see_no_evil:\n\n## LeetCode\n\nId  | Problem | Runtime (Java)\n---: | --- | ---:\n1 | [Two Sum](https://leetcode.com/problems/two-sum/) | 2ms\n2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | 2ms\n3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | 14ms\n5 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | 16ms\n7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | 1ms\n9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | 7ms\n11 | [Max Area](https://leetcode.com/problems/container-with-most-water/) | 1ms\n12 | [Integer to roman][12] | 7ms\n13 | [Roman to Integer][13] | 5ms\n14 | [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) | 4ms\n15 | [3Sum](https://leetcode.com/problems/3sum/) | 39ms\n17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | 0ms\n19 | [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | 0ms\n20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | 4ms\n21 | [Merge two sorted lists](https://leetcode.com/problems/merge-two-sorted-lists/) | 5ms\n22 | [Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) | 0ms\n23 | [Merge K Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) | 5ms\n26 | [Remove Duplicates From Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | 1ms\n27 | [Remove Element](https://leetcode.com/problems/remove-element/) | 0ms\n28 | [Implement strStr()](https://leetcode.com/problems/implement-strstr/) | 2ms\n31 | [Next Permutation](https://leetcode.com/problems/next-permutation/) | 0ms\n33 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | 0ms\n34 | [Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) | 0ms\n35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | 0ms\n36 | [Valid sudoku][36] | 7ms\n38 | [Count and say][38] | 4ms\n39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | 5ms\n40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | 4ms\n46 | [Permutations](https://leetcode.com/problems/permutations/) | 3ms\n47 | [Permutations II](https://leetcode.com/problems/permutations-ii/) | 1ms\n48 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | 0ms\n49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | 12ms\n50 | [Pow x-n][50] | 26ms\n53 | [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | 0ms\n54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | 0ms\n56 | [Merge Intervals](https://leetcode.com/problems/merge-intervals/) | 7ms\n58 | [Length of Last Word](https://leetcode.com/problems/length-of-last-word/) | 0ms\n62 | [Unique Paths](https://leetcode.com/problems/unique-paths/) | 0ms\n64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | 2ms\n65 | [Valid Number](https://leetcode.com/problems/valid-number/) | 1ms\n66 | [Plus One](https://leetcode.com/problems/plus-one/) | 0ms\n67 | [Add Binary](https://leetcode.com/problems/add-binary/) | 1ms\n70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | 2ms\n75 | [Sort Colors](https://leetcode.com/problems/sort-colors/) | 0ms\n76 | [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/) | 5ms\n77 | [Combinations](https://leetcode.com/problems/combinations/) | 2ms\n78 | [Subsets](https://leetcode.com/problems/subsets/) | 1ms\n79 | [Word Search](https://leetcode.com/problems/word-search/) | 4ms\n83 | [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) | 0ms\n86 | [Partition List](https://leetcode.com/problems/partition-list/) | 0ms\n88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | 2ms\n90 | [Subsets II](https://leetcode.com/problems/subsets-ii/) | 1ms\n91 | [Decode Ways](https://leetcode.com/problems/decode-ways/) | 1ms\n92 | [Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/) | 0ms\n94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | 0ms\n98 | [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) | 0ms\n100 | [Same Tree](https://leetcode.com/problems/same-tree/) | 1ms\n101 | [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) | 5ms\n102 | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | 1ms\n103 | [Binary Tree Zigzag Level Order Traversal](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/) | 1ms\n104 | [Maximun Depth of a Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | 0ms\n107 | [Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/) | 1ms\n108 | [Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | 0ms\n110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | 1ms\n111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | 0ms\n112 | [Path Sum](https://leetcode.com/problems/path-sum/) | 0ms\n113 | [Path Sum II](https://leetcode.com/problems/path-sum-ii/) | 1ms\n114 | [Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) | 0ms\n118 | [Pascal Triangle](https://leetcode.com/problems/pascals-triangle/) | 0ms\n119 | [Pascal triangle II][119] | 2ms\n120 | [Triangle](https://leetcode.com/problems/triangle/) | 2ms\n121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | 1ms\n122 | [Best time to buy and sell stock II][122] | 2ms\n125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | 2ms\n133 | [Clone Graph](https://leetcode.com/problems/clone-graph/) | 1ms\n136 | [Single Number](https://leetcode.com/problems/single-number/) | 0ms\n138 | [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/) | 1ms\n139 | [Word Break](https://leetcode.com/problems/word-break/) | 2ms\n141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | 0ms\n144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) | 0ms\n146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | 61ms\n151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | 1ms\n152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | 1ms\n153 | [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | 0ms\n155 | [Min Stack](https://leetcode.com/problems/min-stack/) | 47ms\n160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | 1ms\n162 | [Find Peak Element][162] | 1ms\n163 | [Missing ranges][163] | 13ms\n165 | [Compare Version Numbers][165] | 4ms\n167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | 0ms\n168 | [Excel sheet column title][168] | 0ms\n169 | [Majority Element](https://leetcode.com/problems/majority-element/) | 3ms\n171 | [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/) | 1ms\n172 | [Factorial Trailing Zeroes][172] | 2ms\n179 | [Largest Number](https://leetcode.com/problems/largest-number/) | 3ms\n186 | [Reverse Words in a String II][186] | 3ms\n189 | [Rotate array][189] | 1ms\n190 | [Reverse Bits](https://leetcode.com/problems/reverse-bits/) | 1ms\n191 | [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | 0ms\n195 | [Tenth line][195] | 15ms\n198 | [House Robber](https://leetcode.com/problems/house-robber/) | 0ms\n199 | [Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/) | 1ms\n200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | 1ms\n202 | [Happy Number](https://leetcode.com/problems/happy-number/) | 1ms\n203 | [Remove linked list elements][203] | 2ms\n204 | [Count Primes][204] | 32ms\n205 | [Isomorphic strings][205] | 50ms\n206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | 0ms\n215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | 4ms\n217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | 9ms\n219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | 8ms\n221 | [Maximal Square](https://leetcode.com/problems/maximal-square/) | 4ms\n222 | [Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/) | 1ms\n225 | [Implement Stack Using Queues](https://leetcode.com/problems/implement-stack-using-queues/) | 45ms\n226 | [Invert Binary Tree][226] | 0ms\n228 | [Summary ranges][228] | 0ms\n230 | [Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) | 0ms\n231 | [Power of Two](https://leetcode.com/problems/power-of-two/) | 1ms\n232 | [Implement Queue Using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/) | 42ms\n234 | [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) | 1ms\n235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) | 4ms\n236 | [Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) | 5ms\n237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | 0ms\n238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | 1ms\n240 | [Search a 2D matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) | 13ms\n242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | 4ms\n243 | [Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/) | 1ms\n244 | [Shortest word distence II][244] | 27ms\n245 | [Shortest word distance III][245] | 6ms\n246 | [Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/) | 0ms\n252 | [Meeting rooms][252] | 13ms\n253 | [Meeting rooms][253] | 19ms\n256 | [Paint House](https://leetcode.com/problems/paint-house/) | 1ms\n257 | [Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/) | 7ms\n258 | [Add Digits](https://leetcode.com/problems/add-digits/) | 1ms\n260 | [Single number III][260] | 10ms\n263 | [Ugly Number](https://leetcode.com/problems/ugly-number/) | 1ms\n266 | [Palindrome permutation][266] | 1ms\n268 | [Missing Number](https://leetcode.com/problems/missing-number/) | 0ms\n270 | [Closest Binary Search Tree Value](https://leetcode.com/problems/closest-binary-search-tree-value/) | 0ms\n271 | [Encode and decode strings][271] | 15ms\n274 | [H-Index][274] | 3ms\n278 | [First Bad Version](https://leetcode.com/problems/first-bad-version/) | 10ms\n279 | [Perfect squares][279] | 58ms\n280 | [Wiggle sort][280] | 1ms\n283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/) | 1ms\n288 | [Unique word abbreviation][288] | 79ms\n289 | [Game of Life](https://leetcode.com/problems/game-of-life/) | 0ms\n293 | [Flip game][293] | 1ms\n297 | [Serialize and Deserialize Binary Tree][297] | 23ms\n298 | [Binary tree longest consecuritve sequence][298] | 3ms\n300 | [Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) | 1ms\n303 | [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/) | 52ms\n304 | [Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/) | 57 ms\n311 | [Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/) | 0ms\n318 | [Maximum product of word lengths][318] | 71ms\n320 | [Generalized abbreviation][320] | 21ms\n322 | [Coin Change](https://leetcode.com/problems/coin-change/) | 9ms\n326 | [Power of Three][326] | 21ms\n332 | [Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary/) | 36ms\n338 | [Counting Bits](https://leetcode.com/problems/counting-bits/) | 1ms\n342 | [Power of Four](https://leetcode.com/problems/power-of-four/) | 1ms\n343 | [Integer break][343] | 0ms\n344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | 1ms\n345 | [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | 2ms\n346 | [Moving Average From Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/) | 69ms\n347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | 10ms\n349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | 2ms\n350 | [Intersection of two arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | 2ms\n351 | [Android unlock pattern][351] | 13ms\n357 | [Count numbers with unique digits][357] | 0ms\n359 | [Logger rate limiter][359] | 187ms\n360 | [Sort transformed array][360] | 1ms\n367 | [Valid perfect square](https://leetcode.com/problems/valid-perfect-square/) | 400ms\n369 | [Plus one linked list][369] | 1ms\n374 | [Guess number higher or lower][374] | 2ms\n378 | [Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/) | 1ms\n382 | [Linked list random node][382] | 146ms\n383 | [Ransom note][383] | 18ms\n387 | [First Unique Character in a String][387] | 31ms\n388 | [Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/) | 0ms\n389 | [Find the difference][389] | 9ms\n392 | [Is Subsequence](https://leetcode.com/problems/is-subsequence/) | 0ms\n394 | [Decode String](https://leetcode.com/problems/decode-string/) | 0ms\n398 | [Find the Difference](https://leetcode.com/problems/find-the-difference/) | 1ms\n400 | [Nth Digit][400] | 7ms\n401 | [Binary watch][401] | 1ms\n402 | [Remove K digits][402] | 29ms\n404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | 3ms\n405 | [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) | 0ms\n406 | [Queue reconstruction by height][406] | 15ms\n408 | [Valid Word Abbreviation][408] | 22ms\n409 | [Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | 2ms\n412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | 1ms\n414 | [Third Maximum Number][414] | 6ms\n415 | [Add Strings](https://leetcode.com/problems/add-strings/) | 9ms\n429 | [N-ary Tree Level Order Traveral](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | 4ms\n437 | [Path Sum III](https://leetcode.com/problems/path-sum-iii/) | 6ms\n438 | [Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/) | 9ms\n443 | [String Compression](https://leetcode.com/problems/string-compression/) | 1ms\n448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | 6ms\n451 | [Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/) | 6ms\n461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance/) | 0ms\n463 | [Island Perimeter](https://leetcode.com/problems/island-perimeter/) | 53ms\n476 | [Number Complement](https://leetcode.com/problems/number-complement/) | 5ms\n482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/) | 8ms\n485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | 4ms\n500 | [Keyboard Row](https://leetcode.com/problems/keyboard-row/) | 1ms\n509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | 12ms\n520 | [Detect Capital](https://leetcode.com/problems/detect-capital/) | 14ms\n541 | [Reverse String II](https://leetcode.com/problems/reverse-string-ii/) | 2ms\n543 | [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/) | 0ms\n559 | [Maximum Depth of N-ary Tree](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/) | 2ms\n557 | [Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/) | 5ms\n560 | [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | 12ms\n561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | 20ms\n572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/) | 6ms\n576 | [Permutation in String](https://leetcode.com/problems/permutation-in-string/) | 7ms\n581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/submissions/) | 8ms\n589 | [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | 8ms\n590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | 4ms\n595 | [Big Countries](https://leetcode.com/problems/big-countries/) | 1672ms\n606 | [Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree/) | 12ms\n617 | [Merge Two Binary Tree][617] | 6ms\n621 | [Task Scheduler](https://leetcode.com/problems/task-scheduler/) | 4ms\n623 | [Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/) | 4ms\n637 | [Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/) | 2ms\n647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | 1ms\n654 | [Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | 6ms\n657 | [Robot returns to Origin](https://leetcode.com/problems/robot-return-to-origin/) | 9ms\n669 | [Trim a Binary Search Tree](https://leetcode.com/problems/trim-a-binary-search-tree/) | 0ms\n671 | [Second Minimum Node In a Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/) | 2ms\n674 | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | 2ms\n680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | 7ms\n682 | [Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers/) | 3ms\n692 | [Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/) | 6ms\n693 | [Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits/) | 0ms\n695 | [Max Area of Island](https://leetcode.com/problems/max-area-of-island/) | 2ms\n700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | 2ms\n703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/) | 62ms\n709 | [To Lower Case][709] | 0ms\n719 | [Max Stack](https://leetcode.com/problems/max-stack/) | 89ms\n746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/) | 1ms\n771 | [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/) | 0ms\n784 | [Letter Case Permutation](https://leetcode.com/problems/letter-case-permutation/) | 1ms\n790 | [Rotate String](https://leetcode.com/problems/rotate-string/submissions/) | 0ms\n804 | [Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/) | 4ms\n821 | [Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/) | 4ms\n832 | [Flipping an Image](https://leetcode.com/problems/flipping-an-image/) | 0ms\n844 | [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/) | 0ms\n872 | [Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/) | 0ms\n876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | 1ms\n890 | [Find and Replace Pattern](https://leetcode.com/problems/find-and-replace-pattern/) | 2ms\n891 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | 4ms\n896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array/) | 12ms\n905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | 12ms\n916 | [Word Subsets](https://leetcode.com/problems/word-subsets/submissions/) | 23ms\n917 | [Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/) | 3ms\n922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | 4ms\n925 | [Long Pressed Name](https://leetcode.com/problems/long-pressed-name/) | 3ms\n929 | [Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/) | 10ms\n931 | [Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/) | 3ms\n935 | [Knight Dialer](https://leetcode.com/problems/knight-dialer/) | 20ms\n937 | [Reorder Log Files](https://leetcode.com/problems/reorder-log-files/) | 2ms\n938 | [Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/) | 0ms\n953 | [Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/) | 3ms\n961 | [N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/) | 4ms\n965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | 3ms\n973 | [K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/) | 20ms\n974 | [Subarray Sums Divisible by K](https://leetcode.com/problems/subarray-sums-divisible-by-k/) | 4ms\n977 | [Squares of a sorted array](https://leetcode.com/problems/squares-of-a-sorted-array/) | 20ms\n981 | [Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/) | 211ms\n1002 | [Find Common Characters](https://leetcode.com/problems/find-common-characters/) | 2ms\n1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | 1ms\n\n## References\n\n- Oracle, \"String (Java Platform SE 8)\", _Oracle Documentation_, 2019.\n  \u003chttps://docs.oracle.com/javase/8/docs/api/java/lang/String.html\u003e\n- Jaehyun Park Ph.D., \"Dynamic Programming - CS 97SI\",\n  _Stanford University_, 2015.\n  \u003chttps://web.stanford.edu/class/cs97si/04-dynamic-programming.pdf\u003e\n- Wikipedia, \"Cache replacement policies\", _Wikipedia_, 2019.\n  \u003chttps://en.wikipedia.org/wiki/Cache_replacement_policies\u003e\n- Wikipedia, \"Graph (discrete mathematics)\", _Wikipedia_, 2019.\n  \u003chttps://en.wikipedia.org/wiki/Graph_(discrete_mathematics)\u003e\n- Wikipedia, \"Permutation - Generation in lexicographic order\", _Wikipedia_, 2019.\n  \u003chttps://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order\u003e\n- Wikipedia, \"Prefix sum\", _Wikipedia_, 2019.\n  \u003chttps://en.wikipedia.org/wiki/Prefix_sum\u003e\n- Wikipedia, \"Tree traversal\", _Wikipedia_, 2019.\n  \u003chttps://en.wikipedia.org/wiki/Tree_traversal\u003e\n- Wikipedia, \"Vertex (graph theory)\", _Wikipedia_, 2019.\n  \u003chttps://en.wikipedia.org/wiki/Vertex_(graph_theory)\u003e\n- Back To Back SWE, \"Implement An LRU Cache - The LRU Cache Eviction Policy (LRU\n  Cache on LeetCode)\", _YouTube_, 2019.\n  \u003chttps://www.youtube.com/watch?v=S6IfqDXWa10\u003e\n\n[12]: https://leetcode.com/problems/integer-to-roman/\n[13]: https://leetcode.com/problems/roman-to-integer/\n[36]: https://leetcode.com/problems/valid-sudoku/\n[38]: https://leetcode.com/problems/count-and-say/\n[50]: https://leetcode.com/problems/powx-n/\n[58]: https://leetcode.com/problems/length-of-last-word/\n[66]: https://leetcode.com/problems/plus-one/\n[94]: https://leetcode.com/problems/binary-tree-inorder-traversal/\n[119]: https://leetcode.com/problems/pascals-triangle-ii/\n[122]: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/\n[162]: https://leetcode.com/problems/find-peak-element/\n[163]: https://leetcode.com/problems/missing-ranges/\n[165]: https://leetcode.com/problems/compare-version-numbers/\n[168]: https://leetcode.com/problems/excel-sheet-column-title/\n[172]: https://leetcode.com/problems/factorial-trailing-zeroes/\n[186]: https://leetcode.com/problems/reverse-words-in-a-string-ii/\n[189]: https://leetcode.com/problems/rotate-array/\n[195]: https://leetcode.com/problems/tenth-line/\n[203]: https://leetcode.com/problems/remove-linked-list-elements/\n[204]: https://leetcode.com/problems/count-primes/\n[205]: https://leetcode.com/problems/isomorphic-strings/\n[206]: https://leetcode.com/problems/reverse-linked-list/\n[225]: https://leetcode.com/problems/implement-stack-using-queues/\n[226]: https://leetcode.com/problems/invert-binary-tree/\n[228]: https://leetcode.com/problems/summary-ranges/\n[240]: https://leetcode.com/problems/search-a-2d-matrix-ii/\n[244]: https://leetcode.com/problems/shortest-word-distance-ii/\n[245]: https://leetcode.com/problems/shortest-word-distance-iii/\n[252]: https://leetcode.com/problems/meeting-rooms/\n[253]: https://leetcode.com/problems/meeting-rooms-ii/\n[260]: https://leetcode.com/problems/single-number-iii/\n[266]: https://leetcode.com/problems/palindrome-permutation/\n[270]: https://leetcode.com/problems/closest-binary-search-tree-value/\n[271]: https://leetcode.com/problems/encode-and-decode-strings/\n[274]: https://leetcode.com/problems/h-index/\n[279]: https://leetcode.com/problems/perfect-squares/\n[280]: https://leetcode.com/problems/wiggle-sort/\n[288]: https://leetcode.com/problems/unique-word-abbreviation/\n[293]: https://leetcode.com/problems/flip-game/\n[297]: https://leetcode.com/problems/serialize-and-deserialize-binary-tree/\n[298]: https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/\n[318]: https://leetcode.com/problems/maximum-product-of-word-lengths/\n[320]: https://leetcode.com/problems/generalized-abbreviation/\n[326]: https://leetcode.com/problems/power-of-three/\n[343]: https://leetcode.com/problems/integer-break/\n[351]: https://leetcode.com/problems/android-unlock-patterns/\n[357]: https://leetcode.com/problems/count-numbers-with-unique-digits/\n[359]: https://leetcode.com/problems/logger-rate-limiter/\n[360]: https://leetcode.com/problems/sort-transformed-array/\n[369]: https://leetcode.com/problems/plus-one-linked-list/\n[374]: https://leetcode.com/problems/guess-number-higher-or-lower/\n[382]: https://leetcode.com/problems/linked-list-random-node/\n[383]: https://leetcode.com/problems/ransom-note/\n[387]: https://leetcode.com/problems/first-unique-character-in-a-string/\n[388]: https://leetcode.com/problems/longest-absolute-file-path/\n[389]: https://leetcode.com/problems/find-the-difference/\n[400]: https://leetcode.com/problems/nth-digit/\n[401]: https://leetcode.com/problems/binary-watch/\n[402]: https://leetcode.com/problems/remove-k-digits/\n[406]: https://leetcode.com/problems/queue-reconstruction-by-height/\n[408]: https://leetcode.com/problems/valid-word-abbreviation/\n[414]: https://leetcode.com/problems/third-maximum-number/\n[617]: https://leetcode.com/problems/merge-two-binary-trees/\n[709]: https://leetcode.com/problems/to-lower-case/\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmincong-h%2Falgorithm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmincong-h%2Falgorithm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmincong-h%2Falgorithm/lists"}