LeetCode-Master Study Plan
repository·master·Indexed 13 days ago
https://github.com/youngyangyang04/leetcode-masterA structured, step-by-step roadmap for mastering data structures and algorithms. The guide provides a logical progression from basic topics like Arrays and Linked Lists to advanced concepts like Dynamic Programming and Graph Theory, featuring theoretical foundations, problem sets, and multi-language implementations in C++, Java, Python, Go, and JS.
What's inside LeetCode-Master
- LeetCode-Master is a systematic, step-by-step study plan designed to help developers master data structures and algorithms. The repository provides a structured learning path where problems are ordered by knowledge context and difficulty. Each problem is accompanied by text-and-image solutions and video explanations. It is suitable for learners moving from zero knowledge to advanced levels.
Solve LeetCode 647: Palindromic Substrings
masterThe goal is to count the total number of palindromic substrings within a given string. Substrings with different start or end positions are considered distinct even if they consist of the same characters.
Problem Constraints:
- Input string length $\le 1000$.
- Example: Input
"aaa"$\rightarrow$ Output6("a","a","a","aa","aa","aaa").
Design a Linked List (LeetCode 707)
masterThis problem requires implementing a linked list class with five core operations: retrieving a value at a specific index, adding nodes at the head, adding nodes at the tail, adding nodes at a specific index, and deleting a node at a specific index.
Key requirements:
get(index): Returns the value atindex. Returns-1if the index is invalid.addAtHead(val): Inserts a new node withvalat the beginning.addAtTail(val): Appends a new node withvalto the end.addAtIndex(index, val): Insertsvalbefore theindex-th node. Ifindexequals the list length, append to the end. Ifindex> length, do nothing. Ifindex< 0, insert at the head.deleteAtIndex(index): Removes the node atindexif the index is valid.
Complexity:
- Time Complexity: Operations involving
indexare $O(index)$; others are $O(1)$. - Space Complexity: $O(n)$ to store $n$ nodes.
Solve LeetCode 17: Letter Combinations of a Phone Number
masterThis problem asks to return all possible letter combinations that a string of digits (2-9) can represent, based on a phone keypad mapping.
Key Constraints & Logic:
- Input: A string containing digits 2-9.
- Output: A list of all possible letter combinations.
- Mapping: Use a fixed mapping where 2='abc', 3='def', etc. Note that 1 does not map to any letters.
- Complexity:
- Time Complexity: $O(3^m \times 4^n)$, where $m$ is the count of digits mapping to 3 letters and $n$ is the count of digits mapping to 4 letters.
- Space Complexity: $O(3^m \times 4^n)$.
Solve LeetCode 463: Island Perimeter
masterThe problem asks to calculate the perimeter of a single island in a
row x colgrid wheregrid[i][j] = 1represents land and0represents water. The grid is surrounded by water, and land cells are connected horizontally or vertically.Constraints:
1 <= row, col <= 100grid[i][j]is either0or1.
Explore the LeetCode Problem Roadmap
masterThe
leetcode-masterrepository provides a structured learning path for algorithm practice, organized by topic and optimized according to the learning curve. The roadmap is divided into several major categories, including:- Prerequisites (前序): Algorithm performance analysis (Time/Space complexity, memory usage) and AI tool integration.
- Data Structures: Arrays (数组), Linked Lists (链表), Hash Tables (哈希表), Stacks & Queues (栈与队列), and Monotonic Stacks (单调栈).
- Algorithms: String manipulation (字符串), Two Pointers (双指针法), Binary Trees (二叉树), Backtracking (回溯算法), Greedy Algorithms (贪心算法), Dynamic Programming (动态规划), and Graph Theory (图论).
Each topic contains theoretical foundations, specific LeetCode problems, and summary guides to consolidate knowledge.
Optimize DFS by 'Drowning' the Island
masterInstead of using a separate
visitedarray, you can optimize space by 'drowning' the island. When a cell with value1is encountered, perform a DFS and set the value of each visited cell in thegridto0. This prevents the same island from being counted multiple times and eliminates the need for an auxiliary boolean array.// DFS Optimization: Drowning the island public int maxAreaOfIsland(int[][] grid) { int res = 0; for(int i = 0;i < grid.length;i++){ for(int j = 0; j < grid[0].length;j++){ if(grid[i][j] == 1){ res = Math.max(res, dfs(grid, i, j)); } } } return res; } public int dfs(int[][] grid, int i, int j){ if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0) return 0; grid[i][j] = 0; // Drown the land return 1 + dfs(grid, i - 1, j) + dfs(grid, i + 1, j) + dfs(grid, i, j + 1) + dfs(grid, i, j - 1); }Solve Sudoku using 2D Backtracking
masterTo solve a Sudoku puzzle, use a 2D backtracking approach (also referred to as 2D recursion). Unlike standard 1D backtracking (like permutations or combinations), this method involves nested loops to traverse both rows and columns to find empty cells ('.') and then recursively attempting to place digits '1'-'9' in those cells.
Core Logic: The Backtracking Three-Step
- Recursive Function and Parameters: The function should return a
booltype. This allows the algorithm to immediately stop and returntrueonce a valid complete solution is found at a leaf node of the recursion tree.- Signature:
bool backtracking(vector<vector<char>>& board)
- Signature:
- Termination Condition: This specific implementation does not use an explicit termination condition. Instead, it relies on the fact that as numbers are filled, the board eventually becomes full. If a cell is processed and all 9 digits fail the validity check, the function returns
falseto trigger backtracking. - Single-Layer Search Logic:
- Iterate through rows using a
forloop. - Iterate through columns using a
forloop. - If a cell
board[i][j]is not '.', skip it. - If it is '.', iterate through characters
'1'to'9'. - Check if the digit is valid using
isValid(). - If valid, place the digit, recurse, and if the recursion returns
true, propagatetrueupwards. If not, reset the cell to'.'(backtrack).
- Iterate through rows using a
bool backtracking(vector<vector<char>>& board) { for (int i = 0; i < board.size(); i++) { for (int j = 0; j < board[0].size(); j++) { if (board[i][j] == '.') { for (char k = '1'; k <= '9'; k++) { if (isValid(i, j, k, board)) { board[i][j] = k; if (backtracking(board)) return true; board[i][j] = '.'; } } return false; } } } return true; }- Recursive Function and Parameters: The function should return a
Solve LeetCode 743: Network Delay Time
masterThe problem asks for the minimum time required for a signal sent from node
Kto reach allnnodes in a directed graph. You are given a list oftimeswhere each element is(ui, vi, wi)representing a directed edge fromuitoviwith weightwi. If not all nodes can be reached, return-1./* Problem Constraints: 1 <= k <= n <= 100 1 <= times.length <= 6000 1 <= ui, vi <= n 0 <= wi <= 100 */Master Dynamic Programming (DP) Sub-topics
masterThe Dynamic Programming section is highly granular, categorized into specific patterns to help you recognize problem types:
- Basic DP: Fibonacci numbers, climbing stairs, and different paths.
- Knapsack Problems (背包问题): 0/1 Knapsack (using 1D or 2D arrays), Complete Knapsack, and Multiple Knapsack.
- House Robber Series (打家劫舍): Variations of the house robber problem.
- Stock Series (股票系列): Best time to buy/sell stocks with various constraints (cooldowns, transaction fees, etc.).
- Subsequence Series (子序列系列): Longest Increasing Subsequence, Longest Common Subsequence, Edit Distance, and Palindromic Substrings.
Each sub-category includes theoretical foundations and summary guides.
Solve Max Area of Island using DFS (Two Patterns)
masterTo find the maximum area of an island in a binary matrix, you can use Depth First Search (DFS). The problem requires counting connected
1s (land) in four directions (horizontal and vertical).There are two common patterns for implementing DFS in this context:
- Pattern 1: DFS handles neighbors. The main loop identifies a land cell, increments the count to
1, and then calls DFS to process all subsequent adjacent land cells. - Pattern 2: DFS handles the current node. The main loop identifies a land cell but sets the initial count to
0. The DFS function is responsible for marking the current node as visited, incrementing the count, and then exploring neighbors.
// Pattern 1: DFS handles neighbors // Main loop: count = 1; dfs(grid, visited, i, j); void dfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) { for (int i = 0; i < 4; i++) { int nextx = x + dir[i][0]; int nexty = y + dir[i][1]; if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { visited[nextx][nexty] = true; count++; dfs(grid, visited, nextx, nexty); } } } // Pattern 2: DFS handles current node // Main loop: count = 0; dfs(grid, visited, i, j); void dfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) { if (visited[x][y] || grid[x][y] == 0) return; visited[x][y] = true; count++; for (int i = 0; i < 4; i++) { int nextx = x + dir[i][0]; int nexty = y + dir[i][1]; if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; dfs(grid, visited, nextx, nexty); } }- Pattern 1: DFS handles neighbors. The main loop identifies a land cell, increments the count to
Solve Palindrome Partitioning II using Dynamic Programming
masterTo find the minimum number of cuts required to partition a string
ssuch that every substring is a palindrome, use a two-step Dynamic Programming approach.1. Precompute Palindromes
Create a 2D boolean array
isPalindromic[i][j]whereisPalindromic[i][j]istrueif the substring from indexitojis a palindrome.- Base Case: If
s[i] == s[j]and the distance betweeniandjis $\le 1$, it is a palindrome. - Recursive Step: If
s[i] == s[j]andisPalindromic[i+1][j-1]istrue, it is a palindrome.
2. Compute Minimum Cuts
Use a 1D DP array
dp[i]representing the minimum cuts for the substrings[0...i].- Initialization:
dp[0] = 0. For $i > 0$, initializedp[i]to its maximum possible value (eitheriorINT_MAX). - Transition: For each
ifrom $1$ to $n-1$:- If
isPalindromic[0][i]istrue,dp[i] = 0. - Otherwise, iterate through all possible split points
jfrom $0$ to $i-1$. IfisPalindromic[j+1][i]istrue, updatedp[i] = min(dp[i], dp[j] + 1).
- If
Complexity:
- Time: $O(n^2)$
- Space: $O(n^2)$ to store the
isPalindromictable.
class Solution { public: int minCut(string s) { int n = s.size(); vector<vector<bool>> isPalindromic(n, vector<bool>(n, false)); for (int i = n - 1; i >= 0; i--) { for (int j = i; j < n; j++) { if (s[i] == s[j] && (j - i <= 1 || isPalindromic[i + 1][j - 1])) { isPalindromic[i][j] = true; } } } vector<int> dp(n); for (int i = 0; i < n; i++) dp[i] = i; for (int i = 1; i < n; i++) { if (isPalindromic[0][i]) { dp[i] = 0; continue; } for (int j = 0; j < i; j++) { if (isPalindromic[j + 1][i]) { dp[i] = min(dp[i], dp[j] + 1); } } } return dp[n - 1]; } };- Base Case: If