@@ -505,6 +505,38 @@ def backtrack(opencount, closecount, path):
505505 backtrack (0 , 0 , [])
506506 return res
507507
508+
509+ ############# 39. Combination Sum ############
510+ class Solution :
511+ def combinationSum (self , candidates : List [int ], target : int ) -> List [List [int ]]:
512+ """
513+ N = len(candidates), T = target, M = min(candidates)
514+ N-ary Tree height = T/M
515+ Total nodes in N-ary tree = N^(T/M+1)
516+ Time O(N^(T/M+1)) | Space O(T/M)
517+ """
518+ candidates .sort ()
519+ n = len (candidates )
520+ res = []
521+ def backtrack (i , path , total ):
522+ if total == target :
523+ res .append (path .copy ())
524+ return
525+ elif total > target :
526+ return
527+
528+ else :
529+ for j in range (i , n ):
530+ # If candidates contains duplicates, need to skip by checking (j > 0 and candidates[j] == candidates[j-1])
531+ path .append (candidates [j ])
532+ backtrack (j , path , total + candidates [j ])
533+ path .pop ()
534+
535+ backtrack (0 , [], 0 )
536+ print (res )
537+ return res
538+
539+
508540############# 21. Merge Two Sorted Lists ############
509541# Definition for singly-linked list.
510542class ListNode :
@@ -589,26 +621,23 @@ def minWindow(self, s: str, t: str) -> str:
589621 m , n = len (s ), len (t )
590622 if n > m :
591623 return ""
592- cs , ct = Counter (), Counter (t )
624+ window , need = Counter (), Counter (t )
593625 res = ""
594- i , j = 0 , 0 # left, right pointer to s
595626
596- def valid (window , need ):
627+ def isValid (window , need ):
597628 for c in need .keys ():
598629 if window [c ] < need [c ]:
599630 return False
600631 return True
601632
602- while j < m :
603- cs [s [j ]] += 1
604- j += 1
605-
606- while i < j and valid (cs , ct ):
607- if j - i < len (res ) or res == "" :
608- res = s [i :j ]
609- cs [s [i ]] -= 1
633+ i = 0
634+ for j , c in enumerate (s ):
635+ window [c ] += 1
636+ while i <= j and isValid (window , need ):
637+ if j - i + 1 < len (res ) or res == "" :
638+ res = s [i :j + 1 ]
639+ window [s [i ]] -= 1
610640 i += 1
611-
612641 return res
613642
614643############# 71. Simplify Path ############
@@ -673,6 +702,21 @@ def findMaxLength(self, nums: List[int]) -> int:
673702 res = max (res , j - seen [parity ])
674703 return res
675704
705+
706+ ############# 56. Merge Intervals ############
707+ class Solution :
708+ def merge (self , intervals : List [List [int ]]) -> List [List [int ]]:
709+ intervals .sort () # do normal sort of based on start value so mergeable intervals are next to each other
710+
711+ output = [intervals [0 ]]
712+
713+ for start , end in intervals :
714+ if start <= output [- 1 ][1 ]:
715+ output [- 1 ][1 ] = max (output [- 1 ][1 ], end )
716+ else :
717+ output .append ([start , end ])
718+ return output
719+
676720############# 426. Convert Binary Search Tree to Sorted Doubly Linked List #############
677721class Solution :
678722 def treeToDoublyList (self , root : 'Optional[Node]' ) -> 'Optional[Node]' :
0 commit comments