Skip to content

Commit 363e073

Browse files
committed
228th Commit
1 parent 557f98e commit 363e073

File tree

2 files changed

+249
-38
lines changed

2 files changed

+249
-38
lines changed

README.md

Lines changed: 188 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,6 @@ $ pnpm test <FILE_NAME>
2828
$ pnpm test twoSum.test.ts
2929
```
3030

31-
4. Run benchmarks
32-
33-
```sh
34-
$ pnpm bench
35-
36-
# run a specific bench file
37-
$ pnpm bench <FILE_NAME>
38-
# e.g.
39-
$ pnpm bench twoSum.bench.ts
40-
```
41-
42-
## Directory Structure
43-
44-
```ts
45-
.
46-
├── .github/workflows/ci.yaml
47-
├── src
48-
│ ├── page-<NUM> -> set the table to display 100 rows per page
49-
│ │ └── <TITLE>
50-
│ │ ├── <NAME>.spec.ts
51-
│ │ └── <NAME>.ts
52-
│ └── utils
53-
├── .editorconfig
54-
├── .gitignore
55-
├── biome.json
56-
├── compose.yaml
57-
├── Dockerfile
58-
├── package.json
59-
├── pnpm-lock.yaml
60-
├── README.md
61-
├── tsconfig.json
62-
└── vite.config.ts
63-
```
64-
6531
## Beginning - Algorithms
6632

6733
解題前應該學會的思路與技巧
@@ -340,15 +306,15 @@ Must-do List for Interview Prep
340306
| 45. Jump Game II | Solution | Medium |
341307
| 274. H-Index | Solution | Medium |
342308
| 380. Insert Delete GetRandom O(1) | Solution | Medium |
343-
| 238. Product of Array Except Self | Solution | Medium |
309+
| 238. Product of Array Except Self | [Solution][238] | Medium |
344310
| 134. Gas Station | Solution | Medium |
345311
| 135. Candy | Solution | Hard |
346312
| 42. Trapping Rain Water | Solution | Hard |
347313
| 13. Roman to Integer | [Solution][13] | Easy |
348314
| 12. Integer to Roman | Solution | Medium |
349315
| 58. Length of Last Word | [Solution][58] | Easy |
350316
| 14. Longest Common Prefix | [Solution][14] | Easy |
351-
| 151. Reverse Words in a String | Solution | Medium |
317+
| 151. Reverse Words in a String | [Solution][151] | Medium |
352318
| 6. Zigzag Conversion | [Solution][6] | Medium |
353319
| 28. Find the Index of the First Occurrence in a String | [Solution][28] | Easy |
354320
| 68. Text Justification | [Solution][68] | Hard |
@@ -357,9 +323,11 @@ Must-do List for Interview Prep
357323
[27]: ./src/page-1/27.%20Remove%20Element/removeElement.ts
358324
[26]: ./src/page-1/26.%20Remove%20Duplicates%20from%20Sorted%20Array/removeDuplicates.ts
359325
[189]: ./src/page-2/189.%20Rotate%20Array/rotate.ts
326+
[238]: ./src/page-3/238.%20Product%20of%20Array%20Except%20Self/productExceptSelf.ts
360327
[13]: ./src/page-1/13.%20Roman%20to%20Integer/romanToInt.ts
361328
[58]: ./src/page-1/58.%20Length%20of%20Last%20Word/lengthOfLastWord.ts
362329
[14]: ./src/page-1/14.%20Longest%20Common%20Prefix/longestCommonPrefix.ts
330+
[151]: ./src/page-2/151.%20Reverse%20Words%20in%20a%20String/reverseWords.ts
363331
[6]: ./src/page-1/6.%20Zigzag%20Conversion/convert.ts
364332
[28]: ./src/page-1/28.%20Find%20the%20Index%20of%20the%20First%20Occurrence%20in%20a%20String/strStr.ts
365333
[68]: ./src/page-1/68.%20Text%20Justification/fullJustify.ts
@@ -609,7 +577,191 @@ Must-do List for Interview Prep
609577
| Backtracking | | |
610578
| ----------------------------------------- | -------------- | ------ |
611579
| 17. Letter Combinations of a Phone Number | [Solution][17] | Medium |
580+
| 22. Generate Parentheses | Solution | Medium |
581+
| 39. Combination Sum | Solution | Medium |
582+
| 46. Permutations | Solution | Medium |
583+
| 51. N-Queens | Solution | Hard |
584+
| 78. Subsets | Solution | Medium |
585+
| 79. Word Search | Solution | Medium |
586+
| 131. Palindrome Partitioning | Solution | Medium |
612587

613588
[17]: ./src/page-1/17.%20Letter%20Combinations%20of%20a%20Phone%20Number/letterCombinations.ts
614589

590+
| Binary Search | | |
591+
| ----------------------------------------------------------- | -------------- | ------ |
592+
| 4. Median of Two Sorted Arrays | [Solution][4] | Hard |
593+
| 33. Search in Rotated Sorted Array | Solution | Medium |
594+
| 34. Find First and Last Position of Element in Sorted Array | [Solution][34] | Medium |
595+
| 35. Search Insert Position | [Solution][35] | Easy |
596+
| 74. Search a 2D Matrix | Solution | Medium |
597+
| 124. Binary Tree Maximum Path Sum | Solution | Hard |
598+
| 153. Find Minimum in Rotated Sorted Array | Solution | Medium |
599+
600+
[4]: ./src/page-1/4.%20Median%20of%20Two%20Sorted%20Arrays/findMedianSortedArrays.ts
601+
[34]: ./src/page-1/34.%20Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array/searchRange.ts
602+
[35]: ./src/page-1/35.%20Search%20Insert%20Position/searchInsert.ts
603+
604+
| Binary Tree | | |
605+
| -------------------------------------------------------------- | --------------- | ------ |
606+
| 94. Binary Tree Inorder Traversal | [Solution][94] | Easy |
607+
| 98. Validate Binary Search Tree | Solution | Medium |
608+
| 101. Symmetric Tree | [Solution][101] | Easy |
609+
| 102. Binary Tree Level Order Traversal | Solution | Medium |
610+
| 104. Maximum Depth of Binary Tree | [Solution][104] | Easy |
611+
| 105. Construct Binary Tree from Preorder and Inorder Traversal | Solution | Medium |
612+
| 108. Convert Sorted Array to Binary Search Tree | [Solution][108] | Easy |
613+
| 114. Flatten Binary Tree to Linked List | Solution | Medium |
614+
| 199. Binary Tree Right Side View | [Solution][199] | Medium |
615+
| 226. Invert Binary Tree | Solution | Easy |
616+
| 230. Kth Smallest Element in a BST | Solution | Medium |
617+
| 236. Lowest Common Ancestor of a Binary Tree | [Solution][236] | Medium |
618+
| 437. Path Sum III | [Solution][437] | Medium |
619+
| 543. Diameter of Binary Tree | Solution | Easy |
620+
621+
[94]: ./src/page-1/94.%20Binary%20Tree%20Inorder%20Traversal/inorderTraversal.ts
622+
[101]: ./src/page-2/101.%20Symmetric%20Tree/isSymmetric.ts
623+
[104]: ./src/page-2/104.%20Maximum%20Depth%20of%20Binary%20Tree/maxDepth.ts
624+
[108]: ./src/page-2/108.%20Convert%20Sorted%20Array%20to%20Binary%20Search%20Tree/sortedArrayToBST.ts
625+
[199]: ./src/page-2/199.%20Binary%20Tree%20Right%20Side%20View/rightSideView.ts
626+
[236]: ./src/page-3/236.%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/lowestCommonAncestor.ts
627+
[437]: ./src/page-5/437.%20Path%20Sum%20III/pathSum.ts
628+
629+
| Dynamic Programming | | |
630+
| ----------------------------------- | --------------- | ------ |
631+
| 5. Longest Palindromic Substring | [Solution][5] | Medium |
632+
| 32. Longest Valid Parentheses | Solution | Hard |
633+
| 62. Unique Paths | Solution | Medium |
634+
| 64. Minimum Path Sum | Solution | Medium |
635+
| 70. Climbing Stairs | [Solution][70] | Easy |
636+
| 72. Edit Distance | Solution | Medium |
637+
| 118. Pascal's Triangle | [Solution][118] | Easy |
638+
| 139. Word Break | Solution | Medium |
639+
| 152. Maximum Product Subarray | Solution | Medium |
640+
| 198. House Robber | Solution | Medium |
641+
| 279. Perfect Squares | Solution | Medium |
642+
| 300. Longest Increasing Subsequence | Solution | Medium |
643+
| 322. Coin Change | Solution | Medium |
644+
| 416. Partition Equal Subset Sum | Solution | Medium |
645+
| 1143. Longest Common Subsequence | Solution | Medium |
646+
647+
[5]: ./src/page-1/5.%20Longest%20Palindromic%20Substring/longestPalindrome.ts
648+
[70]: ./src/page-1/70.%20Climbing%20Stairs/climbStairs.ts
649+
[118]: ./src/page-2/118.%20Pascal's%20Triangle/generate.ts
650+
651+
| Graph | | |
652+
| ---------------------- | --------------- | ------ |
653+
| 200. Number of Islands | Solution | Medium |
654+
| 207. Course Schedule | Solution | Medium |
655+
| 994. Rotting Oranges | [Solution][994] | Medium |
656+
657+
[994]: ./src/page-10/994.%20Rotting%20Oranges/orangesRotting.ts
658+
659+
| Greedy | | |
660+
| ------------------------------------ | -------- | ------ |
661+
| 45. Jump Game II | Solution | Medium |
662+
| 55. Jump Game | Solution | Medium |
663+
| 121. Best Time to Buy and Sell Stock | Solution | Easy |
664+
| 763. Partition Labels | Solution | Medium |
665+
666+
| Hashing | | |
667+
| --------------------------------- | ------------- | ------ |
668+
| 1. Two Sum | [Solution][1] | Easy |
669+
| 49. Group Anagrams | Solution | Medium |
670+
| 128. Longest Consecutive Sequence | Solution | Medium |
671+
| 560. Subarray Sum Equals K | Solution | Medium |
672+
673+
[1]: ./src/page-1/1.%20Two%20Sum/twoSum.ts
674+
675+
| Heap | | |
676+
| ------------------------------------ | --------------- | ------ |
677+
| 215. Kth Largest Element in an Array | [Solution][215] | Medium |
678+
| 295. Find Median from Data Stream | Solution | Hard |
679+
| 347. Top K Frequent Elements | Solution | Medium |
680+
681+
[215]: ./src/page-2/215.%20Kth%20Largest%20Element%20in%20an%20Array/findKthLargest.ts
682+
683+
| Linked Lists | | |
684+
| ------------------------------------- | --------------- | ------ |
685+
| 2. Add Two Numbers | [Solution][2] | Medium |
686+
| 19. Remove Nth Node From End of List | Solution | Medium |
687+
| 21. Merge Two Sorted Lists | [Solution][21] | Easy |
688+
| 23. Merge k Sorted Lists | Solution | Hard |
689+
| 24. Swap Nodes in Pairs | Solution | Medium |
690+
| 25. Reverse Nodes in k-Group | Solution | Hard |
691+
| 138. Copy List with Random Pointer | Solution | Medium |
692+
| 141. Linked List Cycle | [Solution][141] | Easy |
693+
| 142. Linked List Cycle II | Solution | Medium |
694+
| 146. LRU Cache | Solution | Medium |
695+
| 148. Sort List | Solution | Medium |
696+
| 160. Intersection of Two Linked Lists | Solution | Easy |
697+
| 206. Reverse Linked List | [Solution][206] | Easy |
698+
| 234. Palindrome Linked List | Solution | Easy |
699+
700+
[2]: ./src/page-1/2.%20Add%20Two%20Numbers/addTwoNumbers.ts
701+
[21]: ./src/page-1/21.%20Merge%20Two%20Sorted%20Lists/mergeTwoLists.ts
702+
[141]: ./src/page-2/141.%20Linked%20List%20Cycle/hasCycle.ts
703+
[206]: ./src/page-2/206.%20Reverse%20Linked%20List/reverseList.ts
704+
705+
| Matrix | | |
706+
| -------------------------- | -------------- | ------ |
707+
| 48. Rotate Image | Solution | Medium |
708+
| 54. Spiral Matrix | [Solution][54] | Medium |
709+
| 73. Set Matrix Zeroes | Solution | Medium |
710+
| 240. Search a 2D Matrix II | Solution | Medium |
711+
712+
[54]: ./src/page-1/54.%20Spiral%20Matrix/spiralOrder.ts
713+
714+
| Sliding Window | | |
715+
| ------------------------------------------------- | -------- | ------ |
716+
| 3. Longest Substring Without Repeating Characters | Solution | Medium |
717+
| 76. Minimum Window Substring | Solution | Hard |
718+
| 239. Sliding Window Maximum | Solution | Hard |
719+
| 438. Find All Anagrams in a String | Solution | Medium |
720+
721+
[3]: ./src/page-1/3.%20Longest%20Substring%20Without%20Repeating%20Characters/lengthOfLongestSubstring.ts
722+
723+
| Stack | | |
724+
| ---------------------------------- | --------------- | ------ |
725+
| 20. Valid Parentheses | [Solution][20] | Easy |
726+
| 84. Largest Rectangle in Histogram | Solution | Hard |
727+
| 155. Min Stack | [Solution][155] | Medium |
728+
| 394. Decode String | [Solution][394] | Medium |
729+
| 739. Daily Temperatures | Solution | Medium |
730+
731+
[20]: ./src/page-1/20.%20Valid%20Parentheses/isValid.ts
732+
[155]: ./src/page-2/155.%20Min%20Stack/MinStack.ts
733+
[394]: ./src/page-4/394.%20Decode%20String/decodeString.ts
734+
735+
| Two Pointers | | |
736+
| ----------------------------- | --------------- | ------ |
737+
| 11. Container With Most Water | [Solution][11] | Medium |
738+
| 15. 3Sum | Solution | Medium |
739+
| 42. Trapping Rain Water | Solution | Hard |
740+
| 283. Move Zeroes | [Solution][283] | Easy |
741+
742+
[11]: ./src/page-1/11.%20Container%20With%20Most%20Water/maxArea.ts
743+
[283]: ./src/page-3/283.%20Move%20Zeroes/moveZeroes.ts
744+
745+
| Trie | | |
746+
| --------------------------------- | -------- | ------ |
747+
| 208. Implement Trie (Prefix Tree) | Solution | Medium |
748+
749+
| Misc | | |
750+
| --------------------------------- | --------------- | ------ |
751+
| 31. Next Permutation | Solution | Medium |
752+
| 41. First Missing Positive | Solution | Hard |
753+
| 53. Maximum Subarray | [Solution][53] | Medium |
754+
| 56. Merge Intervals | Solution | Medium |
755+
| 75. Sort Colors | Solution | Medium |
756+
| 136. Single Number | [Solution][136] | Easy |
757+
| 169. Majority Element | Solution | Easy |
758+
| 189. Rotate Array | [Solution][189] | Medium |
759+
| 238. Product of Array Except Self | [Solution][238] | Medium |
760+
| 287. Find the Duplicate Number | Solution | Medium |
761+
762+
[53]: ./src/page-1/53.%20Maximum%20Subarray/maxSubArray.ts
763+
[136]: ./src/page-2/136.%20Single%20Number/singleNumber.ts
764+
[189]: ./src/page-2/189.%20Rotate%20Array/rotate.ts
765+
[238]: ./src/page-3/238.%20Product%20of%20Array%20Except%20Self/productExceptSelf.ts
766+
615767
</details>

algorithms/dynamic-programming/README.md

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# 動態規劃 (Dynamic Programming)
22

3-
費波那契數:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...
3+
費波那契數:
4+
5+
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...
46

57
```ts
68
F[0] = 0;
@@ -15,7 +17,7 @@ function fibonacci(n: number): number {
1517
}
1618
```
1719

18-
一維動態規劃 (1D Dynamic Programming)
20+
費波那契數用一維動態規劃 (1D Dynamic Programming)
1921

2022
```ts
2123
function fibonacci(n: number): number {
@@ -29,6 +31,63 @@ function fibonacci(n: number): number {
2931
}
3032
```
3133

34+
爬樓梯:
35+
36+
```
37+
___
38+
___| 5
39+
___| 4
40+
___| 3
41+
___| 2
42+
___| 1
43+
0
44+
```
45+
46+
爬到第 n 個階梯,每次爬 1 階或 2 階:
47+
48+
爬到第 1 階,共有 1 種方式:
49+
50+
- 0 -> 1
51+
52+
`[1]`
53+
54+
爬到第 2 階,共有 2 種方式:
55+
56+
- 0 -> 1 -> 2
57+
- 0    -> 2
58+
59+
`[1, 2]`
60+
61+
爬到第 3 階,共有 3 種方式:
62+
63+
- 0 -> 1 -> 2 -> 3
64+
- 0    -> 2 -> 3
65+
- 0 -> 1    -> 3
66+
67+
`[1, 2, 3]`
68+
69+
爬到第 4 階,共有 5 種方式:
70+
71+
- 0 -> 1 -> 2 -> 3 -> 4
72+
- 0    -> 2 -> 3 -> 4
73+
- 0 -> 1    -> 3 -> 4
74+
- 0 -> 1 -> 2    -> 4
75+
- 0    -> 2    -> 4
76+
77+
`[1, 2, 3, 5]`
78+
79+
```ts
80+
function climbStairs(n: number): number {
81+
const dp = [1, 2];
82+
83+
for (let i = 2; i < n; i++) {
84+
dp[i] = dp[i - 1] + dp[i - 2];
85+
}
86+
87+
return dp[n - 1];
88+
}
89+
```
90+
3291
## 0-1 背包問題 (0-1 Knapsack Problem)
3392

3493
問題:給定 n 個物品,每個物品有一定的重量和價值,並給定一個背包的容量。你需要在不超過背包容量的情況下選擇物品,使得所選物品的總價值最大。

0 commit comments

Comments
 (0)