interview-reps 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +59 -2
- package/content/CLAUDE.md +131 -0
- package/content/LADDER.md +166 -0
- package/content/behavioral/QUESTIONS.md +93 -0
- package/content/behavioral/REVIEW.md +86 -0
- package/content/design/D01-short-links/README.md +20 -0
- package/content/design/D02-rate-limiter/README.md +21 -0
- package/content/design/D03-news-feed/README.md +20 -0
- package/content/design/D04-chat-messaging/README.md +20 -0
- package/content/design/D05-notification-service/README.md +20 -0
- package/content/design/D06-typeahead-search/README.md +20 -0
- package/content/design/D07-web-crawler/README.md +20 -0
- package/content/design/D08-file-sync/README.md +20 -0
- package/content/design/D09-ride-matching/README.md +20 -0
- package/content/design/D10-job-scheduler/README.md +20 -0
- package/content/design/D11-metrics-alerting/README.md +20 -0
- package/content/design/D12-payments-ledger/README.md +20 -0
- package/content/design/REVIEW.md +94 -0
- package/content/lib/binary-tree.ts +69 -0
- package/content/lib/graph.ts +50 -0
- package/content/lib/linked-list.ts +37 -0
- package/content/lib/perf.ts +9 -0
- package/content/problems/001-contains-duplicate/README.md +37 -0
- package/content/problems/001-contains-duplicate/solution.test.ts +38 -0
- package/content/problems/002-valid-anagram/README.md +36 -0
- package/content/problems/002-valid-anagram/solution.test.ts +38 -0
- package/content/problems/003-two-sum/README.md +40 -0
- package/content/problems/003-two-sum/solution.test.ts +40 -0
- package/content/problems/004-valid-palindrome/README.md +39 -0
- package/content/problems/004-valid-palindrome/solution.test.ts +38 -0
- package/content/problems/005-best-time-to-buy-and-sell-stock/README.md +38 -0
- package/content/problems/005-best-time-to-buy-and-sell-stock/solution.test.ts +39 -0
- package/content/problems/006-valid-parentheses/README.md +42 -0
- package/content/problems/006-valid-parentheses/solution.test.ts +41 -0
- package/content/problems/007-binary-search/README.md +39 -0
- package/content/problems/007-binary-search/solution.test.ts +56 -0
- package/content/problems/008-reverse-linked-list/README.md +39 -0
- package/content/problems/008-reverse-linked-list/solution.test.ts +41 -0
- package/content/problems/009-merge-two-sorted-lists/README.md +36 -0
- package/content/problems/009-merge-two-sorted-lists/solution.test.ts +43 -0
- package/content/problems/010-linked-list-cycle/README.md +37 -0
- package/content/problems/010-linked-list-cycle/solution.test.ts +39 -0
- package/content/problems/011-invert-binary-tree/README.md +39 -0
- package/content/problems/011-invert-binary-tree/solution.test.ts +32 -0
- package/content/problems/012-maximum-depth-of-binary-tree/README.md +37 -0
- package/content/problems/012-maximum-depth-of-binary-tree/solution.test.ts +29 -0
- package/content/problems/013-same-tree/README.md +37 -0
- package/content/problems/013-same-tree/solution.test.ts +38 -0
- package/content/problems/014-balanced-binary-tree/README.md +37 -0
- package/content/problems/014-balanced-binary-tree/solution.test.ts +38 -0
- package/content/problems/015-climbing-stairs/README.md +35 -0
- package/content/problems/015-climbing-stairs/solution.test.ts +27 -0
- package/content/problems/016-diameter-of-binary-tree/README.md +38 -0
- package/content/problems/016-diameter-of-binary-tree/solution.test.ts +63 -0
- package/content/problems/017-subtree-of-another-tree/README.md +42 -0
- package/content/problems/017-subtree-of-another-tree/solution.test.ts +61 -0
- package/content/problems/018-kth-largest-element-in-a-stream/README.md +49 -0
- package/content/problems/018-kth-largest-element-in-a-stream/solution.test.ts +104 -0
- package/content/problems/019-last-stone-weight/README.md +44 -0
- package/content/problems/019-last-stone-weight/solution.test.ts +67 -0
- package/content/problems/020-min-cost-climbing-stairs/README.md +40 -0
- package/content/problems/020-min-cost-climbing-stairs/solution.test.ts +44 -0
- package/content/problems/021-single-number/README.md +41 -0
- package/content/problems/021-single-number/solution.test.ts +46 -0
- package/content/problems/022-missing-number/README.md +40 -0
- package/content/problems/022-missing-number/solution.test.ts +44 -0
- package/content/problems/101-group-anagrams/README.md +41 -0
- package/content/problems/101-group-anagrams/solution.test.ts +83 -0
- package/content/problems/102-top-k-frequent-elements/README.md +44 -0
- package/content/problems/102-top-k-frequent-elements/solution.test.ts +59 -0
- package/content/problems/103-product-of-array-except-self/README.md +43 -0
- package/content/problems/103-product-of-array-except-self/solution.test.ts +59 -0
- package/content/problems/104-longest-consecutive-sequence/README.md +40 -0
- package/content/problems/104-longest-consecutive-sequence/solution.test.ts +62 -0
- package/content/problems/105-encode-and-decode-strings/README.md +47 -0
- package/content/problems/105-encode-and-decode-strings/solution.test.ts +50 -0
- package/content/problems/106-two-sum-ii-input-array-is-sorted/README.md +43 -0
- package/content/problems/106-two-sum-ii-input-array-is-sorted/solution.test.ts +47 -0
- package/content/problems/107-three-sum/README.md +42 -0
- package/content/problems/107-three-sum/solution.test.ts +79 -0
- package/content/problems/108-container-with-most-water/README.md +40 -0
- package/content/problems/108-container-with-most-water/solution.test.ts +53 -0
- package/content/problems/109-longest-substring-without-repeating-characters/README.md +40 -0
- package/content/problems/109-longest-substring-without-repeating-characters/solution.test.ts +56 -0
- package/content/problems/110-longest-repeating-character-replacement/README.md +41 -0
- package/content/problems/110-longest-repeating-character-replacement/solution.test.ts +51 -0
- package/content/problems/111-permutation-in-string/README.md +38 -0
- package/content/problems/111-permutation-in-string/solution.test.ts +54 -0
- package/content/problems/112-min-stack/README.md +51 -0
- package/content/problems/112-min-stack/solution.test.ts +123 -0
- package/content/problems/113-daily-temperatures/README.md +40 -0
- package/content/problems/113-daily-temperatures/solution.test.ts +56 -0
- package/content/problems/114-evaluate-reverse-polish-notation/README.md +44 -0
- package/content/problems/114-evaluate-reverse-polish-notation/solution.test.ts +56 -0
- package/content/problems/115-generate-parentheses/README.md +38 -0
- package/content/problems/115-generate-parentheses/solution.test.ts +59 -0
- package/content/problems/116-koko-eating-bananas/README.md +41 -0
- package/content/problems/116-koko-eating-bananas/solution.test.ts +53 -0
- package/content/problems/117-find-minimum-in-rotated-sorted-array/README.md +41 -0
- package/content/problems/117-find-minimum-in-rotated-sorted-array/solution.test.ts +69 -0
- package/content/problems/118-search-in-rotated-sorted-array/README.md +41 -0
- package/content/problems/118-search-in-rotated-sorted-array/solution.test.ts +95 -0
- package/content/problems/119-time-based-key-value-store/README.md +50 -0
- package/content/problems/119-time-based-key-value-store/solution.test.ts +107 -0
- package/content/problems/120-reorder-list/README.md +43 -0
- package/content/problems/120-reorder-list/solution.test.ts +83 -0
- package/content/problems/121-remove-nth-node-from-end-of-list/README.md +40 -0
- package/content/problems/121-remove-nth-node-from-end-of-list/solution.test.ts +55 -0
- package/content/problems/122-lru-cache/README.md +55 -0
- package/content/problems/122-lru-cache/solution.test.ts +152 -0
- package/content/problems/123-binary-tree-level-order-traversal/README.md +40 -0
- package/content/problems/123-binary-tree-level-order-traversal/solution.test.ts +42 -0
- package/content/problems/124-binary-tree-right-side-view/README.md +40 -0
- package/content/problems/124-binary-tree-right-side-view/solution.test.ts +56 -0
- package/content/problems/125-validate-binary-search-tree/README.md +46 -0
- package/content/problems/125-validate-binary-search-tree/solution.test.ts +78 -0
- package/content/problems/126-kth-smallest-element-in-a-bst/README.md +39 -0
- package/content/problems/126-kth-smallest-element-in-a-bst/solution.test.ts +61 -0
- package/content/problems/127-lowest-common-ancestor-of-a-bst/README.md +46 -0
- package/content/problems/127-lowest-common-ancestor-of-a-bst/solution.test.ts +106 -0
- package/content/problems/128-kth-largest-element-in-an-array/README.md +42 -0
- package/content/problems/128-kth-largest-element-in-an-array/solution.test.ts +67 -0
- package/content/problems/129-k-closest-points-to-origin/README.md +43 -0
- package/content/problems/129-k-closest-points-to-origin/solution.test.ts +74 -0
- package/content/problems/130-number-of-islands/README.md +44 -0
- package/content/problems/130-number-of-islands/solution.test.ts +69 -0
- package/content/problems/131-course-schedule/README.md +42 -0
- package/content/problems/131-course-schedule/solution.test.ts +82 -0
- package/content/problems/132-rotting-oranges/README.md +46 -0
- package/content/problems/132-rotting-oranges/solution.test.ts +74 -0
- package/content/problems/133-clone-graph/README.md +42 -0
- package/content/problems/133-clone-graph/solution.test.ts +58 -0
- package/content/problems/134-house-robber/README.md +38 -0
- package/content/problems/134-house-robber/solution.test.ts +45 -0
- package/content/problems/135-coin-change/README.md +40 -0
- package/content/problems/135-coin-change/solution.test.ts +45 -0
- package/content/problems/136-longest-increasing-subsequence/README.md +41 -0
- package/content/problems/136-longest-increasing-subsequence/solution.test.ts +53 -0
- package/content/problems/137-word-break/README.md +39 -0
- package/content/problems/137-word-break/solution.test.ts +49 -0
- package/content/problems/138-merge-intervals/README.md +40 -0
- package/content/problems/138-merge-intervals/solution.test.ts +84 -0
- package/content/problems/139-insert-interval/README.md +40 -0
- package/content/problems/139-insert-interval/solution.test.ts +75 -0
- package/content/problems/201-valid-sudoku/README.md +63 -0
- package/content/problems/201-valid-sudoku/solution.test.ts +104 -0
- package/content/problems/202-car-fleet/README.md +46 -0
- package/content/problems/202-car-fleet/solution.test.ts +85 -0
- package/content/problems/203-search-a-2d-matrix/README.md +47 -0
- package/content/problems/203-search-a-2d-matrix/solution.test.ts +108 -0
- package/content/problems/204-copy-list-with-random-pointer/README.md +46 -0
- package/content/problems/204-copy-list-with-random-pointer/solution.test.ts +120 -0
- package/content/problems/205-add-two-numbers/README.md +45 -0
- package/content/problems/205-add-two-numbers/solution.test.ts +74 -0
- package/content/problems/206-find-the-duplicate-number/README.md +47 -0
- package/content/problems/206-find-the-duplicate-number/solution.test.ts +72 -0
- package/content/problems/207-count-good-nodes/README.md +44 -0
- package/content/problems/207-count-good-nodes/solution.test.ts +63 -0
- package/content/problems/208-construct-tree-from-preorder-and-inorder/README.md +52 -0
- package/content/problems/208-construct-tree-from-preorder-and-inorder/solution.test.ts +126 -0
- package/content/problems/209-implement-trie/README.md +52 -0
- package/content/problems/209-implement-trie/solution.test.ts +138 -0
- package/content/problems/210-design-add-and-search-words/README.md +50 -0
- package/content/problems/210-design-add-and-search-words/solution.test.ts +127 -0
- package/content/problems/211-task-scheduler/README.md +47 -0
- package/content/problems/211-task-scheduler/solution.test.ts +64 -0
- package/content/problems/212-design-twitter/README.md +63 -0
- package/content/problems/212-design-twitter/solution.test.ts +137 -0
- package/content/problems/213-subsets/README.md +42 -0
- package/content/problems/213-subsets/solution.test.ts +63 -0
- package/content/problems/214-combination-sum/README.md +44 -0
- package/content/problems/214-combination-sum/solution.test.ts +75 -0
- package/content/problems/215-permutations/README.md +42 -0
- package/content/problems/215-permutations/solution.test.ts +70 -0
- package/content/problems/216-word-search/README.md +50 -0
- package/content/problems/216-word-search/solution.test.ts +86 -0
- package/content/problems/217-letter-combinations-of-a-phone-number/README.md +53 -0
- package/content/problems/217-letter-combinations-of-a-phone-number/solution.test.ts +57 -0
- package/content/problems/218-palindrome-partitioning/README.md +42 -0
- package/content/problems/218-palindrome-partitioning/solution.test.ts +77 -0
- package/content/problems/219-max-area-of-island/README.md +43 -0
- package/content/problems/219-max-area-of-island/solution.test.ts +78 -0
- package/content/problems/220-pacific-atlantic-water-flow/README.md +43 -0
- package/content/problems/220-pacific-atlantic-water-flow/solution.test.ts +86 -0
- package/content/problems/221-course-schedule-ii/README.md +42 -0
- package/content/problems/221-course-schedule-ii/solution.test.ts +107 -0
- package/content/problems/222-graph-valid-tree/README.md +42 -0
- package/content/problems/222-graph-valid-tree/solution.test.ts +75 -0
- package/content/problems/223-redundant-connection/README.md +42 -0
- package/content/problems/223-redundant-connection/solution.test.ts +68 -0
- package/content/problems/224-network-delay-time/README.md +44 -0
- package/content/problems/224-network-delay-time/solution.test.ts +70 -0
- package/content/problems/225-cheapest-flights-within-k-stops/README.md +45 -0
- package/content/problems/225-cheapest-flights-within-k-stops/solution.test.ts +85 -0
- package/content/problems/226-house-robber-ii/README.md +41 -0
- package/content/problems/226-house-robber-ii/solution.test.ts +51 -0
- package/content/problems/227-longest-palindromic-substring/README.md +41 -0
- package/content/problems/227-longest-palindromic-substring/solution.test.ts +69 -0
- package/content/problems/228-decode-ways/README.md +44 -0
- package/content/problems/228-decode-ways/solution.test.ts +50 -0
- package/content/problems/229-maximum-product-subarray/README.md +40 -0
- package/content/problems/229-maximum-product-subarray/solution.test.ts +55 -0
- package/content/problems/230-partition-equal-subset-sum/README.md +39 -0
- package/content/problems/230-partition-equal-subset-sum/solution.test.ts +54 -0
- package/content/problems/231-unique-paths/README.md +41 -0
- package/content/problems/231-unique-paths/solution.test.ts +45 -0
- package/content/problems/232-longest-common-subsequence/README.md +41 -0
- package/content/problems/232-longest-common-subsequence/solution.test.ts +48 -0
- package/content/problems/233-coin-change-ii/README.md +44 -0
- package/content/problems/233-coin-change-ii/solution.test.ts +45 -0
- package/content/problems/234-edit-distance/README.md +45 -0
- package/content/problems/234-edit-distance/solution.test.ts +48 -0
- package/content/problems/235-maximum-subarray/README.md +40 -0
- package/content/problems/235-maximum-subarray/solution.test.ts +63 -0
- package/content/problems/236-jump-game/README.md +40 -0
- package/content/problems/236-jump-game/solution.test.ts +68 -0
- package/content/problems/237-gas-station/README.md +42 -0
- package/content/problems/237-gas-station/solution.test.ts +56 -0
- package/content/problems/238-partition-labels/README.md +40 -0
- package/content/problems/238-partition-labels/solution.test.ts +54 -0
- package/content/problems/239-non-overlapping-intervals/README.md +40 -0
- package/content/problems/239-non-overlapping-intervals/solution.test.ts +61 -0
- package/content/problems/240-meeting-rooms-ii/README.md +42 -0
- package/content/problems/240-meeting-rooms-ii/solution.test.ts +65 -0
- package/content/problems/241-rotate-image/README.md +41 -0
- package/content/problems/241-rotate-image/solution.test.ts +72 -0
- package/content/problems/242-spiral-matrix/README.md +40 -0
- package/content/problems/242-spiral-matrix/solution.test.ts +90 -0
- package/content/problems/243-set-matrix-zeroes/README.md +44 -0
- package/content/problems/243-set-matrix-zeroes/solution.test.ts +153 -0
- package/content/problems/301-trapping-rain-water/README.md +42 -0
- package/content/problems/301-trapping-rain-water/solution.test.ts +63 -0
- package/content/problems/302-minimum-window-substring/README.md +43 -0
- package/content/problems/302-minimum-window-substring/solution.test.ts +56 -0
- package/content/problems/303-sliding-window-maximum/README.md +42 -0
- package/content/problems/303-sliding-window-maximum/solution.test.ts +63 -0
- package/content/problems/304-largest-rectangle-in-histogram/README.md +42 -0
- package/content/problems/304-largest-rectangle-in-histogram/solution.test.ts +57 -0
- package/content/problems/305-median-of-two-sorted-arrays/README.md +42 -0
- package/content/problems/305-median-of-two-sorted-arrays/solution.test.ts +84 -0
- package/content/problems/306-merge-k-sorted-lists/README.md +42 -0
- package/content/problems/306-merge-k-sorted-lists/solution.test.ts +72 -0
- package/content/problems/307-reverse-nodes-in-k-group/README.md +42 -0
- package/content/problems/307-reverse-nodes-in-k-group/solution.test.ts +86 -0
- package/content/problems/308-binary-tree-maximum-path-sum/README.md +44 -0
- package/content/problems/308-binary-tree-maximum-path-sum/solution.test.ts +91 -0
- package/content/problems/309-serialize-and-deserialize-binary-tree/README.md +50 -0
- package/content/problems/309-serialize-and-deserialize-binary-tree/solution.test.ts +120 -0
- package/content/problems/310-find-median-from-data-stream/README.md +47 -0
- package/content/problems/310-find-median-from-data-stream/solution.test.ts +102 -0
- package/content/problems/311-word-search-ii/README.md +51 -0
- package/content/problems/311-word-search-ii/solution.test.ts +144 -0
- package/content/problems/312-n-queens/README.md +41 -0
- package/content/problems/312-n-queens/solution.test.ts +91 -0
- package/content/problems/313-word-ladder/README.md +42 -0
- package/content/problems/313-word-ladder/solution.test.ts +85 -0
- package/content/problems/314-alien-dictionary/README.md +43 -0
- package/content/problems/314-alien-dictionary/solution.test.ts +127 -0
- package/content/problems/315-longest-increasing-path-in-a-matrix/README.md +42 -0
- package/content/problems/315-longest-increasing-path-in-a-matrix/solution.test.ts +118 -0
- package/content/problems/316-swim-in-rising-water/README.md +44 -0
- package/content/problems/316-swim-in-rising-water/solution.test.ts +138 -0
- package/content/product/P01-tool-lending/README.md +28 -0
- package/content/product/P02-city-bike-share/README.md +28 -0
- package/content/product/P03-caring-from-afar/README.md +29 -0
- package/content/product/P04-first-garden/README.md +28 -0
- package/content/product/P05-save-for-later/README.md +28 -0
- package/content/product/P06-ship-or-hold/README.md +28 -0
- package/content/product/P07-photo-backup-goals/README.md +28 -0
- package/content/product/P08-quarter-roadmap/README.md +28 -0
- package/content/product/P09-grocer-delivery/README.md +29 -0
- package/content/product/P10-notes-for-work/README.md +28 -0
- package/content/product/P11-fitness-coaching/README.md +28 -0
- package/content/product/P12-low-price-rival/README.md +28 -0
- package/content/product/P13-coffee-cups/README.md +28 -0
- package/content/product/P14-bike-repair-market/README.md +27 -0
- package/content/product/P15-scooter-rides/README.md +27 -0
- package/content/product/P16-school-lunch-boxes/README.md +28 -0
- package/content/product/P17-weekly-users-drop/README.md +28 -0
- package/content/product/P18-ride-cancellations/README.md +28 -0
- package/content/product/P19-signups-up-revenue-flat/README.md +28 -0
- package/content/product/P20-longer-sessions/README.md +28 -0
- package/content/product/REVIEW.md +107 -0
- package/dist/cli.mjs +4462 -0
- package/package.json +38 -4
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { spiralOrder } from './solution'
|
|
3
|
+
|
|
4
|
+
/** A rows × cols grid where every cell holds a different value: row * 1000 + column. */
|
|
5
|
+
const grid = (rows: number, cols: number): number[][] =>
|
|
6
|
+
Array.from({ length: rows }, (_, r) => Array.from({ length: cols }, (_, c) => r * 1000 + c))
|
|
7
|
+
|
|
8
|
+
describe('242 spiral matrix', () => {
|
|
9
|
+
it('reads a wide grid', () => {
|
|
10
|
+
expect(
|
|
11
|
+
spiralOrder([
|
|
12
|
+
[2, 4, 6, 8],
|
|
13
|
+
[10, 12, 14, 16],
|
|
14
|
+
[18, 20, 22, 24],
|
|
15
|
+
]),
|
|
16
|
+
).toEqual([2, 4, 6, 8, 16, 24, 22, 20, 18, 10, 12, 14])
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('reads a tall grid', () => {
|
|
20
|
+
expect(
|
|
21
|
+
spiralOrder([
|
|
22
|
+
[7, 8, 9],
|
|
23
|
+
[1, 2, 3],
|
|
24
|
+
[4, 5, 6],
|
|
25
|
+
[0, -1, -2],
|
|
26
|
+
]),
|
|
27
|
+
).toEqual([7, 8, 9, 3, 6, -2, -1, 0, 4, 1, 2, 5])
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('reads a square grid with an odd side, ending in the centre', () => {
|
|
31
|
+
expect(
|
|
32
|
+
spiralOrder([
|
|
33
|
+
[5, 1, 9],
|
|
34
|
+
[3, 0, 2],
|
|
35
|
+
[8, 4, 6],
|
|
36
|
+
]),
|
|
37
|
+
).toEqual([5, 1, 9, 2, 6, 4, 8, 3, 0])
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('reads a 4 × 4 grid with two rings', () => {
|
|
41
|
+
expect(spiralOrder(grid(4, 4))).toEqual([
|
|
42
|
+
0, 1, 2, 3, 1003, 2003, 3003, 3002, 3001, 3000, 2000, 1000, 1001, 1002, 2002, 2001,
|
|
43
|
+
])
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('handles a single cell', () => {
|
|
47
|
+
expect(spiralOrder([[42]])).toEqual([42])
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('handles a single row', () => {
|
|
51
|
+
expect(spiralOrder([[3, -1, 4, -1, 5]])).toEqual([3, -1, 4, -1, 5])
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('handles a single column', () => {
|
|
55
|
+
expect(spiralOrder([[3], [-1], [4], [-1], [5]])).toEqual([3, -1, 4, -1, 5])
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('handles two rows and two columns', () => {
|
|
59
|
+
expect(spiralOrder([[1, 2], [3, 4]])).toEqual([1, 2, 4, 3])
|
|
60
|
+
expect(spiralOrder([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])).toEqual([1, 2, 3, 4, 5, 10, 9, 8, 7, 6])
|
|
61
|
+
expect(spiralOrder([[1, 2], [3, 4], [5, 6], [7, 8]])).toEqual([1, 2, 4, 6, 8, 7, 5, 3])
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('handles a wide grid whose inner ring is a single row', () => {
|
|
65
|
+
expect(spiralOrder(grid(3, 6))).toEqual([
|
|
66
|
+
0, 1, 2, 3, 4, 5, 1005, 2005, 2004, 2003, 2002, 2001, 2000, 1000, 1001, 1002, 1003, 1004,
|
|
67
|
+
])
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('handles a tall grid whose inner ring is a single column', () => {
|
|
71
|
+
expect(spiralOrder(grid(6, 3))).toEqual([
|
|
72
|
+
0, 1, 2, 1002, 2002, 3002, 4002, 5002, 5001, 5000, 4000, 3000, 2000, 1000, 1001, 2001, 3001, 4001,
|
|
73
|
+
])
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('visits every cell of a large grid exactly once', () => {
|
|
77
|
+
for (const [rows, cols] of [[57, 31], [31, 57], [40, 40], [1, 200], [200, 1]] as const) {
|
|
78
|
+
const result = spiralOrder(grid(rows, cols))
|
|
79
|
+
expect(result).toHaveLength(rows * cols)
|
|
80
|
+
expect(new Set(result).size).toBe(rows * cols)
|
|
81
|
+
expect(result.slice(0, cols)).toEqual(grid(rows, cols)[0])
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('does not modify the input', () => {
|
|
86
|
+
const matrix = grid(3, 4)
|
|
87
|
+
spiralOrder(matrix)
|
|
88
|
+
expect(matrix).toEqual(grid(3, 4))
|
|
89
|
+
})
|
|
90
|
+
})
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# 243 · Set Matrix Zeroes
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: matrix (first row and column as markers) · Target: 25 min · NeetCode 150 · LeetCode 73
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
`matrix` is a grid of integers with `m` rows and `n` columns. For every cell that holds `0` at the start, set every cell in its row and every cell in its column to `0`.
|
|
8
|
+
|
|
9
|
+
Only zeros that were there at the start count: a cell you've just set to `0` doesn't clear its own row and column.
|
|
10
|
+
|
|
11
|
+
Do it **in place**: change the matrix you were given. The function returns nothing (`void`); the tests read the matrix they passed in. Aim for O(1) extra memory, not a copy of the grid or a list of every zero row and column.
|
|
12
|
+
|
|
13
|
+
## Examples
|
|
14
|
+
|
|
15
|
+
| `matrix` | After the call |
|
|
16
|
+
|---|---|
|
|
17
|
+
| `[[4, 7, 2], [3, 0, 9], [5, 6, 8]]` | `[[4, 0, 2], [0, 0, 0], [5, 0, 8]]` |
|
|
18
|
+
| `[[0, 2, 3], [4, 5, 6], [7, 8, 9]]` | `[[0, 0, 0], [0, 5, 6], [0, 8, 9]]` |
|
|
19
|
+
| `[[1, -2], [-3, 4]]` | `[[1, -2], [-3, 4]]` (no zeros, nothing changes) |
|
|
20
|
+
|
|
21
|
+
## Constraints
|
|
22
|
+
|
|
23
|
+
- `1 <= m, n <= 200` (the performance test uses a 1,200 × 1,200 grid with 500,000 zeros)
|
|
24
|
+
- Values are any integers.
|
|
25
|
+
|
|
26
|
+
## Run it
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pnpm watch 243
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
33
|
+
|
|
34
|
+
Time O(m·n) · Space **O(1)**. Remembering which rows and columns to clear in two sets or arrays is O(m + n) space and passes the tests; the O(1) version reuses part of the matrix itself to hold that information. The tests can't measure memory, so the last step is on your honour.
|
|
35
|
+
|
|
36
|
+
Copying the grid and clearing a whole row and column for every zero is O(m·n·(m + n)) when there are many zeros, and the performance test rejects it.
|
|
37
|
+
|
|
38
|
+
</details>
|
|
39
|
+
|
|
40
|
+
## Follow-ups an interviewer might ask
|
|
41
|
+
|
|
42
|
+
- Can you do it with O(1) extra space? What goes wrong if a marker lands in the first row or column?
|
|
43
|
+
- In which order must you clear the marker row and column at the end, and why?
|
|
44
|
+
- The grid is too big to fit in memory and arrives one row at a time. What would you do?
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { setZeroes } from './solution'
|
|
4
|
+
|
|
5
|
+
/** Runs the solution on `matrix`, checks nothing was returned, and gives back the same (changed) matrix. */
|
|
6
|
+
function zeroed(matrix: number[][]): number[][] {
|
|
7
|
+
const result = setZeroes(matrix)
|
|
8
|
+
expect(result).toBeUndefined()
|
|
9
|
+
return matrix
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
describe('243 set matrix zeroes', () => {
|
|
13
|
+
it('clears the row and column of one zero', () => {
|
|
14
|
+
expect(
|
|
15
|
+
zeroed([
|
|
16
|
+
[4, 7, 2],
|
|
17
|
+
[3, 0, 9],
|
|
18
|
+
[5, 6, 8],
|
|
19
|
+
]),
|
|
20
|
+
).toEqual([
|
|
21
|
+
[4, 0, 2],
|
|
22
|
+
[0, 0, 0],
|
|
23
|
+
[5, 0, 8],
|
|
24
|
+
])
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('clears for two zeros in different rows and columns', () => {
|
|
28
|
+
expect(
|
|
29
|
+
zeroed([
|
|
30
|
+
[1, 2, 3, 4],
|
|
31
|
+
[5, 6, 0, 7],
|
|
32
|
+
[0, 8, 9, 1],
|
|
33
|
+
]),
|
|
34
|
+
).toEqual([
|
|
35
|
+
[0, 2, 0, 4],
|
|
36
|
+
[0, 0, 0, 0],
|
|
37
|
+
[0, 0, 0, 0],
|
|
38
|
+
])
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('handles a zero in the top-left corner', () => {
|
|
42
|
+
expect(
|
|
43
|
+
zeroed([
|
|
44
|
+
[0, 2, 3],
|
|
45
|
+
[4, 5, 6],
|
|
46
|
+
[7, 8, 9],
|
|
47
|
+
]),
|
|
48
|
+
).toEqual([
|
|
49
|
+
[0, 0, 0],
|
|
50
|
+
[0, 5, 6],
|
|
51
|
+
[0, 8, 9],
|
|
52
|
+
])
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('handles a zero in the first row only', () => {
|
|
56
|
+
expect(
|
|
57
|
+
zeroed([
|
|
58
|
+
[1, 0, 3],
|
|
59
|
+
[4, 5, 6],
|
|
60
|
+
]),
|
|
61
|
+
).toEqual([
|
|
62
|
+
[0, 0, 0],
|
|
63
|
+
[4, 0, 6],
|
|
64
|
+
])
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('handles a zero in the first column only', () => {
|
|
68
|
+
expect(
|
|
69
|
+
zeroed([
|
|
70
|
+
[1, 2, 3],
|
|
71
|
+
[0, 5, 6],
|
|
72
|
+
[7, 8, 9],
|
|
73
|
+
]),
|
|
74
|
+
).toEqual([
|
|
75
|
+
[0, 2, 3],
|
|
76
|
+
[0, 0, 0],
|
|
77
|
+
[0, 8, 9],
|
|
78
|
+
])
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('handles zeros in the first row and the first column at different places', () => {
|
|
82
|
+
expect(
|
|
83
|
+
zeroed([
|
|
84
|
+
[1, 2, 0, 4],
|
|
85
|
+
[5, 6, 7, 8],
|
|
86
|
+
[0, 9, 1, 2],
|
|
87
|
+
[3, 4, 5, 6],
|
|
88
|
+
]),
|
|
89
|
+
).toEqual([
|
|
90
|
+
[0, 0, 0, 0],
|
|
91
|
+
[0, 6, 0, 8],
|
|
92
|
+
[0, 0, 0, 0],
|
|
93
|
+
[0, 4, 0, 6],
|
|
94
|
+
])
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('leaves a matrix with no zeros alone', () => {
|
|
98
|
+
expect(zeroed([[1, -2], [-3, 4]])).toEqual([[1, -2], [-3, 4]])
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('handles a matrix of all zeros', () => {
|
|
102
|
+
expect(zeroed([[0, 0], [0, 0]])).toEqual([[0, 0], [0, 0]])
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('handles one cell, one row and one column', () => {
|
|
106
|
+
expect(zeroed([[0]])).toEqual([[0]])
|
|
107
|
+
expect(zeroed([[5]])).toEqual([[5]])
|
|
108
|
+
expect(zeroed([[3, 0, 4, 5]])).toEqual([[0, 0, 0, 0]])
|
|
109
|
+
expect(zeroed([[3], [4], [0]])).toEqual([[0], [0], [0]])
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('does not spread from zeros it has just written', () => {
|
|
113
|
+
expect(
|
|
114
|
+
zeroed([
|
|
115
|
+
[1, 1, 1, 1],
|
|
116
|
+
[1, 0, 1, 1],
|
|
117
|
+
[1, 1, 1, 1],
|
|
118
|
+
[1, 1, 1, 1],
|
|
119
|
+
]),
|
|
120
|
+
).toEqual([
|
|
121
|
+
[1, 0, 1, 1],
|
|
122
|
+
[0, 0, 0, 0],
|
|
123
|
+
[1, 0, 1, 1],
|
|
124
|
+
[1, 0, 1, 1],
|
|
125
|
+
])
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('changes the matrix it was given rather than returning a new one', () => {
|
|
129
|
+
const matrix = [[1, 0], [2, 3]]
|
|
130
|
+
setZeroes(matrix)
|
|
131
|
+
expect(matrix).toEqual([[0, 0], [2, 0]])
|
|
132
|
+
})
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
describe('243 performance', () => {
|
|
136
|
+
it('handles a 1,200 × 1,200 matrix with 500,000 zeros in under 200 ms (clearing a row and column for every zero, O(m·n·(m + n)), is too slow here)', () => {
|
|
137
|
+
const size = 1_200
|
|
138
|
+
const block = 1_000
|
|
139
|
+
const valueAt = (r: number, c: number): number => 1 + ((r * 7 + c * 13) % 97)
|
|
140
|
+
// Inside the top-left 1,000 × 1,000 block, every other cell is 0. Rows and columns 1,000+ have no zeros of their own.
|
|
141
|
+
const hasZero = (r: number, c: number): boolean => r < block && c < block && (r + c) % 2 === 0
|
|
142
|
+
const matrix = Array.from({ length: size }, (_, r) =>
|
|
143
|
+
Array.from({ length: size }, (_, c) => (hasZero(r, c) ? 0 : valueAt(r, c))),
|
|
144
|
+
)
|
|
145
|
+
// Only the bottom-right 200 × 200 corner keeps its values.
|
|
146
|
+
const expected = Array.from({ length: size }, (_, r) =>
|
|
147
|
+
Array.from({ length: size }, (_, c) => (r >= block && c >= block ? valueAt(r, c) : 0)),
|
|
148
|
+
)
|
|
149
|
+
const ms = timeMs(() => setZeroes(matrix))
|
|
150
|
+
expect(matrix).toEqual(expected)
|
|
151
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
152
|
+
})
|
|
153
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# 301 · Trapping Rain Water
|
|
2
|
+
|
|
3
|
+
**Level 3 · Hard** · Pattern: two pointers · Target: 40 min · NeetCode 150 · LeetCode 42
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
`height` describes a row of bars standing side by side, each one unit wide. `height[i]` is the height of the `i`-th bar (a height of `0` means there's no bar there, just floor).
|
|
8
|
+
|
|
9
|
+
Rain falls on the whole row and settles wherever it is held in on both sides by taller bars. Water that isn't walled in on one side runs off the end of the row. Return the total number of unit squares of water that stay trapped.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `height` | Output |
|
|
14
|
+
|---|---|
|
|
15
|
+
| `[3, 0, 2, 0, 4]` | `7` |
|
|
16
|
+
| `[1, 4, 2, 5, 0, 3, 1]` | `5` |
|
|
17
|
+
| `[5, 4, 3, 2]` | `0` |
|
|
18
|
+
|
|
19
|
+
In the first example, the water above each position is `0, 3, 1, 3, 0`.
|
|
20
|
+
|
|
21
|
+
## Constraints
|
|
22
|
+
|
|
23
|
+
- `0 <= height.length <= 2 · 10^4` (the performance test uses 60,000 bars)
|
|
24
|
+
- `0 <= height[i] <= 10^5`
|
|
25
|
+
|
|
26
|
+
## Run it
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pnpm watch 301
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
33
|
+
|
|
34
|
+
Time O(n) · Space **O(1)**. Precomputing the tallest bar to the left and to the right of every position is O(n) time and O(n) space, and passes. Scanning both directions again for every position is O(n²), which the performance test rejects.
|
|
35
|
+
|
|
36
|
+
</details>
|
|
37
|
+
|
|
38
|
+
## Follow-ups an interviewer might ask
|
|
39
|
+
|
|
40
|
+
- Can you do it in one pass with O(1) extra space? Why is it safe to settle the side with the shorter wall?
|
|
41
|
+
- Solve it a third way, by filling the water layer by layer as you meet each new right wall.
|
|
42
|
+
- What changes in 2-D, where the bars form a height map on a grid? (LeetCode 407)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { trap } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('301 trapping rain water', () => {
|
|
6
|
+
it('fills a basin between two tall walls', () => {
|
|
7
|
+
expect(trap([3, 0, 2, 0, 4])).toBe(7)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('fills several separate pools', () => {
|
|
11
|
+
expect(trap([1, 4, 2, 5, 0, 3, 1])).toBe(5)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('traps nothing on a falling or rising slope', () => {
|
|
15
|
+
expect(trap([5, 4, 3, 2])).toBe(0)
|
|
16
|
+
expect(trap([1, 2, 3, 4, 5])).toBe(0)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('handles empty and very short rows', () => {
|
|
20
|
+
expect(trap([])).toBe(0)
|
|
21
|
+
expect(trap([7])).toBe(0)
|
|
22
|
+
expect(trap([3, 9])).toBe(0)
|
|
23
|
+
expect(trap([2, 0, 2])).toBe(2)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('traps nothing on flat ground or a single peak', () => {
|
|
27
|
+
expect(trap([0, 0, 0])).toBe(0)
|
|
28
|
+
expect(trap([4, 4, 4, 4])).toBe(0)
|
|
29
|
+
expect(trap([1, 3, 6, 3, 1])).toBe(0)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('is limited by the shorter of the two walls', () => {
|
|
33
|
+
expect(trap([6, 1, 1, 1, 2])).toBe(3)
|
|
34
|
+
expect(trap([2, 0, 0, 0, 9])).toBe(6)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('handles plateaus and an inner bump', () => {
|
|
38
|
+
expect(trap([4, 4, 1, 3, 1, 4, 4])).toBe(7)
|
|
39
|
+
expect(trap([5, 2, 2, 0, 2, 2, 5])).toBe(17)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('lets water spill off an open end', () => {
|
|
43
|
+
expect(trap([0, 3, 0, 1, 0])).toBe(1)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('handles large heights', () => {
|
|
47
|
+
expect(trap([100_000, 0, 0, 100_000])).toBe(200_000)
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
describe('301 performance', () => {
|
|
52
|
+
it('handles 60,000 bars in under 200 ms (O(n²) is too slow here)', () => {
|
|
53
|
+
const n = 60_000
|
|
54
|
+
// A deep valley: walls of height n at both ends, a V-shaped floor between them.
|
|
55
|
+
const height = Array.from({ length: n }, (_, i) =>
|
|
56
|
+
i === 0 || i === n - 1 ? n : Math.abs(i - n / 2),
|
|
57
|
+
)
|
|
58
|
+
let expected = 0
|
|
59
|
+
for (let i = 1; i < n - 1; i++) expected += n - height[i]!
|
|
60
|
+
const ms = timeMs(() => expect(trap(height)).toBe(expected))
|
|
61
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
62
|
+
})
|
|
63
|
+
})
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# 302 · Minimum Window Substring
|
|
2
|
+
|
|
3
|
+
**Level 3 · Hard** · Pattern: sliding window · Target: 40 min · NeetCode 150 · LeetCode 76
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
You get two strings, `s` and `t`. Find the shortest contiguous piece of `s` that contains every character of `t`, counting repeats: if `t` has two `'a'`s, the piece needs at least two `'a'`s. The piece may contain other characters too, and the order doesn't matter.
|
|
8
|
+
|
|
9
|
+
Return that piece. If several pieces tie for the shortest, return the one that starts first. If no piece of `s` works, return the empty string `''`.
|
|
10
|
+
|
|
11
|
+
Letters are case-sensitive: `'a'` and `'A'` are different characters.
|
|
12
|
+
|
|
13
|
+
## Examples
|
|
14
|
+
|
|
15
|
+
| `s` | `t` | Output |
|
|
16
|
+
|---|---|---|
|
|
17
|
+
| `'PQRAXBYCAB'` | `'ABC'` | `'CAB'` |
|
|
18
|
+
| `'mississippi'` | `'pss'` | `'ssip'` |
|
|
19
|
+
| `'baxab'` | `'ab'` | `'ba'` (ties with `'ab'`, but starts first) |
|
|
20
|
+
| `'abc'` | `'abd'` | `''` |
|
|
21
|
+
|
|
22
|
+
## Constraints
|
|
23
|
+
|
|
24
|
+
- `1 <= s.length, t.length <= 10^5` (the performance test uses an `s` of 30,000 characters)
|
|
25
|
+
- `s` and `t` contain only English letters, upper and lower case.
|
|
26
|
+
|
|
27
|
+
## Run it
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pnpm watch 302
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
34
|
+
|
|
35
|
+
Time O(|s| + |t|) · Space O(1), since there are only 52 possible letters (O(|t|) if you count with a map). Trying every start position and extending it until it covers `t` is O(|s|²), which the performance test rejects.
|
|
36
|
+
|
|
37
|
+
</details>
|
|
38
|
+
|
|
39
|
+
## Follow-ups an interviewer might ask
|
|
40
|
+
|
|
41
|
+
- How do you know the window covers `t` without re-comparing all the counts at every step?
|
|
42
|
+
- What if `t`'s characters had to appear in order, as a subsequence of the window? (LeetCode 727)
|
|
43
|
+
- If `s` is huge but only a few of its characters appear in `t`, how could you skip the rest?
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { minWindow } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('302 minimum window substring', () => {
|
|
6
|
+
it('finds the shortest piece covering every character', () => {
|
|
7
|
+
expect(minWindow('PQRAXBYCAB', 'ABC')).toBe('CAB')
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('counts repeated characters in t', () => {
|
|
11
|
+
expect(minWindow('mississippi', 'pss')).toBe('ssip')
|
|
12
|
+
expect(minWindow('aab', 'aab')).toBe('aab')
|
|
13
|
+
expect(minWindow('abacab', 'aab')).toBe('aba')
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('returns the piece that starts first when several tie', () => {
|
|
17
|
+
expect(minWindow('baxab', 'ab')).toBe('ba')
|
|
18
|
+
expect(minWindow('xyzzyx', 'xy')).toBe('xy')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('returns an empty string when no piece works', () => {
|
|
22
|
+
expect(minWindow('abc', 'abd')).toBe('')
|
|
23
|
+
expect(minWindow('a', 'aa')).toBe('')
|
|
24
|
+
expect(minWindow('ab', 'abc')).toBe('')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('handles single characters', () => {
|
|
28
|
+
expect(minWindow('z', 'z')).toBe('z')
|
|
29
|
+
expect(minWindow('qrstq', 't')).toBe('t')
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('returns all of s when only all of it works', () => {
|
|
33
|
+
expect(minWindow('hello', 'oleh')).toBe('hello')
|
|
34
|
+
expect(minWindow('kayak', 'kayak')).toBe('kayak')
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('is case-sensitive', () => {
|
|
38
|
+
expect(minWindow('aAbBa', 'Aa')).toBe('aA')
|
|
39
|
+
expect(minWindow('abc', 'A')).toBe('')
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('ignores extra copies beyond what t needs', () => {
|
|
43
|
+
expect(minWindow('ccccacccb', 'ab')).toBe('acccb')
|
|
44
|
+
expect(minWindow('zzzazzbzzzab', 'ab')).toBe('ab')
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
describe('302 performance', () => {
|
|
49
|
+
it('handles an s of 30,000 characters in under 200 ms (extending from every start, O(|s|²), is too slow here)', () => {
|
|
50
|
+
const n = 30_000
|
|
51
|
+
// Only the last two characters complete t, so a window from any earlier start runs to the end.
|
|
52
|
+
const s = `C${'a'.repeat(n - 3)}BC`
|
|
53
|
+
const ms = timeMs(() => expect(minWindow(s, 'BC')).toBe('BC'))
|
|
54
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
55
|
+
})
|
|
56
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# 303 · Sliding Window Maximum
|
|
2
|
+
|
|
3
|
+
**Level 3 · Hard** · Pattern: sliding window (monotonic deque) · Target: 40 min · NeetCode 150 · LeetCode 239
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
You get an array of integers `nums` and a window size `k`. Picture a window covering `k` neighbouring elements, starting at the left end of `nums` and moving right one position at a time until it reaches the right end.
|
|
8
|
+
|
|
9
|
+
Return an array holding the largest value inside the window at each of its positions, in order. There are `nums.length - k + 1` positions, so that is the length of the answer.
|
|
10
|
+
|
|
11
|
+
Don't modify `nums`.
|
|
12
|
+
|
|
13
|
+
## Examples
|
|
14
|
+
|
|
15
|
+
| `nums` | `k` | Output |
|
|
16
|
+
|---|---|---|
|
|
17
|
+
| `[4, -2, 7, 1, 1, 5, 3]` | `3` | `[7, 7, 7, 5, 5]` |
|
|
18
|
+
| `[9, 8, 7, 6]` | `2` | `[9, 8, 7]` |
|
|
19
|
+
| `[5]` | `1` | `[5]` |
|
|
20
|
+
|
|
21
|
+
## Constraints
|
|
22
|
+
|
|
23
|
+
- `1 <= k <= nums.length <= 2 · 10^5` (the performance test uses 200,000 values with `k = 10,000`)
|
|
24
|
+
- `-10^4 <= nums[i] <= 10^4` in the examples; the performance test uses a wider range.
|
|
25
|
+
|
|
26
|
+
## Run it
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pnpm watch 303
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
33
|
+
|
|
34
|
+
Time O(n) · Space O(k). A heap of (value, index) pairs that drops stale entries lazily is O(n log n) and also passes. Finding the maximum of each window from scratch is O(n·k), which the performance test rejects.
|
|
35
|
+
|
|
36
|
+
</details>
|
|
37
|
+
|
|
38
|
+
## Follow-ups an interviewer might ask
|
|
39
|
+
|
|
40
|
+
- Why is every index added and removed at most once, and why does that make the whole thing O(n)?
|
|
41
|
+
- Can you do it with two precomputed arrays (running maxima within fixed blocks of size `k`) instead?
|
|
42
|
+
- Return the sliding window **median** instead. Why is that harder? (LeetCode 480)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { maxSlidingWindow } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('303 sliding window maximum', () => {
|
|
6
|
+
it('reports the maximum at every window position', () => {
|
|
7
|
+
expect(maxSlidingWindow([4, -2, 7, 1, 1, 5, 3], 3)).toEqual([7, 7, 7, 5, 5])
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('handles a falling sequence', () => {
|
|
11
|
+
expect(maxSlidingWindow([9, 8, 7, 6], 2)).toEqual([9, 8, 7])
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('handles a single element', () => {
|
|
15
|
+
expect(maxSlidingWindow([5], 1)).toEqual([5])
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('returns the array itself when k is 1', () => {
|
|
19
|
+
expect(maxSlidingWindow([3, -1, 4, -1, 5], 1)).toEqual([3, -1, 4, -1, 5])
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('returns one value when the window covers everything', () => {
|
|
23
|
+
expect(maxSlidingWindow([2, 8, -3, 6], 4)).toEqual([8])
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('handles a rising sequence', () => {
|
|
27
|
+
expect(maxSlidingWindow([1, 2, 3, 4, 5], 3)).toEqual([3, 4, 5])
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('drops the maximum once it leaves the window', () => {
|
|
31
|
+
expect(maxSlidingWindow([10, 1, 2, 3, 4, 0], 3)).toEqual([10, 3, 4, 4])
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('handles repeated maxima and all-equal values', () => {
|
|
35
|
+
expect(maxSlidingWindow([6, 2, 6, 2, 1, 1], 2)).toEqual([6, 6, 6, 2, 1])
|
|
36
|
+
expect(maxSlidingWindow([7, 7, 7, 7], 3)).toEqual([7, 7])
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('handles negative values', () => {
|
|
40
|
+
expect(maxSlidingWindow([-5, -9, -2, -8, -7, -6], 2)).toEqual([-5, -2, -2, -7, -6])
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('does not modify the input', () => {
|
|
44
|
+
const nums = [3, 1, 4, 1, 5, 9]
|
|
45
|
+
maxSlidingWindow(nums, 3)
|
|
46
|
+
expect(nums).toEqual([3, 1, 4, 1, 5, 9])
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
describe('303 performance', () => {
|
|
51
|
+
it('handles 200,000 values with k = 10,000 in under 150 ms (re-scanning every window, O(n·k), is too slow here)', () => {
|
|
52
|
+
const n = 200_000
|
|
53
|
+
const k = 10_000
|
|
54
|
+
// Strictly falling values, so the maximum leaves every window as it slides.
|
|
55
|
+
const nums = Array.from({ length: n }, (_, i) => n - i)
|
|
56
|
+
let result: number[] = []
|
|
57
|
+
const ms = timeMs(() => {
|
|
58
|
+
result = maxSlidingWindow(nums, k)
|
|
59
|
+
})
|
|
60
|
+
expect(result).toEqual(nums.slice(0, n - k + 1))
|
|
61
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(150)
|
|
62
|
+
})
|
|
63
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# 304 · Largest Rectangle in Histogram
|
|
2
|
+
|
|
3
|
+
**Level 3 · Hard** · Pattern: monotonic stack · Target: 40 min · NeetCode 150 · LeetCode 84
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
`heights` describes a bar chart: bar `i` is one unit wide and `heights[i]` units tall, and the bars stand next to each other with no gaps.
|
|
8
|
+
|
|
9
|
+
Find the largest area of an axis-aligned rectangle that fits entirely inside the bars. The rectangle can span several neighbouring bars, but its height can't exceed the shortest bar it spans. Return that area.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `heights` | Output |
|
|
14
|
+
|---|---|
|
|
15
|
+
| `[3, 1, 3, 2, 2]` | `6` |
|
|
16
|
+
| `[2, 4, 4, 1]` | `8` |
|
|
17
|
+
| `[5]` | `5` |
|
|
18
|
+
|
|
19
|
+
In the first example, the best rectangle is 2 tall and spans the last three bars.
|
|
20
|
+
|
|
21
|
+
## Constraints
|
|
22
|
+
|
|
23
|
+
- `1 <= heights.length <= 10^5` (the performance test uses 70,000 bars)
|
|
24
|
+
- `0 <= heights[i] <= 10^4` in the examples; the performance test uses heights up to 70,000.
|
|
25
|
+
|
|
26
|
+
## Run it
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pnpm watch 304
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
33
|
+
|
|
34
|
+
Time O(n) · Space O(n). Trying every bar as the rectangle's height and spreading out from it to both sides is O(n²) on rising or flat input, and so is trying every pair of ends. The performance test rejects both.
|
|
35
|
+
|
|
36
|
+
</details>
|
|
37
|
+
|
|
38
|
+
## Follow-ups an interviewer might ask
|
|
39
|
+
|
|
40
|
+
- Why does every bar get pushed and popped at most once? Where does the width of each popped bar's rectangle come from?
|
|
41
|
+
- How do you deal with the bars left over when you reach the end, and with runs of equal heights?
|
|
42
|
+
- Use this to find the largest rectangle of 1s in a 0/1 grid. (LeetCode 85)
|