From 5de5051b950fb29f22a691e4d316897eb1700162 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Mon, 30 Dec 2024 10:01:02 +0800 Subject: [PATCH] 259th Commit --- README.md | 674 +++++++++--------- src/page-1/72. Edit Distance/README.md | 112 +++ .../72. Edit Distance/minDistance.test.ts | 8 + src/page-1/72. Edit Distance/minDistance.ts | 36 + .../1768. Merge Strings Alternately/README.md | 6 +- .../README.md | 20 + 6 files changed, 519 insertions(+), 337 deletions(-) create mode 100644 src/page-1/72. Edit Distance/README.md create mode 100644 src/page-1/72. Edit Distance/minDistance.test.ts create mode 100644 src/page-1/72. Edit Distance/minDistance.ts diff --git a/README.md b/README.md index 66a2fca..a4721f5 100644 --- a/README.md +++ b/README.md @@ -257,12 +257,14 @@ Problems: | 62. Unique Paths | [Solution][62] | Medium | 詳解 | | 1143. Longest Common Subsequence | [Solution][1143] | Medium | 詳解 | | 714. Best Time to Buy and Sell Stock with Transaction Fee | [Solution][714] | Medium | [詳解][714-explanation] | -| 72. Edit Distance | Solution | Medium | 詳解 | +| 72. Edit Distance | [Solution][72] | Medium | [詳解][72-explanation] | [62]: ./src/page-1/62.%20Unique%20Paths/uniquePaths.ts [1143]: ./src/page-11/1143.%20Longest%20Common%20Subsequence/longestCommonSubsequence.ts [714]: ./src/page-7/714.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Transaction%20Fee/maxProfit.ts [714-explanation]: ./src/page-7/714.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Transaction%20Fee/README.md +[72]: ./src/page-1/72.%20Edit%20Distance/minDistance.ts +[72-explanation]: ./src/page-1/72.%20Edit%20Distance/README.md | Bit Manipulation | | | | | --------------------------------------------- | ---------------- | ------ | ---- | @@ -308,32 +310,32 @@ Must-do List for Interview Prep Problems: -| Array / String | | | -| ------------------------------------------------------ | --------------- | ------ | -| 88. Merge Sorted Array | [Solution][88] | Easy | -| 27. Remove Element | [Solution][27] | Easy | -| 26. Remove Duplicates from Sorted Array | [Solution][26] | Easy | -| 80. Remove Duplicates from Sorted Array II | Solution | Medium | -| 169. Majority Element | Solution | Easy | -| 189. Rotate Array | [Solution][189] | Medium | -| 121. Best Time to Buy and Sell Stock | Solution | Easy | -| 122. Best Time to Buy and Sell Stock II | Solution | Medium | -| 55. Jump Game | Solution | Medium | -| 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][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][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 | +| Array / String | | | | +| ------------------------------------------------------ | --------------- | ------ | ---- | +| 88. Merge Sorted Array | [Solution][88] | Easy | 詳解 | +| 27. Remove Element | [Solution][27] | Easy | 詳解 | +| 26. Remove Duplicates from Sorted Array | [Solution][26] | Easy | 詳解 | +| 80. Remove Duplicates from Sorted Array II | Solution | Medium | 詳解 | +| 169. Majority Element | Solution | Easy | 詳解 | +| 189. Rotate Array | [Solution][189] | Medium | 詳解 | +| 121. Best Time to Buy and Sell Stock | Solution | Easy | 詳解 | +| 122. Best Time to Buy and Sell Stock II | Solution | Medium | 詳解 | +| 55. Jump Game | Solution | Medium | 詳解 | +| 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][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][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 | 詳解 | [88]: ./src/page-1//88.%20Merge%20Sorted%20Array/merge.ts [27]: ./src/page-1/27.%20Remove%20Element/removeElement.ts @@ -348,244 +350,246 @@ Problems: [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 -| Two Pointers | | | -| --------------------------------------- | -------- | ------ | -| 125. Valid Palindrome | Solution | Easy | -| 392. Is Subsequence | Solution | Easy | -| 167. Two Sum II - Input Array Is Sorted | Solution | Medium | -| 11. Container With Most Water | Solution | Medium | -| 15. 3Sum | Solution | Medium | - -| Sliding Window | | | -| ------------------------------------------------- | ------------- | ------ | -| 209. Minimum Size Subarray Sum | Solution | Medium | -| 3. Longest Substring Without Repeating Characters | [Solution][3] | Medium | -| 30. Substring with Concatenation of All Words | Solution | Hard | -| 76. Minimum Window Substring | Solution | Hard | +| Two Pointers | | | | +| --------------------------------------- | -------- | ------ | ---- | +| 125. Valid Palindrome | Solution | Easy | 詳解 | +| 392. Is Subsequence | Solution | Easy | 詳解 | +| 167. Two Sum II - Input Array Is Sorted | Solution | Medium | 詳解 | +| 11. Container With Most Water | Solution | Medium | 詳解 | +| 15. 3Sum | Solution | Medium | 詳解 | + +| Sliding Window | | | | +| ------------------------------------------------- | ------------- | ------ | ---- | +| 209. Minimum Size Subarray Sum | Solution | Medium | 詳解 | +| 3. Longest Substring Without Repeating Characters | [Solution][3] | Medium | 詳解 | +| 30. Substring with Concatenation of All Words | Solution | Hard | 詳解 | +| 76. Minimum Window Substring | Solution | Hard | 詳解 | [3]: ./src/page-1/3.%20Longest%20Substring%20Without%20Repeating%20Characters/lengthOfLongestSubstring.ts -| Matrix | | | -| --------------------- | -------- | ------ | -| 36. Valid Sudoku | Solution | Medium | -| 54. Spiral Matrix | Solution | Medium | -| 48. Rotate Image | Solution | Medium | -| 73. Set Matrix Zeroes | Solution | Medium | -| 289. Game of Life | Solution | Medium | - -| Hashmap | | | -| --------------------------------- | ------------- | ------ | -| 383. Ransom Note | Solution | Easy | -| 205. Isomorphic Strings | Solution | Easy | -| 290. Word Pattern | Solution | Easy | -| 242. Valid Anagram | Solution | Easy | -| 49. Group Anagrams | Solution | Medium | -| 1. Two Sum | [Solution][1] | Easy | -| 202. Happy Number | Solution | Easy | -| 219. Contains Duplicate II | Solution | Easy | -| 128. Longest Consecutive Sequence | Solution | Medium | +| Matrix | | | | +| --------------------- | -------- | ------ | ---- | +| 36. Valid Sudoku | Solution | Medium | 詳解 | +| 54. Spiral Matrix | Solution | Medium | 詳解 | +| 48. Rotate Image | Solution | Medium | 詳解 | +| 73. Set Matrix Zeroes | Solution | Medium | 詳解 | +| 289. Game of Life | Solution | Medium | 詳解 | + +| Hashmap | | | | +| --------------------------------- | ------------- | ------ | ---- | +| 383. Ransom Note | Solution | Easy | 詳解 | +| 205. Isomorphic Strings | Solution | Easy | 詳解 | +| 290. Word Pattern | Solution | Easy | 詳解 | +| 242. Valid Anagram | Solution | Easy | 詳解 | +| 49. Group Anagrams | Solution | Medium | 詳解 | +| 1. Two Sum | [Solution][1] | Easy | 詳解 | +| 202. Happy Number | Solution | Easy | 詳解 | +| 219. Contains Duplicate II | Solution | Easy | 詳解 | +| 128. Longest Consecutive Sequence | Solution | Medium | 詳解 | [1]: ./src/page-1/1.%20Two%20Sum/twoSum.ts -| Intervals | | | -| ----------------------------------------------- | --------------- | ------ | -| 228. Summary Ranges | Solution | Easy | -| 56. Merge Intervals | Solution | Medium | -| 57. Insert Interval | Solution | Medium | -| 452. Minimum Number of Arrows to Burst Balloons | [Solution][452] | Medium | +| Intervals | | | | +| ----------------------------------------------- | --------------- | ------ | ---- | +| 228. Summary Ranges | Solution | Easy | 詳解 | +| 56. Merge Intervals | Solution | Medium | 詳解 | +| 57. Insert Interval | Solution | Medium | 詳解 | +| 452. Minimum Number of Arrows to Burst Balloons | [Solution][452] | Medium | 詳解 | [452]: ./src/page-5/452.%20Minimum%20Number%20of%20Arrows%20to%20Burst%20Balloons/findMinArrowShots.ts -| Stack | | | -| ------------------------------------- | --------------- | ------ | -| 20. Valid Parentheses | [Solution][20] | Easy | -| 71. Simplify Path | Solution | Medium | -| 155. Min Stack | [Solution][155] | Medium | -| 150. Evaluate Reverse Polish Notation | Solution | Medium | -| 224. Basic Calculator | Solution | Hard | +| Stack | | | | +| ------------------------------------- | --------------- | ------ | ---- | +| 20. Valid Parentheses | [Solution][20] | Easy | 詳解 | +| 71. Simplify Path | Solution | Medium | 詳解 | +| 155. Min Stack | [Solution][155] | Medium | 詳解 | +| 150. Evaluate Reverse Polish Notation | Solution | Medium | 詳解 | +| 224. Basic Calculator | Solution | Hard | 詳解 | [20]: ./src/page-1/20.%20Valid%20Parentheses/isValid.ts [155]: ./src/page-2/155.%20Min%20Stack/MinStack.ts -| Linked List | | | -| ----------------------------------------- | --------------- | ------ | -| 141. Linked List Cycle | [Solution][141] | Easy | -| 2. Add Two Numbers | [Solution][2] | Medium | -| 21. Merge Two Sorted Lists | [Solution][21] | Easy | -| 138. Copy List with Random Pointer | Solution | Medium | -| 92. Reverse Linked List II | Solution | Medium | -| 25. Reverse Nodes in k-Group | Solution | Hard | -| 19. Remove Nth Node From End of List | Solution | Medium | -| 82. Remove Duplicates from Sorted List II | Solution | Medium | -| 61. Rotate List | Solution | Medium | -| 86. Partition List | Solution | Medium | -| 146. LRU Cache | Solution | Medium | +| Linked List | | | | +| ----------------------------------------- | --------------- | ------ | ---- | +| 141. Linked List Cycle | [Solution][141] | Easy | 詳解 | +| 2. Add Two Numbers | [Solution][2] | Medium | 詳解 | +| 21. Merge Two Sorted Lists | [Solution][21] | Easy | 詳解 | +| 138. Copy List with Random Pointer | Solution | Medium | 詳解 | +| 92. Reverse Linked List II | Solution | Medium | 詳解 | +| 25. Reverse Nodes in k-Group | Solution | Hard | 詳解 | +| 19. Remove Nth Node From End of List | Solution | Medium | 詳解 | +| 82. Remove Duplicates from Sorted List II | Solution | Medium | 詳解 | +| 61. Rotate List | Solution | Medium | 詳解 | +| 86. Partition List | Solution | Medium | 詳解 | +| 146. LRU Cache | Solution | Medium | 詳解 | [141]: ./src/page-2/141.%20Linked%20List%20Cycle/hasCycle.ts [2]: ./src/page-1/2.%20Add%20Two%20Numbers/addTwoNumbers.ts [21]: ./src/page-1/21.%20Merge%20Two%20Sorted%20Lists/mergeTwoLists.ts -| Binary Tree General | | | -| --------------------------------------------------------------- | --------------- | ------ | -| 104. Maximum Depth of Binary Tree | [Solution][104] | Easy | -| 100. Same Tree | [Solution][100] | Easy | -| 226. Invert Binary Tree | Solution | Easy | -| 101. Symmetric Tree | [Solution][101] | Easy | -| 105. Construct Binary Tree from Preorder and Inorder Traversal | Solution | Medium | -| 106. Construct Binary Tree from Inorder and Postorder Traversal | Solution | Medium | -| 117. Populating Next Right Pointers in Each Node II | Solution | Medium | -| 114. Flatten Binary Tree to Linked List | Solution | Medium | -| 112. Path Sum | [Solution][112] | Easy | -| 129. Sum Root to Leaf Numbers | Solution | Medium | -| 124. Binary Tree Maximum Path Sum | Solution | Hard | -| 173. Binary Search Tree Iterator | Solution | Medium | -| 222. Count Complete Tree Nodes | Solution | Easy | -| 236. Lowest Common Ancestor of a Binary Tree | Solution | Medium | +| Binary Tree General | | | | +| --------------------------------------------------------------- | --------------- | ------ | ---- | +| 104. Maximum Depth of Binary Tree | [Solution][104] | Easy | 詳解 | +| 100. Same Tree | [Solution][100] | Easy | 詳解 | +| 226. Invert Binary Tree | Solution | Easy | 詳解 | +| 101. Symmetric Tree | [Solution][101] | Easy | 詳解 | +| 105. Construct Binary Tree from Preorder and Inorder Traversal | Solution | Medium | 詳解 | +| 106. Construct Binary Tree from Inorder and Postorder Traversal | Solution | Medium | 詳解 | +| 117. Populating Next Right Pointers in Each Node II | Solution | Medium | 詳解 | +| 114. Flatten Binary Tree to Linked List | Solution | Medium | 詳解 | +| 112. Path Sum | [Solution][112] | Easy | 詳解 | +| 129. Sum Root to Leaf Numbers | Solution | Medium | 詳解 | +| 124. Binary Tree Maximum Path Sum | Solution | Hard | 詳解 | +| 173. Binary Search Tree Iterator | Solution | Medium | 詳解 | +| 222. Count Complete Tree Nodes | Solution | Easy | 詳解 | +| 236. Lowest Common Ancestor of a Binary Tree | Solution | Medium | 詳解 | [104]: ./src/page-2/104.%20Maximum%20Depth%20of%20Binary%20Tree/maxDepth.ts [100]: ./src/page-1/100.%20Same%20Tree/isSameTree.ts [101]: ./src/page-2/101.%20Symmetric%20Tree/isSymmetric.ts [112]: ./src/page-2/112.%20Path%20Sum/hasPathSum.ts -| Binary Tree BFS | | | -| --------------------------------------------- | -------- | ------ | -| 199. Binary Tree Right Side View | Solution | Medium | -| 637. Average of Levels in Binary Tree | Solution | Easy | -| 102. Binary Tree Level Order Traversal | Solution | Medium | -| 103. Binary Tree Zigzag Level Order Traversal | Solution | Medium | - -| Binary Search Tree | | | -| --------------------------------------- | -------- | ------ | -| 530. Minimum Absolute Difference in BST | Solution | Easy | -| 230. Kth Smallest Element in a BST | Solution | Medium | -| 98. Validate Binary Search Tree | Solution | Medium | - -| Graph General | | | -| ----------------------- | -------- | ------ | -| 200. Number of Islands | Solution | Medium | -| 130. Surrounded Regions | Solution | Medium | -| 133. Clone Graph | Solution | Medium | -| 399. Evaluate Division | Solution | Medium | -| 207. Course Schedule | Solution | Medium | -| 210. Course Schedule II | Solution | Medium | - -| Graph BFS | | | -| ----------------------------- | -------- | ------ | -| 909. Snakes and Ladders | Solution | Medium | -| 433. Minimum Genetic Mutation | Solution | Medium | -| 127. Word Ladder | Solution | Hard | - -| Trie | | | -| ----------------------------------------------- | --------------- | ------ | -| 208. Implement Trie (Prefix Tree) | [Solution][208] | Medium | -| 211. Design Add and Search Words Data Structure | Solution | Medium | -| 212. Word Search II | Solution | Hard | +| Binary Tree BFS | | | | +| --------------------------------------------- | -------- | ------ | ---- | +| 199. Binary Tree Right Side View | Solution | Medium | 詳解 | +| 637. Average of Levels in Binary Tree | Solution | Easy | 詳解 | +| 102. Binary Tree Level Order Traversal | Solution | Medium | 詳解 | +| 103. Binary Tree Zigzag Level Order Traversal | Solution | Medium | 詳解 | + +| Binary Search Tree | | | | +| --------------------------------------- | -------- | ------ | ---- | +| 530. Minimum Absolute Difference in BST | Solution | Easy | 詳解 | +| 230. Kth Smallest Element in a BST | Solution | Medium | 詳解 | +| 98. Validate Binary Search Tree | Solution | Medium | 詳解 | + +| Graph General | | | | +| ----------------------- | -------- | ------ | ---- | +| 200. Number of Islands | Solution | Medium | 詳解 | +| 130. Surrounded Regions | Solution | Medium | 詳解 | +| 133. Clone Graph | Solution | Medium | 詳解 | +| 399. Evaluate Division | Solution | Medium | 詳解 | +| 207. Course Schedule | Solution | Medium | 詳解 | +| 210. Course Schedule II | Solution | Medium | 詳解 | + +| Graph BFS | | | | +| ----------------------------- | -------- | ------ | ---- | +| 909. Snakes and Ladders | Solution | Medium | 詳解 | +| 433. Minimum Genetic Mutation | Solution | Medium | 詳解 | +| 127. Word Ladder | Solution | Hard | 詳解 | + +| Trie | | | | +| ----------------------------------------------- | --------------- | ------ | ---- | +| 208. Implement Trie (Prefix Tree) | [Solution][208] | Medium | 詳解 | +| 211. Design Add and Search Words Data Structure | Solution | Medium | 詳解 | +| 212. Word Search II | Solution | Hard | 詳解 | [208]: ./src/page-2/208.%20Implement%20Trie%20(Prefix%20Tree)/Trie.ts -| Backtracking | | | -| ----------------------------------------- | -------------- | ------ | -| 17. Letter Combinations of a Phone Number | [Solution][17] | Medium | -| 77. Combinations | Solution | Medium | -| 46. Permutations | [Solution][46] | Medium | -| 39. Combination Sum | [Solution][39] | Medium | -| 52. N-Queens II | Solution | Hard | -| 22. Generate Parentheses | Solution | Medium | -| 79. Word Search | Solution | Medium | +| Backtracking | | | | +| ----------------------------------------- | -------------- | ------ | ---- | +| 17. Letter Combinations of a Phone Number | [Solution][17] | Medium | 詳解 | +| 77. Combinations | Solution | Medium | 詳解 | +| 46. Permutations | [Solution][46] | Medium | 詳解 | +| 39. Combination Sum | [Solution][39] | Medium | 詳解 | +| 52. N-Queens II | Solution | Hard | 詳解 | +| 22. Generate Parentheses | Solution | Medium | 詳解 | +| 79. Word Search | Solution | Medium | 詳解 | [17]: ./src/page-1/17.%20Letter%20Combinations%20of%20a%20Phone%20Number/letterCombinations.ts [46]: ./src/page-1/46.%20Permutations/permute.ts [39]: ./src/page-1/39.%20Combination%20Sum/combinationSum.ts -| Divide & Conquer | | | -| ----------------------------------------------- | --------------- | ------ | -| 108. Convert Sorted Array to Binary Search Tree | [Solution][108] | Easy | -| 148. Sort List | Solution | Medium | -| 427. Construct Quad Tree | Solution | Medium | -| 23. Merge k Sorted Lists | Solution | Hard | +| Divide & Conquer | | | | +| ----------------------------------------------- | --------------- | ------ | ---- | +| 108. Convert Sorted Array to Binary Search Tree | [Solution][108] | Easy | 詳解 | +| 148. Sort List | Solution | Medium | 詳解 | +| 427. Construct Quad Tree | Solution | Medium | 詳解 | +| 23. Merge k Sorted Lists | Solution | Hard | 詳解 | [108]: ./src/page-2/108.%20Convert%20Sorted%20Array%20to%20Binary%20Search%20Tree/sortedArrayToBST.ts -| Kadane's Algorithm | | | -| ---------------------------------- | -------------- | ------ | -| 53. Maximum Subarray | [Solution][53] | Medium | -| 918. Maximum Sum Circular Subarray | Solution | Medium | +| Kadane's Algorithm | | | | +| ---------------------------------- | -------------- | ------ | ---- | +| 53. Maximum Subarray | [Solution][53] | Medium | 詳解 | +| 918. Maximum Sum Circular Subarray | Solution | Medium | 詳解 | [53]: ./src/page-1/53.%20Maximum%20Subarray/maxSubArray.ts -| Binary Search | | | -| ----------------------------------------------------------- | -------------- | ------ | -| 35. Search Insert Position | [Solution][35] | Easy | -| 74. Search a 2D Matrix | Solution | Medium | -| 162. Find Peak Element | Solution | Medium | -| 33. Search in Rotated Sorted Array | Solution | Medium | -| 34. Find First and Last Position of Element in Sorted Array | [Solution][34] | Medium | -| 153. Find Minimum in Rotated Sorted Array | Solution | Medium | -| 4. Median of Two Sorted Arrays | [Solution][4] | Hard | +| Binary Search | | | | +| ----------------------------------------------------------- | -------------- | ------ | ---- | +| 35. Search Insert Position | [Solution][35] | Easy | 詳解 | +| 74. Search a 2D Matrix | Solution | Medium | 詳解 | +| 162. Find Peak Element | Solution | Medium | 詳解 | +| 33. Search in Rotated Sorted Array | Solution | Medium | 詳解 | +| 34. Find First and Last Position of Element in Sorted Array | [Solution][34] | Medium | 詳解 | +| 153. Find Minimum in Rotated Sorted Array | Solution | Medium | 詳解 | +| 4. Median of Two Sorted Arrays | [Solution][4] | Hard | 詳解 | [35]: ./src/page-1/35.%20Search%20Insert%20Position/searchInsert.ts [34]: ./src/page-1/34.%20Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array/searchRange.ts [4]: ./src/page-1/4.%20Median%20of%20Two%20Sorted%20Arrays/findMedianSortedArrays.ts -| Heap | | | -| ------------------------------------ | -------- | ------ | -| 215. Kth Largest Element in an Array | Solution | Medium | -| 502. IPO | Solution | Hard | -| 373. Find K Pairs with Smallest Sums | Solution | Medium | -| 295. Find Median from Data Stream | Solution | Hard | - -| Bit Manipulation | | | -| --------------------------------- | --------------- | ------ | -| 67. Add Binary | [Solution][67] | Easy | -| 190. Reverse Bits | [Solution][190] | Easy | -| 191. Number of 1 Bits | [Solution][191] | Easy | -| 136. Single Number | [Solution][136] | Easy | -| 137. Single Number II | Solution | Medium | -| 201. Bitwise AND of Numbers Range | Solution | Medium | +| Heap | | | | +| ------------------------------------ | -------- | ------ | ---- | +| 215. Kth Largest Element in an Array | Solution | Medium | 詳解 | +| 502. IPO | Solution | Hard | 詳解 | +| 373. Find K Pairs with Smallest Sums | Solution | Medium | 詳解 | +| 295. Find Median from Data Stream | Solution | Hard | 詳解 | + +| Bit Manipulation | | | | +| --------------------------------- | --------------- | ------ | ---- | +| 67. Add Binary | [Solution][67] | Easy | 詳解 | +| 190. Reverse Bits | [Solution][190] | Easy | 詳解 | +| 191. Number of 1 Bits | [Solution][191] | Easy | 詳解 | +| 136. Single Number | [Solution][136] | Easy | 詳解 | +| 137. Single Number II | Solution | Medium | 詳解 | +| 201. Bitwise AND of Numbers Range | Solution | Medium | 詳解 | [67]: ./src/page-1/67.%20Add%20Binary/addBinary.ts [190]: ./src/page-2/190.%20Reverse%20Bits/reverseBits.ts [191]: ./src/page-2/191.%20Number%20of%201%20Bits/hammingWeight.ts [136]: ./src/page-2/136.%20Single%20Number/singleNumber.ts -| Math | | | -| ------------------------------ | -------------- | ------ | -| 9. Palindrome Number | [Solution][9] | Easy | -| 66. Plus One | [Solution][66] | Easy | -| 172. Factorial Trailing Zeroes | Solution | Medium | -| 69. Sqrt(x) | [Solution][69] | Easy | -| 50. Pow(x, n) | [Solution][50] | Medium | -| 149. Max Points on a Line | Solution | Hard | +| Math | | | | +| ------------------------------ | -------------- | ------ | ---- | +| 9. Palindrome Number | [Solution][9] | Easy | 詳解 | +| 66. Plus One | [Solution][66] | Easy | 詳解 | +| 172. Factorial Trailing Zeroes | Solution | Medium | 詳解 | +| 69. Sqrt(x) | [Solution][69] | Easy | 詳解 | +| 50. Pow(x, n) | [Solution][50] | Medium | 詳解 | +| 149. Max Points on a Line | Solution | Hard | 詳解 | [9]: ./src/page-1/9.%20Palindrome%20Number/isPalindrome.ts [66]: ./src/page-1/66.%20Plus%20One/plusOne.ts [69]: ./src/page-1/69.%20Sqrt(x)/mySqrt.ts [50]: ./src/page-1/50.%20Pow(x,%20n)/myPow.ts -| 1D DP | | | -| ----------------------------------- | --------------- | ------ | -| 70. Climbing Stairs | [Solution][70] | Easy | -| 198. House Robber | [Solution][198] | Medium | -| 139. Word Break | Solution | Medium | -| 322. Coin Change | Solution | Medium | -| 300. Longest Increasing Subsequence | Solution | Medium | +| 1D DP | | | | +| ----------------------------------- | --------------- | ------ | ---- | +| 70. Climbing Stairs | [Solution][70] | Easy | 詳解 | +| 198. House Robber | [Solution][198] | Medium | 詳解 | +| 139. Word Break | Solution | Medium | 詳解 | +| 322. Coin Change | Solution | Medium | 詳解 | +| 300. Longest Increasing Subsequence | Solution | Medium | 詳解 | [70]: ./src/page-1/70.%20Climbing%20Stairs/climbStairs.ts [198]: ./src/page-2/198.%20House%20Robber/rob.ts -| Multidimensional DP | | | -| ---------------------------------------- | ------------- | ------ | -| 120. Triangle | Solution | Medium | -| 64. Minimum Path Sum | Solution | Medium | -| 63. Unique Paths II | Solution | Medium | -| 5. Longest Palindromic Substring | [Solution][5] | Medium | -| 97. Interleaving String | Solution | Medium | -| 72. Edit Distance | Solution | Medium | -| 123. Best Time to Buy and Sell Stock III | Solution | Hard | -| 188. Best Time to Buy and Sell Stock IV | Solution | Hard | -| 221. Maximal Square | Solution | Medium | +| Multidimensional DP | | | | +| ---------------------------------------- | -------------- | ------ | ---------------------- | +| 120. Triangle | Solution | Medium | 詳解 | +| 64. Minimum Path Sum | Solution | Medium | 詳解 | +| 63. Unique Paths II | Solution | Medium | 詳解 | +| 5. Longest Palindromic Substring | [Solution][5] | Medium | 詳解 | +| 97. Interleaving String | Solution | Medium | 詳解 | +| 72. Edit Distance | [Solution][72] | Medium | [詳解][72-explanation] | +| 123. Best Time to Buy and Sell Stock III | Solution | Hard | 詳解 | +| 188. Best Time to Buy and Sell Stock IV | Solution | Hard | 詳解 | +| 221. Maximal Square | Solution | Medium | 詳解 | [5]: ./src/page-1/5.%20Longest%20Palindromic%20Substring/longestPalindrome.ts +[72]: ./src/page-1/72.%20Edit%20Distance/minDistance.ts +[72-explanation]: ./src/page-1/72.%20Edit%20Distance/README.md ## Impactful - Top 100 Liked @@ -596,51 +600,51 @@ Problems: Problems: -| Backtracking | | | -| ----------------------------------------- | -------------- | ------ | -| 17. Letter Combinations of a Phone Number | [Solution][17] | Medium | -| 22. Generate Parentheses | Solution | Medium | -| 39. Combination Sum | [Solution][39] | Medium | -| 46. Permutations | [Solution][46] | Medium | -| 51. N-Queens | Solution | Hard | -| 78. Subsets | Solution | Medium | -| 79. Word Search | Solution | Medium | -| 131. Palindrome Partitioning | Solution | Medium | +| Backtracking | | | | +| ----------------------------------------- | -------------- | ------ | ---- | +| 17. Letter Combinations of a Phone Number | [Solution][17] | Medium | 詳解 | +| 22. Generate Parentheses | Solution | Medium | 詳解 | +| 39. Combination Sum | [Solution][39] | Medium | 詳解 | +| 46. Permutations | [Solution][46] | 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 [39]: ./src/page-1/39.%20Combination%20Sum/combinationSum.ts [46]: ./src/page-1/46.%20Permutations/permute.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 | +| 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 | +| 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 @@ -650,143 +654,145 @@ Problems: [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][62] | 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][198] | 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][1143] | Medium | +| Dynamic Programming | | | | +| ----------------------------------- | ---------------- | ------ | ---------------------- | +| 5. Longest Palindromic Substring | [Solution][5] | Medium | 詳解 | +| 32. Longest Valid Parentheses | Solution | Hard | 詳解 | +| 62. Unique Paths | [Solution][62] | Medium | 詳解 | +| 64. Minimum Path Sum | Solution | Medium | 詳解 | +| 70. Climbing Stairs | [Solution][70] | Easy | 詳解 | +| 72. Edit Distance | [Solution][72] | Medium | [詳解][72-explanation] | +| 118. Pascal's Triangle | [Solution][118] | Easy | 詳解 | +| 139. Word Break | Solution | Medium | 詳解 | +| 152. Maximum Product Subarray | Solution | Medium | 詳解 | +| 198. House Robber | [Solution][198] | 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][1143] | Medium | 詳解 | [5]: ./src/page-1/5.%20Longest%20Palindromic%20Substring/longestPalindrome.ts [62]: ./src/page-1/62.%20Unique%20Paths/uniquePaths.ts [70]: ./src/page-1/70.%20Climbing%20Stairs/climbStairs.ts +[72]: ./src/page-1/72.%20Edit%20Distance/minDistance.ts +[72-explanation]: ./src/page-1/72.%20Edit%20Distance/README.md [118]: ./src/page-2/118.%20Pascal's%20Triangle/generate.ts [198]: ./src/page-2/198.%20House%20Robber/rob.ts [1143]: ./src/page-11/1143.%20Longest%20Common%20Subsequence/longestCommonSubsequence.ts -| Graph | | | -| ---------------------- | --------------- | ------ | -| 200. Number of Islands | Solution | Medium | -| 207. Course Schedule | Solution | Medium | -| 994. Rotting Oranges | [Solution][994] | Medium | +| 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 | +| 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 | +| 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 | +| 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 | +| 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 | +| 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 | +| 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 | +| 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 | +| 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][208] | Medium | +| Trie | | | | +| --------------------------------- | --------------- | ------ | ---- | +| 208. Implement Trie (Prefix Tree) | [Solution][208] | Medium | 詳解 | [208]: ./src/page-2/208.%20Implement%20Trie%20(Prefix%20Tree)/Trie.ts -| 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 | +| 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 diff --git a/src/page-1/72. Edit Distance/README.md b/src/page-1/72. Edit Distance/README.md new file mode 100644 index 0000000..778c389 --- /dev/null +++ b/src/page-1/72. Edit Distance/README.md @@ -0,0 +1,112 @@ +# 72. 編輯距離 (Edit Distance) + +給定兩個字串 word1 和 word2,返回將 word1 轉換為 word2 所使用的最少操作數。 + +你可以對字詞進行以下三種操作: + +- 插入一個字元 +- 刪除一個字元 +- 替換一個字元 + +範例 1: + +```coffee +輸入: word1 = "horse", word2 = "ros" +輸出: 3 +說明: +horse -> rorse (將 'h' 替換為 'r') +rorse -> rose (刪除 'r') +rose -> ros (刪除 'e') +``` + +範例 2: + +```coffee +輸入: word1 = "intention", word2 = "execution" +輸出: 5 +說明: +intention -> inention (刪除 't') +inention -> enention (將 'i' 替換為 'e') +enention -> exention (將 'n' 替換為 'x') +exention -> exection (將 'n' 替換為 'c') +exection -> execution (插入 'u') +``` + +## 解題 + +- 插入操作:`dp[i][j-1] + 1` + +```coffee +word1: "abc" -> "abcd" +word2: "abcd" +``` + +| | "" | a | b | c | d | +| --- | --- | --- | --- | --- | --- | +| "" | 0 | 1 | 2 | 3 | 4 | +| a | 1 | 0 | 1 | 2 | 3 | +| ab | 2 | 1 | 0 | 1 | 2 | +| abc | 3 | 2 | 1 | 0 | 1 | + +- 刪除操作:`dp[i-1][j] + 1` + +```coffee +word1: "abc" -> "ab" +word2: "ab" +``` + +| | "" | a | b | c | +| --- | --- | --- | --- | --- | +| "" | 0 | 1 | 2 | 3 | +| a | 1 | 0 | 1 | 2 | +| ab | 2 | 1 | 0 | 1 | +| abc | 3 | 2 | 1 | 1 | + +- 替換操作:`dp[i-1][j-1] + 1` + +```coffee +word1: "abc" -> "abd" +word2: "abd" +``` + +| | "" | a | b | c | +| --- | --- | --- | --- | --- | +| "" | 0 | 1 | 2 | 3 | +| a | 1 | 0 | 1 | 2 | +| ab | 2 | 1 | 0 | 1 | +| abc | 3 | 2 | 1 | 1 | +| adc | 3 | 2 | 1 | 1 | + +```ts +export const minDistance: MinDistance = (word1, word2) => { + const m = word1.length; + const n = word2.length; + + // 建立一個二維 DP 表 + const dp: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + + // 空字串情況處理 + for (let i = 0; i <= m; i++) dp[i][0] = i; // 刪除 word1 中的所有字元 + for (let j = 0; j <= n; j++) dp[0][j] = j; // 插入所有字元以匹配 word2 + + // 填充 DP 表 + for (let i = 1; i <= m; i++) { + for (let j = 1; j <= n; j++) { + if (word1[i - 1] === word2[j - 1]) { + // 字元匹配,無需操作 + dp[i][j] = dp[i - 1][j - 1]; + } else { + // 選擇最少的插入、刪除或替換操作 + dp[i][j] = Math.min( + dp[i][j - 1] + 1, // 插入 + dp[i - 1][j] + 1, // 刪除 + dp[i - 1][j - 1] + 1, // 替換 + ); + } + } + } + + // 結果位於右下角格子中 + return dp[m][n]; +}; +``` diff --git a/src/page-1/72. Edit Distance/minDistance.test.ts b/src/page-1/72. Edit Distance/minDistance.test.ts new file mode 100644 index 0000000..cb3c100 --- /dev/null +++ b/src/page-1/72. Edit Distance/minDistance.test.ts @@ -0,0 +1,8 @@ +import { minDistance } from './minDistance'; + +describe('72. Edit Distance', () => { + test('minDistance', () => { + expect(minDistance('horse', 'ros')).toBe(3); + expect(minDistance('intention', 'execution')).toBe(5); + }); +}); diff --git a/src/page-1/72. Edit Distance/minDistance.ts b/src/page-1/72. Edit Distance/minDistance.ts new file mode 100644 index 0000000..cd66fc4 --- /dev/null +++ b/src/page-1/72. Edit Distance/minDistance.ts @@ -0,0 +1,36 @@ +type MinDistance = (word1: string, word2: string) => number; + +/** + * Accepted + */ +export const minDistance: MinDistance = (word1, word2) => { + const m = word1.length; + const n = word2.length; + + // Create a 2D DP array + const dp: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + + // If one of the strings is empty, we need to delete or insert all characters of the other string + for (let i = 0; i <= m; i++) dp[i][0] = i; // Deleting all characters from word1 + for (let j = 0; j <= n; j++) dp[0][j] = j; // Inserting all characters to match word2 + + // Fill the DP table + for (let i = 1; i <= m; i++) { + for (let j = 1; j <= n; j++) { + if (word1[i - 1] === word2[j - 1]) { + // Characters match, no operation needed + dp[i][j] = dp[i - 1][j - 1]; + } else { + // Choose the minimum of insert, delete, or replace operation + dp[i][j] = Math.min( + dp[i][j - 1] + 1, // Insert + dp[i - 1][j] + 1, // Delete + dp[i - 1][j - 1] + 1, // Replace + ); + } + } + } + + // The result is in the bottom-right cell + return dp[m][n]; +}; diff --git a/src/page-17/1768. Merge Strings Alternately/README.md b/src/page-17/1768. Merge Strings Alternately/README.md index 24e56fe..2efb21e 100644 --- a/src/page-17/1768. Merge Strings Alternately/README.md +++ b/src/page-17/1768. Merge Strings Alternately/README.md @@ -6,7 +6,7 @@ 範例 1: -```ts +```coffee 輸入: word1 = "abc", word2 = "pqr" 輸出: "apbqcr" 說明: 字串合併情況如下所示: @@ -17,7 +17,7 @@ word2: p q r 範例 2: -```ts +```coffee 輸入: word1 = "ab", word2 = "pqrs" 輸出: "apbqrs" 說明: 注意,word2 比 word1 長,"rs" 需要追加到合併後字串的結尾。 @@ -28,7 +28,7 @@ word2: p q r s 範例 3: -```ts +```coffee 輸入: word1 = "abcd", word2 = "pq" 輸出: "apbqcd" 說明: 注意,word1 比 word2 長,"cd" 需要追加到合併後字串的結尾。 diff --git a/src/page-7/714. Best Time to Buy and Sell Stock with Transaction Fee/README.md b/src/page-7/714. Best Time to Buy and Sell Stock with Transaction Fee/README.md index 48a598b..c442339 100644 --- a/src/page-7/714. Best Time to Buy and Sell Stock with Transaction Fee/README.md +++ b/src/page-7/714. Best Time to Buy and Sell Stock with Transaction Fee/README.md @@ -9,6 +9,26 @@ - 你不得同時進行多項交易 (即,您必須在再次購買之前出售股票)。 - 每次買賣股票只收取一次手續費。 +範例 1: + +```coffee +輸入: prices = [1,3,2,8,4,9], fee = 2 +輸出: 8 +說明: 可以透過以下方式實現最大利潤: +- 以 prices[0] = 1 買入 +- 以 prices[3] = 8 賣出 +- 以 prices[4] = 4 買入 +- 以 prices[5] = 9 賣出 +總利潤為 ((8 - 1) - 2) + ((9 - 4) - 2) = 8。 +``` + +範例 2: + +```coffee +輸入: prices = [1,3,7,5,10,3], fee = 3 +輸出: 6 +``` + ## 解題 使用動態規劃 (Dynamic Programming)