Перейти к содержимому

Boats to Save People (LeetCode 881) | Two Pointers Explained

Dry Run

0:00 / 0:00

Boats to Save People (LeetCode 881) | Two Pointers Explained

1 просмотр · 10 дн. назад
Dry Run
1 просмотр · 10 дн. назад
Find the fewest boats with two pointers (LeetCode 881), explained visually and traced line by line. Sort the weights; the heaviest person always takes a boat, and the lightest joins only if they fit. Loop while L is at most R, so the last person is never left behind. O(n log n) time because of the sort, in Python. Episode 7 of Season 1 on Dry Run, Part 2, pair sums. Season 1 is one pattern, two pointers, across 22 interview problems in 7 parts. Every episode goes intuition, brute force, key insight, code, then a full dry run. 0:00 Start 0:41 Recap 1:02 The problem 1:37 Brute force 2:20 Key insight 3:02 The code 3:30 Dry run 4:30 Common mistakes 5:04 Complexity 5:27 Interview follow-ups 6:06 The pattern Pattern: two pointers on sorted data, greedy pairing of heaviest and lightest Time: O(n log n) · Extra memory: L, R and the boat count, besides the sort Previous: 1679 Max Number of K-Sum Pairs Next: 977 Squares of a Sorted Array #TwoPointers #LeetCode #DSA