Skip to content

Commit

Permalink
228th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Oct 18, 2024
1 parent 557f98e commit 363e073
Show file tree
Hide file tree
Showing 2 changed files with 249 additions and 38 deletions.
224 changes: 188 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,6 @@ $ pnpm test <FILE_NAME>
$ pnpm test twoSum.test.ts
```

4. Run benchmarks

```sh
$ pnpm bench

# run a specific bench file
$ pnpm bench <FILE_NAME>
# e.g.
$ pnpm bench twoSum.bench.ts
```

## Directory Structure

```ts
.
├── .github/workflows/ci.yaml
├── src
│ ├── page-<NUM> -> set the table to display 100 rows per page
│ │ └── <TITLE>
│ │ ├── <NAME>.spec.ts
│ │ └── <NAME>.ts
│ └── utils
├── .editorconfig
├── .gitignore
├── biome.json
├── compose.yaml
├── Dockerfile
├── package.json
├── pnpm-lock.yaml
├── README.md
├── tsconfig.json
└── vite.config.ts
```

## Beginning - Algorithms

解題前應該學會的思路與技巧
Expand Down Expand Up @@ -340,15 +306,15 @@ Must-do List for Interview Prep
| 45. Jump Game II | Solution | Medium |
| 274. H-Index | Solution | Medium |
| 380. Insert Delete GetRandom O(1) | Solution | Medium |
| 238. Product of Array Except Self | Solution | Medium |
| 238. Product of Array Except Self | [Solution][238] | Medium |
| 134. Gas Station | Solution | Medium |
| 135. Candy | Solution | Hard |
| 42. Trapping Rain Water | Solution | Hard |
| 13. Roman to Integer | [Solution][13] | Easy |
| 12. Integer to Roman | Solution | Medium |
| 58. Length of Last Word | [Solution][58] | Easy |
| 14. Longest Common Prefix | [Solution][14] | Easy |
| 151. Reverse Words in a String | Solution | Medium |
| 151. Reverse Words in a String | [Solution][151] | Medium |
| 6. Zigzag Conversion | [Solution][6] | Medium |
| 28. Find the Index of the First Occurrence in a String | [Solution][28] | Easy |
| 68. Text Justification | [Solution][68] | Hard |
Expand All @@ -357,9 +323,11 @@ Must-do List for Interview Prep
[27]: ./src/page-1/27.%20Remove%20Element/removeElement.ts
[26]: ./src/page-1/26.%20Remove%20Duplicates%20from%20Sorted%20Array/removeDuplicates.ts
[189]: ./src/page-2/189.%20Rotate%20Array/rotate.ts
[238]: ./src/page-3/238.%20Product%20of%20Array%20Except%20Self/productExceptSelf.ts
[13]: ./src/page-1/13.%20Roman%20to%20Integer/romanToInt.ts
[58]: ./src/page-1/58.%20Length%20of%20Last%20Word/lengthOfLastWord.ts
[14]: ./src/page-1/14.%20Longest%20Common%20Prefix/longestCommonPrefix.ts
[151]: ./src/page-2/151.%20Reverse%20Words%20in%20a%20String/reverseWords.ts
[6]: ./src/page-1/6.%20Zigzag%20Conversion/convert.ts
[28]: ./src/page-1/28.%20Find%20the%20Index%20of%20the%20First%20Occurrence%20in%20a%20String/strStr.ts
[68]: ./src/page-1/68.%20Text%20Justification/fullJustify.ts
Expand Down Expand Up @@ -609,7 +577,191 @@ Must-do List for Interview Prep
| Backtracking | | |
| ----------------------------------------- | -------------- | ------ |
| 17. Letter Combinations of a Phone Number | [Solution][17] | Medium |
| 22. Generate Parentheses | Solution | Medium |
| 39. Combination Sum | Solution | Medium |
| 46. Permutations | Solution | Medium |
| 51. N-Queens | Solution | Hard |
| 78. Subsets | Solution | Medium |
| 79. Word Search | Solution | Medium |
| 131. Palindrome Partitioning | Solution | Medium |

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

| Binary Search | | |
| ----------------------------------------------------------- | -------------- | ------ |
| 4. Median of Two Sorted Arrays | [Solution][4] | Hard |
| 33. Search in Rotated Sorted Array | Solution | Medium |
| 34. Find First and Last Position of Element in Sorted Array | [Solution][34] | Medium |
| 35. Search Insert Position | [Solution][35] | Easy |
| 74. Search a 2D Matrix | Solution | Medium |
| 124. Binary Tree Maximum Path Sum | Solution | Hard |
| 153. Find Minimum in Rotated Sorted Array | Solution | Medium |

[4]: ./src/page-1/4.%20Median%20of%20Two%20Sorted%20Arrays/findMedianSortedArrays.ts
[34]: ./src/page-1/34.%20Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array/searchRange.ts
[35]: ./src/page-1/35.%20Search%20Insert%20Position/searchInsert.ts

| Binary Tree | | |
| -------------------------------------------------------------- | --------------- | ------ |
| 94. Binary Tree Inorder Traversal | [Solution][94] | Easy |
| 98. Validate Binary Search Tree | Solution | Medium |
| 101. Symmetric Tree | [Solution][101] | Easy |
| 102. Binary Tree Level Order Traversal | Solution | Medium |
| 104. Maximum Depth of Binary Tree | [Solution][104] | Easy |
| 105. Construct Binary Tree from Preorder and Inorder Traversal | Solution | Medium |
| 108. Convert Sorted Array to Binary Search Tree | [Solution][108] | Easy |
| 114. Flatten Binary Tree to Linked List | Solution | Medium |
| 199. Binary Tree Right Side View | [Solution][199] | Medium |
| 226. Invert Binary Tree | Solution | Easy |
| 230. Kth Smallest Element in a BST | Solution | Medium |
| 236. Lowest Common Ancestor of a Binary Tree | [Solution][236] | Medium |
| 437. Path Sum III | [Solution][437] | Medium |
| 543. Diameter of Binary Tree | Solution | Easy |

[94]: ./src/page-1/94.%20Binary%20Tree%20Inorder%20Traversal/inorderTraversal.ts
[101]: ./src/page-2/101.%20Symmetric%20Tree/isSymmetric.ts
[104]: ./src/page-2/104.%20Maximum%20Depth%20of%20Binary%20Tree/maxDepth.ts
[108]: ./src/page-2/108.%20Convert%20Sorted%20Array%20to%20Binary%20Search%20Tree/sortedArrayToBST.ts
[199]: ./src/page-2/199.%20Binary%20Tree%20Right%20Side%20View/rightSideView.ts
[236]: ./src/page-3/236.%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/lowestCommonAncestor.ts
[437]: ./src/page-5/437.%20Path%20Sum%20III/pathSum.ts

| Dynamic Programming | | |
| ----------------------------------- | --------------- | ------ |
| 5. Longest Palindromic Substring | [Solution][5] | Medium |
| 32. Longest Valid Parentheses | Solution | Hard |
| 62. Unique Paths | Solution | Medium |
| 64. Minimum Path Sum | Solution | Medium |
| 70. Climbing Stairs | [Solution][70] | Easy |
| 72. Edit Distance | Solution | Medium |
| 118. Pascal's Triangle | [Solution][118] | Easy |
| 139. Word Break | Solution | Medium |
| 152. Maximum Product Subarray | Solution | Medium |
| 198. House Robber | Solution | Medium |
| 279. Perfect Squares | Solution | Medium |
| 300. Longest Increasing Subsequence | Solution | Medium |
| 322. Coin Change | Solution | Medium |
| 416. Partition Equal Subset Sum | Solution | Medium |
| 1143. Longest Common Subsequence | Solution | Medium |

[5]: ./src/page-1/5.%20Longest%20Palindromic%20Substring/longestPalindrome.ts
[70]: ./src/page-1/70.%20Climbing%20Stairs/climbStairs.ts
[118]: ./src/page-2/118.%20Pascal's%20Triangle/generate.ts

| Graph | | |
| ---------------------- | --------------- | ------ |
| 200. Number of Islands | Solution | Medium |
| 207. Course Schedule | Solution | Medium |
| 994. Rotting Oranges | [Solution][994] | Medium |

[994]: ./src/page-10/994.%20Rotting%20Oranges/orangesRotting.ts

| Greedy | | |
| ------------------------------------ | -------- | ------ |
| 45. Jump Game II | Solution | Medium |
| 55. Jump Game | Solution | Medium |
| 121. Best Time to Buy and Sell Stock | Solution | Easy |
| 763. Partition Labels | Solution | Medium |

| Hashing | | |
| --------------------------------- | ------------- | ------ |
| 1. Two Sum | [Solution][1] | Easy |
| 49. Group Anagrams | Solution | Medium |
| 128. Longest Consecutive Sequence | Solution | Medium |
| 560. Subarray Sum Equals K | Solution | Medium |

[1]: ./src/page-1/1.%20Two%20Sum/twoSum.ts

| Heap | | |
| ------------------------------------ | --------------- | ------ |
| 215. Kth Largest Element in an Array | [Solution][215] | Medium |
| 295. Find Median from Data Stream | Solution | Hard |
| 347. Top K Frequent Elements | Solution | Medium |

[215]: ./src/page-2/215.%20Kth%20Largest%20Element%20in%20an%20Array/findKthLargest.ts

| Linked Lists | | |
| ------------------------------------- | --------------- | ------ |
| 2. Add Two Numbers | [Solution][2] | Medium |
| 19. Remove Nth Node From End of List | Solution | Medium |
| 21. Merge Two Sorted Lists | [Solution][21] | Easy |
| 23. Merge k Sorted Lists | Solution | Hard |
| 24. Swap Nodes in Pairs | Solution | Medium |
| 25. Reverse Nodes in k-Group | Solution | Hard |
| 138. Copy List with Random Pointer | Solution | Medium |
| 141. Linked List Cycle | [Solution][141] | Easy |
| 142. Linked List Cycle II | Solution | Medium |
| 146. LRU Cache | Solution | Medium |
| 148. Sort List | Solution | Medium |
| 160. Intersection of Two Linked Lists | Solution | Easy |
| 206. Reverse Linked List | [Solution][206] | Easy |
| 234. Palindrome Linked List | Solution | Easy |

[2]: ./src/page-1/2.%20Add%20Two%20Numbers/addTwoNumbers.ts
[21]: ./src/page-1/21.%20Merge%20Two%20Sorted%20Lists/mergeTwoLists.ts
[141]: ./src/page-2/141.%20Linked%20List%20Cycle/hasCycle.ts
[206]: ./src/page-2/206.%20Reverse%20Linked%20List/reverseList.ts

| Matrix | | |
| -------------------------- | -------------- | ------ |
| 48. Rotate Image | Solution | Medium |
| 54. Spiral Matrix | [Solution][54] | Medium |
| 73. Set Matrix Zeroes | Solution | Medium |
| 240. Search a 2D Matrix II | Solution | Medium |

[54]: ./src/page-1/54.%20Spiral%20Matrix/spiralOrder.ts

| Sliding Window | | |
| ------------------------------------------------- | -------- | ------ |
| 3. Longest Substring Without Repeating Characters | Solution | Medium |
| 76. Minimum Window Substring | Solution | Hard |
| 239. Sliding Window Maximum | Solution | Hard |
| 438. Find All Anagrams in a String | Solution | Medium |

[3]: ./src/page-1/3.%20Longest%20Substring%20Without%20Repeating%20Characters/lengthOfLongestSubstring.ts

| Stack | | |
| ---------------------------------- | --------------- | ------ |
| 20. Valid Parentheses | [Solution][20] | Easy |
| 84. Largest Rectangle in Histogram | Solution | Hard |
| 155. Min Stack | [Solution][155] | Medium |
| 394. Decode String | [Solution][394] | Medium |
| 739. Daily Temperatures | Solution | Medium |

[20]: ./src/page-1/20.%20Valid%20Parentheses/isValid.ts
[155]: ./src/page-2/155.%20Min%20Stack/MinStack.ts
[394]: ./src/page-4/394.%20Decode%20String/decodeString.ts

| Two Pointers | | |
| ----------------------------- | --------------- | ------ |
| 11. Container With Most Water | [Solution][11] | Medium |
| 15. 3Sum | Solution | Medium |
| 42. Trapping Rain Water | Solution | Hard |
| 283. Move Zeroes | [Solution][283] | Easy |

[11]: ./src/page-1/11.%20Container%20With%20Most%20Water/maxArea.ts
[283]: ./src/page-3/283.%20Move%20Zeroes/moveZeroes.ts

| Trie | | |
| --------------------------------- | -------- | ------ |
| 208. Implement Trie (Prefix Tree) | Solution | Medium |

| Misc | | |
| --------------------------------- | --------------- | ------ |
| 31. Next Permutation | Solution | Medium |
| 41. First Missing Positive | Solution | Hard |
| 53. Maximum Subarray | [Solution][53] | Medium |
| 56. Merge Intervals | Solution | Medium |
| 75. Sort Colors | Solution | Medium |
| 136. Single Number | [Solution][136] | Easy |
| 169. Majority Element | Solution | Easy |
| 189. Rotate Array | [Solution][189] | Medium |
| 238. Product of Array Except Self | [Solution][238] | Medium |
| 287. Find the Duplicate Number | Solution | Medium |

[53]: ./src/page-1/53.%20Maximum%20Subarray/maxSubArray.ts
[136]: ./src/page-2/136.%20Single%20Number/singleNumber.ts
[189]: ./src/page-2/189.%20Rotate%20Array/rotate.ts
[238]: ./src/page-3/238.%20Product%20of%20Array%20Except%20Self/productExceptSelf.ts

</details>
63 changes: 61 additions & 2 deletions algorithms/dynamic-programming/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# 動態規劃 (Dynamic Programming)

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

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...

```ts
F[0] = 0;
Expand All @@ -15,7 +17,7 @@ function fibonacci(n: number): number {
}
```

一維動態規劃 (1D Dynamic Programming)
費波那契數用一維動態規劃 (1D Dynamic Programming)

```ts
function fibonacci(n: number): number {
Expand All @@ -29,6 +31,63 @@ function fibonacci(n: number): number {
}
```

爬樓梯:

```
___
___| 5
___| 4
___| 3
___| 2
___| 1
0
```

爬到第 n 個階梯,每次爬 1 階或 2 階:

爬到第 1 階,共有 1 種方式:

- 0 -> 1

`[1]`

爬到第 2 階,共有 2 種方式:

- 0 -> 1 -> 2
- 0    -> 2

`[1, 2]`

爬到第 3 階,共有 3 種方式:

- 0 -> 1 -> 2 -> 3
- 0    -> 2 -> 3
- 0 -> 1    -> 3

`[1, 2, 3]`

爬到第 4 階,共有 5 種方式:

- 0 -> 1 -> 2 -> 3 -> 4
- 0    -> 2 -> 3 -> 4
- 0 -> 1    -> 3 -> 4
- 0 -> 1 -> 2    -> 4
- 0    -> 2    -> 4

`[1, 2, 3, 5]`

```ts
function climbStairs(n: number): number {
const dp = [1, 2];

for (let i = 2; i < n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}

return dp[n - 1];
}
```

## 0-1 背包問題 (0-1 Knapsack Problem)

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

0 comments on commit 363e073

Please sign in to comment.