Skip to content

💻 🍷 Solutions to LeetCode problems

Notifications You must be signed in to change notification settings

weak-head/leetcode

Repository files navigation

leetcode

Build Status codecov Codacy Coverage Gitpod Codacy Code Quality flake8 CodeQL Python 3.9 black

Open in Gitpod

Open in Gitpod

And you are ready to play with the repo. Or alternatively setup your own local environment...

Environment setup

# Install python
pyenv install 3.9.0

# Create virtual env
pyenv virtualenv 3.9.0 leetcode
pyenv local leetcode

# Install dependencies
pip install pipenv
pipenv install --dev

# Optional, enable pre-commit hook
pre-commit install

Typical flow

# Create a new template for the problem
# Example:
#   make new 1 https://leetcode.com/problems/two-sum/
make new <id> <url>

# Stage changes and run test cases for this problem
# This will execute:
#   - git add .
#   - pre-commit
#   - pytest
make add

# Commit changes
# This will:
#   - stage changes
#   - run test cases
#   - generate commit message
#   - commit changes
make commit

List of Problems (Total: 404)

# Title Solution Test cases
1 Two Sum src tst
2 Add Two Numbers src tst
3 Longest Substring Without Repeating Characters src tst
4 Median Of Two Sorted Arrays src tst
5 Longest Palindromic Substring src tst
6 Zigzag Conversion src tst
7 Reverse Integer src tst
8 String To Integer Atoi src tst
9 Palindrome Number src tst
10 Regular Expression Matching src tst
11 Container With Most Water src tst
12 Integer To Roman src tst
13 Roman To Integer src tst
14 Longest Common Prefix src tst
15 3Sum src tst
16 3Sum Closest src tst
17 Letter Combinations Of A Phone Number src tst
18 4Sum src tst
19 Remove Nth Node From End Of List src tst
20 Valid Parentheses src tst
21 Merge Two Sorted Lists src tst
22 Generate Parentheses src tst
23 Merge K Sorted Lists src tst
24 Swap Nodes In Pairs src tst
25 Reverse Nodes In K Group src tst
26 Remove Duplicates From Sorted Array src tst
27 Remove Element src tst
28 Implement Strstr src tst
29 Divide Two Integers src tst
30 Substring With Concatenation Of All Words src tst
31 Next Permutation src tst
32 Longest Valid Parentheses src tst
33 Search In Rotated Sorted Array src tst
34 Find First And Last Position Of Element In Sorted Array src tst
35 Search Insert Position src tst
36 Valid Sudoku src tst
37 Sudoku Solver src tst
38 Count And Say src tst
39 Combination Sum src tst
40 Combination Sum Ii src tst
41 First Missing Positive src tst
42 Trapping Rain Water src tst
43 Multiply Strings src tst
44 Wildcard Matching src tst
45 Jump Game Ii src tst
46 Permutations src tst
47 Permutations Ii src tst
48 Rotate Image src tst
49 Group Anagrams src tst
50 Powx N src tst
51 N Queens src tst
52 N Queens Ii src tst
53 Maximum Subarray src tst
54 Spiral Matrix src tst
55 Jump Game src tst
56 Merge Intervals src tst
58 Length Of Last Word src tst
61 Rotate List src tst
63 Unique Paths Ii src tst
64 Minimum Path Sum src tst
71 Simplify Path src tst
72 Edit Distance src tst
74 Search A 2D Matrix src tst
75 Sort Colors src tst
76 Minimum Window Substring src tst
77 Combinations src tst
78 Subsets src tst
79 Word Search src tst
84 Largest Rectangle In Histogram src tst
86 Partition List src tst
90 Subsets Ii src tst
91 Decode Ways src tst
92 Reverse Linked List Ii src tst
95 Unique Binary Search Trees Ii src tst
98 Validate Binary Search Tree src tst
99 Recover Binary Search Tree src tst
101 Symmetric Tree src tst
102 Binary Tree Level Order Traversal src tst
103 Binary Tree Zigzag Level Order Traversal src tst
105 Construct Binary Tree From Preorder And Inorder Traversal src tst
108 Convert Sorted Array To Binary Search Tree src tst
109 Convert Sorted List To Binary Search Tree src tst
112 Path Sum src tst
113 Path Sum Ii src tst
118 Pascals Triangle src tst
120 Triangle src tst
121 Best Time To Buy And Sell Stock src tst
122 Best Time To Buy And Sell Stock Ii src tst
123 Best Time To Buy And Sell Stock Iii src tst
124 Binary Tree Maximum Path Sum src tst
127 Word Ladder src tst
130 Surrounded Regions src tst
133 Clone Graph src tst
135 Candy src tst
136 Single Number src tst
138 Copy List With Random Pointer src tst
139 Word Break src tst
141 Linked List Cycle src tst
142 Linked List Cycle Ii src tst
146 Lru Cache src tst
150 Evaluate Reverse Polish Notation src tst
152 Maximum Product Subarray src tst
157 Read N Characters Given Read4 src tst
158 Read N Characters Given Read4 Ii Call Multiple Times src tst
160 Intersection Of Two Linked Lists src tst
161 One Edit Distance src tst
174 Dungeon Game src tst
188 Best Time To Buy And Sell Stock Iv src tst
191 Number Of 1 Bits src tst
198 House Robber src tst
199 Binary Tree Right Side View src tst
200 Number Of Islands src tst
204 Count Primes src tst
205 Isomorphic Strings src tst
206 Reverse Linked List src tst
207 Course Schedule src tst
208 Implement Trie Prefix Tree src tst
210 Course Schedule Ii src tst
212 Word Search Ii src tst
213 House Robber Ii src tst
215 Kth Largest Element In An Array src tst
221 Maximal Square src tst
224 Basic Calculator src tst
225 Implement Stack Using Queues src tst
227 Basic Calculator Ii src tst
232 Implement Queue Using Stacks src tst
234 Palindrome Linked List src tst
236 Lowest Common Ancestor Of A Binary Tree src tst
238 Product Of Array Except Self src tst
239 Sliding Window Maximum src tst
240 Search A 2D Matrix Ii src tst
242 Valid Anagram src tst
243 Shortest Word Distance src tst
244 Shortest Word Distance Ii src tst
246 Strobogrammatic Number src tst
249 Group Shifted Strings src tst
252 Meeting Rooms src tst
253 Meeting Rooms Ii src tst
256 Paint House src tst
257 Binary Tree Paths src tst
259 3Sum Smaller src tst
261 Graph Valid Tree src tst
265 Paint House Ii src tst
268 Missing Number src tst
269 Alien Dictionary src tst
273 Integer To English Words src tst
277 Find The Celebrity src tst
278 First Bad Version src tst
284 Peeking Iterator src tst
285 Inorder Successor In Bst src tst
286 Walls And Gates src tst
295 Find Median From Data Stream src tst
300 Longest Increasing Subsequence src tst
303 Range Sum Query Immutable src tst
304 Range Sum Query 2D Immutable src tst
309 Best Time To Buy And Sell Stock With Cooldown src tst
310 Minimum Height Trees src tst
311 Sparse Matrix Multiplication src tst
312 Burst Balloons src tst
314 Binary Tree Vertical Order Traversal src tst
317 Shortest Distance From All Buildings src tst
322 Coin Change src tst
323 Number Of Connected Components In An Undirected Graph src tst
325 Maximum Size Subarray Sum Equals K src tst
326 Power Of Three src tst
328 Odd Even Linked List src tst
334 Increasing Triplet Subsequence src tst
337 House Robber Iii src tst
339 Nested List Weight Sum src tst
340 Longest Substring With At Most K Distinct Characters src tst
341 Flatten Nested List Iterator src tst
343 Integer Break src tst
346 Moving Average From Data Stream src tst
347 Top K Frequent Elements src tst
348 Design Tic Tac Toe src tst
353 Design Snake Game src tst
354 Russian Doll Envelopes src tst
355 Design Twitter src tst
359 Logger Rate Limiter src tst
362 Design Hit Counter src tst
364 Nested List Weight Sum Ii src tst
376 Wiggle Subsequence src tst
377 Combination Sum Iv src tst
380 Insert Delete Getrandom O1 src tst
382 Linked List Random Node src tst
384 Shuffle An Array src tst
387 First Unique Character In A String src tst
398 Random Pick Index src tst
413 Arithmetic Slices src tst
415 Add Strings src tst
417 Pacific Atlantic Water Flow src tst
423 Reconstruct Original Digits From English src tst
426 Convert Binary Search Tree To Sorted Doubly Linked List src tst
428 Serialize And Deserialize N Ary Tree src tst
435 Non Overlapping Intervals src tst
437 Path Sum Iii src tst
438 Find All Anagrams In A String src tst
446 Arithmetic Slices Ii Subsequence src tst
450 Delete Node In A Bst src tst
451 Sort Characters By Frequency src tst
452 Minimum Number Of Arrows To Burst Balloons src tst
459 Repeated Substring Pattern src tst
460 Lfu Cache src tst
463 Island Perimeter src tst
465 Optimal Account Balancing src tst
471 Encode String With Shortest Length src tst
474 Ones And Zeroes src tst
478 Generate Random Point In A Circle src tst
489 Robot Room Cleaner src tst
496 Next Greater Element I src tst
503 Next Greater Element Ii src tst
509 Fibonacci Number src tst
516 Longest Palindromic Subsequence src tst
518 Coin Change 2 src tst
524 Longest Word In Dictionary Through Deleting src tst
535 Encode And Decode Tinyurl src tst
536 Construct Binary Tree From String src tst
538 Convert Bst To Greater Tree src tst
542 01 Matrix src tst
543 Diameter Of Binary Tree src tst
545 Boundary Of Binary Tree src tst
547 Number Of Provinces src tst
554 Brick Wall src tst
556 Next Greater Element Iii src tst
560 Subarray Sum Equals K src tst
573 Squirrel Simulation src tst
575 Distribute Candies src tst
581 Shortest Unsorted Continuous Subarray src tst
582 Kill Process src tst
583 Delete Operation For Two Strings src tst
588 Design In Memory File System src tst
589 N Ary Tree Preorder Traversal src tst
594 Longest Harmonious Subsequence src tst
609 Find Duplicate File In System src tst
616 Add Bold Tag In String src tst
622 Design Circular Queue src tst
623 Add One Row To Tree src tst
630 Course Schedule Iii src tst
632 Smallest Range Covering Elements From K Lists src tst
637 Average Of Levels In Binary Tree src tst
642 Design Search Autocomplete System src tst
645 Set Mismatch src tst
647 Palindromic Substrings src tst
650 2 Keys Keyboard src tst
651 4 Keys Keyboard src tst
653 Two Sum Iv Input Is A Bst src tst
662 Maximum Width Of Binary Tree src tst
666 Path Sum Iv src tst
667 Beautiful Arrangement Ii src tst
668 Kth Smallest Number In Multiplication Table src tst
669 Trim A Binary Search Tree src tst
692 Top K Frequent Words src tst
694 Number Of Distinct Islands src tst
695 Max Area Of Island src tst
696 Count Binary Substrings src tst
698 Partition To K Equal Sum Subsets src tst
706 Design Hashmap src tst
708 Insert Into A Sorted Circular Linked List src tst
714 Best Time To Buy And Sell Stock With Transaction Fee src tst
721 Accounts Merge src tst
723 Candy Crush src tst
727 Minimum Window Subsequence src tst
729 My Calendar I src tst
733 Flood Fill src tst
739 Daily Temperatures src tst
745 Prefix And Suffix Search src tst
750 Number Of Corner Rectangles src tst
759 Employee Free Time src tst
764 Largest Plus Sign src tst
772 Basic Calculator Iii src tst
774 Minimize Max Distance To Gas Station src tst
775 Global And Local Inversions src tst
778 Swim In Rising Water src tst
784 Letter Case Permutation src tst
785 Is Graph Bipartite src tst
787 Cheapest Flights Within K Stops src tst
792 Number Of Matching Subsequences src tst
795 Number Of Subarrays With Bounded Maximum src tst
797 All Paths From Source To Target src tst
815 Bus Routes src tst
820 Short Encoding Of Words src tst
821 Shortest Distance To A Character src tst
823 Binary Trees With Factors src tst
827 Making A Large Island src tst
829 Consecutive Numbers Sum src tst
841 Keys And Rooms src tst
848 Shifting Letters src tst
855 Exam Room src tst
856 Score Of Parentheses src tst
860 Lemonade Change src tst
863 All Nodes Distance K In Binary Tree src tst
869 Reordered Power Of 2 src tst
870 Advantage Shuffle src tst
871 Minimum Number Of Refueling Stops src tst
875 Koko Eating Bananas src tst
876 Middle Of The Linked List src tst
877 Stone Game src tst
882 Reachable Nodes In Subdivided Graph src tst
887 Super Egg Drop src tst
895 Maximum Frequency Stack src tst
899 Orderly Queue src tst
906 Super Palindromes src tst
907 Sum Of Subarray Minimums src tst
916 Word Subsets src tst
922 Sort Array By Parity Ii src tst
923 3Sum With Multiplicity src tst
936 Stamping The Sequence src tst
946 Validate Stack Sequences src tst
947 Most Stones Removed With Same Row Or Column src tst
953 Verifying An Alien Dictionary src tst
966 Vowel Spellchecker src tst
969 Pancake Sorting src tst
970 Powerful Integers src tst
971 Flip Binary Tree To Match Preorder Traversal src tst
973 K Closest Points To Origin src tst
978 Longest Turbulent Subarray src tst
981 Time Based Key Value Store src tst
986 Interval List Intersections src tst
987 Vertical Order Traversal Of A Binary Tree src tst
990 Satisfiability Of Equality Equations src tst
991 Broken Calculator src tst
1000 Minimum Cost To Merge Stones src tst
1004 Max Consecutive Ones Iii src tst
1005 Maximize Sum Of Array After K Negations src tst
1006 Clumsy Factorial src tst
1007 Minimum Domino Rotations For Equal Row src tst
1008 Construct Binary Search Tree From Preorder Traversal src tst
1009 Complement Of Base 10 Integer src tst
1010 Pairs Of Songs With Total Durations Divisible By 60 src tst
1011 Capacity To Ship Packages Within D Days src tst
1013 Partition Array Into Three Parts With Equal Sum src tst
1038 Binary Search Tree To Greater Sum Tree src tst
1041 Robot Bounded In Circle src tst
1047 Remove All Adjacent Duplicates In String src tst
1048 Longest String Chain src tst
1057 Campus Bikes src tst
1059 All Paths From Source Lead To Destination src tst
1086 High Five src tst
1091 Shortest Path In Binary Matrix src tst
1105 Filling Bookcase Shelves src tst
1120 Maximum Average Subtree src tst
1133 Largest Unique Number src tst
1136 Parallel Courses src tst
1140 Stone Game Ii src tst
1143 Longest Common Subsequence src tst
1151 Minimum Swaps To Group All 1S Together src tst
1153 String Transforms Into Another String src tst
1165 Single Row Keyboard src tst
1166 Design File System src tst
1167 Minimum Cost To Connect Sticks src tst
1182 Shortest Distance To Target Color src tst
1186 Maximum Subarray Sum With One Deletion src tst
1191 K Concatenation Maximum Sum src tst
1197 Minimum Knight Moves src tst
1198 Find Smallest Common Element In All Rows src tst
1209 Remove All Adjacent Duplicates In String Ii src tst
1217 Minimum Cost To Move Chips To The Same Position src tst
1228 Missing Number In Arithmetic Progression src tst
1229 Meeting Scheduler src tst
1236 Web Crawler src tst
1242 Web Crawler Multithreaded src tst
1244 Design A Leaderboard src tst
1249 Minimum Remove To Make Valid Parentheses src tst
1275 Find Winner On A Tic Tac Toe Game src tst
1302 Deepest Leaves Sum src tst
1329 Sort The Matrix Diagonally src tst
1332 Remove Palindromic Subsequences src tst
1335 Minimum Difficulty Of A Job Schedule src tst
1337 The K Weakest Rows In A Matrix src tst
1339 Maximum Product Of Splitted Binary Tree src tst
1342 Number Of Steps To Reduce A Number To Zero src tst
1353 Maximum Number Of Events That Can Be Attended src tst
1354 Construct Target Array With Multiple Sums src tst
1396 Design Underground System src tst
1406 Stone Game Iii src tst
1423 Maximum Points You Can Obtain From Cards src tst
1428 Leftmost Column With At Least A One src tst
1437 Check If All 1S Are At Least Length K Places Away src tst
1448 Count Good Nodes In Binary Tree src tst
1461 Check If A String Contains All Binary Codes Of Size K src tst
1471 The K Strongest Values In An Array src tst
1494 Parallel Courses Ii src tst
1498 Number Of Subsequences That Satisfy The Given Sum Condition src tst
1500 Design A File Sharing System src tst
1551 Minimum Operations To Make Array Equal src tst
1584 Min Cost To Connect All Points src tst
1629 Slowest Key src tst
1631 Path With Minimum Effort src tst
1641 Count Sorted Vowel Strings src tst
1642 Furthest Building You Can Reach src tst
1644 Lowest Common Ancestor Of A Binary Tree Ii src tst
1646 Get Maximum In Generated Array src tst
1650 Lowest Common Ancestor Of A Binary Tree Iii src tst
1657 Determine If Two Strings Are Close src tst
1658 Minimum Operations To Reduce X To Zero src tst
1663 Smallest String With A Given Numeric Value src tst
1673 Find The Most Competitive Subsequence src tst
1675 Minimize Deviation In Array src tst
1676 Lowest Common Ancestor Of A Binary Tree Iv src tst
1679 Max Number Of K Sum Pairs src tst
1680 Concatenation Of Consecutive Binary Numbers src tst
1704 Determine If String Halves Are Alike src tst
1710 Maximum Units On A Truck src tst
1721 Swapping Nodes In A Linked List src tst