[go: up one dir, main page]

0% found this document useful (0 votes)
16 views4 pages

Pattern For LC

The document outlines various algorithmic techniques for solving common coding problems, categorized into ten main strategies including Sliding Window, Two Pointers, Fast & Slow Pointers, Merge Intervals, Binary Search, Bit Manipulation, Dynamic Programming, Graph Algorithms, Heap/Priority Queue, and Backtracking. Each category includes identification keywords, common algorithms, approaches, and example problems from LeetCode. The final advice emphasizes the importance of recognizing problem types and applying appropriate strategies for efficient problem-solving.

Uploaded by

royalminigaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Pattern For LC

The document outlines various algorithmic techniques for solving common coding problems, categorized into ten main strategies including Sliding Window, Two Pointers, Fast & Slow Pointers, Merge Intervals, Binary Search, Bit Manipulation, Dynamic Programming, Graph Algorithms, Heap/Priority Queue, and Backtracking. Each category includes identification keywords, common algorithms, approaches, and example problems from LeetCode. The final advice emphasizes the importance of recognizing problem types and applying appropriate strategies for efficient problem-solving.

Uploaded by

royalminigaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Sliding Window:

Identification:
The problem involves subarrays or substrings.
Given constraints often require an efficient solution (O(n)) instead of brute force
(O(n²)).
Keywords: "contiguous subarray", "smallest/largest subarray", "longest/shortest
substring", "at most k times".

Common Algorithms:
Fixed-size window: Use when the window size is fixed.
Variable-size window: Use when the window size changes dynamically.

Approach:
Maintain a start and end pointer (window).
Expand end to grow the window.
If constraints are violated, shrink start to restore validity.
Keep track of the best answer.

Example Problems:
Longest Substring Without Repeating Characters (LeetCode 3)
Minimum Window Substring (LeetCode 76)
Max Consecutive Ones III (LeetCode 1004)

2. Two Pointers

Identification:
The problem involves sorted arrays or linked lists.
The question asks for a pair or triplet sum.
Keywords: "sorted", "sum to target", "remove duplicates".

Common Algorithms:
Opposite Ends: One pointer at the start, one at the end.
Same Direction: Two pointers moving at different speeds (Fast & Slow pointers).

Approach:
Use two pointers that either move towards or away from each other.
Compare elements and adjust pointers accordingly.
Reduce redundant computations by leveraging sorted order.

Example Problems:
Two Sum II – Input Array Is Sorted (LeetCode 167)
Container With Most Water (LeetCode 11)
Remove Duplicates from Sorted Array (LeetCode 26)

3. Fast & Slow Pointers (Floyd’s Algorithm)

Identification:
Used in linked lists and cycle detection.
Keywords: "loop", "detect cycle", "middle of linked list".

Common Algorithms:
Tortoise and Hare (Cycle Detection)
Finding Middle of Linked List
Detecting the Start of a Cycle

Approach:
Use two pointers (slow and fast).
Move slow by one step, fast by two steps.
If fast meets slow, there’s a cycle.
To find the cycle start, reset one pointer to head and move both one step at a
time.

Example Problems:
Linked List Cycle (LeetCode 141)
Find the Duplicate Number (LeetCode 287)
Middle of the Linked List (LeetCode 876)

4. Merge Intervals

Identification:
Problem involves ranges, intervals, or time slots.
Keywords: "merge overlapping intervals", "find gaps".

Common Algorithms:
Sorting + Merging
Greedy Approach for Interval Scheduling

Approach:
Sort intervals by start time.
Iterate through the intervals and merge overlapping ones.
Use a list or stack to store merged intervals.

Example Problems:
Merge Intervals (LeetCode 56)
Insert Interval (LeetCode 57)
Meeting Rooms II (LeetCode 253)

5. Binary Search

Identification:
The input is sorted or the problem requires a logarithmic solution.
Keywords: "find the minimum/maximum", "search in sorted".

Common Algorithms:
Standard Binary Search
Lower Bound / Upper Bound
Binary Search on Answer (e.g., Minimize Max Distance)

Approach:
Define low and high pointers.
Find mid, check if it satisfies conditions.
If condition is met, move left or right accordingly.

Example Problems:
Binary Search (LeetCode 704)
Find Peak Element (LeetCode 162)
Median of Two Sorted Arrays (LeetCode 4)

6. Bit Manipulation

Identification:
The problem involves numbers and operations like XOR, AND, OR.
Keywords: "find unique number", "toggle bits".

Common Algorithms:
XOR for finding unique numbers
Bit Shifting for power of two checks
Approach:
Use XOR properties for finding missing numbers or single elements.
Use bitwise AND/OR for toggling or checking bit positions.

Example Problems:
Single Number (LeetCode 136)
Missing Number (LeetCode 268)
Number of 1 Bits (LeetCode 191)

7. Dynamic Programming (DP)

Identification:
The problem has overlapping subproblems.
Keywords: "minimum/maximum ways", "subproblem solutions".

Common Algorithms:
Top-down (Recursion + Memoization)
Bottom-up (Tabulation)
Knapsack-like approaches

Approach:
Identify state representation.
Define recurrence relation.
Optimize using memoization/tabulation.

Example Problems:
Climbing Stairs (LeetCode 70)
Longest Increasing Subsequence (LeetCode 300)
House Robber (LeetCode 198)

8. Graph Algorithms (BFS/DFS)

Identification:
The problem involves grids, graphs, trees.
Keywords: "shortest path", "connected components", "cycle detection".

Common Algorithms:
DFS & BFS for traversal
Dijkstra’s Algorithm for shortest path
Union-Find for connected components

Approach:
DFS for depth-based exploration.
BFS for shortest paths and level-wise traversal.
Union-Find for connectivity.

Example Problems:
Number of Islands (LeetCode 200)
Word Ladder (LeetCode 127)
Course Schedule (LeetCode 207)

9. Heap / Priority Queue

Identification:
The problem requires efficient min/max retrieval.
Keywords: "top K elements", "minimum/maximum element".
Common Algorithms:
Min-Heap for smallest elements
Max-Heap for largest elements

Approach:
Use a priority queue (heap).
Add and remove elements efficiently.
Maintain heap size for top-K problems.

Example Problems:
Kth Largest Element in an Array (LeetCode 215)
Top K Frequent Elements (LeetCode 347)
Find Median from Data Stream (LeetCode 295)

10. Backtracking

Identification:
The problem requires generating all possible solutions.
Keywords: "combinations", "permutations", "generate all".
Common Algorithms:
Recursive decision tree
Pruning for optimization

Approach:
Try a possible move.
Recur to explore deeper.
Backtrack when a move is invalid.

Example Problems:
Subset Generation (LeetCode 78)
Word Search (LeetCode 79)
N-Queens (LeetCode 51)

Final Advice
Solve problems using patterns, not random practice.
Identify the problem type first, then apply the best approach.
Mastering these patterns will make Leetcode much easier!

You might also like