feat: add lc 46, 39, 78

This commit is contained in:
Prad Nukala committed 2026-09-24 14:36:19 -04:00
1 parent ac4dc654fc
commit f0f479d6cf
8 files changed
+496

No files matched your search

+75
View File
@@ -0,0 +1,75 @@
"""
39. Combination Sum
Difficulty: Medium
https://leetcode.com/problems/combination-sum/
──────────────────────────────────────────────────
Given an array of distinct integers candidates and a target integer
target, return a list of all unique combinations of candidates where
the chosen numbers sum to target. You may return the combinations in
any order.
The same number may be chosen from candidates an unlimited number of
times. Two combinations are unique if the frequency of at least one of
the chosen numbers is different.
The test cases are generated such that the number of unique
combinations that sum up to target is less than 150 combinations for
the given input.
Example 1:
Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used
multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.
Example 2:
Input: candidates = [2,3,5], target = 8
Output: [[2,2,2,2],[2,3,3],[3,5]]
Example 3:
Input: candidates = [2], target = 1
Output: []
Constraints:
• 1 <= candidates.length <= 30
• 2 <= candidates[i] <= 40
• All elements of candidates are distinct.
• 1 <= target <= 40
"""
class Solution:
def combinationSum(self, candidates: list[int], target: int) -> list[list[int]]:
candidates.sort()
result, path = [], []
def backtrack(start, remaining):
if remaining == 0:
result.append(path[:])
return
for i in range(start, len(candidates)):
if candidates[i] > remaining:
break
path.append(candidates[i])
backtrack(i, remaining - candidates[i])
path.pop()
backtrack(0, target)
return result
+59
View File
@@ -0,0 +1,59 @@
"""
46. Permutations
Difficulty: Medium
https://leetcode.com/problems/permutations/
──────────────────────────────────────────────────
Given an array nums of distinct integers, return all the possible
permutations. You can return the answer in any order.
Example 1:
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2:
Input: nums = [0,1]
Output: [[0,1],[1,0]]
Example 3:
Input: nums = [1]
Output: [[1]]
Constraints:
• 1 <= nums.length <= 6
• -10 <= nums[i] <= 10
• All the integers of nums are unique.
"""
class Solution:
def permute(self, nums: list[int]) -> list[list[int]]:
result, path = [], []
used = [False] * len(nums)
def backtrack():
if len(path) == len(nums):
result.append(path[:])
return
for i in range(len(nums)):
if used[i]:
continue
used[i] = True
path.append(nums[i])
backtrack()
path.pop()
used[i] = False
backtrack()
return result
+50
View File
@@ -0,0 +1,50 @@
"""
78. Subsets
Difficulty: Medium
https://leetcode.com/problems/subsets/
──────────────────────────────────────────────────
Given an integer array nums of unique elements, return all possible
subsets (the power set).
The solution set must not contain duplicate subsets. Return the
solution in any order.
Example 1:
Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Example 2:
Input: nums = [0]
Output: [[],[0]]
Constraints:
• 1 <= nums.length <= 10
• -10 <= nums[i] <= 10
• All the numbers of nums are unique.
"""
class Solution:
def subsets(self, nums: list[int]) -> list[list[int]]:
res = []
def backtrack(i, path):
res.append(path[:])
for num_idx in range(i, len(nums)):
path.append(nums[num_idx])
backtrack(num_idx + 1, path)
path.pop()
backtrack(0, [])
return res