16-Hour Algorithms
16-Hour Dense LeetCode Algorithm Training Course
Course Overview
This intensive course focuses on pattern recognition and systematic problem-solving approaches. Each session builds upon previous concepts while introducing new algorithmic patterns commonly tested in technical interviews.
Hour 1-2: Foundation & Two Pointers
Problem 1: Two Sum (LeetCode #1)
Description: Given an array of integers nums
and an integer target
, return indices of the two numbers such that they add up to target
.
Algorithm: TWO-SUM(nums, target)
1 n ← LENGTH(nums)2 hash_map ← empty hash table3 for i ← 0 to n-14 complement ← target - nums[i]5 if complement ∈ hash_map6 return [hash_map[complement], i]7 hash_map[nums[i]] ← i8 return []
Time Complexity: O(n) | Space Complexity: O(n)
Problem 2: Valid Palindrome (LeetCode #125)
Description: A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward.
Algorithm: IS-PALINDROME(s)
1 left ← 02 right ← LENGTH(s) - 13 while left < right4 while left < right and NOT ALPHANUMERIC(s[left])5 left ← left + 16 while left < right and NOT ALPHANUMERIC(s[right])7 right ← right - 18 if LOWERCASE(s[left]) ≠ LOWERCASE(s[right])9 return FALSE10 left ← left + 111 right ← right - 112 return TRUE
Time Complexity: O(n) | Space Complexity: O(1)
Problem 3: Container With Most Water (LeetCode #11)
Description: You are given an integer array height
of length n
. Find two lines that together with the x-axis form a container that holds the most water.
Algorithm: MAX-AREA(height)
1 left ← 02 right ← LENGTH(height) - 13 max_area ← 04 while left < right5 width ← right - left6 area ← width × MIN(height[left], height[right])7 max_area ← MAX(max_area, area)8 if height[left] < height[right]9 left ← left + 110 else11 right ← right - 112 return max_area
Time Complexity: O(n) | Space Complexity: O(1)
Problem 4: 3Sum (LeetCode #15)
Description: Given an integer array nums
, return all the triplets [nums[i], nums[j], nums[k]]
such that i != j
, i != k
, and j != k
, and nums[i] + nums[j] + nums[k] == 0
.
Algorithm: THREE-SUM(nums)
1 SORT(nums)2 result ← empty list3 n ← LENGTH(nums)4 for i ← 0 to n-35 if i > 0 and nums[i] = nums[i-1]6 continue7 left ← i + 18 right ← n - 19 while left < right10 sum ← nums[i] + nums[left] + nums[right]11 if sum = 012 ADD-TO-LIST(result, [nums[i], nums[left], nums[right]])13 while left < right and nums[left] = nums[left+1]14 left ← left + 115 while left < right and nums[right] = nums[right-1]16 right ← right - 117 left ← left + 118 right ← right - 119 else if sum < 020 left ← left + 121 else22 right ← right - 123 return result
Time Complexity: O(n²) | Space Complexity: O(1)
Hour 3-4: Sliding Window & String Manipulation
Problem 5: Longest Substring Without Repeating Characters (LeetCode #3)
Description: Given a string s
, find the length of the longest substring without repeating characters.
Algorithm: LENGTH-OF-LONGEST-SUBSTRING(s)
1 n ← LENGTH(s)2 char_map ← empty hash table3 left ← 04 max_length ← 05 for right ← 0 to n-16 if s[right] ∈ char_map and char_map[s[right]] ≥ left7 left ← char_map[s[right]] + 18 char_map[s[right]] ← right9 max_length ← MAX(max_length, right - left + 1)10 return max_length
Time Complexity: O(n) | Space Complexity: O(min(m,n)) where m is charset size
Problem 6: Minimum Window Substring (LeetCode #76)
Description: Given two strings s
and t
, return the minimum window substring of s
such that every character in t
is included in the window.
Algorithm: MIN-WINDOW(s, t)
1 if LENGTH(s) < LENGTH(t)2 return ""3 t_count ← CHARACTER-FREQUENCY(t)4 required ← SIZE(t_count)5 left ← 0, right ← 06 formed ← 07 window_counts ← empty hash table8 ans ← [∞, 0, 0] // length, left, right9 while right < LENGTH(s)10 char ← s[right]11 window_counts[char] ← window_counts[char] + 112 if char ∈ t_count and window_counts[char] = t_count[char]13 formed ← formed + 114 while left ≤ right and formed = required15 if right - left + 1 < ans[0]16 ans ← [right - left + 1, left, right]17 char ← s[left]18 window_counts[char] ← window_counts[char] - 119 if char ∈ t_count and window_counts[char] < t_count[char]20 formed ← formed - 121 left ← left + 122 right ← right + 123 return ans[0] = ∞ ? "" : s[ans[1] : ans[2] + 1]
Time Complexity: O(|s| + |t|) | Space Complexity: O(|s| + |t|)
Problem 7: Sliding Window Maximum (LeetCode #239)
Description: You are given an array of integers nums
, and a sliding window of size k
which is moving from left to right. Return the max sliding window.
Algorithm: MAX-SLIDING-WINDOW(nums, k)
1 deque ← empty double-ended queue2 result ← empty list3 for i ← 0 to LENGTH(nums) - 14 while NOT EMPTY(deque) and deque[0] ≤ i - k5 REMOVE-FRONT(deque)6 while NOT EMPTY(deque) and nums[deque[BACK(deque)]] ≤ nums[i]7 REMOVE-BACK(deque)8 ADD-BACK(deque, i)9 if i ≥ k - 110 ADD-TO-LIST(result, nums[deque[0]])11 return result
Time Complexity: O(n) | Space Complexity: O(k)
Problem 8: Longest Repeating Character Replacement (LeetCode #424)
Description: You can choose any character and change it to any other character. Find the length of the longest substring containing the same letter you can get after performing at most k
operations.
Algorithm: CHARACTER-REPLACEMENT(s, k)
1 left ← 02 max_count ← 03 max_length ← 04 count ← empty hash table5 for right ← 0 to LENGTH(s) - 16 count[s[right]] ← count[s[right]] + 17 max_count ← MAX(max_count, count[s[right]])8 while right - left + 1 - max_count > k9 count[s[left]] ← count[s[left]] - 110 left ← left + 111 max_length ← MAX(max_length, right - left + 1)12 return max_length
Time Complexity: O(n) | Space Complexity: O(1)
Hour 5-6: Arrays & Matrix Manipulation
Problem 9: Rotate Image (LeetCode #48)
Description: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees clockwise in-place.
Algorithm: ROTATE(matrix)
1 n ← LENGTH(matrix)2 // Transpose matrix3 for i ← 0 to n-14 for j ← i to n-15 SWAP(matrix[i][j], matrix[j][i])6 // Reverse each row7 for i ← 0 to n-18 left ← 09 right ← n - 110 while left < right11 SWAP(matrix[i][left], matrix[i][right])12 left ← left + 113 right ← right - 1
Time Complexity: O(n²) | Space Complexity: O(1)
Problem 10: Spiral Matrix (LeetCode #54)
Description: Given an m x n matrix, return all elements of the matrix in spiral order.
Algorithm: SPIRAL-ORDER(matrix)
1 if EMPTY(matrix)2 return []3 result ← empty list4 top ← 0, bottom ← LENGTH(matrix) - 15 left ← 0, right ← LENGTH(matrix[0]) - 16 while top ≤ bottom and left ≤ right7 // Traverse right8 for col ← left to right9 ADD-TO-LIST(result, matrix[top][col])10 top ← top + 111 // Traverse down12 for row ← top to bottom13 ADD-TO-LIST(result, matrix[row][right])14 right ← right - 115 if top ≤ bottom16 // Traverse left17 for col ← right downto left18 ADD-TO-LIST(result, matrix[bottom][col])19 bottom ← bottom - 120 if left ≤ right21 // Traverse up22 for row ← bottom downto top23 ADD-TO-LIST(result, matrix[row][left])24 left ← left + 125 return result
Time Complexity: O(m×n) | Space Complexity: O(1)
Problem 11: Set Matrix Zeroes (LeetCode #73)
Description: Given an m x n integer matrix, if an element is 0, set its entire row and column to 0’s. Do it in-place.
Algorithm: SET-ZEROES(matrix)
1 m ← LENGTH(matrix), n ← LENGTH(matrix[0])2 first_row_zero ← FALSE, first_col_zero ← FALSE3 // Check if first row has zero4 for j ← 0 to n-15 if matrix[0][j] = 06 first_row_zero ← TRUE7 break8 // Check if first column has zero9 for i ← 0 to m-110 if matrix[i][0] = 011 first_col_zero ← TRUE12 break13 // Use first row and column as markers14 for i ← 1 to m-115 for j ← 1 to n-116 if matrix[i][j] = 017 matrix[i][0] ← 018 matrix[0][j] ← 019 // Set zeros based on markers20 for i ← 1 to m-121 for j ← 1 to n-122 if matrix[i][0] = 0 or matrix[0][j] = 023 matrix[i][j] ← 024 // Handle first row and column25 if first_row_zero26 for j ← 0 to n-127 matrix[0][j] ← 028 if first_col_zero29 for i ← 0 to m-130 matrix[i][0] ← 0
Time Complexity: O(m×n) | Space Complexity: O(1)
Problem 12: Product of Array Except Self (LeetCode #238)
Description: Given an integer array nums
, return an array such that answer[i]
is equal to the product of all elements of nums
except nums[i]
.
Algorithm: PRODUCT-EXCEPT-SELF(nums)
1 n ← LENGTH(nums)2 result ← array of size n3 // Left pass4 result[0] ← 15 for i ← 1 to n-16 result[i] ← result[i-1] × nums[i-1]7 // Right pass8 right ← 19 for i ← n-1 downto 010 result[i] ← result[i] × right11 right ← right × nums[i]12 return result
Time Complexity: O(n) | Space Complexity: O(1)
Hour 7-8: Linked Lists & Fast/Slow Pointers
Problem 13: Reverse Linked List (LeetCode #206)
Description: Given the head of a singly linked list, reverse the list and return the reversed list.
Algorithm: REVERSE-LIST(head)
1 prev ← NIL2 current ← head3 while current ≠ NIL4 next_temp ← current.next5 current.next ← prev6 prev ← current7 current ← next_temp8 return prev
Time Complexity: O(n) | Space Complexity: O(1)
Problem 14: Linked List Cycle II (LeetCode #142)
Description: Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.
Algorithm: DETECT-CYCLE(head)
1 if head = NIL or head.next = NIL2 return NIL3 slow ← head4 fast ← head5 // Phase 1: Detect cycle6 while fast ≠ NIL and fast.next ≠ NIL7 slow ← slow.next8 fast ← fast.next.next9 if slow = fast10 break11 if fast = NIL or fast.next = NIL12 return NIL13 // Phase 2: Find cycle start14 slow ← head15 while slow ≠ fast16 slow ← slow.next17 fast ← fast.next18 return slow
Time Complexity: O(n) | Space Complexity: O(1)
Problem 15: Merge Two Sorted Lists (LeetCode #21)
Description: You are given the heads of two sorted linked lists. Merge them into one sorted list.
Algorithm: MERGE-TWO-LISTS(list1, list2)
1 dummy ← new ListNode(0)2 current ← dummy3 while list1 ≠ NIL and list2 ≠ NIL4 if list1.val ≤ list2.val5 current.next ← list16 list1 ← list1.next7 else8 current.next ← list29 list2 ← list2.next10 current ← current.next11 current.next ← list1 ≠ NIL ? list1 : list212 return dummy.next
Time Complexity: O(n + m) | Space Complexity: O(1)
Problem 16: Remove Nth Node From End of List (LeetCode #19)
Description: Given the head of a linked list, remove the nth node from the end of the list and return its head.
Algorithm: REMOVE-NTH-FROM-END(head, n)
1 dummy ← new ListNode(0)2 dummy.next ← head3 first ← dummy4 second ← dummy5 // Move first n+1 steps ahead6 for i ← 0 to n7 first ← first.next8 // Move both pointers until first reaches end9 while first ≠ NIL10 first ← first.next11 second ← second.next12 // Remove the nth node13 second.next ← second.next.next14 return dummy.next
Time Complexity: O(n) | Space Complexity: O(1)
Hour 9-10: Stack & Queue Patterns
Problem 17: Valid Parentheses (LeetCode #20)
Description: Given a string s containing just the characters ’(’, ’)’, ’{’, ’}’, ’[’ and ’]’, determine if the input string is valid.
Algorithm: IS-VALID(s)
1 stack ← empty stack2 mapping ← {')': '(', '}': '{', ']': '['}3 for char in s4 if char ∈ mapping5 top_element ← EMPTY(stack) ? '#' : POP(stack)6 if mapping[char] ≠ top_element7 return FALSE8 else9 PUSH(stack, char)10 return EMPTY(stack)
Time Complexity: O(n) | Space Complexity: O(n)
Problem 18: Daily Temperatures (LeetCode #739)
Description: Given an array of integers temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature.
Algorithm: DAILY-TEMPERATURES(temperatures)
1 n ← LENGTH(temperatures)2 result ← array of size n initialized to 03 stack ← empty stack4 for i ← 0 to n-15 while NOT EMPTY(stack) and temperatures[TOP(stack)] < temperatures[i]6 prev_index ← POP(stack)7 result[prev_index] ← i - prev_index8 PUSH(stack, i)9 return result
Time Complexity: O(n) | Space Complexity: O(n)
Problem 19: Largest Rectangle in Histogram (LeetCode #84)
Description: Given an array of integers heights representing the histogram’s bar height, return the area of the largest rectangle in the histogram.
Algorithm: LARGEST-RECTANGLE-AREA(heights)
1 stack ← empty stack2 max_area ← 03 index ← 04 while index < LENGTH(heights)5 if EMPTY(stack) or heights[index] ≥ heights[TOP(stack)]6 PUSH(stack, index)7 index ← index + 18 else9 top ← POP(stack)10 width ← EMPTY(stack) ? index : index - TOP(stack) - 111 area ← heights[top] × width12 max_area ← MAX(max_area, area)13 while NOT EMPTY(stack)14 top ← POP(stack)15 width ← EMPTY(stack) ? index : index - TOP(stack) - 116 area ← heights[top] × width17 max_area ← MAX(max_area, area)18 return max_area
Time Complexity: O(n) | Space Complexity: O(n)
Problem 20: Implement Queue using Stacks (LeetCode #232)
Description: Implement a first in first out (FIFO) queue using only two stacks.
Algorithm: MyQueue Class
Class MyQueue: stack1 ← empty stack // for enqueue stack2 ← empty stack // for dequeue
ENQUEUE(x):1 PUSH(stack1, x)
DEQUEUE():1 if EMPTY(stack2)2 while NOT EMPTY(stack1)3 PUSH(stack2, POP(stack1))4 return POP(stack2)
PEEK():1 if EMPTY(stack2)2 while NOT EMPTY(stack1)3 PUSH(stack2, POP(stack1))4 return TOP(stack2)
EMPTY():1 return EMPTY(stack1) and EMPTY(stack2)
Time Complexity: O(1) amortized for all operations | Space Complexity: O(n)
Hour 11-12: Binary Search & Sorted Arrays
Problem 21: Binary Search (LeetCode #704)
Description: Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums.
Algorithm: BINARY-SEARCH(nums, target)
1 left ← 02 right ← LENGTH(nums) - 13 while left ≤ right4 mid ← left + (right - left) / 25 if nums[mid] = target6 return mid7 else if nums[mid] < target8 left ← mid + 19 else10 right ← mid - 111 return -1
Time Complexity: O(log n) | Space Complexity: O(1)
Problem 22: Search in Rotated Sorted Array (LeetCode #33)
Description: There is an integer array nums sorted in ascending order. Given the array after rotating it and an integer target, return the index of target.
Algorithm: SEARCH-ROTATED(nums, target)
1 left ← 02 right ← LENGTH(nums) - 13 while left ≤ right4 mid ← left + (right - left) / 25 if nums[mid] = target6 return mid7 if nums[left] ≤ nums[mid] // Left half is sorted8 if nums[left] ≤ target < nums[mid]9 right ← mid - 110 else11 left ← mid + 112 else // Right half is sorted13 if nums[mid] < target ≤ nums[right]14 left ← mid + 115 else16 right ← mid - 117 return -1
Time Complexity: O(log n) | Space Complexity: O(1)
Problem 23: Find Minimum in Rotated Sorted Array (LeetCode #153)
Description: Suppose an array of length n sorted in ascending order is rotated. Find the minimum element.
Algorithm: FIND-MIN(nums)
1 left ← 02 right ← LENGTH(nums) - 13 while left < right4 mid ← left + (right - left) / 25 if nums[mid] > nums[right]6 left ← mid + 17 else8 right ← mid9 return nums[left]
Time Complexity: O(log n) | Space Complexity: O(1)
Problem 24: Search a 2D Matrix (LeetCode #74)
Description: You are given an m x n integer matrix with properties: integers in each row are sorted left to right, and the first integer of each row is greater than the last integer of the previous row.
Algorithm: SEARCH-MATRIX(matrix, target)
1 if EMPTY(matrix) or EMPTY(matrix[0])2 return FALSE3 m ← LENGTH(matrix)4 n ← LENGTH(matrix[0])5 left ← 06 right ← m × n - 17 while left ≤ right8 mid ← left + (right - left) / 29 mid_value ← matrix[mid / n][mid % n]10 if mid_value = target11 return TRUE12 else if mid_value < target13 left ← mid + 114 else15 right ← mid - 116 return FALSE
Time Complexity: O(log(m×n)) | Space Complexity: O(1)
Hour 13-14: Trees & Depth-First Search
Problem 25: Maximum Depth of Binary Tree (LeetCode #104)
Description: Given the root of a binary tree, return its maximum depth.
Algorithm: MAX-DEPTH(root)
1 if root = NIL2 return 03 left_depth ← MAX-DEPTH(root.left)4 right_depth ← MAX-DEPTH(root.right)5 return 1 + MAX(left_depth, right_depth)
Time Complexity: O(n) | Space Complexity: O(h) where h is height
Problem 26: Validate Binary Search Tree (LeetCode #98)
Description: Given the root of a binary tree, determine if it is a valid binary search tree (BST).
Algorithm: IS-VALID-BST(root)
1 return VALIDATE(root, -∞, +∞)
VALIDATE(node, min_val, max_val):1 if node = NIL2 return TRUE3 if node.val ≤ min_val or node.val ≥ max_val4 return FALSE5 return VALIDATE(node.left, min_val, node.val) and VALIDATE(node.right, node.val, max_val)
Time Complexity: O(n) | Space Complexity: O(h)
Problem 27: Lowest Common Ancestor of a Binary Search Tree (LeetCode #235)
Description: Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.
Algorithm: LOWEST-COMMON-ANCESTOR(root, p, q)
1 while root ≠ NIL2 if p.val < root.val and q.val < root.val3 root ← root.left4 else if p.val > root.val and q.val > root.val5 root ← root.right6 else7 return root8 return NIL
Time Complexity: O(h) | Space Complexity: O(1)
Problem 28: Binary Tree Right Side View (LeetCode #199)
Description: Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
Algorithm: RIGHT-SIDE-VIEW(root)
1 result ← empty list2 RIGHT-VIEW-DFS(root, 0, result)3 return result
RIGHT-VIEW-DFS(node, level, result):1 if node = NIL2 return3 if level = LENGTH(result)4 ADD-TO-LIST(result, node.val)5 RIGHT-VIEW-DFS(node.right, level + 1, result)6 RIGHT-VIEW-DFS(node.left, level + 1, result)
Time Complexity: O(n) | Space Complexity: O(h)
Hour 15-16: Dynamic Programming Foundations
Problem 29: Climbing Stairs (LeetCode #70)
Description: You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Algorithm: CLIMB-STAIRS(n)
1 if n ≤ 22 return n3 prev2 ← 14 prev1 ← 25 for i ← 3 to n6 current ← prev1 + prev27 prev2 ← prev18 prev1 ← current9 return prev1
Time Complexity: O(n) | Space Complexity: O(1)
Problem 30: House Robber (LeetCode #198)
Description: You are a robber planning to rob houses along a street. You cannot rob two adjacent houses. Given an array representing the amount of money in each house, return the maximum amount you can rob.
Algorithm: ROB(nums)
1 if EMPTY(nums)2 return 03 if LENGTH(nums) = 14 return nums[0]5 prev2 ← nums[0]6 prev1 ← MAX(nums[0], nums[1])7 for i ← 2 to LENGTH(nums) - 18 current ← MAX(prev1, prev2 + nums[i])9 prev2 ← prev110 prev1 ← current11 return prev1
Time Complexity: O(n) | Space Complexity: O(1)
Problem 31: Longest Increasing Subsequence (LeetCode #300)
Description: Given an integer array nums, return the length of the longest strictly increasing subsequence.
Algorithm: LENGTH-OF-LIS(nums)
1 if EMPTY(nums)2 return 03 dp ← array of size LENGTH(nums) initialized to 14 for i ← 1 to LENGTH(nums) - 15 for j ← 0 to i - 16 if nums[j] < nums[i]7 dp[i] ← MAX(dp[i], dp[j] + 1)8 return MAX(dp)
Time Complexity: O(n²) | Space Complexity: O(n)
Problem 32: Coin Change (LeetCode #322)
Description: You are given an integer array coins representing coins of different denominations and an integer amount. Return the fewest number of coins needed to make up that amount.
Algorithm: COIN-CHANGE(coins, amount)
1 dp ← array of size amount + 1 initialized to amount + 12 dp[0] ← 03 for i ← 1 to amount4 for coin in coins5 if coin ≤ i6 dp[i] ← MIN(dp[i], dp[i - coin] + 1)7 return dp[amount] > amount ? -1 : dp[amount]
Time Complexity: O(amount × n) | Space Complexity: O(amount)
Study Strategy & Tips
During Each Session
-
Read & Understand (5 minutes per problem)
- Identify the core problem type
- Note constraints and edge cases
-
Plan & Code (15-20 minutes per problem)
- Write pseudocode first
- Implement with clear variable names
- Test with examples
-
Optimize & Review (5-10 minutes per problem)
- Analyze time/space complexity
- Consider alternative approaches