From b30dd098bc13e822369e74e134c1d3401232bcff Mon Sep 17 00:00:00 2001 From: Sadaf <137484958+SadafKausar2025@users.noreply.github.com> Date: Sun, 16 Jun 2024 15:09:03 +0530 Subject: [PATCH 1/2] Added leetcode-40 Combination sum II --- dsa-problems/leetcode-problems/0000-0099.md | 1191 ++++++++--------- .../0000-0099/0040-Combination-sum-II.md | 324 +++++ 2 files changed, 919 insertions(+), 596 deletions(-) create mode 100644 dsa-solutions/lc-solutions/0000-0099/0040-Combination-sum-II.md diff --git a/dsa-problems/leetcode-problems/0000-0099.md b/dsa-problems/leetcode-problems/0000-0099.md index 1291e0a59..9a0debf33 100644 --- a/dsa-problems/leetcode-problems/0000-0099.md +++ b/dsa-problems/leetcode-problems/0000-0099.md @@ -8,602 +8,601 @@ keywords: - leetcode problems problems --- - export const problems = [ - { - "problemName": "1. Two Sum", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/two-sum/", - "solutionLink": "/dsa-solutions/lc-solutions/0000-0099/two-sum" - }, - { - "problemName": "2. Add Two Numbers", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/add-two-numbers/", - "solutionLink": "#" - }, - { - "problemName": "3. Longest Substring Without Repeating Characters", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/longest-substring-without-repeating-characters/", - "solutionLink": "#" - }, - { - "problemName": "4. Median of Two Sorted Arrays", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/median-of-two-sorted-arrays/", - "solutionLink": "#" - }, - { - "problemName": "5. Longest Palindromic Substring", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/longest-palindromic-substring/", - "solutionLink": "#" - }, - { - "problemName": "6. ZigZag Conversion", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/zigzag-conversion/", - "solutionLink": "#" - }, - { - "problemName": "7. Reverse Integer", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/reverse-integer/", - "solutionLink": "#" - }, - { - "problemName": "8. String to Integer (atoi)", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/string-to-integer-atoi/", - "solutionLink": "#" - }, - { - "problemName": "9. Palindrome Number", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/palindrome-number/", - "solutionLink": "#" - }, - { - "problemName": "10. Regular Expression Matching", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/regular-expression-matching/", - "solutionLink": "#" - }, - { - "problemName": "11. Container With Most Water", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/container-with-most-water/", - "solutionLink": "#" - }, - { - "problemName": "12. Integer to Roman", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/integer-to-roman/", - "solutionLink": "#" - }, - { - "problemName": "13. Roman to Integer", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/roman-to-integer/", - "solutionLink": "#" - }, - { - "problemName": "14. Longest Common Prefix", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/longest-common-prefix/", - "solutionLink": "#" - }, - { - "problemName": "15. 3Sum", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/3sum/", - "solutionLink": "#" - }, - { - "problemName": "16. 3Sum Closest", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/3sum-closest/", - "solutionLink": "#" - }, - { - "problemName": "17. Letter Combinations of a Phone Number", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/letter-combinations-of-a-phone-number/", - "solutionLink": "#" - }, - { - "problemName": "18. 4Sum", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/4sum/", - "solutionLink": "#" - }, - { - "problemName": "19. Remove Nth Node From End of List", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/remove-nth-node-from-end-of-list/", - "solutionLink": "#" - }, - { - "problemName": "20. Valid Parentheses", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/valid-parentheses/", - "solutionLink": "#" - }, - { - "problemName": "21. Merge Two Sorted Lists", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/merge-two-sorted-lists/", - "solutionLink": "#" - }, - { - "problemName": "22. Generate Parentheses", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/generate-parentheses/", - "solutionLink": "#" - }, - { - "problemName": "23. Merge k Sorted Lists", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/merge-k-sorted-lists/", - "solutionLink": "#" - }, - { - "problemName": "24. Swap Nodes in Pairs", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/swap-nodes-in-pairs/", - "solutionLink": "#" - }, - { - "problemName": "25. Reverse Nodes in k-Group", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/reverse-nodes-in-k-group/", - "solutionLink": "#" - }, - { - "problemName": "26. Remove Duplicates from Sorted Array", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/remove-duplicates-from-sorted-array/", - "solutionLink": "#" - }, - { - "problemName": "27. Remove Element", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/remove-element/", - "solutionLink": "#" - }, - { - "problemName": "28. Implement strStr()", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/implement-strstr/", - "solutionLink": "#" - }, - { - "problemName": "29. Divide Two Integers", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/divide-two-integers/", - "solutionLink": "#" - }, - { - "problemName": "30. Substring with Concatenation of All Words", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/substring-with-concatenation-of-all-words/", - "solutionLink": "#" - }, - { - "problemName": "31. Next Permutation", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/next-permutation/", - "solutionLink": "#" - }, - { - "problemName": "32. Longest Valid Parentheses", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/longest-valid-parentheses/", - "solutionLink": "#" - }, - { - "problemName": "33. Search in Rotated Sorted Array", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/search-in-rotated-sorted-array/", - "solutionLink": "#" - }, - { - "problemName": "34. Find First and Last Position of Element in Sorted Array", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/", - "solutionLink": "#" - }, - { - "problemName": "35. Search Insert Position", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/search-insert-position/", - "solutionLink": "#" - }, - { - "problemName": "36. Valid Sudoku", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/valid-sudoku/", - "solutionLink": "#" - }, - { - "problemName": "37. Sudoku Solver", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/sudoku-solver/", - "solutionLink": "#" - }, - { - "problemName": "38. Count and Say", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/count-and-say/", - "solutionLink": "#" - }, - { - "problemName": "39. Combination Sum", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/combination-sum/", - "solutionLink": "#" - }, - { - "problemName": "40. Combination Sum II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/combination-sum-ii/", - "solutionLink": "#" - }, - { - "problemName": "41. First Missing Positive", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/first-missing-positive/", - "solutionLink": "#" - }, - { - "problemName": "42. Trapping Rain Water", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/trapping-rain-water/", - "solutionLink": "#" - }, - { - "problemName": "43. Multiply Strings", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/multiply-strings/", - "solutionLink": "#" - }, - { - "problemName": "44. Wildcard Matching", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/wildcard-matching/", - "solutionLink": "#" - }, - { - "problemName": "45. Jump Game II", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/jump-game-ii/", - "solutionLink": "#" - }, - { - "problemName": "46. Permutations", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/permutations/", - "solutionLink": "#" - }, - { - "problemName": "47. Permutations II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/permutations-ii/", - "solutionLink": "#" - }, - { - "problemName": "48. Rotate Image", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/rotate-image/", - "solutionLink": "#" - }, - { - "problemName": "49. Group Anagrams", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/group-anagrams/", - "solutionLink": "#" - }, - { - "problemName": "50. Pow(x, n)", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/powx-n/", - "solutionLink": "#" - }, - { - "problemName": "51. N-Queens", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/n-queens/", - "solutionLink": "#" - }, - { - "problemName": "52. N-Queens II", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/n-queens-ii/", - "solutionLink": "#" - }, - { - "problemName": "53. Maximum Subarray", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/maximum-subarray/", - "solutionLink": "#" - }, - { - "problemName": "54. Spiral Matrix", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/spiral-matrix/", - "solutionLink": "#" - }, - { - "problemName": "55. Jump Game", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/jump-game/", - "solutionLink": "#" - }, - { - "problemName": "56. Merge Intervals", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/merge-intervals/", - "solutionLink": "#" - }, - { - "problemName": "57. Insert Interval", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/insert-interval/", - "solutionLink": "#" - }, - { - "problemName": "58. Length of Last Word", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/length-of-last-word/", - "solutionLink": "#" - }, - { - "problemName": "59. Spiral Matrix II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/spiral-matrix-ii/", - "solutionLink": "#" - }, - { - "problemName": "60. Permutation Sequence", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/permutation-sequence/", - "solutionLink": "#" - }, - { - "problemName": "61. Rotate List", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/rotate-list/", - "solutionLink": "#" - }, - { - "problemName": "62. Unique Paths", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/unique-paths/", - "solutionLink": "#" - }, - { - "problemName": "63. Unique Paths II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/unique-paths-ii/", - "solutionLink": "#" - }, - { - "problemName": "64. Minimum Path Sum", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/minimum-path-sum/", - "solutionLink": "#" - }, - { - "problemName": "65. Valid Number", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/valid-number/", - "solutionLink": "#" - }, - { - "problemName": "66. Plus One", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/plus-one/", - "solutionLink": "#" - }, - { - "problemName": "67. Add Binary", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/add-binary/", - "solutionLink": "#" - }, - { - "problemName": "68. Text Justification", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/text-justification/", - "solutionLink": "#" - }, - { - "problemName": "69. Sqrt(x)", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/sqrtx/", - "solutionLink": "#" - }, - { - "problemName": "70. Climbing Stairs", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/climbing-stairs/", - "solutionLink": "#" - }, - { - "problemName": "71. Simplify Path", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/simplify-path/", - "solutionLink": "#" - }, - { - "problemName": "72. Edit Distance", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/edit-distance/", - "solutionLink": "#" - }, - { - "problemName": "73. Set Matrix Zeroes", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/set-matrix-zeroes/", - "solutionLink": "#" - }, - { - "problemName": "74. Search a 2D Matrix", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/search-a-2d-matrix/", - "solutionLink": "#" - }, - { - "problemName": "75. Sort Colors", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/sort-colors/", - "solutionLink": "/dsa-solutions/lc-solutions/0000-0099/sort-colors" - }, - { - "problemName": "76. Minimum Window Substring", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/minimum-window-substring/", - "solutionLink": "#" - }, - { - "problemName": "77. Combinations", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/combinations/", - "solutionLink": "#" - }, - { - "problemName": "78. Subsets", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/subsets/", - "solutionLink": "#" - }, - { - "problemName": "79. Word Search", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/word-search/", - "solutionLink": "#" - }, - { - "problemName": "80. Remove Duplicates from Sorted Array II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/", - "solutionLink": "#" - }, - { - "problemName": "81. Search in Rotated Sorted Array II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/search-in-rotated-sorted-array-ii/", - "solutionLink": "#" - }, - { - "problemName": "82. Remove Duplicates from Sorted List II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/", - "solutionLink": "#" - }, - { - "problemName": "83. Remove Duplicates from Sorted List", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/remove-duplicates-from-sorted-list/", - "solutionLink": "#" - }, - { - "problemName": "84. Largest Rectangle in Histogram", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/largest-rectangle-in-histogram/", - "solutionLink": "#" - }, - { - "problemName": "85. Maximal Rectangle", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/maximal-rectangle/", - "solutionLink": "#" - }, - { - "problemName": "86. Partition List", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/partition-list/", - "solutionLink": "#" - }, - { - "problemName": "87. Scramble String", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/scramble-string/", - "solutionLink": "#" - }, - { - "problemName": "88. Merge Sorted Array", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/merge-sorted-array/", - "solutionLink": "#" - }, - { - "problemName": "89. Gray Code", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/gray-code/", - "solutionLink": "#" - }, - { - "problemName": "90. Subsets II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/subsets-ii/", - "solutionLink": "#" - }, - { - "problemName": "91. Decode Ways", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/decode-ways/", - "solutionLink": "#" - }, - { - "problemName": "92. Reverse Linked List II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/reverse-linked-list-ii/", - "solutionLink": "#" - }, - { - "problemName": "93. Restore IP Addresses", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/restore-ip-addresses/", - "solutionLink": "#" - }, - { - "problemName": "94. Binary Tree Inorder Traversal", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/binary-tree-inorder-traversal/", - "solutionLink": "#" - }, - { - "problemName": "95. Unique Binary Search Trees II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/unique-binary-search-trees-ii/", - "solutionLink": "#" - }, - { - "problemName": "96. Unique Binary Search Trees", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/unique-binary-search-trees/", - "solutionLink": "#" - }, - { - "problemName": "97. Interleaving String", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/interleaving-string/", - "solutionLink": "#" - }, - { - "problemName": "98. Validate Binary Search Tree", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/validate-binary-search-tree/", - "solutionLink": "#" - }, - { - "problemName": "99. Recover Binary Search Tree", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/recover-binary-search-tree/", - "solutionLink": "#" - } +{ +"problemName": "1. Two Sum", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/two-sum/", +"solutionLink": "/dsa-solutions/lc-solutions/0000-0099/two-sum" +}, +{ +"problemName": "2. Add Two Numbers", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/add-two-numbers/", +"solutionLink": "#" +}, +{ +"problemName": "3. Longest Substring Without Repeating Characters", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/longest-substring-without-repeating-characters/", +"solutionLink": "#" +}, +{ +"problemName": "4. Median of Two Sorted Arrays", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/median-of-two-sorted-arrays/", +"solutionLink": "#" +}, +{ +"problemName": "5. Longest Palindromic Substring", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/longest-palindromic-substring/", +"solutionLink": "#" +}, +{ +"problemName": "6. ZigZag Conversion", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/zigzag-conversion/", +"solutionLink": "#" +}, +{ +"problemName": "7. Reverse Integer", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/reverse-integer/", +"solutionLink": "#" +}, +{ +"problemName": "8. String to Integer (atoi)", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/string-to-integer-atoi/", +"solutionLink": "#" +}, +{ +"problemName": "9. Palindrome Number", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/palindrome-number/", +"solutionLink": "#" +}, +{ +"problemName": "10. Regular Expression Matching", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/regular-expression-matching/", +"solutionLink": "#" +}, +{ +"problemName": "11. Container With Most Water", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/container-with-most-water/", +"solutionLink": "#" +}, +{ +"problemName": "12. Integer to Roman", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/integer-to-roman/", +"solutionLink": "#" +}, +{ +"problemName": "13. Roman to Integer", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/roman-to-integer/", +"solutionLink": "#" +}, +{ +"problemName": "14. Longest Common Prefix", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/longest-common-prefix/", +"solutionLink": "#" +}, +{ +"problemName": "15. 3Sum", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/3sum/", +"solutionLink": "#" +}, +{ +"problemName": "16. 3Sum Closest", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/3sum-closest/", +"solutionLink": "#" +}, +{ +"problemName": "17. Letter Combinations of a Phone Number", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/letter-combinations-of-a-phone-number/", +"solutionLink": "#" +}, +{ +"problemName": "18. 4Sum", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/4sum/", +"solutionLink": "#" +}, +{ +"problemName": "19. Remove Nth Node From End of List", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/remove-nth-node-from-end-of-list/", +"solutionLink": "#" +}, +{ +"problemName": "20. Valid Parentheses", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/valid-parentheses/", +"solutionLink": "#" +}, +{ +"problemName": "21. Merge Two Sorted Lists", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/merge-two-sorted-lists/", +"solutionLink": "#" +}, +{ +"problemName": "22. Generate Parentheses", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/generate-parentheses/", +"solutionLink": "#" +}, +{ +"problemName": "23. Merge k Sorted Lists", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/merge-k-sorted-lists/", +"solutionLink": "#" +}, +{ +"problemName": "24. Swap Nodes in Pairs", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/swap-nodes-in-pairs/", +"solutionLink": "#" +}, +{ +"problemName": "25. Reverse Nodes in k-Group", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/reverse-nodes-in-k-group/", +"solutionLink": "#" +}, +{ +"problemName": "26. Remove Duplicates from Sorted Array", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/remove-duplicates-from-sorted-array/", +"solutionLink": "#" +}, +{ +"problemName": "27. Remove Element", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/remove-element/", +"solutionLink": "#" +}, +{ +"problemName": "28. Implement strStr()", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/implement-strstr/", +"solutionLink": "#" +}, +{ +"problemName": "29. Divide Two Integers", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/divide-two-integers/", +"solutionLink": "#" +}, +{ +"problemName": "30. Substring with Concatenation of All Words", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/substring-with-concatenation-of-all-words/", +"solutionLink": "#" +}, +{ +"problemName": "31. Next Permutation", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/next-permutation/", +"solutionLink": "#" +}, +{ +"problemName": "32. Longest Valid Parentheses", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/longest-valid-parentheses/", +"solutionLink": "#" +}, +{ +"problemName": "33. Search in Rotated Sorted Array", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/search-in-rotated-sorted-array/", +"solutionLink": "#" +}, +{ +"problemName": "34. Find First and Last Position of Element in Sorted Array", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/", +"solutionLink": "#" +}, +{ +"problemName": "35. Search Insert Position", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/search-insert-position/", +"solutionLink": "#" +}, +{ +"problemName": "36. Valid Sudoku", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/valid-sudoku/", +"solutionLink": "#" +}, +{ +"problemName": "37. Sudoku Solver", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/sudoku-solver/", +"solutionLink": "#" +}, +{ +"problemName": "38. Count and Say", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/count-and-say/", +"solutionLink": "#" +}, +{ +"problemName": "39. Combination Sum", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/combination-sum/", +"solutionLink": "#" +}, +{ +"problemName": "40. Combination Sum II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/combination-sum-ii/", +"solutionLink": "/dsa-solutions/lc-solutions/0000-0099/combination-sum-II" +}, +{ +"problemName": "41. First Missing Positive", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/first-missing-positive/", +"solutionLink": "#" +}, +{ +"problemName": "42. Trapping Rain Water", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/trapping-rain-water/", +"solutionLink": "#" +}, +{ +"problemName": "43. Multiply Strings", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/multiply-strings/", +"solutionLink": "#" +}, +{ +"problemName": "44. Wildcard Matching", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/wildcard-matching/", +"solutionLink": "#" +}, +{ +"problemName": "45. Jump Game II", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/jump-game-ii/", +"solutionLink": "#" +}, +{ +"problemName": "46. Permutations", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/permutations/", +"solutionLink": "#" +}, +{ +"problemName": "47. Permutations II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/permutations-ii/", +"solutionLink": "#" +}, +{ +"problemName": "48. Rotate Image", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/rotate-image/", +"solutionLink": "#" +}, +{ +"problemName": "49. Group Anagrams", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/group-anagrams/", +"solutionLink": "#" +}, +{ +"problemName": "50. Pow(x, n)", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/powx-n/", +"solutionLink": "#" +}, +{ +"problemName": "51. N-Queens", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/n-queens/", +"solutionLink": "#" +}, +{ +"problemName": "52. N-Queens II", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/n-queens-ii/", +"solutionLink": "#" +}, +{ +"problemName": "53. Maximum Subarray", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/maximum-subarray/", +"solutionLink": "#" +}, +{ +"problemName": "54. Spiral Matrix", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/spiral-matrix/", +"solutionLink": "#" +}, +{ +"problemName": "55. Jump Game", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/jump-game/", +"solutionLink": "#" +}, +{ +"problemName": "56. Merge Intervals", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/merge-intervals/", +"solutionLink": "#" +}, +{ +"problemName": "57. Insert Interval", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/insert-interval/", +"solutionLink": "#" +}, +{ +"problemName": "58. Length of Last Word", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/length-of-last-word/", +"solutionLink": "#" +}, +{ +"problemName": "59. Spiral Matrix II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/spiral-matrix-ii/", +"solutionLink": "#" +}, +{ +"problemName": "60. Permutation Sequence", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/permutation-sequence/", +"solutionLink": "#" +}, +{ +"problemName": "61. Rotate List", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/rotate-list/", +"solutionLink": "#" +}, +{ +"problemName": "62. Unique Paths", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/unique-paths/", +"solutionLink": "#" +}, +{ +"problemName": "63. Unique Paths II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/unique-paths-ii/", +"solutionLink": "#" +}, +{ +"problemName": "64. Minimum Path Sum", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/minimum-path-sum/", +"solutionLink": "#" +}, +{ +"problemName": "65. Valid Number", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/valid-number/", +"solutionLink": "#" +}, +{ +"problemName": "66. Plus One", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/plus-one/", +"solutionLink": "#" +}, +{ +"problemName": "67. Add Binary", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/add-binary/", +"solutionLink": "#" +}, +{ +"problemName": "68. Text Justification", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/text-justification/", +"solutionLink": "#" +}, +{ +"problemName": "69. Sqrt(x)", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/sqrtx/", +"solutionLink": "#" +}, +{ +"problemName": "70. Climbing Stairs", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/climbing-stairs/", +"solutionLink": "#" +}, +{ +"problemName": "71. Simplify Path", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/simplify-path/", +"solutionLink": "#" +}, +{ +"problemName": "72. Edit Distance", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/edit-distance/", +"solutionLink": "#" +}, +{ +"problemName": "73. Set Matrix Zeroes", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/set-matrix-zeroes/", +"solutionLink": "#" +}, +{ +"problemName": "74. Search a 2D Matrix", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/search-a-2d-matrix/", +"solutionLink": "#" +}, +{ +"problemName": "75. Sort Colors", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/sort-colors/", +"solutionLink": "/dsa-solutions/lc-solutions/0000-0099/sort-colors" +}, +{ +"problemName": "76. Minimum Window Substring", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/minimum-window-substring/", +"solutionLink": "#" +}, +{ +"problemName": "77. Combinations", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/combinations/", +"solutionLink": "#" +}, +{ +"problemName": "78. Subsets", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/subsets/", +"solutionLink": "#" +}, +{ +"problemName": "79. Word Search", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/word-search/", +"solutionLink": "#" +}, +{ +"problemName": "80. Remove Duplicates from Sorted Array II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/", +"solutionLink": "#" +}, +{ +"problemName": "81. Search in Rotated Sorted Array II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/search-in-rotated-sorted-array-ii/", +"solutionLink": "#" +}, +{ +"problemName": "82. Remove Duplicates from Sorted List II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/", +"solutionLink": "#" +}, +{ +"problemName": "83. Remove Duplicates from Sorted List", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/remove-duplicates-from-sorted-list/", +"solutionLink": "#" +}, +{ +"problemName": "84. Largest Rectangle in Histogram", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/largest-rectangle-in-histogram/", +"solutionLink": "#" +}, +{ +"problemName": "85. Maximal Rectangle", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/maximal-rectangle/", +"solutionLink": "#" +}, +{ +"problemName": "86. Partition List", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/partition-list/", +"solutionLink": "#" +}, +{ +"problemName": "87. Scramble String", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/scramble-string/", +"solutionLink": "#" +}, +{ +"problemName": "88. Merge Sorted Array", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/merge-sorted-array/", +"solutionLink": "#" +}, +{ +"problemName": "89. Gray Code", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/gray-code/", +"solutionLink": "#" +}, +{ +"problemName": "90. Subsets II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/subsets-ii/", +"solutionLink": "#" +}, +{ +"problemName": "91. Decode Ways", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/decode-ways/", +"solutionLink": "#" +}, +{ +"problemName": "92. Reverse Linked List II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/reverse-linked-list-ii/", +"solutionLink": "#" +}, +{ +"problemName": "93. Restore IP Addresses", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/restore-ip-addresses/", +"solutionLink": "#" +}, +{ +"problemName": "94. Binary Tree Inorder Traversal", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/binary-tree-inorder-traversal/", +"solutionLink": "#" +}, +{ +"problemName": "95. Unique Binary Search Trees II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/unique-binary-search-trees-ii/", +"solutionLink": "#" +}, +{ +"problemName": "96. Unique Binary Search Trees", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/unique-binary-search-trees/", +"solutionLink": "#" +}, +{ +"problemName": "97. Interleaving String", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/interleaving-string/", +"solutionLink": "#" +}, +{ +"problemName": "98. Validate Binary Search Tree", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/validate-binary-search-tree/", +"solutionLink": "#" +}, +{ +"problemName": "99. Recover Binary Search Tree", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/recover-binary-search-tree/", +"solutionLink": "#" +} ] -Now, you can see the list of problems in a table format. You can click on the problem link to view the problem on the LeetCode website. You can also click on the solution link to view the solution of the problem. \ No newline at end of file +Now, you can see the list of problems in a table format. You can click on the problem link to view the problem on the LeetCode website. You can also click on the solution link to view the solution of the problem. diff --git a/dsa-solutions/lc-solutions/0000-0099/0040-Combination-sum-II.md b/dsa-solutions/lc-solutions/0000-0099/0040-Combination-sum-II.md new file mode 100644 index 000000000..1b9853b23 --- /dev/null +++ b/dsa-solutions/lc-solutions/0000-0099/0040-Combination-sum-II.md @@ -0,0 +1,324 @@ +--- +id: combination-sum-II +title: Combination Sum-II(LeetCode) +sidebar_label: 0040-Combination Sum-II +tags: + - Array + - Backtracking +description: Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target. +sidebar_position: 40 +--- + +## Problem Statement + +Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target. + +Each number in candidates may only be used once in the combination. + +Note: The solution set must not contain duplicate combinations. + +Example 1: + +Input: candidates = [10,1,2,7,6,1,5], target = 8 +Output: +[ +[1,1,6], +[1,2,5], +[1,7], +[2,6] +] +Example 2: + +Input: candidates = [2,5,2,1,2], target = 5 +Output: +[ +[1,2,2], +[5] +] + +Constraints: + +- 1 `<=` candidates.length `<=` 100 +- 1 `<=` candidates[i] `<=` 50 +- 1 `<=` target `<=` 30 + +## Solutions: + +### Intuition + + The intuition behind the solution involves a backtracking technique, which is similar to depth-first search (DFS). + + Since we need to find all combinations that add up to the target and any number in the candidates can be used only once, we sort the candidates first. Sorting the array helps us to easily skip over duplicate elements and avoid generating duplicate combinations. + + Then, we perform **DFS with backtracking**, starting from the beginning of the candidates array and exploring each possibility by either including or excluding a candidate. After including a candidate in the combination, we call the function recursively, reducing the target by the value of that candidate and moving to the next index. + + While exploring, we keep track of the current sum of the combination, and when the sum equals the target, we add the current combination to the answer. If at any point, the sum exceeds target or we reach the end of the candidates array, we backtrack. + + To prevent duplicates, we skip subsequent candidates that are the same as the previous one at each stage in the loop. This way, we ensure that if a number has been used in a combination, the same number is not used immediately again to form another similar combination. + + The result of the function will be a list of lists, where each inner list is a unique combination of numbers that sum to the target value. + + Learn more about Backtracking patterns. + +### Solution Approach + +The solution uses depth-first search (DFS), backtracking, and sorting to effectively find all unique combinations. Let's break down how each part of the code contributes to the solution: + +1. **Sorting the Candidates:** The candidates array is first sorted to ensure that we can easily skip duplicates. + + - candidates.sort() + +2. **Depth-First Search (DFS) Function:** The dfs function is defined to handle the recursion, taking two parameters: i (the current index in the candidates list) and s (the remaining sum needed to reach the target). + + - def dfs(i: int, s: int): + +3. **Condition to Add to the Answer:** Inside the dfs function, we check if the remaining sum s is zero, meaning we found a valid combination that sums to the target. In that case, we add a copy of the current combination to the answer list. + + - if s == 0: + - ans.append(t[:]) + +4. **Base Cases for Termination:** We return if the index i moves beyond the length of the candidates or if the remaining sum s is less than the current candidate, which means we can't reach the desired total with the current and subsequent candidates since they are all larger. + + - if i >= len(candidates) or s < candidates[i]: + - return + +5. **Loop Over the Candidates:** Starting from the current index to the end of candidates, we try to include the candidate in the combination: + + - for j in range(i, len(candidates)): + +6. **Skipping Duplicates:** Before including a candidate in the combination, we skip over it if it's the same as its predecessor to avoid duplicates in our answer list (since we’ve already considered this value in the previous steps). + + - if j > i and candidates[j] == candidates[j - 1]: + - continue + +7. **Backtracking:** After including a candidate, the dfs function is called recursively with the updated index (j+1) and the updated remaining sum (s - candidates[j]). After this recursive call, we backtrack by removing the last candidate from the combination and moving on to the next candidate. + + - t.append(candidates[j]) + - dfs(j + 1, s - candidates[j]) + - t.pop() + +The solution utilizes a list ans to store all the unique combinations and a temporary list t to store the current combination being constructed. + +After defining dfs, the solution begins the search with: + +1. ans = [] +2. t = [] +3. dfs(0, target) + +The DFS and backtracking continue until all possible combinations that meet the criteria have been explored, after which ans is returned, containing all the valid combinations. + +This approach efficiently explores all possible combinations and prunes the search space to avoid duplicates and unnecessary searches, thereby finding the solution set in an optimal manner. + + + + + ```cpp + #include + #include + #include + + using namespace std; + + void findCombination(int ind, int target, vector& arr, vector>& ans, vector& ds) { + if (target == 0) { + ans.push_back(ds); + return; + } + + for (int i = ind; i < arr.size(); i++) { + if (i > ind && arr[i] == arr[i - 1]) + continue; + if (arr[i] > target) + break; + + ds.push_back(arr[i]); + findCombination(i + 1, target - arr[i], arr, ans, ds); + ds.pop_back(); + } + } + + class Solution { + public: + vector> combinationSum2(vector& candidates, int target) { + sort(candidates.begin(), candidates.end()); + vector> ans; + vector ds; + findCombination(0, target, candidates, ans, ds); + return ans; + } + }; + ``` + + + + ```java + import java.util.*; + + public class Solution { + public void findCombination(int ind, int target, int[] arr, List> ans, List ds) { + if (target == 0) { + ans.add(new ArrayList<>(ds)); + return; + } + + for (int i = ind; i < arr.length; i++) { + if (i > ind && arr[i] == arr[i - 1]) + continue; + if (arr[i] > target) + break; + + ds.add(arr[i]); + findCombination(i + 1, target - arr[i], arr, ans, ds); + ds.remove(ds.size() - 1); + } + } + + public List> combinationSum2(int[] candidates, int target) { + Arrays.sort(candidates); + List> ans = new ArrayList<>(); + findCombination(0, target, candidates, ans, new ArrayList<>()); + return ans; + } + + public static void main(String[] args) { + Solution sol = new Solution(); + int[] candidates = {10, 1, 2, 7, 6, 1, 5}; + int target = 8; + List> result = sol.combinationSum2(candidates, target); + for (List combination : result) { + System.out.println(combination); + } + } + } + ``` + + + + ```python + def findCombination(ind, target, arr, ans, ds): + if target == 0: + ans.append(list(ds)) + return + + for i in range(ind, len(arr)): + if i > ind and arr[i] == arr[i - 1]: + continue + if arr[i] > target: + break + + ds.append(arr[i]) + findCombination(i + 1, target - arr[i], arr, ans, ds) + ds.pop() + + class Solution: + def combinationSum2(self, candidates, target): + candidates.sort() + ans = [] + findCombination(0, target, candidates, ans, []) + return ans + + # Driver code + if __name__ == "__main__": + sol = Solution() + candidates = [10, 1, 2, 7, 6, 1, 5] + target = 8 + result = sol.combinationSum2(candidates, target) + for combination in result: + print(combination) + ``` + + + + ```c + #include + #include + + void findCombination(int ind, int target, int* arr, int arrSize, int** ans, int* returnSize, int* ds, int dsSize) { + if (target == 0) { + ans[*returnSize] = (int*)malloc(dsSize * sizeof(int)); + for (int i = 0; i < dsSize; i++) { + ans[*returnSize][i] = ds[i]; + } + (*returnSize)++; + return; + } + + for (int i = ind; i < arrSize; i++) { + if (i > ind && arr[i] == arr[i - 1]) + continue; + if (arr[i] > target) + break; + + ds[dsSize] = arr[i]; + findCombination(i + 1, target - arr[i], arr, arrSize, ans, returnSize, ds, dsSize + 1); + } + } + + int** combinationSum2(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes) { + qsort(candidates, candidatesSize, sizeof(int), cmpfunc); + int** ans = (int**)malloc(1000 * sizeof(int*)); + int* ds = (int*)malloc(1000 * sizeof(int)); + *returnSize = 0; + + findCombination(0, target, candidates, candidatesSize, ans, returnSize, ds, 0); + + *returnColumnSizes = (int*)malloc(*returnSize * sizeof(int)); + for (int i = 0; i < *returnSize; i++) { + (*returnColumnSizes)[i] = returnColumnSizes[i]; + } + + return ans; + } + + int cmpfunc(const void* a, const void* b) { + return (*(int*)a - *(int*)b); + } + + // Driver code + int main() { + int candidates[] = {10, 1, 2, 7, 6, 1, 5}; + int target = 8; + int returnSize; + int* returnColumnSizes; + int** result = combinationSum2(candidates, sizeof(candidates) / sizeof(candidates[0]), target, &returnSize, &returnColumnSizes); + for (int i = 0; i < returnSize; i++) { + for (int j = 0; j < returnColumnSizes[i]; j++) { + printf("%d ", result[i][j]); + } + printf("\n"); + } + return 0; + } + ``` + + + + +## Time and Space Complexity + +The provided Python code solves the combination sum problem where each number in the array candidates can only be used once to find all unique combinations that sum up to a given target. + +### Time Complexity + +The time complexity of this code primarily depends on the depth of the recursion and the number of recursive calls made at each level. + + 1. The recursion depth is at most the target value if we choose candidates with a value of 1 every time. However, since the same candidate cannot be used repeatedly, the recursion depth is constrained by the number of candidates in the worst case. + + 2. At every level of recursion, we iterate over the remaining candidates, so, in the worst case, the number of recursive calls can be exponential in nature, implied by $O(2^n)$, where n is the number of candidates. However, since we skip duplicates after sorting, the number of branches in the recursion tree can be less than that. + + 3. A more accurate bound is not straightforward because it depends on the candidates and the target value. The worst-case time complexity, without considering duplicates, is $O(2^n)$, where n is the number of candidates. + + 4. The sorting operation at the start of the code takes $O(n*log(n))$ time. + Combining the sorting with the recursion leads to a worst-case time complexity of $O(n*log(n) + 2^n)$. Considering that the exponential part is more dominant, we can approximate it as $O(2^n)$. + +### Space Complexity + +The space complexity of the code consists of: + + 1. Space used by the recursion stack, which in the worst case is equivalent to the depth of recursion, at most $O(n)$ if all candidates are used. + + 2. Space for the temporary list t, which stores the current combination, will at most contain n values, adding another O(n). + + 3. Finally, the output list ans that could potentially hold all unique combinations of candidates. In the worst case, this could be all possible combinations which can be exponential, represented as $O(2^n)$. + + 4. Hence, the overall space complexity, considering the output space and the recursion stack depth, is O(n + 2^n). Typically, the output space can be a separate consideration, and if we exclude it, the space complexity for computation is $O(n)$. However, if we include the space needed for the output, it would be $O(2^n)$ due to the possibility of storing all combinations. From 0c78e7f3a656175663567ac06a08c4044d875608 Mon Sep 17 00:00:00 2001 From: Sadaf <137484958+SadafKausar2025@users.noreply.github.com> Date: Sun, 16 Jun 2024 16:46:49 +0530 Subject: [PATCH 2/2] corrected --- .../0000-0099/0040-Combination-sum-II.md | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0040-Combination-sum-II.md b/dsa-solutions/lc-solutions/0000-0099/0040-Combination-sum-II.md index 1b9853b23..c5bf5de4d 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0040-Combination-sum-II.md +++ b/dsa-solutions/lc-solutions/0000-0099/0040-Combination-sum-II.md @@ -66,44 +66,44 @@ The solution uses depth-first search (DFS), backtracking, and sorting to effecti 1. **Sorting the Candidates:** The candidates array is first sorted to ensure that we can easily skip duplicates. - - candidates.sort() + - `candidates.sort()` 2. **Depth-First Search (DFS) Function:** The dfs function is defined to handle the recursion, taking two parameters: i (the current index in the candidates list) and s (the remaining sum needed to reach the target). - - def dfs(i: int, s: int): + - `def dfs(i: int, s: int):` 3. **Condition to Add to the Answer:** Inside the dfs function, we check if the remaining sum s is zero, meaning we found a valid combination that sums to the target. In that case, we add a copy of the current combination to the answer list. - - if s == 0: - - ans.append(t[:]) + - `if s == 0:` + - `ans.append(t[:])` 4. **Base Cases for Termination:** We return if the index i moves beyond the length of the candidates or if the remaining sum s is less than the current candidate, which means we can't reach the desired total with the current and subsequent candidates since they are all larger. - - if i >= len(candidates) or s < candidates[i]: + - `if i >= len(candidates) or s < candidates[i]:` - return 5. **Loop Over the Candidates:** Starting from the current index to the end of candidates, we try to include the candidate in the combination: - - for j in range(i, len(candidates)): + - `for j in range(i, len(candidates)):` 6. **Skipping Duplicates:** Before including a candidate in the combination, we skip over it if it's the same as its predecessor to avoid duplicates in our answer list (since we’ve already considered this value in the previous steps). - - if j > i and candidates[j] == candidates[j - 1]: + - `if j > i and candidates[j] == candidates[j - 1]:` - continue 7. **Backtracking:** After including a candidate, the dfs function is called recursively with the updated index (j+1) and the updated remaining sum (s - candidates[j]). After this recursive call, we backtrack by removing the last candidate from the combination and moving on to the next candidate. - - t.append(candidates[j]) - - dfs(j + 1, s - candidates[j]) - - t.pop() + - `t.append(candidates[j])` + - `dfs(j + 1, s - candidates[j])` + - `t.pop()` The solution utilizes a list ans to store all the unique combinations and a temporary list t to store the current combination being constructed. After defining dfs, the solution begins the search with: -1. ans = [] -2. t = [] -3. dfs(0, target) +1. `ans = []` +2. `t = []` +3. `dfs(0, target)` The DFS and backtracking continue until all possible combinations that meet the criteria have been explored, after which ans is returned, containing all the valid combinations. @@ -317,8 +317,8 @@ The space complexity of the code consists of: 1. Space used by the recursion stack, which in the worst case is equivalent to the depth of recursion, at most $O(n)$ if all candidates are used. - 2. Space for the temporary list t, which stores the current combination, will at most contain n values, adding another O(n). + 2. Space for the temporary list t, which stores the current combination, will at most contain n values, adding another $O(n)$. 3. Finally, the output list ans that could potentially hold all unique combinations of candidates. In the worst case, this could be all possible combinations which can be exponential, represented as $O(2^n)$. - 4. Hence, the overall space complexity, considering the output space and the recursion stack depth, is O(n + 2^n). Typically, the output space can be a separate consideration, and if we exclude it, the space complexity for computation is $O(n)$. However, if we include the space needed for the output, it would be $O(2^n)$ due to the possibility of storing all combinations. + 4. Hence, the overall space complexity, considering the output space and the recursion stack depth, is $O(n + 2^n)$. Typically, the output space can be a separate consideration, and if we exclude it, the space complexity for computation is $O(n)$. However, if we include the space needed for the output, it would be $O(2^n)$ due to the possibility of storing all combinations.