interview-reps 0.0.0-stage → 0.3.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 +4418 -0
- package/package.json +38 -4
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { maxProduct } from './solution'
|
|
4
|
+
|
|
5
|
+
/** Multiplying by a negative can give -0, which is a valid answer of 0. Adding 0 turns -0 into 0. */
|
|
6
|
+
function best(nums: readonly number[]): number {
|
|
7
|
+
return maxProduct(nums) + 0
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
describe('229 maximum product subarray', () => {
|
|
11
|
+
it('runs the README examples', () => {
|
|
12
|
+
expect(best([3, -1, 4])).toBe(4)
|
|
13
|
+
expect(best([-2, -3, 5])).toBe(30)
|
|
14
|
+
expect(best([-4, 0, -2])).toBe(0)
|
|
15
|
+
expect(best([2, -5, -2, -4, 3])).toBe(24)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('handles a single element', () => {
|
|
19
|
+
expect(best([-7])).toBe(-7)
|
|
20
|
+
expect(best([0])).toBe(0)
|
|
21
|
+
expect(best([6])).toBe(6)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('handles all negatives', () => {
|
|
25
|
+
expect(best([-1, -2, -3])).toBe(6)
|
|
26
|
+
expect(best([-1, -2, -3, -4])).toBe(24)
|
|
27
|
+
expect(best([-3, -3])).toBe(9)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('restarts after a zero', () => {
|
|
31
|
+
expect(best([-2, 0, -3, 4, 0, -1])).toBe(4)
|
|
32
|
+
expect(best([0, -5])).toBe(0)
|
|
33
|
+
expect(best([-3, 0, -3])).toBe(0)
|
|
34
|
+
expect(best([2, 3, 0, 4, 5])).toBe(20)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('keeps a large negative product in case another negative comes along', () => {
|
|
38
|
+
expect(best([2, -3, 4, -1])).toBe(24)
|
|
39
|
+
expect(best([-2, 5, 1, 3, -4, 0, 9])).toBe(120)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('handles ones and the largest values', () => {
|
|
43
|
+
expect(best([1, 1, 1])).toBe(1)
|
|
44
|
+
expect(best([10, -10, 10, -10, 10])).toBe(100_000)
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
describe('229 performance', () => {
|
|
49
|
+
it('handles 60,000 numbers in under 200 ms (O(n²) is too slow here)', () => {
|
|
50
|
+
// Mostly 1s with a -1 every 7th place and one 7: no run can beat the 7 alone.
|
|
51
|
+
const nums = Array.from({ length: 60_000 }, (_, i) => (i === 30_000 ? 7 : i % 7 === 3 ? -1 : 1))
|
|
52
|
+
const ms = timeMs(() => expect(best(nums)).toBe(7))
|
|
53
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
54
|
+
})
|
|
55
|
+
})
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# 230 · Partition Equal Subset Sum
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: 0/1 knapsack (dynamic programming over sums) · Target: 30 min · NeetCode 150 · LeetCode 416
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
Decide whether the numbers in `nums` can be split into two groups with **equal sums**. Every element must go into exactly one of the two groups. Return `true` if it can be done and `false` otherwise.
|
|
8
|
+
|
|
9
|
+
## Examples
|
|
10
|
+
|
|
11
|
+
| `nums` | Output | Why |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| `[3, 1, 4, 2]` | `true` | `3 + 2` and `1 + 4` |
|
|
14
|
+
| `[6, 1, 1, 2, 2]` | `true` | `6` on its own, and `1 + 1 + 2 + 2` |
|
|
15
|
+
| `[2, 4, 7]` | `false` | the total, 13, is odd |
|
|
16
|
+
| `[1, 3, 8]` | `false` | each half would need to be 6, and no group adds up to 6 |
|
|
17
|
+
|
|
18
|
+
## Constraints
|
|
19
|
+
|
|
20
|
+
- `1 <= nums.length <= 200` (the performance test uses 28 numbers)
|
|
21
|
+
- `1 <= nums[i] <= 100`
|
|
22
|
+
|
|
23
|
+
## Run it
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pnpm watch 230
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
30
|
+
|
|
31
|
+
Time O(n · S) · Space O(S), where `S` is half the total. Trying every subset is O(2^n) and fails the performance test; memoised recursion on (index, sum so far) passes.
|
|
32
|
+
|
|
33
|
+
</details>
|
|
34
|
+
|
|
35
|
+
## Follow-ups an interviewer might ask
|
|
36
|
+
|
|
37
|
+
- Why must you loop over the sums from high to low if you keep a single row? What breaks the other way round?
|
|
38
|
+
- Return the two groups, not just `true` or `false`.
|
|
39
|
+
- Split into `k` groups with equal sums instead. (LeetCode 698)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { canPartition } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('230 partition equal subset sum', () => {
|
|
6
|
+
it('runs the README examples', () => {
|
|
7
|
+
expect(canPartition([3, 1, 4, 2])).toBe(true)
|
|
8
|
+
expect(canPartition([6, 1, 1, 2, 2])).toBe(true)
|
|
9
|
+
expect(canPartition([2, 4, 7])).toBe(false)
|
|
10
|
+
expect(canPartition([1, 3, 8])).toBe(false)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('returns false for a single number', () => {
|
|
14
|
+
expect(canPartition([5])).toBe(false)
|
|
15
|
+
expect(canPartition([100])).toBe(false)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('handles two numbers', () => {
|
|
19
|
+
expect(canPartition([4, 4])).toBe(true)
|
|
20
|
+
expect(canPartition([4, 5])).toBe(false)
|
|
21
|
+
expect(canPartition([2, 6])).toBe(false)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('uses each number at most once', () => {
|
|
25
|
+
// Half is 6. Using the 1 six times, or the 3 twice, would reach it; each appears only once.
|
|
26
|
+
expect(canPartition([1, 3, 8])).toBe(false)
|
|
27
|
+
expect(canPartition([3, 9, 6])).toBe(true)
|
|
28
|
+
expect(canPartition([2, 10, 4])).toBe(false)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('returns false when the total is even but half is out of reach', () => {
|
|
32
|
+
expect(canPartition([2, 2, 2, 2, 10])).toBe(false)
|
|
33
|
+
expect(canPartition([1, 1, 50])).toBe(false)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('beats "put the biggest in the lighter group"', () => {
|
|
37
|
+
expect(canPartition([3, 3, 3, 4, 5])).toBe(true)
|
|
38
|
+
expect(canPartition([1, 2, 3, 4, 5, 6, 7])).toBe(true)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('handles repeated values', () => {
|
|
42
|
+
expect(canPartition([7, 7, 7, 7])).toBe(true)
|
|
43
|
+
expect(canPartition([7, 7, 7])).toBe(false)
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
describe('230 performance', () => {
|
|
48
|
+
it('handles 28 numbers in under 100 ms (trying every subset takes seconds)', () => {
|
|
49
|
+
// Every number is even but half the total is 81, which is odd, so no group can reach it.
|
|
50
|
+
const nums = Array.from({ length: 28 }, (_, i) => 2 * ((i % 5) + 1))
|
|
51
|
+
const ms = timeMs(() => expect(canPartition(nums)).toBe(false))
|
|
52
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(100)
|
|
53
|
+
})
|
|
54
|
+
})
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# 231 · Unique Paths
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: 2-D dynamic programming (grid paths) · Target: 25 min · NeetCode 150 · LeetCode 62
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
A robot stands on the top-left cell of a grid with `m` rows and `n` columns. On each move it goes exactly one cell **right** or one cell **down**, and it never leaves the grid.
|
|
8
|
+
|
|
9
|
+
Return how many different routes take it to the bottom-right cell.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `m` | `n` | Output | Why |
|
|
14
|
+
|---|---|---|---|
|
|
15
|
+
| `2` | `3` | `3` | one down move somewhere among two right moves |
|
|
16
|
+
| `4` | `4` | `20` | |
|
|
17
|
+
| `5` | `3` | `15` | |
|
|
18
|
+
| `1` | `6` | `1` | a single row: right, right, right… |
|
|
19
|
+
|
|
20
|
+
## Constraints
|
|
21
|
+
|
|
22
|
+
- `1 <= m, n <= 100` (the performance test uses a 16 × 17 grid)
|
|
23
|
+
- The answer is at most `2 · 10^9`.
|
|
24
|
+
|
|
25
|
+
## Run it
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm watch 231
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
32
|
+
|
|
33
|
+
Time O(m · n) · Space O(n) with one row of counts (O(m · n) with a full table also passes). Plain recursion that walks every route is exponential and fails the performance test. There is also an O(min(m, n)) way to count with a formula.
|
|
34
|
+
|
|
35
|
+
</details>
|
|
36
|
+
|
|
37
|
+
## Follow-ups an interviewer might ask
|
|
38
|
+
|
|
39
|
+
- Some cells are blocked. How does your table change? (LeetCode 63)
|
|
40
|
+
- Each cell has a cost: find the cheapest route instead of counting them. (LeetCode 64)
|
|
41
|
+
- Count the routes with a formula. Why is it a binomial coefficient, and how do you avoid overflow while computing it?
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { uniquePaths } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('231 unique paths', () => {
|
|
6
|
+
it('runs the README examples', () => {
|
|
7
|
+
expect(uniquePaths(2, 3)).toBe(3)
|
|
8
|
+
expect(uniquePaths(4, 4)).toBe(20)
|
|
9
|
+
expect(uniquePaths(5, 3)).toBe(15)
|
|
10
|
+
expect(uniquePaths(1, 6)).toBe(1)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('has one route on a single cell, row or column', () => {
|
|
14
|
+
expect(uniquePaths(1, 1)).toBe(1)
|
|
15
|
+
expect(uniquePaths(1, 100)).toBe(1)
|
|
16
|
+
expect(uniquePaths(100, 1)).toBe(1)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('handles two-wide grids', () => {
|
|
20
|
+
expect(uniquePaths(2, 2)).toBe(2)
|
|
21
|
+
expect(uniquePaths(2, 9)).toBe(9)
|
|
22
|
+
expect(uniquePaths(9, 2)).toBe(9)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('gives the same count when rows and columns swap', () => {
|
|
26
|
+
expect(uniquePaths(3, 6)).toBe(21)
|
|
27
|
+
expect(uniquePaths(6, 3)).toBe(21)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('handles long thin grids', () => {
|
|
31
|
+
expect(uniquePaths(3, 100)).toBe(5050)
|
|
32
|
+
expect(uniquePaths(100, 3)).toBe(5050)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('handles larger square grids', () => {
|
|
36
|
+
expect(uniquePaths(10, 10)).toBe(48_620)
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
describe('231 performance', () => {
|
|
41
|
+
it('counts a 16 × 17 grid in under 50 ms (walking all 300 million routes takes seconds)', () => {
|
|
42
|
+
const ms = timeMs(() => expect(uniquePaths(16, 17)).toBe(300_540_195))
|
|
43
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(50)
|
|
44
|
+
})
|
|
45
|
+
})
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# 232 · Longest Common Subsequence
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: 2-D dynamic programming (two strings) · Target: 25 min · NeetCode 150 · LeetCode 1143
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
A **subsequence** of a string is what's left after deleting any number of its characters (possibly none) without changing the order of the rest. For example, `"tie"` is a subsequence of `"tribe"`, but `"eit"` is not.
|
|
8
|
+
|
|
9
|
+
Return the length of the longest string that is a subsequence of both `text1` and `text2`, or `0` if they share no characters.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `text1` | `text2` | Output | Why |
|
|
14
|
+
|---|---|---|---|
|
|
15
|
+
| `"stone"` | `"longest"` | `3` | `"one"` |
|
|
16
|
+
| `"kitchen"` | `"thick"` | `2` | for example `"tc"` or `"ic"` |
|
|
17
|
+
| `"mississippi"` | `"sips"` | `3` | for example `"sip"` |
|
|
18
|
+
| `"cat"` | `"dog"` | `0` | no letters in common |
|
|
19
|
+
|
|
20
|
+
## Constraints
|
|
21
|
+
|
|
22
|
+
- `1 <= text1.length, text2.length <= 1000` (the performance test uses two 15-letter strings)
|
|
23
|
+
- Both strings contain only lowercase English letters.
|
|
24
|
+
|
|
25
|
+
## Run it
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm watch 232
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
32
|
+
|
|
33
|
+
Time O(m · n) · Space O(min(m, n)) with two rows (O(m · n) with a full table also passes). Plain recursion that tries skipping a character from either string is exponential and fails the performance test.
|
|
34
|
+
|
|
35
|
+
</details>
|
|
36
|
+
|
|
37
|
+
## Follow-ups an interviewer might ask
|
|
38
|
+
|
|
39
|
+
- Return one longest common subsequence, not just its length. What do you need to keep to walk it back?
|
|
40
|
+
- How is this related to the fewest insertions and deletions that turn one string into the other?
|
|
41
|
+
- Find the longest common **substring** (contiguous) instead. What changes in the recurrence?
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { longestCommonSubsequence } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('232 longest common subsequence', () => {
|
|
6
|
+
it('runs the README examples', () => {
|
|
7
|
+
expect(longestCommonSubsequence('stone', 'longest')).toBe(3)
|
|
8
|
+
expect(longestCommonSubsequence('kitchen', 'thick')).toBe(2)
|
|
9
|
+
expect(longestCommonSubsequence('mississippi', 'sips')).toBe(3)
|
|
10
|
+
expect(longestCommonSubsequence('cat', 'dog')).toBe(0)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('handles single characters', () => {
|
|
14
|
+
expect(longestCommonSubsequence('a', 'a')).toBe(1)
|
|
15
|
+
expect(longestCommonSubsequence('a', 'b')).toBe(0)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('returns the full length for identical strings', () => {
|
|
19
|
+
expect(longestCommonSubsequence('banana', 'banana')).toBe(6)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('returns the shorter length when one is a subsequence of the other', () => {
|
|
23
|
+
expect(longestCommonSubsequence('algorithm', 'lgrm')).toBe(4)
|
|
24
|
+
expect(longestCommonSubsequence('lgrm', 'algorithm')).toBe(4)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('respects order', () => {
|
|
28
|
+
expect(longestCommonSubsequence('abc', 'cba')).toBe(1)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('does not commit to the first match it sees', () => {
|
|
32
|
+
expect(longestCommonSubsequence('abcz', 'zabc')).toBe(3)
|
|
33
|
+
expect(longestCommonSubsequence('zabc', 'abcz')).toBe(3)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('handles repeated letters', () => {
|
|
37
|
+
expect(longestCommonSubsequence('aaaa', 'aa')).toBe(2)
|
|
38
|
+
expect(longestCommonSubsequence('banana', 'atana')).toBe(4)
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
describe('232 performance', () => {
|
|
43
|
+
it('compares two 15-letter strings in under 50 ms (plain recursion explores millions of pairs)', () => {
|
|
44
|
+
// The only shared letter is the x in the middle of each.
|
|
45
|
+
const ms = timeMs(() => expect(longestCommonSubsequence('abcdefgxabcdefg', 'hijklmnxhijklmn')).toBe(1))
|
|
46
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(50)
|
|
47
|
+
})
|
|
48
|
+
})
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# 233 · Coin Change II
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: unbounded knapsack (counting combinations) · Target: 30 min · NeetCode 150 · LeetCode 518
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
You get a list of distinct coin denominations, `coins`, and a target `amount`. You have an unlimited supply of every denomination.
|
|
8
|
+
|
|
9
|
+
Return how many different **combinations** of coins add up to exactly `amount`. Order doesn't matter: `1 + 2` and `2 + 1` are the same combination. An `amount` of `0` has exactly one combination (no coins). If nothing works, return `0`.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `coins` | `amount` | Output | Why |
|
|
14
|
+
|---|---|---|---|
|
|
15
|
+
| `[1, 3]` | `4` | `2` | `1 + 1 + 1 + 1`, `1 + 3` |
|
|
16
|
+
| `[1, 2, 3]` | `4` | `4` | `1+1+1+1`, `1+1+2`, `2+2`, `1+3` |
|
|
17
|
+
| `[2, 3]` | `7` | `1` | `2 + 2 + 3` |
|
|
18
|
+
| `[4]` | `2` | `0` | the only coin is too big |
|
|
19
|
+
| `[3, 7]` | `0` | `1` | the empty combination |
|
|
20
|
+
|
|
21
|
+
## Constraints
|
|
22
|
+
|
|
23
|
+
- `1 <= coins.length <= 300`
|
|
24
|
+
- `1 <= coins[i] <= 5000`, all distinct, in any order
|
|
25
|
+
- `0 <= amount <= 5000` (the performance test uses 400)
|
|
26
|
+
- The answer fits in a 32-bit integer.
|
|
27
|
+
|
|
28
|
+
## Run it
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pnpm watch 233
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
35
|
+
|
|
36
|
+
Time O(amount · coins) · Space O(amount). Plain recursion ("use this coin again, or move on to the next one?") is exponential and fails the performance test; memoising it on (coin index, amount left) passes.
|
|
37
|
+
|
|
38
|
+
</details>
|
|
39
|
+
|
|
40
|
+
## Follow-ups an interviewer might ask
|
|
41
|
+
|
|
42
|
+
- Swap the two loops of your table. What does it count now, and why?
|
|
43
|
+
- What if each coin can be used at most once?
|
|
44
|
+
- Compare with problem 135 (fewest coins). Which parts of the solution stay the same?
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { change } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('233 coin change ii', () => {
|
|
6
|
+
it('runs the README examples', () => {
|
|
7
|
+
expect(change([1, 3], 4)).toBe(2)
|
|
8
|
+
expect(change([1, 2, 3], 4)).toBe(4)
|
|
9
|
+
expect(change([2, 3], 7)).toBe(1)
|
|
10
|
+
expect(change([4], 2)).toBe(0)
|
|
11
|
+
expect(change([3, 7], 0)).toBe(1)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('counts combinations, not orderings', () => {
|
|
15
|
+
// 1 + 2 and 2 + 1 are one combination, so the answer is 2 (1+1+1 and 1+2), not 3.
|
|
16
|
+
expect(change([1, 2], 3)).toBe(2)
|
|
17
|
+
expect(change([2, 5, 3, 6], 10)).toBe(5)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('gives the same answer whatever order the coins come in', () => {
|
|
21
|
+
expect(change([5, 10, 25], 60)).toBe(13)
|
|
22
|
+
expect(change([25, 10, 5], 60)).toBe(13)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('handles a single coin', () => {
|
|
26
|
+
expect(change([7], 21)).toBe(1)
|
|
27
|
+
expect(change([7], 20)).toBe(0)
|
|
28
|
+
expect(change([1], 9)).toBe(1)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('returns 0 when no combination fits', () => {
|
|
32
|
+
expect(change([4, 6], 9)).toBe(0)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('ignores coins bigger than the amount', () => {
|
|
36
|
+
expect(change([5000, 2], 6)).toBe(1)
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
describe('233 performance', () => {
|
|
41
|
+
it('counts the ways to make 400 in under 50 ms (plain recursion takes about a second)', () => {
|
|
42
|
+
const ms = timeMs(() => expect(change([1, 2, 5, 10, 20, 50, 100], 400)).toBe(1_886_815))
|
|
43
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(50)
|
|
44
|
+
})
|
|
45
|
+
})
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# 234 · Edit Distance
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: 2-D dynamic programming (two strings) · Target: 30 min · NeetCode 150 · LeetCode 72
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
You may change a string with three kinds of single-character edits:
|
|
8
|
+
|
|
9
|
+
- **insert** one character anywhere,
|
|
10
|
+
- **delete** one character,
|
|
11
|
+
- **replace** one character with a different one.
|
|
12
|
+
|
|
13
|
+
Return the fewest edits that turn `word1` into `word2`.
|
|
14
|
+
|
|
15
|
+
## Examples
|
|
16
|
+
|
|
17
|
+
| `word1` | `word2` | Output | Why |
|
|
18
|
+
|---|---|---|---|
|
|
19
|
+
| `"flaw"` | `"lawn"` | `2` | delete `f`, insert `n` at the end |
|
|
20
|
+
| `"plant"` | `"plan"` | `1` | delete the `t` |
|
|
21
|
+
| `"dog"` | `"cat"` | `3` | replace all three letters |
|
|
22
|
+
| `""` | `"abc"` | `3` | insert three letters |
|
|
23
|
+
|
|
24
|
+
## Constraints
|
|
25
|
+
|
|
26
|
+
- `0 <= word1.length, word2.length <= 500` (the performance test uses two 12-letter words)
|
|
27
|
+
- Both words contain only lowercase English letters.
|
|
28
|
+
|
|
29
|
+
## Run it
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pnpm watch 234
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
36
|
+
|
|
37
|
+
Time O(m · n) · Space O(min(m, n)) with two rows (O(m · n) with a full table also passes). Plain recursion that tries all three edits at every mismatch is exponential and fails the performance test.
|
|
38
|
+
|
|
39
|
+
</details>
|
|
40
|
+
|
|
41
|
+
## Follow-ups an interviewer might ask
|
|
42
|
+
|
|
43
|
+
- Return the list of edits, not just how many. What do you need to keep to walk them back?
|
|
44
|
+
- What if insertions, deletions and replacements have different costs?
|
|
45
|
+
- Decide whether two strings are exactly one edit apart, in O(n) time and O(1) space.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { minDistance } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('234 edit distance', () => {
|
|
6
|
+
it('runs the README examples', () => {
|
|
7
|
+
expect(minDistance('flaw', 'lawn')).toBe(2)
|
|
8
|
+
expect(minDistance('plant', 'plan')).toBe(1)
|
|
9
|
+
expect(minDistance('dog', 'cat')).toBe(3)
|
|
10
|
+
expect(minDistance('', 'abc')).toBe(3)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('handles empty words', () => {
|
|
14
|
+
expect(minDistance('', '')).toBe(0)
|
|
15
|
+
expect(minDistance('abc', '')).toBe(3)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('returns 0 for identical words', () => {
|
|
19
|
+
expect(minDistance('same', 'same')).toBe(0)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('uses a replace when it is cheaper than delete plus insert', () => {
|
|
23
|
+
expect(minDistance('cat', 'cut')).toBe(1)
|
|
24
|
+
expect(minDistance('a', 'b')).toBe(1)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('handles inserts and deletes in the middle', () => {
|
|
28
|
+
expect(minDistance('abcdef', 'azced')).toBe(3)
|
|
29
|
+
expect(minDistance('gumbo', 'gambol')).toBe(2)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('mixes all three kinds of edit', () => {
|
|
33
|
+
expect(minDistance('kitten', 'sitting')).toBe(3)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('handles repeated letters', () => {
|
|
37
|
+
expect(minDistance('aaa', 'aaaaa')).toBe(2)
|
|
38
|
+
expect(minDistance('abab', 'baba')).toBe(2)
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
describe('234 performance', () => {
|
|
43
|
+
it('compares two 12-letter words in under 50 ms (plain recursion takes about a second)', () => {
|
|
44
|
+
// No letters in common, so every letter must be replaced.
|
|
45
|
+
const ms = timeMs(() => expect(minDistance('abcdefghijkl', 'mnopqrstuvwx')).toBe(12))
|
|
46
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(50)
|
|
47
|
+
})
|
|
48
|
+
})
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# 235 · Maximum Subarray
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: greedy (running best sum, Kadane's algorithm) · Target: 25 min · NeetCode 150 · LeetCode 53
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
You get a non-empty array of integers `nums`. Pick a block of one or more **adjacent** elements and add them up. Return the largest total any such block can have.
|
|
8
|
+
|
|
9
|
+
The block can't be empty, so if every number is negative the answer is still a negative number. Don't modify `nums`.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `nums` | Output |
|
|
14
|
+
|---|---|
|
|
15
|
+
| `[-3, 6, -2, 7, -8, 4]` | `11` (the block `6, -2, 7`) |
|
|
16
|
+
| `[2, 4, 1]` | `7` (the whole array) |
|
|
17
|
+
| `[-6, -2, -9]` | `-2` (just the `-2`) |
|
|
18
|
+
|
|
19
|
+
## Constraints
|
|
20
|
+
|
|
21
|
+
- `1 <= nums.length <= 10^5` (the performance test uses 50,000)
|
|
22
|
+
- `-10^4 <= nums[i] <= 10^4` in the examples; the performance test includes one larger value.
|
|
23
|
+
|
|
24
|
+
## Run it
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pnpm watch 235
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
31
|
+
|
|
32
|
+
Time O(n) · Space O(1). Trying every start and end with a running total is O(n²), which the performance test rejects (50,000 numbers is over a billion pairs).
|
|
33
|
+
|
|
34
|
+
</details>
|
|
35
|
+
|
|
36
|
+
## Follow-ups an interviewer might ask
|
|
37
|
+
|
|
38
|
+
- Return the start and end positions of the best block, not just its sum.
|
|
39
|
+
- Solve it with divide and conquer in O(n log n). Where does the best block cross the middle?
|
|
40
|
+
- The array is circular (the block may wrap from the end back to the start). What changes? (LeetCode 918.)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { maxSubarraySum } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('235 maximum subarray', () => {
|
|
6
|
+
it('finds a run in the middle', () => {
|
|
7
|
+
// 6 + -2 + 7 = 11
|
|
8
|
+
expect(maxSubarraySum([-3, 6, -2, 7, -8, 4])).toBe(11)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('takes the whole array when every value is positive', () => {
|
|
12
|
+
expect(maxSubarraySum([2, 4, 1])).toBe(7)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('returns the largest single value when every value is negative', () => {
|
|
16
|
+
expect(maxSubarraySum([-6, -2, -9])).toBe(-2)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('handles a single element', () => {
|
|
20
|
+
expect(maxSubarraySum([5])).toBe(5)
|
|
21
|
+
expect(maxSubarraySum([-4])).toBe(-4)
|
|
22
|
+
expect(maxSubarraySum([0])).toBe(0)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('handles zeros mixed with negatives', () => {
|
|
26
|
+
expect(maxSubarraySum([-1, 0, -3])).toBe(0)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('crosses a dip when the far side is worth it', () => {
|
|
30
|
+
// 5 + -4 + 6 = 7 beats 6 alone
|
|
31
|
+
expect(maxSubarraySum([5, -4, 6])).toBe(7)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('does not cross a dip that costs more than it gains', () => {
|
|
35
|
+
expect(maxSubarraySum([5, -6, 4])).toBe(5)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('finds the best run at the very end', () => {
|
|
39
|
+
expect(maxSubarraySum([3, -10, 1, 2, 3])).toBe(6)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('handles large values', () => {
|
|
43
|
+
expect(maxSubarraySum([10_000, -1, 10_000, -10_000])).toBe(19_999)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('does not modify the input', () => {
|
|
47
|
+
const nums = [1, -2, 3]
|
|
48
|
+
maxSubarraySum(nums)
|
|
49
|
+
expect(nums).toEqual([1, -2, 3])
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
describe('235 performance', () => {
|
|
54
|
+
it('handles 50,000 numbers in under 100 ms (trying every subarray, O(n²), is too slow here)', () => {
|
|
55
|
+
const n = 50_000
|
|
56
|
+
// Repeating blocks of [2, 2, -5]: each block loses 1, so no long run pays off.
|
|
57
|
+
const nums: number[] = Array.from({ length: n }, (_, i) => (i % 3 === 2 ? -5 : 2))
|
|
58
|
+
// The array ends ..., 2, -5, 2, 1_000_000: the best run is the last two elements.
|
|
59
|
+
nums[n - 1] = 1_000_000
|
|
60
|
+
const ms = timeMs(() => expect(maxSubarraySum(nums)).toBe(1_000_002))
|
|
61
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(100)
|
|
62
|
+
})
|
|
63
|
+
})
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# 236 · Jump Game
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: greedy (furthest reach) · Target: 25 min · NeetCode 150 · LeetCode 55
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
You stand on the first position of an array `nums` of non-negative integers. From position `i` you may move forward by any number of steps from `1` up to `nums[i]` (a `0` means you can't move from there).
|
|
8
|
+
|
|
9
|
+
Return `true` if you can get to the last position, and `false` otherwise. Going past the end also counts as getting there. An array with one element is already finished. Don't modify `nums`.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `nums` | Output |
|
|
14
|
+
|---|---|
|
|
15
|
+
| `[1, 3, 0, 0, 2, 0]` | `true` (0 → 1 → 4 → 5) |
|
|
16
|
+
| `[2, 0, 1, 0, 3]` | `false` (no route gets past position 3, which holds a `0`) |
|
|
17
|
+
| `[0]` | `true` |
|
|
18
|
+
|
|
19
|
+
## Constraints
|
|
20
|
+
|
|
21
|
+
- `1 <= nums.length <= 10^4` (the performance tests use 120,000)
|
|
22
|
+
- `0 <= nums[i] <= 10^5`
|
|
23
|
+
|
|
24
|
+
## Run it
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pnpm watch 236
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
31
|
+
|
|
32
|
+
Time O(n) · Space O(1). Marking every position you could land on from every reachable position is O(n²) when the jumps are long, and the performance test rejects it. Recursion that tries every jump length is exponential without memoisation, and it can also overflow the stack.
|
|
33
|
+
|
|
34
|
+
</details>
|
|
35
|
+
|
|
36
|
+
## Follow-ups an interviewer might ask
|
|
37
|
+
|
|
38
|
+
- Return the smallest number of jumps needed to reach the end (Jump Game II, LeetCode 45).
|
|
39
|
+
- Solve it scanning from the end towards the start instead. Why does that work too?
|
|
40
|
+
- What if some values could be negative (jumps backwards)? Which approach still works?
|