Skip to content

DmitryNaimark/leetcode-solutions-python

Repository files navigation

LeetCode Solutions (Python)

This repository contains solutions for LeetCode problems I've solved(using Python), links to official(and clever unofficial) solutions.

Solutions are grouped by Topics(see topics folder).

Feel free to Fork, Clone or Star this repository.

Also, check out my JavaScript Repository, which contains JS solutions for all problems below and a lot more.

Difficulty Title Tags My Solutions LeetCode Solution Other cool solutions Solved on my own? Date
1. Two Sum HashTable, Array Simple HashTable 2019-10-15
276. Paint Fence DP, Probability, Math - 1) Decent explanation Probability, DP 2019-10-16
496. Next Greater Element I Stack, Monotonic Stack, deque Monotonic Stack 2019-10-17
1232. Check If It Is a Straight Line Geometry, Math - 1) Check slopes, Python "all",

2) Is Triangle area == 0
Formulas: Slopes, Triangle area 2019-10-21
1233. Remove Sub-Folders from the Filesystem String, Substring, HashSet, Sorting - 1) 1. Sort by length -> use HashSet 2. Sort lexic. -> compare only with previous potentian parent Difficult time/space estim., interesting solutions 2019-10-22
232. Implement Queue using Stacks Stack, Queue 2 Stacks(1 reversed) to get amortized O(1) pop 2019-10-22
674. Longest Continuous Increasing Subsequence Array, Subseq. - Anchor technique, Sliding Window 2019-10-22
120. Triangle DP, Bottom-Up DP - 1) Bottom-up explanation DP Bottom-up, In-place 2019-10-24
1022. Sum of Root To Leaf Binary Numbers Tree, Recursive, Iterative, Stack - Stack with Tuple or 2 values, Iterative DFS 2019-10-24
129. Sum Root to Leaf Numbers Tree, Recursive, Iterative, Stack - Same as #1022 2019-10-24
124. Binary Tree Maximum Path Sum Tree, Path, nonlocal, Recursive Max gain idea: left_sum = max(left_sum, 0), to shorten code 2019-10-25
1171. Remove Zero Sum Consecutive Nodes from Linked List LinkedList, HashTable, Prefix Sum, Delete - 1) Smart two-pass solution (last),

2) Explanation picture
No memory leak in GC, smart two pass 2019-10-29
733. Flood Fill Matrix, DFS, BFS, Iterative, Recursive Good template for DFS, BFS problems 2019-10-29
443. String Compression String, Two Pointers Iterate and check the next to handle last char, Iterate over digits without split 2019-10-29
138. Copy List with Random Pointer LinkedList, Default Dict, HashTable, Recursive 1) Awesome use of Default Dict, great one pass, two pass code,

2) O(1) official solution explained
1. No need for new List, store nodes in Dict only 2. Default Dict - create node on the fly 2019-10-30
594. Longest Harmonious Subseq. Array, HashTable, Counter, List Compreh., Sorting Tricky iteration after Sorting, Counter + List Compreh. 2019-11-04
548. Split Array with Equal Sum Array, HashTable, Prefix Sum 1) Short Python solutions with "any", "check", slicing, list compreh. Prefix sums, HashTable, Skip recalculating Zeros 2019-11-06
1254. Number of Closed Islands Matrix, BFS, DFS - Fill from edges, BFS, DFS 2019-12-12
121. Best Time to Buy and Sell Stock Array, Rolling min Rolling min 2019-12-14
217. Contains Duplicate HashTable, Sorting Sorting = better Space complexity 2019-12-14
56. Merge Intervals Intervals, Merging Sort by start only, compare with last added 2019-12-23
238. Product of Array Except Self Array, Prefix Product, Rolling Product Idea to use prod[i] as product to the left excluding prod[i] 2019-12-30
53. Maximum Subarray Array, Kadane, Divide and Conquer Kadane, Suboptimal non-obvious Divide and Conquer 2020-01-01
152. Maximum Product Subarray Array, Rolling Products - Rolling Products, swap min and max 2020-01-01
153. Find Minimum in Rotated Sorted Array Array, Binary Search Invariants: 1. [-Inf, left], [right, Inf] ascending 2. min is inside [left, right] 2020-01-01
33. Search in Rotated Sorted Array Array, Binary Search 1) Clean Binary Search conditions for one pass Complex one-pass solution 2020-01-04
15. 3Sum Array, Two Pointers - 1) Sorting, two pointers solution explained A lot of tricks to stop/skip in the loop 2020-01-04
11. Container With Most Water Array, Two Pointers, Area Two pointers, move shorter 2020-01-05
371. Sum of Two Integers Bitwise, Xor, Shift, And, Two's Compl. - 1) Explanation for 32-bit languages (JS),

2) Python mask fix for infinite bits in two's compl.
Not easy: Xor, Shift, Carry. Python TLE for negative numbers, mask workaround 2020-01-06
191. Number of 1 Bits Bitwise 1. check if every bit is 1, or 2. use n &= n - 1 to remove right-most 1 2020-01-06
268. Missing Number Bitwise, XOR, Gauss, Arithmetic Progression, Binary Search 1) Binary search solutions (in post and comments) Xor is best, careful with Binary Search after sorting 2020-01-10
338. Counting Bits Bitwise, DP, Remove bit: left, right, meaningful DP, remove bit from the left, right, right-most 1 2020-01-10
190. Reverse Bits Bitwise, Binary, String reverse - Handle left-most zeros 2020-01-10
70. Climbing Stairs DP Easy DP 2020-01-21
322. Coin Change DP, Top-down, Bottom-up Top-down, Bottom-up DP 2020-01-21
300. Longest Increasing Subsequence DP Difficult binary search official solution 2020-01-21
168. Excel Sheet Column Title String, Convertion, Radix - 1) Offset by 1: Radix 26: 1-26, Radix 10: 0-9,

2) n -= 1 to get rid of offset,

3) Another good explanation
Offset by 1 in Radix 2020-02-01
225. Implement Stack using Queues Queue, Stack Use queue and rotate 2020-02-03
269. Alien Dictionary Graph, Khan, cycle, directed - 1) Good code, explanation in comments,

2) Decent explanation
Difficult: Khan algo, cycle detection, degree in graph 2020-03-11
1389. Create Target Array in the Given Order Array - Simple insert 2020-03-22
1143. Longest Common Subsequence DP, String, Bottom-up - Simple DP, DP space impr. 2020-04-12

About

Python Solutions for LeetCode problems.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published