kshannon / Leetcode-Solutions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Leetcode-Solutions

PYTHON

Array

# Title Solution Time Space Difficulty Tag Note
0011 Container With Most Water Python O(n) O(1) Medium
0015 3 Sum Python O(n^2) O(1) Medium Two Pointers
0026 Remove Duplicates from Sorted Array Python O(n) O(1) Easy Two Pointers
0027 Remove Element Python O(n) O(1) Easy
0031 Next Permutation Python O(n) O(1) Medium Tricky
0048 Rotate Image Python O(n^2) O(1) Medium
0054 Spiral Matrix Python O(m * n) O(1) Medium
0059 Spiral Matrix II Python O(n^2) O(1) Medium
0066 Plus One Python O(n) O(1) Easy
0073 Set Matrix Zeroes Python O(m * n) O(1) Medium
0118 Pascal's Triangle Python O(n^2) O(1) Easy
0110 Pascal's Triangle C++ Python O(n^2) O(1) Easy
0121 Best Time to Buy and Sell Stock Python O(n) O(1) Easy
0128 Longest Consecutive Sequence Python O(n) O(n) Medium Tricky
0157 Read N Characters Given Read4 Python O(n) O(1) Easy πŸ”’
0163 Missing Ranges Python O(n) O(1) Medium πŸ”’
0189 Rotate Array Python O(n) O(1) Medium
0215 Kth Largest Element in an Array Python O(n) ~ O(n^2) O(1) Medium EPI
0238 Product of Array Except Self Python O(n) O(1) Medium LintCode
0240 Search a 2D Matrix II Python O(m + n) O(1) Medium EPI, LintCode
0243 Shortest Word Distance Python O(n) O(1) Easy πŸ”’
0289 Game of Life Python O(m * n) O(1) Medium
0442 Find All Duplicates in an Array Python O(n) O(1) Medium
0448 Find All Numbers Disappeared in an Array Python O(n) O(1) Easy
0453 Minimum Moves to Equal Array Elements Python O(n) O(1) Medium πŸ”’
0462 Minimum Moves to Equal Array Elements II Python O(n) O(1) Medium πŸ”’
0498 Diagonal Traverse Python O(n) O(n) Medium
0532 K-dff Pairs in an Array Python O(logn) O(N) Medium
0566 Reshape the Matrix Python O(n) O(n) Easy
0605 Can Place Flowers Python O(n) O(1) Easy
1200 Minimum Absolute Difference Python O(nlogn) O(n) Easy
1475 Final Prices With a Special Discount in a Shop Python O(n) O(n) Easy Mono Stack
1629 Slowest Key Python O(n) O(1) Easy
9993 Count Pairs Array Whose Sum Less X Python O(n) O(n) Easy MLE
9994 N largest elements from a list Python O(n) O(n) Easy MLE
9995 Sparse Matrix Multiplication Python O(n) O(n) Easy FB
9996 Check Given Array Contains Duplicate Elements within K Distance Python O(n^2) on average O(n) Easy FB
9998 Amazon Fresh Promotion Python O(n^2) on average O(n) Medium

Backtracking

# Title Solution Time Space Difficulty Tag Note
0017 Letter Combinations of a Phone Number Python O(n * 4^n) O(n) Medium
0022 Generate Parentheses Python O(4^n / n^(3/2)) O(n) Medium
0078 Subsets Python O(n * 2^n) O(1) Medium
0079 Word Search Python O(m * n * 3^l) O(l) Medium
0090 Subsets II Python O(n * 2^n) O(1) Medium

Binary Heap

# Title Solution Time Space Difficulty Tag Note
0703 Kth Largest Element in a Stream Python O(nlogk) O(k) Easy
1046 Last Stone Weight Python O(nlogn) O(n) Easy

Binary Search

# Title Solution Time Space Difficulty Tag Note
0033 Search in Rotated Sorted Array Python O(logn) O(1) Medium
0162 Find Peak Element Python O(logn) O(1) Medium
0034 Search for a Range Python O(logn) O(1) Medium
0035 Search Insert Position Python O(logn) O(1) Easy
0074 Search a 2D Matrix Python O(logm + logn) O(1) Medium
0153 Find Minimum in Rotated Sorted Array Python O(logn) O(1) Medium
0278 First Bad Version Python O(logn) O(1) Easy LintCode
0367 Valid Perfect Square Python O(logn) O(1) Easy
0374 Guess Number Higher or Lower Python O(logn) O(1) Easy
0658 Find K Closest Elements Python O(logn + k) O(1) Medium
0700 Search in a Binary Search Tree Python O(logn) O(logn) Easy
0702 Search in a Sorted Array of Unknown Size Python O(logn) O(logn) Easy
0875 Koko Eating Bananas Python O(nlogr) O(1) Medium

Binary Search Tree

# Title Solution Time Space Difficulty Tag Note
0230 Kth Smallest Element in a BST Python O(max(h, k)) O(min(h, k)) Medium
0235 Lowest Common Ancestor of a Binary Search Tree Python O(h) O(1) Easy EPI
0270 Closest Binary Search Tree Value Python O(h) O(1) Easy
0450 Delete Node in a BST Python O(h) O(h) Medium

Bit Manipulation

# Title Solution Time Space Difficulty Tag Note
0190 Reverse Bits Python O(1) O(1) Easy
0191 Number of 1 Bits Python O(1) O(1) Easy
0268 Missing Number Python O(n) O(1) Medium
0342 Power of Four Python O(1) O(1) Easy
0389 Find the Difference Python O(n) O(1) Easy

Breadth-First Search

# Title Solution Time Space Difficulty Tag Note
0102 Binary Tree Level Order Traversal Python O(n) O(n) Medium
0103 Binary Tree Zigzag Level Order Traversal Python O(n) O(n) Medium
0133 Clone Graph Python O(n) O(n) Medium
0286 Walls and Gates Python O(m * n) O(g) Medium πŸ”’
0339 Nested List Weight Sum Python O(N) O(N) Medium
0364 Nested List Weight Sum II Python O(N) O(N) Medium
0490 The Maze Python O(max(r, c) * w) O(w) Medium
0994 Rotting Oranges Python O(m * n) O(m * n) Medium
9997 Zombie in Matrix Python O(m * n) O(m * n) Medium

Depth-First Search

# Title Solution Time Space Difficulty Tag Note
0112 Path Sum Python O(n) O(h) Easy
0113 Path Sum II Python O(n) O(h) Medium
0199 Binary Tree Right Side View Python O(n) O(h) Medium
0200 Number of Islands Python O(m * n) O(m * n) Medium
0207 Course Schedule Python O(|V| + |E|) O(|E|) Medium Topological Sort
0210 Course Schedule II Python O(|V| + |E|) O(|E|) Medium Topological Sort
0236 Lowest Common Ancestor of a Binary Tree Python O(n) O(h) Medium EPI
0250 Count Univalue Subtrees Python O(n) O(h) Medium πŸ”’
0257 Binary Tree Paths Python O(n * h) O(h) Easy

Design

# Title Solution Time Space Difficulty Tag Note
0146 LRU Cache Python O(1) O(k) Hard
0346 Moving Average from Data Stream Python O(N) O(M) Easy
1348 Tweet Counts Per Frequency Python add: O(logn)
query: O(c)
O(n) Medium
1570 Dot Product of Two Sparse Vectors Python ctor: O(n)
dot_product: O(min(n, m))
O(n) Medium πŸ”’

Dynamic Programming

# Title Solution Time Space Difficulty Tag Note
0053 Maximum Subarray Python O(n) O(1) Medium
0062 Unique Paths Python O(m * n) O(m + n) Medium
0063 Unique Paths II Python O(m * n) O(m + n) Medium
0070 Climbing Stairs Python O(logn) O(1) Easy Matrix Exponentiation
0096 Unique Binary Search Trees Python O(n) O(1) Medium Math
0123 Best Time to Buy and Sell Stock III Python O(n) O(1) Hard
0152 Maximum Product Subarray Python O(n) O(1) Medium
0198 House Robber Python O(n) O(1) Medium
0213 House Robber II Python O(n) O(1) Medium
0303 Range Sum Query - Immutable Python ctor: O(n), lookup: O(1) O(n) Easy
0322 Coin Change Python O(n * k) O(k) Medium
0718 Maximum Length of Repeated Subarray Python O(m * n) O(min(m, n)) Medium DP, Hash, Binary Search


Greedy

# Title Solution Time Space Difficulty Tag Note
0055 Jump Game Python O(n) O(1) Medium
0122 Best Time to Buy and Sell Stock II Python O(n) O(1) Easy
0435 Non-overlapping Intervals Python O(nlogn) O(1) Medium Line Sweep
0621 Task Scheduler Python O(n) O(1) Medium
0763 Partition Labels Python O(n) O(n) Medium
1249 Minimum Remove to Make Valid Parentheses Python O(n) O(1) Medium Stack
1710 Maximum Units on a Truck Python O(nlogn) O(1) Easy

Hash Table

# Title Solution Time Space Difficulty Tag Note
0001 Two Sum Python O(n) O(n) Easy
0003 Longest Substring Without Repeating Characters Python O(n) O(1) Medium
0036 Valid Sudoku Python O(9^2) O(9) Easy
0049 Group Anagrams Python O(n * glogg) O(n) Medium
0205 Isomorphic Strings Python O(n) O(1) Easy
0217 Contains Duplicate Python O(n) O(n) Easy
0249 Group Shifted Strings Python O(nlogn) O(n) Medium πŸ”’
0387 First Unique Character in a String Python O(n) O(n) Easy
0438 Find All Anagrams in a String Python O(n) O(1) Easy
0523 Continuous Subarray Sum Python O(n) O(k) Medium
0560 Subarray Sum Equals K Python O(n) O(n) Medium
0599 Minimum Index Sum of Two Lists Python O((m + n) * l) O(m * l) Easy
0811 Subdomain Visit Count Python O(n) O(n) Medium
1152 Analyze User Website Visit Pattern Python O(n^3) O(n^3) Medium πŸ”’ Hash
1817 Finding the Users Active Minutes Python O(n) O(n) Medium

Linked List

# Title Solution Time Space Difficulty Tag Note
0002 Add Two Numbers Python O(n) O(1) Medium
0021 Merge Two Sorted Lists Python O(n) O(1) Easy
0024 Swap Nodes in Pairs Python O(n) O(1) Easy
0138 Copy List with Random Pointer Python O(n) O(1) Medium
0206 Reverse Linked List Python O(n) O(1) Easy
0986 Interval List Intersections Python O(m + n) O(1) Medium

Math

# Title Solution Time Space Difficulty Tag Note
0007 Reverse Integer Python O(1) O(1) Easy
0013 Roman to Integer Python O(n) O(1) Easy
0326 Power of Three Python O(1) O(1) Easy
0338 Counting Bits Python O(n) O(n) Medium
1492 The kth Factor of n Python O(sqrt(n)) O(1) Medium
1904 The Number of Full Rounds You Have Played Python O(1) O(1) Medium

Recursion

# Title Solution Time Space Difficulty Tag Note
0046 Permutations Python O(n * n!) O(n) Medium
0047 Permutations II Python O(n * n!) O(n) Medium
0095 Unique Binary Search Trees II Python O(4^n / n^(3/2) O(4^n / n^(3/2) Medium
0098 Validate Binary Search Tree Python O(n) O(1) Medium
0100 Same Tree Python O(n) O(h) Easy
0104 Maximum Depth of Binary Tree Python O(n) O(h) Easy Blind 75
0110 Balanced Binary Tree Python O(n) O(h) Easy
0404 Sum of Left Leaves Python O(n) O(h) Easy
0437 Path Sum III Python](./Python/path-sum-iii.py) O(n) O(h) Easy

Sort

# Title Solution Time Space Difficulty Tag Note
0056 Merge Intervals Python O(nlogn) O(1) Medium
0057 Insert Interval Python O(n) O(1) Medium
0075 Sort Colors Python O(n) O(1) Medium Tri Partition
0088 Merge Sorted Array Python O(n) O(1) Easy
0252 Meeting Rooms Python O(nlogn) O(n) Easy πŸ”’
0253 Meeting Rooms II Python O(nlogn) O(n) Medium πŸ”’
0274 H-Index Python O(n) O(n) Medium Counting Sort
0347 Top K Frequent Elements Python O(n) O(n) Medium Quick Select, Heap, Bucket Sort
0692 Top K Frequent Words Python O(n + klogk) on average O(n) Medium Quick Select, Heap, Bucket Sort
0969 Pancake Sorting Python O(n^2) O(l) Medium
0973 K Closest Points to Origin Python O(n) on average O(1) Medium Quick Select, Heap
1094 Car Pooling Python O(nlogn) O(n) Medium variant of Meeting Rooms II
9999 Top K Frequently Mentioned Keywords Python O(n) on average O(1) Medium Quick Select, Heap

Stack

# Title Solution Time Space Difficulty Tag Note
0020 Valid Parentheses Python O(n) O(n) Easy
0071 Simplify Path Python O(n) O(n) Medium
0101 Symmetric Tree Python O(n) O(h) Easy
0150 Evaluate Reverse Polish Notation Python O(n) O(n) Medium
0682 Baseball Game Python O(n) O(n) Easy
0739 Daily Temperatures Python O(n) O(n) Medium

String

# Title Solution Time Space Difficulty Tag Note
0005 Longest Palindromic Substring Python O(n) O(n) Medium Blind75
0008 String to Integer (atoi) Python O(n) O(1) Medium
0014 Longest Common Prefix Python O(n * k) O(1) Easy
0028 Implement strStr() Python O(n + k) O(k) Easy KMP Algorithm
0038 Count and Say Python O(n * 2^n) O(2^n) Easy
0043 Multiply Strings Python O(m * n) O(m + n) Medium
0067 Add Binary Python O(n) O(1) Easy
0076 Minimum Window Substring Python O(n) O(k) Hard
0125 Valid Palindrome Python O(n) O(1) Easy Blind 75
0151 Reverse Words in a String Python O(n) O(1) Medium
0271 Encode and Decode Strings Python O(n) O(1) Medium πŸ”’
0383 Ransom Note Python O(n) O(1) Easy
0415 Add Strings Python O(n) O(1) Easy
0468 Validate IP Address Python O(1) O(1) Medium
0557 Reverse Words in a String III Python O(n) O(1) Easy
0647 Palindromic Substrings Python O(n) O(n) Medium
0680 Valid Palindrome II Python O(n) O(1) Easy
0819 Most Common Word Python O(m + n) O(m + n) Easy
0953 Verifying an Alien Dictionary Python O(n * l) O(1) Easy
1347 Minimum Number of Steps to Make Two Strings Anagram Python O(n) O(1) Medium

Tree

# Title Solution Time Space Difficulty Tag Note
0094 Binary Tree Inorder Traversal Python O(n) O(1) Medium Morris Traversal
0144 Binary Tree Preorder Traversal Python O(n) O(1) Medium Morris Traversal
0145 Binary Tree Postorder Traversal Python O(n) O(1) Hard Morris Traversal
0226 Invert Binary Tree Python O(n) O(h), O(w) Easy
0543 Diameter of Binary Tree Python O(n) O(h) Easy
0572 Subtree of Another Tree Python O(m * n) O(h) Easy
0938 Range Sum of BST Python O(n) O(h) Easy DFS
0987 Vertical Order Traversal of a Binary Tree Python O(nlogn) O(n) Medium DFS

Two Pointers

# Title Solution Time Space Difficulty Tag Note
0019 Remove Nth Node From End of List Python O(n) O(1) Easy
0143 Reorder List Python O(n) O(1) Medium
0167 Two Sum II - Input array is sorted Python O(n) O(1) Medium
0209 Minimum Size Subarray Sum Python O(n) O(1) Medium Binary Search, Sliding Window
0283 Move Zeroes Python O(n) O(1) Easy
0287 Find the Duplicate Number Python O(n) O(1) Medium Binary Search, Two Pointers
0344 Reverse String Python O(n) O(1) Easy
0345 Reverse Vowels of a String Python O(n) O(1) Easy
0349 Intersection of Two Arrays Python O(m + n) O(min(m, n)) Easy EPI Hash, Binary Search
0424 Longest Repeating Character Replacement Python O(n) O(1) Medium Sliding Window
0350 Intersection of Two Arrays II Python O(m + n) O(1) Easy
0567 Permutation in String Python O(n) O(1) Medium
1248 Count Number of Nice Subarrays Python O(n) O(k) Medium variant of Subarrays with K Different Integers Two Pointers, Sliding Window
1493 Longest Subarray of 1's After Deleting One Element Python O(n) O(1) Medium Sliding Window
1604 Alert Using Same Key-Card Three or More Times in a One Hour Period Python O(nlogn) O(n) Medium Two Pointers, Sliding Window

About


Languages

Language:Python 100.0%