forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumSet_Maximum_Number.py
More file actions
40 lines (29 loc) · 957 Bytes
/
SumSet_Maximum_Number.py
File metadata and controls
40 lines (29 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
: Find the subset with maximum sum less than or equal to a given target.
"""
def max_subset_sum(nums, target):
best_sum = 0
best_set = []
def backtrack(i, current, current_sum):
nonlocal best_sum, best_set
# If the sum exceeds target, stop here
if current_sum > target:
return
# Update best subset if current sum is better
if current_sum > best_sum:
best_sum = current_sum
best_set = current[:]
# Explore remaining numbers
for j in range(i, len(nums)):
current.append(nums[j])
backtrack(j + 1, current, current_sum + nums[j])
current.pop()
backtrack(0, [], 0)
return best_sum, best_set
# Example usage
nums = [7, 2, 4, 9, 5]
target = 12
best_sum, best_subset = max_subset_sum(nums, target)
print(f"Target: {target}")
print(f"Best Sum: {best_sum}")
print(f"Best Subset: {best_subset}")
---