This is a respository containing my Python3 solutions to LeetCode's daily problems.
I intend to revist each problem at least once to improve upon my intial solution, although I want at least a day to pass between writing my initial solution and attempting an improved solution. The notes column will call out either items I'd like to attend to on an attempt at an improved solution or else will contain snippets of things I learned or found useful in solving the problem more elegantly.
| Problem | Initial Solution | Improved Solution | Notes |
|---|---|---|---|
| https://leetcode.com/problems/length-of-last-word/ | Initial Solution | --- | To do: Construct a more elegant, one line solution exploiting existing string methods more fully |
| https://leetcode.com/problems/word-break/ | Initial Solution | --- | I'm certain the optimal solution uses tries. After reviewing tries I'll reattempt this problem. https://en.wikipedia.org/wiki/Trie |
| https://leetcode.com/problems/first-missing-positive/ | Initial Solution | --- | Although my initial solution is accepted by leetcode, I'm utilizing memory the optimal solution almost certainly wouldn't. Upon revisiting this problem, I'd like to ensure I utilize as little memory as possible (perhaps performing in place operations on the array?). |
| https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3480/ | Initial Solution | --- | The "to_remove" list is cumbersome and inelegant. I would like to use a queue when reattempting this problem. |
| https://leetcode.com/problems/combination-sum/ | Initial Solution | --- | I'd like to solve this in another way upon my reattempt. |
| https://leetcode.com/problems/k-diff-pairs-in-an-array/description/ | Initial Solution | --- | To do: Improve time complexity |
| https://leetcode.com/problems/remove-covered-intervals/ | Initial Solution | Improved Soltuion | I'd like to write a solution that modifies the "intervals" list in place by deleting out intervals that are encompassed by some other interval in the list |
| https://leetcode.com/problems/complement-of-base-10-integer/ | Initial Solution | --- | Upon my reattempt I would like to avoid using the bin() funciton. It was convienent but I think building the machinery myself to convert from base ten to binary would be more helpful practice than simply invoking a function I already know about. |
| https://leetcode.com/problems/insert-into-a-binary-search-tree/ | Initial Soltuion | --- | Note: This solution initially unexpectedly failed. I suspect a bug in the leetcode platform? An empty tree was passed as a test case and insert function I wrote didn't handle it appropriately (namely, the if clause that checks whether the root node is None didn't seem to fire). An acceptance was achieved after simply including that if clause again at the end of the program. I'd like to test this further and understand why this unexpected behavior is occuring. |
| https://leetcode.com/problems/binary-search/description/ | Initial Solution | --- | My initial solution is a brute force method. I would like to implement binary search upon my reattempt. |
| https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/ | Initial Solution | --- | This solution beat almost 80% of Python3 solutions. I'd like to improve this measure. |
| https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3496/ | Initial Solution | --- | My initial solution doubles the total memory usage required by constructing the solution array out of the appropriate slices of the original array and then copying, element wise, this solution array over the original array. It's an inelegant solution that permited me to quickly solve the problem using the slice syntax I'm familiar with. Upon my reattempt I should like to do away with this second array and simply operate on the original array in-place. |
| https://leetcode.com/problems/search-a-2d-matrix/ | Initial Solution | --- | There are some optimizations that can be made in searching the rows for values (treat each row as a binary search, which it is by construciton). |
| https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/ | Initial Solution | --- | This solution beat almost 80% of Python3 solutions w/r/t time complexity. I'd like to improve that measure upon my reattempt. |
| https://leetcode.com/problems/clone-graph/ | Initial Solution | --- | I'd like to implement a DFS method next. |
| https://leetcode.com/problems/minimum-height-trees/ | Initial Solution | --- | How would I solve this if I didn't know that there exist, at most, 2 Minimum Height Trees for any given graph? |
| https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position/ | Initial Solution | --- | I'd like to decrease runtime |
| https://leetcode.com/problems/check-array-formation-through-concatenation/ | Initial Solution | --- | What's one improvement that could be made to decrease total runtime? |
| https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/ | Initial Solution | --- | This solution exploits the fact that these binary trees only contain distinct values. It would be better to write a program that could handle duplicate values. |
| https://leetcode.com/problems/beautiful-arrangement/ | Initial Solution | --- | Solve differently and explain tradeoffs. |
| https://leetcode.com/problems/merge-two-sorted-lists/ | Initial Solution | --- | I'd like to implement a solution that uses less memory. |
| https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ | Initial Solution | --- | What's another way of solving this? |
| https://leetcode.com/problems/kth-missing-positive-number/ | Initial Solution | --- | The runtime of this solution beats only about 30% of entries. Write another solution that improves upon this. |
| https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/579/week-1-january-1st-january-7th/3595/ | Initial Solution | --- | This solution does well on total memeory usage but beats only about 20% of other solutions w/r/t time complexity. Write another solution that improves upon this. |
| https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/ | Initial Solution | --- | My solution certainly isn't optimal. How might I solve this another way that makes at least one improvement in either space or time complexity? |
| https://leetcode.com/problems/word-ladder/ | Initial Solution | --- | Try using tries. |
| https://leetcode.com/problems/create-sorted-array-through-instructions/ | Initial Solution | --- | I rely heavily here upon SortedList() and the bisect module. Would be good to independetly implement these both in the first rework of this problem. |
| https://leetcode.com/problems/merge-sorted-array/ | Initial Solution | --- | This solution is very much a first attempt and that fact is reflected in its considerable running time. Aim to push this metric down in a future rework. |
| https://leetcode.com/problems/add-two-numbers/ | Initial Solution | --- | How could I improve the runtime? The memory usage? |
| https://leetcode.com/problems/boats-to-save-people/ | Initial Solution | Improved Solution | What's another way I could write a solution that passes all test cases? Would binary search work in this case just as well as my improved solution? |
| https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/ | Initial Solution | --- | How could I solve this in another way? |
| https://leetcode.com/problems/get-maximum-in-generated-array/ | Initial Solution | --- | How could I solve this by iteratively building the array with append? |
| https://leetcode.com/problems/kth-largest-element-in-an-array/ | Initial Solution | --- | This solution is fast (beating about 96% of other entries) but fails to beat more than about 45% of other entires re: memory usage. How could memory usage be improved? |
| https://leetcode.com/problems/count-sorted-vowel-strings/ | Initial Solution | --- | How could I solve this differently? |
| https://leetcode.com/problems/max-number-of-k-sum-pairs/ | Initial Solution | Improved Solution | How could I improve that initial solution further without using a dictionary? |
| https://leetcode.com/problems/longest-palindromic-substring/ | Initial Solution | --- | How could I solve this differently? |
| https://leetcode.com/problems/valid-parentheses/ | Initial Solution | --- | How could I solve this problem differently? |
| https://leetcode.com/problems/find-the-most-competitive-subsequence/ | Initial Solution | --- | Could I solve this as efficiently in another manner? |
| https://leetcode.com/problems/determine-if-two-strings-are-close/ | Initial Solution | --- | How could I solve this so as to minimize runtime. |
| https://leetcode.com/problems/merge-k-sorted-lists | Initial Solution | Improved Solution | My Initial Solution produces a TLE error on LeetCode. It passes only 132 of 133 test cases. My Improved Solution uses a heap. How could I solve this in another way? |
| https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/ | Initial Solution | --- | How could I solve this problem so as to minimize space and time complexity? |
| https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/ | Initial Solution | --- | How could I write this so as to improve runtime? To reduce its memory footprint? |
| https://leetcode.com/problems/smallest-string-with-a-given-numeric-value | Initial Solution | --- | My initial solution isn't particularly efficient. How could I improve both space and time complexity? |
| https://leetcode.com/problems/number-of-1-bits/ | Initial Solution | --- | When I initially attempted this problem, I cast n to a string as s = str(n). I encountered unexpected behavior: namely, only the rightmost cluster of ones was "sticking." So, for example, n = 110010001011 became s = '11'. I assume this was an error in the platform. I'd like to understand why this was happening. |
| https://leetcode.com/problems/linked-list-cycle | Initial Solution | --- | This solution isn't particularly elegant. Upon a rewrite, I should like to improve the runtime. |
| https://leetcode.com/problems/simplify-path | Initial Solution | --- | Approaching this problem in the way that I've done here (by using a stack) is straightforwardly correct. But what other approach, if any, might I take? |
| https://leetcode.com/problems/shortest-distance-to-a-character/ | Initial Solution | --- | What's another algorithm I could use to solve this? |
| https://leetcode.com/problems/peeking-iterator/ | Initial Solution | --- | How could I write this differently? |
| https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/ | Initial Solution | --- | How could I reduce runtime? Reduce memory usage? |
| https://leetcode.com/problems/letter-case-permutation/ | Initial Solution | --- | I relied entirely on "product". How would I write this from scrtach without relying upon inbuilt methods? |
| https://leetcode.com/problems/container-with-most-water/ | Initial Solution | --- | How could I solve this another way? |
| https://leetcode.com/problems/remove-palindromic-subsequences/ | Initial Solution | --- | It would be good to solve a modified version of the problem wherein one could only delete subarrays instead of being able to delete subsequences. |
| https://leetcode.com/problems/verifying-an-alien-dictionary/ | Initial Solution | --- | It would be worthwhile to solve this without leveraging the built-in functions I'm using here (zip, ziplongest at least). |
| https://leetcode.com/problems/fibonacci-number/ | Initial Solution | I've implemented another solution, which is worse than my initial solution. It is inlcuded as a comment at the end of the Initial Solution link. | Runtime: 28 ms, faster than 84.54% of Python3 online submissions for Fibonacci Number. Memory Usage: 14.2 MB, less than 41.72% of Python3 online submissions for Fibonacci Number. |
| https://leetcode.com/problems/fibonacci-number/ | Initial Solution | Improved Solution | Runtime: 24 ms, faster than 94.50% of Python3 online submissions for N-th Tribonacci Number. Memory Usage: 14.3 MB, less than 45.27% of Python3 online submissions for N-th Tribonacci Number. |
| https://leetcode.com/problems/combine-two-tables | Initial Solution | --- | Runtime: 302 ms, faster than 94.80% of MySQL online submissions for Combine Two Tables. Memory Usage: 0B, less than 100.00% of MySQL online submissions for Combine Two Tables. |
| https://leetcode.com/problems/two-sum/ | Initial Solution | --- | Runtime: Runtime: 58 ms Memory Usage: 14.4 MB. |
| https://leetcode.com/problems/palindrome-number/ | Initial Solution | --- | Runtime: 121 ms, faster than 5.29% of Swift online submissions for Palindrome Number. Memory Usage: 14.5 MB, less than 7.20% of Swift online submissions for Palindrome Number. |
| https://leetcode.com/problems/plus-one/ | Initial Solution | --- | ERROR: process exited with signal SIGILL. // I believe this is a solution. TO DO: 1) test with Xcode and determine whether this a limitation or bug in LeetCode's platform. 2) Write another, different solution that succeeds on LeetCode's platform. |
| https://leetcode.com/problems/longest-common-prefix | Initial Solution | --- | Runtime: 45 ms, faster than 57.52% of Python3 online submissions for Longest Common Prefix.Memory Usage: 14 MB, less than 50.87% of Python3 online submissions for Longest Common Prefix. |
| https://leetcode.com/problems/median-of-two-sorted-arrays | Initial Solution | --- | Runtime: 93 ms, faster than 91.24% of Python3 online submissions for Median of Two Sorted Arrays. Memory Usage: 14.2 MB, less than 68.93% of Python3 online submissions for Median of Two Sorted Arrays. |
| https://leetcode.com/problems/min-cost-climbing-stairs/ | Initial Solution | Solved again (using the same strategy, but the code is a bit more readable and intuitive) | Runtime: 87 ms Memory Usage: 14 MB |
| https://leetcode.com/problems/running-sum-of-1d-array/ | Initial Solution | --- | Runtime: 71 ms, faster than 35.88% of Python3 online submissions for Running Sum of 1d Array. Memory Usage: 14.2 MB, less than 27.31% of Python3 online submissions for Running Sum of 1d Array. |
| https://leetcode.com/problems/find-pivot-index/ | Initial Solution | --- | Runtime: 324 ms, faster than 16.72% of Python3 online submissions for Find Pivot Index. Memory Usage: 15.3 MB, less than 48.84% of Python3 online submissions for Find Pivot Index. |
| https://leetcode.com/problems/remove-duplicates-from-sorted-array/ | Initial Solution | --- | Runtime 149 ms Beats 15.23% Memory 15.6 MB Beats 9.22% |
| https://leetcode.com/problems/remove-element/ | Initial Solution | --- | Runtime 37 ms Beats 44.15% Memory 13.9 MB Beats 48.13% |
| https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string | Initial Solution | --- | Runtime 27 ms Beats 88.93% Memory 13.8 MB Beats 50.18% |
| https://leetcode.com/problems/search-insert-position | Initial Solution | --- | Runtime 146 ms Beats 5.3% Memory 14.5 MB Beats 97.73% |
| https://leetcode.com/problems/plus-one/ | Initial Solution | --- | Runtime 37 ms Beats 40.99% Memory 14 MB Beats 5.53% |