This repository contains various Leetcode problems that highlight valuable algorithms and data structures
All code is provided in Java and my own personal Pseduo Code allowing for easy translation of these solutions to any target language.
These notes originally come from an Obsidian notebook and may contain internal links and tags such as [[]] or the "#" symbol or the tags at the top of every document. If you add these to your own obsidian notebook, problems will link together in the graph based on similar data structures, algorithms, time complexity, space complexity, difficulty, and more.
Problem | Summary | Link To Full Solution | Tags |
---|---|---|---|
1095. Find in Mountain Array | Run three different Binary Searches. The first one will find the peak of the mountain array, the second will search the left sorted portion for the target, the third the right sorted portion for the target. | Markdown Solution | BinarySearch, sorted, O_logn, O_1space, Hard, CustomInterface |
1220. Count Vowel Permuatations | Initialize the base case as 1 way to start which each vowel. For each length greater than 1, apply the rules in reverse saying if the current vowel is x , add all the combinations of what the previous vowel could've been. Return all combinations of the final length added together |
Markdown Solution | 1D_DP, 2D_DP, O_n, O_1space, Hard |
210. Course Schedule II | Create adjacency map from prerequisite list. Then run Topological Sort, DFS on every course, assuming you get true from all prerequisites and have taken them, take the current course in the result list. Keep cycle set for visiting, any cycle means false no valid path. | Markdown Solution | Topological_Sort, Graph, DFS, O_n2, O_n2space, Medium, HashMap, HashSet |
2642. Design Graph With Shortest Path Calculator | Store the graph as an adjacency map, when called for the shortest path, run Dijkstra's shortest path algorithm between the two nodes, explained in solution. | Markdown Solution | Dijkstra, Graph, ObjectCreation, customComparator, Heap, HashMap, O_n2, O_n2space, Hard, Greedy |
779. K-th Symbol in Grammar | Run a directed DFS the height of the graph making a decision to go right or left at each node, inverting the value when going right. | Markdown Solution | DFS, traversal, TwoPointer, O_n, O_1space, Medium, BinarySearch |
2050. Parallel Courses III | Find the longest continuous path in the graph formed by the prereq edges and adding the time to take each course at every node. DFS at every course and store the result in a DP table of index node. | Markdown Solution | 1D_DP, DFS, Graph, HashMap, O_n2, O_n2space, Hard, Recursive |
1361. Validate Binary Tree Nodes | First find the node that is not the child of any other node, that is the root. Then run BFS/DFS on the tree starting at the root, ensure no cycles or nodes with multiple parents with a visited set. Return that the visited set is size n |
Markdown Solution | BinaryTree, BFS, HashSet, Queue, cycleDetection, O_n, O_nspace, Medium |
1793. Maximum Score of a Good Subarray | Start left and right pointers at element k, while you can expand to a new element, expand to whichever is greater/will hurt the score least (same condition). Calculate local score and keep track of global max score to return. | Markdown Solution | TwoPointer, O_n, O_1space, Hard |
341. Flatten Nested List Iterator | Run a DFS on the nested ints unpacking every value and adding every list back to the stack in reverse order, add all ints to a queue. Return next element of the queue/ if queue is empty | Markdown Solution | ObjectCreation, CustomInterface, Stack, Queue, DFS, O_n, O_nspace, Medium |
1143. Longest Common Subsequence | 2D-DP array, Compare every character in one string to every character in the other. If they are equal, subsequence is 1 plus the subsequence at the two characters before. Otherwise take the max of deleting either of the current characters | Markdown Solution | 2D_DP, Subsequence, O_mxn, O_mxnspace, Medium, |
62. Unique Paths | Initialize there is one way to get the origin. Explore the entire 2D grid normally, way to get to square (i, j) is the ways to get to the square to the left (i - 1, j) plus the square above (i, j - 1) if they are valid. Return the final square's total |
Markdown Solution | 2D_DP, 2Dgrid, O_mxn, O_mxnspace, Medium |
1. Two Sum | Use HashMap of seen numbers and their indices to check for complement value to target | Markdown Solution | HashMap, O_n, O_nspace, Easy |
1582. Special Positions in a Binary Matrix | Keep track of a boolean matrix the same size as the original. For every row and column, sum them and if the sum is not 1 set the whole row or column of the bool matrix to false. Result is the sum of all positions that are true and equal to 1. | Markdown Solution | 2Dgrid, O_mxn, O_mxnspace, O_mplusnspace, Easy |
3. Longest Substring Without Repeating Characters | Start left and right pointers at 0. Try to increment right pointer and increase window size while no repeat characters (check with HashSet). Once a repeat character is found, increase left until the window is valid again. Keep track of largest length window (right - left + 1) | Markdown Solution | SlidingWindow, HashSet, O_n, O_nspace, Medium |
287. Find the Duplicate Number | Connect each element value to index of that value as linked list graph, Use Floyd's cycle detection algorithm, both phases, to find start of cycle | Markdown Solution | FloydsAlgorithm, linkedlist, cycleDetection, O_n, O_1space, Medium |
78. Subsets | Recursive backtracking considering including or not including every element in the subset. Keep an index i of the current element to consider and increment each call to prevent duplicates (never consider element 0 after element 1) | Markdown Solution | Backtracking, Recursive, O_2pown, O_2pownspace, Medium |
90. Subsets II | Same recursive backtracking as 78. Subsets but when considering all cases without the current element, skip over all duplicates of the current element's value | Markdown Solution | Backtracking, Recursive, Sort, O_2pown, O_2pownspace, Medium |
2353. Design a Food Rating System | Create a custom Food object and store a map of food name to object and cuisine to heap of foods with custom comparator. When asked for the highest rated, keep polling the heap until you get a food with a rating value that is current, based on the food map. | Markdown Solution | ObjectCreation, customComparator, HashMap, Heap, O_nlogk, O_nspace, Medium |