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,51 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { characterReplacement } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('110 longest repeating character replacement', () => {
|
|
6
|
+
it('replaces enough letters to cover the whole string', () => {
|
|
7
|
+
expect(characterReplacement('ABAB', 2)).toBe(4)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('finds the best window with one replacement', () => {
|
|
11
|
+
expect(characterReplacement('AABABBA', 1)).toBe(4)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('handles k = 0', () => {
|
|
15
|
+
expect(characterReplacement('AAAA', 0)).toBe(4)
|
|
16
|
+
expect(characterReplacement('ABBBCC', 0)).toBe(3)
|
|
17
|
+
expect(characterReplacement('ABCD', 0)).toBe(1)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('handles a single character', () => {
|
|
21
|
+
expect(characterReplacement('Z', 0)).toBe(1)
|
|
22
|
+
expect(characterReplacement('Z', 1)).toBe(1)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('never goes past the string length when k is large', () => {
|
|
26
|
+
expect(characterReplacement('ABC', 3)).toBe(3)
|
|
27
|
+
expect(characterReplacement('ABCDE', 10)).toBe(5)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('handles many different letters', () => {
|
|
31
|
+
expect(characterReplacement('ABCDEFGAAAAH', 2)).toBe(6)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('finds a window that is not at the start', () => {
|
|
35
|
+
expect(characterReplacement('ABCDDDDBDD', 1)).toBe(7)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('handles the best letter changing along the string', () => {
|
|
39
|
+
expect(characterReplacement('AAABBBBBAAA', 2)).toBe(7)
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
describe('110 performance', () => {
|
|
44
|
+
it('handles 50,000 letters with k = 12,500 in under 200 ms (O(n²) is too slow here)', () => {
|
|
45
|
+
// Alternating letters: a window can be at most k Bs plus k + 1 As.
|
|
46
|
+
const s = 'AB'.repeat(25_000)
|
|
47
|
+
const k = 12_500
|
|
48
|
+
const ms = timeMs(() => expect(characterReplacement(s, k)).toBe(2 * k + 1))
|
|
49
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
50
|
+
})
|
|
51
|
+
})
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# 111 · Permutation in String
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: sliding window · Target: 25 min · NeetCode 150 · LeetCode 567
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
You get two lowercase strings `s1` and `s2`. Return `true` if some contiguous substring of `s2` is a rearrangement of `s1` (same letters, same counts, any order), and `false` otherwise.
|
|
8
|
+
|
|
9
|
+
## Examples
|
|
10
|
+
|
|
11
|
+
| `s1` | `s2` | Output | Why |
|
|
12
|
+
|---|---|---|---|
|
|
13
|
+
| `"ab"` | `"eidbaooo"` | `true` | `"ba"` appears in `s2` |
|
|
14
|
+
| `"ab"` | `"eidboaoo"` | `false` | `b` and `a` are never next to each other |
|
|
15
|
+
| `"abc"` | `"ab"` | `false` | `s1` is longer than `s2` |
|
|
16
|
+
|
|
17
|
+
## Constraints
|
|
18
|
+
|
|
19
|
+
- `1 <= s1.length, s2.length <= 10^4` (the performance test uses a 500-letter `s1` and a 50,500-letter `s2`)
|
|
20
|
+
- Both strings contain only lowercase letters `a`–`z`.
|
|
21
|
+
|
|
22
|
+
## Run it
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pnpm watch 111
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
29
|
+
|
|
30
|
+
Time O(n + m), or O(26 · n) if you compare two count arrays at each step · Space O(1) (26 counters). Sorting every window is O(n · m log m) and too slow.
|
|
31
|
+
|
|
32
|
+
</details>
|
|
33
|
+
|
|
34
|
+
## Follow-ups an interviewer might ask
|
|
35
|
+
|
|
36
|
+
- Return the index where the first match starts.
|
|
37
|
+
- Find All Anagrams in a String (LeetCode 438): return every starting index.
|
|
38
|
+
- What changes if the strings can contain any Unicode characters?
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { checkInclusion } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('111 permutation in string', () => {
|
|
6
|
+
it('finds a rearrangement in the middle', () => {
|
|
7
|
+
expect(checkInclusion('ab', 'eidbaooo')).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('rejects letters that are present but not together', () => {
|
|
11
|
+
expect(checkInclusion('ab', 'eidboaoo')).toBe(false)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('returns false when s1 is longer than s2', () => {
|
|
15
|
+
expect(checkInclusion('abc', 'ab')).toBe(false)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('handles single letters and equal strings', () => {
|
|
19
|
+
expect(checkInclusion('a', 'a')).toBe(true)
|
|
20
|
+
expect(checkInclusion('a', 'b')).toBe(false)
|
|
21
|
+
expect(checkInclusion('abc', 'cba')).toBe(true)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('finds a match at the very start and at the very end', () => {
|
|
25
|
+
expect(checkInclusion('xyz', 'zyxaaaa')).toBe(true)
|
|
26
|
+
expect(checkInclusion('xyz', 'aaaazxy')).toBe(true)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('rejects the same letters with different counts', () => {
|
|
30
|
+
expect(checkInclusion('aab', 'cabbac')).toBe(false)
|
|
31
|
+
expect(checkInclusion('aab', 'abb')).toBe(false)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('handles repeated letters in s1', () => {
|
|
35
|
+
expect(checkInclusion('aab', 'cbaab')).toBe(true)
|
|
36
|
+
expect(checkInclusion('adc', 'dcda')).toBe(true)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('finds a match right after a near miss', () => {
|
|
40
|
+
expect(checkInclusion('abc', 'bbbca')).toBe(true)
|
|
41
|
+
})
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
describe('111 performance', () => {
|
|
45
|
+
it('finds a 500-letter rearrangement at the end of 50,500 letters in under 200 ms (sorting every window is too slow here)', () => {
|
|
46
|
+
const m = 500
|
|
47
|
+
const s1 = Array.from({ length: m }, (_, i) => String.fromCharCode(97 + ((i * 7) % 26))).join('')
|
|
48
|
+
// The filler never contains 'z' but s1 does, so no window inside the filler can match.
|
|
49
|
+
const filler = Array.from({ length: 100 * m }, (_, i) => String.fromCharCode(97 + (i % 25))).join('')
|
|
50
|
+
const s2 = filler + [...s1].reverse().join('')
|
|
51
|
+
const ms = timeMs(() => expect(checkInclusion(s1, s2)).toBe(true))
|
|
52
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
53
|
+
})
|
|
54
|
+
})
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# 112 · Min Stack
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: stack (design) · Target: 25 min · NeetCode 150 · LeetCode 155
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
Build a stack class, `MinStack`, that supports the usual stack operations and can also report its smallest element at any moment. Every operation must run in O(1) time.
|
|
8
|
+
|
|
9
|
+
- `push(val)` puts `val` on top.
|
|
10
|
+
- `pop()` removes the top element.
|
|
11
|
+
- `top()` returns the top element without removing it.
|
|
12
|
+
- `getMin()` returns the smallest element currently in the stack.
|
|
13
|
+
|
|
14
|
+
`pop`, `top` and `getMin` are only ever called when the stack is non-empty, so you don't need to handle that case.
|
|
15
|
+
|
|
16
|
+
## Examples
|
|
17
|
+
|
|
18
|
+
| Call | Returns | Stack afterwards (bottom → top) |
|
|
19
|
+
|---|---|---|
|
|
20
|
+
| `new MinStack()` | | `[]` |
|
|
21
|
+
| `push(-2)` | | `[-2]` |
|
|
22
|
+
| `push(0)` | | `[-2, 0]` |
|
|
23
|
+
| `push(-3)` | | `[-2, 0, -3]` |
|
|
24
|
+
| `getMin()` | `-3` | `[-2, 0, -3]` |
|
|
25
|
+
| `pop()` | | `[-2, 0]` |
|
|
26
|
+
| `top()` | `0` | `[-2, 0]` |
|
|
27
|
+
| `getMin()` | `-2` | `[-2, 0]` |
|
|
28
|
+
|
|
29
|
+
## Constraints
|
|
30
|
+
|
|
31
|
+
- `-2^31 <= val <= 2^31 - 1`
|
|
32
|
+
- At most `3 * 10^4` calls in total (the performance test makes about 250,000 calls on a stack up to 50,000 deep).
|
|
33
|
+
- `pop`, `top` and `getMin` are only called on a non-empty stack.
|
|
34
|
+
|
|
35
|
+
## Run it
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pnpm watch 112
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
42
|
+
|
|
43
|
+
Time O(1) for every operation · Space O(n).
|
|
44
|
+
|
|
45
|
+
</details>
|
|
46
|
+
|
|
47
|
+
## Follow-ups an interviewer might ask
|
|
48
|
+
|
|
49
|
+
- How much extra memory does your design use? Can you reduce it for typical inputs?
|
|
50
|
+
- How would you also support `getMax()`? What about `popMin()` (remove the smallest element)? Does O(1) still hold?
|
|
51
|
+
- Build a queue that reports its minimum in O(1) amortised time.
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { MinStack } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('112 min stack', () => {
|
|
6
|
+
it('runs the README example', () => {
|
|
7
|
+
const stack = new MinStack()
|
|
8
|
+
stack.push(-2)
|
|
9
|
+
stack.push(0)
|
|
10
|
+
stack.push(-3)
|
|
11
|
+
expect(stack.getMin()).toBe(-3)
|
|
12
|
+
stack.pop()
|
|
13
|
+
expect(stack.top()).toBe(0)
|
|
14
|
+
expect(stack.getMin()).toBe(-2)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('handles a single element', () => {
|
|
18
|
+
const stack = new MinStack()
|
|
19
|
+
stack.push(5)
|
|
20
|
+
expect(stack.top()).toBe(5)
|
|
21
|
+
expect(stack.getMin()).toBe(5)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('restores the previous minimum after popping the current one', () => {
|
|
25
|
+
const stack = new MinStack()
|
|
26
|
+
stack.push(3)
|
|
27
|
+
stack.push(5)
|
|
28
|
+
stack.push(1)
|
|
29
|
+
stack.push(4)
|
|
30
|
+
expect(stack.getMin()).toBe(1)
|
|
31
|
+
stack.pop()
|
|
32
|
+
expect(stack.getMin()).toBe(1)
|
|
33
|
+
stack.pop()
|
|
34
|
+
expect(stack.top()).toBe(5)
|
|
35
|
+
expect(stack.getMin()).toBe(3)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('keeps a repeated minimum until every copy is popped', () => {
|
|
39
|
+
const stack = new MinStack()
|
|
40
|
+
stack.push(0)
|
|
41
|
+
stack.push(1)
|
|
42
|
+
stack.push(0)
|
|
43
|
+
expect(stack.getMin()).toBe(0)
|
|
44
|
+
stack.pop()
|
|
45
|
+
expect(stack.getMin()).toBe(0)
|
|
46
|
+
stack.pop()
|
|
47
|
+
expect(stack.getMin()).toBe(0)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('handles the same minimum pushed several times in a row', () => {
|
|
51
|
+
const stack = new MinStack()
|
|
52
|
+
stack.push(2)
|
|
53
|
+
stack.push(2)
|
|
54
|
+
stack.push(2)
|
|
55
|
+
stack.pop()
|
|
56
|
+
stack.pop()
|
|
57
|
+
expect(stack.getMin()).toBe(2)
|
|
58
|
+
expect(stack.top()).toBe(2)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('handles increasing and then decreasing pushes', () => {
|
|
62
|
+
const stack = new MinStack()
|
|
63
|
+
for (const v of [1, 2, 3]) stack.push(v)
|
|
64
|
+
expect(stack.getMin()).toBe(1)
|
|
65
|
+
for (const v of [0, -1]) stack.push(v)
|
|
66
|
+
expect(stack.getMin()).toBe(-1)
|
|
67
|
+
stack.pop()
|
|
68
|
+
expect(stack.getMin()).toBe(0)
|
|
69
|
+
stack.pop()
|
|
70
|
+
expect(stack.getMin()).toBe(1)
|
|
71
|
+
expect(stack.top()).toBe(3)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('handles extreme 32-bit values', () => {
|
|
75
|
+
const stack = new MinStack()
|
|
76
|
+
stack.push(2 ** 31 - 1)
|
|
77
|
+
stack.push(-(2 ** 31))
|
|
78
|
+
expect(stack.getMin()).toBe(-(2 ** 31))
|
|
79
|
+
stack.pop()
|
|
80
|
+
expect(stack.getMin()).toBe(2 ** 31 - 1)
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('can be emptied and reused', () => {
|
|
84
|
+
const stack = new MinStack()
|
|
85
|
+
stack.push(-5)
|
|
86
|
+
stack.pop()
|
|
87
|
+
stack.push(7)
|
|
88
|
+
expect(stack.getMin()).toBe(7)
|
|
89
|
+
expect(stack.top()).toBe(7)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('keeps separate instances independent', () => {
|
|
93
|
+
const a = new MinStack()
|
|
94
|
+
const b = new MinStack()
|
|
95
|
+
a.push(1)
|
|
96
|
+
b.push(10)
|
|
97
|
+
b.push(-10)
|
|
98
|
+
expect(a.getMin()).toBe(1)
|
|
99
|
+
expect(a.top()).toBe(1)
|
|
100
|
+
expect(b.getMin()).toBe(-10)
|
|
101
|
+
})
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
describe('112 performance', () => {
|
|
105
|
+
it('handles ~250,000 calls on a 50,000-deep stack in under 150 ms (scanning for the min is too slow here)', () => {
|
|
106
|
+
const n = 50_000
|
|
107
|
+
let mismatches = 0
|
|
108
|
+
const ms = timeMs(() => {
|
|
109
|
+
const stack = new MinStack()
|
|
110
|
+
// Every push is a new minimum, so every pop removes the current minimum.
|
|
111
|
+
for (let i = 0; i < n; i++) {
|
|
112
|
+
stack.push(n - i)
|
|
113
|
+
if (stack.getMin() !== n - i) mismatches++
|
|
114
|
+
}
|
|
115
|
+
for (let i = n - 1; i >= 1; i--) {
|
|
116
|
+
stack.pop()
|
|
117
|
+
if (stack.getMin() !== n - i + 1 || stack.top() !== n - i + 1) mismatches++
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
expect(mismatches, 'wrong getMin/top results').toBe(0)
|
|
121
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(150)
|
|
122
|
+
})
|
|
123
|
+
})
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# 113 · Daily Temperatures
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: monotonic stack · Target: 25 min · NeetCode 150 · LeetCode 739
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
`temperatures[i]` is the temperature on day `i`. For each day, work out how many days you'd have to wait until a strictly warmer day. If no warmer day ever comes, the answer for that day is `0`.
|
|
8
|
+
|
|
9
|
+
Return an array `answer` of the same length, where `answer[i]` is that wait for day `i`. A day with the same temperature doesn't count as warmer.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `temperatures` | Output |
|
|
14
|
+
|---|---|
|
|
15
|
+
| `[73, 74, 75, 71, 69, 72, 76, 73]` | `[1, 1, 4, 2, 1, 1, 0, 0]` |
|
|
16
|
+
| `[30, 40, 50, 60]` | `[1, 1, 1, 0]` |
|
|
17
|
+
| `[30, 60, 90]` | `[1, 1, 0]` |
|
|
18
|
+
|
|
19
|
+
## Constraints
|
|
20
|
+
|
|
21
|
+
- `1 <= temperatures.length <= 10^5` (the performance test uses 100,000)
|
|
22
|
+
- `30 <= temperatures[i] <= 100`
|
|
23
|
+
|
|
24
|
+
## Run it
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pnpm watch 113
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
31
|
+
|
|
32
|
+
Time O(n) · Space O(n). Each index is pushed and popped at most once.
|
|
33
|
+
|
|
34
|
+
</details>
|
|
35
|
+
|
|
36
|
+
## Follow-ups an interviewer might ask
|
|
37
|
+
|
|
38
|
+
- Can you do it with O(1) extra space besides the output array?
|
|
39
|
+
- Return the next warmer *temperature* instead of the number of days.
|
|
40
|
+
- Next Greater Element II: the days wrap around, so the last day can look at the first.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { dailyTemperatures } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('113 daily temperatures', () => {
|
|
6
|
+
it('handles a mixed week', () => {
|
|
7
|
+
expect(dailyTemperatures([73, 74, 75, 71, 69, 72, 76, 73])).toEqual([1, 1, 4, 2, 1, 1, 0, 0])
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('handles strictly rising temperatures', () => {
|
|
11
|
+
expect(dailyTemperatures([30, 40, 50, 60])).toEqual([1, 1, 1, 0])
|
|
12
|
+
expect(dailyTemperatures([30, 60, 90])).toEqual([1, 1, 0])
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('handles a single day', () => {
|
|
16
|
+
expect(dailyTemperatures([50])).toEqual([0])
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('returns all zeros when it only gets colder', () => {
|
|
20
|
+
expect(dailyTemperatures([90, 80, 70, 60])).toEqual([0, 0, 0, 0])
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('does not count an equal temperature as warmer', () => {
|
|
24
|
+
expect(dailyTemperatures([70, 70, 70])).toEqual([0, 0, 0])
|
|
25
|
+
expect(dailyTemperatures([70, 70, 71])).toEqual([2, 1, 0])
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('finds a warmer day far ahead', () => {
|
|
29
|
+
expect(dailyTemperatures([80, 60, 70, 60, 70, 90])).toEqual([5, 1, 3, 1, 1, 0])
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('handles dips and recoveries', () => {
|
|
33
|
+
expect(dailyTemperatures([55, 38, 53, 81, 61, 93, 97, 32, 43, 78])).toEqual([3, 1, 1, 2, 1, 1, 0, 1, 1, 0])
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('handles the temperature bounds', () => {
|
|
37
|
+
expect(dailyTemperatures([100, 30, 100, 30])).toEqual([0, 1, 0, 0])
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
describe('113 performance', () => {
|
|
42
|
+
it('handles 100,000 days in under 150 ms (O(n²) is too slow here)', () => {
|
|
43
|
+
const n = 100_000
|
|
44
|
+
// Slowly cooling from 99 down to 31, then one hot day at the very end.
|
|
45
|
+
const temperatures = Array.from({ length: n }, (_, i) => (i === n - 1 ? 100 : 99 - Math.floor((i * 69) / n)))
|
|
46
|
+
let answer: number[] = []
|
|
47
|
+
const ms = timeMs(() => {
|
|
48
|
+
answer = dailyTemperatures(temperatures)
|
|
49
|
+
})
|
|
50
|
+
// Every day waits for that last hot day.
|
|
51
|
+
expect(answer).toHaveLength(n)
|
|
52
|
+
const firstWrong = answer.findIndex((wait, i) => wait !== n - 1 - i)
|
|
53
|
+
expect(firstWrong, `day ${firstWrong} should wait ${n - 1 - firstWrong} days, got ${answer[firstWrong]}`).toBe(-1)
|
|
54
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(150)
|
|
55
|
+
})
|
|
56
|
+
})
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# 114 · Evaluate Reverse Polish Notation
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: stack · Target: 25 min · NeetCode 150 · LeetCode 150
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
You get an arithmetic expression in Reverse Polish Notation (postfix), split into string tokens. Each token is either an integer (possibly negative, like `"-11"`) or one of the operators `"+"`, `"-"`, `"*"`, `"/"`. An operator applies to the two values that come before it, in order: `["5", "3", "-"]` means `5 - 3`.
|
|
8
|
+
|
|
9
|
+
Return the value of the whole expression as a number.
|
|
10
|
+
|
|
11
|
+
- Division between integers **truncates toward zero**: `7 / 2` is `3`, and `-7 / 2` is `-3` (not `-4`).
|
|
12
|
+
- The expression is always valid, and there's never a division by zero.
|
|
13
|
+
|
|
14
|
+
## Examples
|
|
15
|
+
|
|
16
|
+
| `tokens` | Output | Why |
|
|
17
|
+
|---|---|---|
|
|
18
|
+
| `["2", "1", "+", "3", "*"]` | `9` | `(2 + 1) * 3` |
|
|
19
|
+
| `["4", "13", "5", "/", "+"]` | `6` | `4 + (13 / 5)` = `4 + 2` |
|
|
20
|
+
| `["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]` | `22` | `10 * (6 / ((9 + 3) * -11)) + 17 + 5` |
|
|
21
|
+
|
|
22
|
+
## Constraints
|
|
23
|
+
|
|
24
|
+
- `1 <= tokens.length <= 10^4`
|
|
25
|
+
- Every token is an operator or an integer in `[-200, 200]`.
|
|
26
|
+
- The final answer and every intermediate value fit in a 32-bit signed integer.
|
|
27
|
+
|
|
28
|
+
## Run it
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pnpm watch 114
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
35
|
+
|
|
36
|
+
Time O(n) · Space O(n).
|
|
37
|
+
|
|
38
|
+
</details>
|
|
39
|
+
|
|
40
|
+
## Follow-ups an interviewer might ask
|
|
41
|
+
|
|
42
|
+
- Why does `Math.floor` give the wrong answer for negative division? What's the cleanest way to truncate in TypeScript?
|
|
43
|
+
- Convert an ordinary infix expression like `3 + 4 * 2` to RPN. (Shunting-yard.)
|
|
44
|
+
- Basic Calculator (LeetCode 224): evaluate an infix string with `+`, `-` and parentheses directly.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { evalRPN } from './solution'
|
|
3
|
+
|
|
4
|
+
describe('114 evaluate reverse polish notation', () => {
|
|
5
|
+
it('adds and multiplies', () => {
|
|
6
|
+
expect(evalRPN(['2', '1', '+', '3', '*'])).toBe(9)
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
it('truncates integer division', () => {
|
|
10
|
+
expect(evalRPN(['4', '13', '5', '/', '+'])).toBe(6)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('evaluates a long expression with a negative operand', () => {
|
|
14
|
+
expect(evalRPN(['10', '6', '9', '3', '+', '-11', '*', '/', '*', '17', '+', '5', '+'])).toBe(22)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('returns a lone number', () => {
|
|
18
|
+
expect(evalRPN(['42'])).toBe(42)
|
|
19
|
+
expect(evalRPN(['-7'])).toBe(-7)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('keeps the operand order for - and /', () => {
|
|
23
|
+
expect(evalRPN(['5', '3', '-'])).toBe(2)
|
|
24
|
+
expect(evalRPN(['3', '5', '-'])).toBe(-2)
|
|
25
|
+
expect(evalRPN(['6', '3', '/'])).toBe(2)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('truncates negative division toward zero', () => {
|
|
29
|
+
expect(evalRPN(['-7', '2', '/'])).toBe(-3)
|
|
30
|
+
expect(evalRPN(['7', '-2', '/'])).toBe(-3)
|
|
31
|
+
expect(evalRPN(['-7', '-2', '/'])).toBe(3)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('truncates a small negative quotient to zero before using it', () => {
|
|
35
|
+
// -1 / 2 is 0 (not -1), so the answer is 0 + 5.
|
|
36
|
+
expect(evalRPN(['-1', '2', '/', '5', '+'])).toBe(5)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('handles negative tokens and multi-digit numbers', () => {
|
|
40
|
+
expect(evalRPN(['-11', '-200', '*'])).toBe(2200)
|
|
41
|
+
expect(evalRPN(['100', '-25', '+'])).toBe(75)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('handles deeply nested operators', () => {
|
|
45
|
+
// 1 2 3 4 5 + + + + = 1 + (2 + (3 + (4 + 5)))
|
|
46
|
+
expect(evalRPN(['1', '2', '3', '4', '5', '+', '+', '+', '+'])).toBe(15)
|
|
47
|
+
// ((((2 * 3) * 4) - 4) / 5)
|
|
48
|
+
expect(evalRPN(['2', '3', '*', '4', '*', '4', '-', '5', '/'])).toBe(4)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('handles a long chain of operations', () => {
|
|
52
|
+
// 1 1 + 1 + 1 + ... (5,000 ones) = 5,000
|
|
53
|
+
const tokens = ['1', ...Array.from({ length: 4_999 }, () => ['1', '+']).flat()]
|
|
54
|
+
expect(evalRPN(tokens)).toBe(5_000)
|
|
55
|
+
})
|
|
56
|
+
})
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# 115 · Generate Parentheses
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: backtracking / stack · Target: 25 min · NeetCode 150 · LeetCode 22
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
Given `n`, return every string made of exactly `n` opening and `n` closing parentheses that is well-formed: reading left to right, the number of `)` never gets ahead of the number of `(`, and both counts end equal.
|
|
8
|
+
|
|
9
|
+
Return the strings in any order, with no duplicates.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `n` | Output (any order) |
|
|
14
|
+
|---|---|
|
|
15
|
+
| `3` | `["((()))", "(()())", "(())()", "()(())", "()()()"]` |
|
|
16
|
+
| `1` | `["()"]` |
|
|
17
|
+
|
|
18
|
+
## Constraints
|
|
19
|
+
|
|
20
|
+
- `1 <= n <= 8` (the performance test uses `n = 12`, which has 208,012 answers)
|
|
21
|
+
|
|
22
|
+
## Run it
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pnpm watch 115
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
29
|
+
|
|
30
|
+
Time O(4^n / √n): proportional to the number of answers (the n-th Catalan number) times their length · Space O(n) for the recursion, not counting the output.
|
|
31
|
+
|
|
32
|
+
</details>
|
|
33
|
+
|
|
34
|
+
## Follow-ups an interviewer might ask
|
|
35
|
+
|
|
36
|
+
- How many strings are there for a given `n`? (The Catalan numbers: 1, 2, 5, 14, 42, …) Why does that count tell you the best possible running time?
|
|
37
|
+
- Can you write it iteratively, or build the answers for `n` from the answers for smaller sizes?
|
|
38
|
+
- Valid Parentheses (problem 006) checks one string. How does your generator guarantee every string it produces would pass that check?
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { generateParenthesis } from './solution'
|
|
4
|
+
|
|
5
|
+
const sorted = (strings: readonly string[]): string[] => [...strings].sort()
|
|
6
|
+
|
|
7
|
+
/** True when the string is a well-formed sequence of n pairs. */
|
|
8
|
+
function isWellFormed(s: string, n: number): boolean {
|
|
9
|
+
if (s.length !== 2 * n) return false
|
|
10
|
+
let depth = 0
|
|
11
|
+
for (const ch of s) {
|
|
12
|
+
if (ch === '(') depth++
|
|
13
|
+
else if (ch === ')') depth--
|
|
14
|
+
else return false
|
|
15
|
+
if (depth < 0) return false
|
|
16
|
+
}
|
|
17
|
+
return depth === 0
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe('115 generate parentheses', () => {
|
|
21
|
+
it('generates all five strings for n = 3', () => {
|
|
22
|
+
expect(sorted(generateParenthesis(3))).toEqual(['((()))', '(()())', '(())()', '()(())', '()()()'])
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('handles n = 1', () => {
|
|
26
|
+
expect(generateParenthesis(1)).toEqual(['()'])
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('handles n = 2', () => {
|
|
30
|
+
expect(sorted(generateParenthesis(2))).toEqual(['(())', '()()'])
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('generates 14 distinct, well-formed strings for n = 4', () => {
|
|
34
|
+
const result = generateParenthesis(4)
|
|
35
|
+
expect(result).toHaveLength(14)
|
|
36
|
+
expect(new Set(result).size).toBe(14)
|
|
37
|
+
expect(result.every((s) => isWellFormed(s, 4))).toBe(true)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('generates exactly 1,430 distinct, well-formed strings for n = 8', () => {
|
|
41
|
+
const result = generateParenthesis(8)
|
|
42
|
+
expect(result).toHaveLength(1_430)
|
|
43
|
+
expect(new Set(result).size).toBe(1_430)
|
|
44
|
+
expect(result.every((s) => isWellFormed(s, 8))).toBe(true)
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
describe('115 performance', () => {
|
|
49
|
+
it('generates all 208,012 strings for n = 12 in under 400 ms (building all 2^24 strings and filtering is too slow here)', () => {
|
|
50
|
+
let result: string[] = []
|
|
51
|
+
const ms = timeMs(() => {
|
|
52
|
+
result = generateParenthesis(12)
|
|
53
|
+
})
|
|
54
|
+
expect(result).toHaveLength(208_012)
|
|
55
|
+
expect(new Set(result).size).toBe(208_012)
|
|
56
|
+
expect(result.every((s) => isWellFormed(s, 12))).toBe(true)
|
|
57
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(400)
|
|
58
|
+
})
|
|
59
|
+
})
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# 116 · Koko Eating Bananas
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: binary search on the answer · Target: 25 min · NeetCode 150 · LeetCode 875
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
There are `piles.length` piles of bananas, with `piles[i]` bananas in pile `i`. Koko picks a whole-number eating speed `k` (bananas per hour) and keeps it fixed. Each hour she chooses one pile and eats `k` bananas from it. If the pile has fewer than `k` left, she finishes that pile and waits out the rest of the hour without starting another pile.
|
|
8
|
+
|
|
9
|
+
So a pile of `p` bananas takes `ceil(p / k)` hours. Return the **smallest** `k` that lets her finish every pile within `h` hours.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `piles` | `h` | Output | Why |
|
|
14
|
+
|---|---|---|---|
|
|
15
|
+
| `[3, 6, 7, 11]` | `8` | `4` | `1 + 2 + 2 + 3 = 8` hours; at speed 3 it takes 10 |
|
|
16
|
+
| `[30, 11, 23, 4, 20]` | `5` | `30` | one pile per hour, so she must finish the biggest pile in an hour |
|
|
17
|
+
| `[30, 11, 23, 4, 20]` | `6` | `23` | `2 + 1 + 1 + 1 + 1 = 6` hours |
|
|
18
|
+
|
|
19
|
+
## Constraints
|
|
20
|
+
|
|
21
|
+
- `1 <= piles.length <= 10^4`
|
|
22
|
+
- `piles.length <= h <= 10^9` (so an answer always exists)
|
|
23
|
+
- `1 <= piles[i] <= 10^9`
|
|
24
|
+
|
|
25
|
+
## Run it
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm watch 116
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
32
|
+
|
|
33
|
+
Time O(n log m), where m is the largest pile · Space O(1).
|
|
34
|
+
|
|
35
|
+
</details>
|
|
36
|
+
|
|
37
|
+
## Follow-ups an interviewer might ask
|
|
38
|
+
|
|
39
|
+
- What property of "hours needed at speed k" makes your approach correct?
|
|
40
|
+
- What are the smallest and largest speeds that could possibly be the answer?
|
|
41
|
+
- Capacity to Ship Packages Within D Days (LeetCode 1011) and Split Array Largest Sum (LeetCode 410) use the same idea. How would you adapt it?
|