Boats to Save People
Find the minimum number of boats to carry people within a weight limit using sorting and two pointers, in O(n log n) time.
Arrays, Strings, Sliding Window, Two Pointers, Searching & Binary Search, Sorting, Linked List, Stack & Queue, Recursion & Backtracking, Trees, Heap / Priority Queue, Graphs, Dynamic Programming, Greedy, Bit Manipulation, Trie, Math & Number Theory, Design (LLD / OOP), SQL, System Design (HLD)
180 notes in 20 topics
Find the minimum number of boats to carry people within a weight limit using sorting and two pointers, in O(n log n) time.
Count triplets that can form a triangle using sorting and two pointers, in O(n^2) time.
Find the minimum swaps to make two arrays strictly increasing using a linear dynamic-programming scan over paired positions, in O(n) time.
Check if two sentences become equal by inserting one contiguous run of words, using two pointers over the common prefix and suffix, in O(n) time.
Compute the shortest distance from each index to a target character using a two-pass left-then-right scan, in O(n) time.
Check if a string is a valid interleaving of two others using dynamic programming (a naive two pointer greedy fails), in O(m*n) time.
Check whether a DNA strand is its own reverse complement using two pointers from both ends with base-pair matching (A-T, C-G), in O(n) time.
Check whether one string is a subsequence of another using two pointers in O(n) time and O(1) space.
Understand the two core two pointer patterns — opposite-direction (converging) and same-direction (fast/slow or read/write) — with when to use each in...
Master the fast and slow pointer technique (Floyd’s algorithm): detect cycles, find the middle, and locate the nth node from the end, all in O(n) time...
Classic iterative binary search on a sorted array in O(log n) time, with Python, Java and C++ code and an overflow-safe midpoint.
Recursive binary search on a sorted array in O(log n) time with O(log n) call-stack space, in Python, Java and C++.