Episode 7 — DSA with JavaScript / 7.8 — Advanced Array Problems
7.8 — Quick Revision: Advanced Arrays
Key techniques
| Technique | When to use | Time |
|---|---|---|
| Two pointers (opposite) | Sorted array, pair finding | O(n) |
| Two pointers (same dir) | Sliding window, fast/slow | O(n) |
| Prefix sum | Range queries, subarray sums | O(n) build, O(1) query |
| Variable sliding window | Substring/subarray with constraint | O(n) |
| Matrix traversal | Spiral, diagonal, zigzag | O(m×n) |
Matrix operations
| Operation | Method | Time |
|---|---|---|
| Rotate 90° | Transpose + reverse rows | O(n²) |
| Spiral order | 4 boundaries | O(m×n) |
| Search sorted | Binary search (1D mapping) | O(log mn) |
| Set zeroes | First row/col as markers | O(mn), O(1) space |
Self-check drill
SC-001
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-002
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-003
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-004
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-005
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-006
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-007
- Q: Min window substring?
- A: Variable window + frequency maps
SC-008
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-009
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-010
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-011
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-012
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-013
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-014
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-015
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-016
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-017
- Q: Min window substring?
- A: Variable window + frequency maps
SC-018
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-019
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-020
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-021
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-022
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-023
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-024
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-025
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-026
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-027
- Q: Min window substring?
- A: Variable window + frequency maps
SC-028
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-029
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-030
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-031
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-032
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-033
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-034
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-035
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-036
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-037
- Q: Min window substring?
- A: Variable window + frequency maps
SC-038
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-039
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-040
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-041
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-042
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-043
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-044
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-045
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-046
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-047
- Q: Min window substring?
- A: Variable window + frequency maps
SC-048
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-049
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-050
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-051
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-052
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-053
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-054
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-055
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-056
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-057
- Q: Min window substring?
- A: Variable window + frequency maps
SC-058
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-059
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-060
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-061
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-062
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-063
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-064
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-065
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-066
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-067
- Q: Min window substring?
- A: Variable window + frequency maps
SC-068
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-069
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-070
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-071
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-072
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-073
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-074
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-075
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-076
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-077
- Q: Min window substring?
- A: Variable window + frequency maps
SC-078
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-079
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-080
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-081
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-082
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-083
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-084
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-085
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-086
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-087
- Q: Min window substring?
- A: Variable window + frequency maps
SC-088
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-089
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-090
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-091
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-092
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-093
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-094
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-095
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-096
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-097
- Q: Min window substring?
- A: Variable window + frequency maps
SC-098
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-099
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-100
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-101
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-102
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-103
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-104
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-105
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-106
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-107
- Q: Min window substring?
- A: Variable window + frequency maps
SC-108
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-109
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-110
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-111
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-112
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-113
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-114
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-115
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-116
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-117
- Q: Min window substring?
- A: Variable window + frequency maps
SC-118
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-119
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-120
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-121
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-122
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-123
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-124
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-125
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-126
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-127
- Q: Min window substring?
- A: Variable window + frequency maps
SC-128
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-129
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-130
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-131
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-132
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-133
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-134
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-135
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-136
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-137
- Q: Min window substring?
- A: Variable window + frequency maps
SC-138
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-139
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-140
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-141
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-142
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-143
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-144
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-145
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-146
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-147
- Q: Min window substring?
- A: Variable window + frequency maps
SC-148
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-149
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-150
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-151
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-152
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-153
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-154
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-155
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-156
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-157
- Q: Min window substring?
- A: Variable window + frequency maps
SC-158
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-159
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-160
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-161
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-162
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-163
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-164
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-165
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-166
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-167
- Q: Min window substring?
- A: Variable window + frequency maps
SC-168
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-169
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-170
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-171
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-172
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-173
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-174
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-175
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-176
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-177
- Q: Min window substring?
- A: Variable window + frequency maps
SC-178
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-179
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-180
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-181
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-182
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-183
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-184
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-185
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-186
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-187
- Q: Min window substring?
- A: Variable window + frequency maps
SC-188
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-189
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-190
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse
SC-191
- Q: Container water approach?
- A: Two pointers, move shorter side
SC-192
- Q: 3Sum avoid duplicates?
- A: Sort, skip same values
SC-193
- Q: Prefix sum for range?
- A: prefix[r] - prefix[l-1]
SC-194
- Q: Spiral traverse?
- A: 4 boundaries, shrink after each direction
SC-195
- Q: Rotate matrix?
- A: Transpose + reverse rows
SC-196
- Q: Search sorted 2D?
- A: Binary search on flattened index
SC-197
- Q: Min window substring?
- A: Variable window + frequency maps
SC-198
- Q: Sliding window max?
- A: Deque maintaining max candidates
SC-199
- Q: Merge intervals?
- A: Sort by start, extend overlapping
SC-200
- Q: Next permutation?
- A: Find rightmost ascent, swap ceiling, reverse