Skip to content

Solutions for Leetcode Problems (tested using pytest and continuous integration)

Notifications You must be signed in to change notification settings

mathusanm6/LeetCode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Leetcode

Cpp Format & Test Python CI (conda)

Description

This repository contains my solutions to LeetCode problems. I will be updating this repository with my solutions as I solve more problems. I have included a test suite for each solution.

Running Tests

Python Tests

To run the Python test suite, use the following command in the repository directory:

pytest python

C++ Tests

To build and run the C++ test suite, use the following commands in the repository directory:

cd cpp
make all
make run_tests

Algorithms

Arrays & Hashing

# Title Solution Time Space Difficulty Tag Note
1 Two Sum Python, C++ O(n) O(n) Easy
49 Group Anagrams Python O(NK) O(NK) Medium N is the number of strings and K is the maximum length of a single string
88 Merge Sorted Array Python O(n + m) O(1) Easy Two-Pointers, Reverse
128 Longest Consecutive Sequence Python O(n) O(n) Medium Tricky solution!
135 Candy Python O(n) O(n) Hard Enjoyed solving it!
217 Contains Duplicate Python O(n) O(n) Easy
238 Product of Array Except Self Python O(n) O(1) Medium Interesting solution!
242 Valid Anagram Python O(n) O(n) Easy Given Unicode characters support for Python3, the follow-up question is irrelevant
271 Encode and Decode Strings Python O(n) O(n) Medium
347 Top K Frequent Elements Python O(N*log(K)) O(N + K) Medium Requires MinHeap
383 Ransom Note Python O(n) O(1) Easy Fixed List

Two-Pointers

# Title Solution Time Space Difficulty Tag Note
11 Container With Most Water Python O(n) O(1) Medium
15 3Sum Python O(n^2) O(1) Medium
42 Trapping Rain Water Python O(n), Python O(1) O(n) O(n) or O(1) Hard Initially proposed an O(n) space complexity solution, but discovered an O(1) alternative.
125 Valid Palindrome Python O(n) O(1) Easy
167 Two Sum II - Input Array is Sorted Python O(n) O(1) Medium

Sliding-Window

# Title Solution Time Space Difficulty Tag Note
3 Longest Substring Without Repeating Characters Python O(n) O(min(n, m)), n being size of the string and m being the size of the alphabet Medium A better solution would be to jump to the point where we have a valid string
76 Minimum Window Substring Python O(m + n), m being size of s and n the size of t O(l), l being the number of unique characters in t Hard
121 Best Time to But and Sell Stock Python O(n) O(1) Easy
209 Minimum Size Subarray Sum Python O(n) O(1) Medium
239 Sliding Window Maximum Python O(n), n being the length of nums O(k), k being the window size Hard
424 Longest Repeating Character Replacement Python O(n) O(1) Medium
567 Permutation in String Python O(n), n being the length of s2 O(k), k being the length of s1 Medium

Stack

# Title Solution Time Space Difficulty Tag Note
20 Valid Parentheses Python O(n) O(n) Easy
22 Generate Parentheses Python O(4^n/sqrt(n)) O(n) Medium
150 Evaluate Reverse Polish Notation Python O(n) O(n) Medium
155 Min Stack Python O(1) O(1) Medium Tuple in stack is interesting !

Matrix

# Title Solution Time Space Difficulty Tag Note
36 Valid Sudoku Python O(1) O(1) Medium

Intervals

# Title Solution Time Space Difficulty Tag Note
228 Summary Ranges Python O(n) O(n) Easy

Linked-List

# Title Solution Time Space Difficulty Tag Note
141 Linked List Cycle Python O(n) O(1) Easy

Binary-Tree-General

# Title Solution Time Space Difficulty Tag Note
104 Maximum Depth of Binary Tree Python O(n) O(1) Easy DFS Use FIFO for BFS

Binary-Tree-BFS

# Title Solution Time Space Difficulty Tag Note
199 Binary Tree Right Side View Python O(n) O(w) where w is the maximum width of the binary tree Medium

Binary-Search-Tree

# Title Solution Time Space Difficulty Tag Note
530 Minimum Absolute Difference in BST Python O(n) O(1) Easy In-order traversal

Graph-General

# Title Solution Time Space Difficulty Tag Note
200 Number of Islands Python O(n * m) O(1) Medium