Skip to content

Commit 9a60a68

Browse files
LYLY
authored andcommitted
Backpack VI
1 parent dd1a32f commit 9a60a68

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

backpack_vi.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Solution:
4+
# @param {int[]} nums an integer array and all positive numbers, no duplicates
5+
# @param {int} target an integer
6+
# @return {int} an integer
7+
def backPackVI(self, nums, target):
8+
# Write your code here
9+
'''
10+
1. 先对nums排序
11+
2. 利用result[0]到result[i - 1]的结果迭代出result[i]
12+
'''
13+
result = [0] * (target + 1)
14+
result[0] = 1
15+
nums.sort()
16+
for i in xrange(1, target + 1):
17+
for j in xrange(len(nums)):
18+
if i >= nums[j]:
19+
result[i] += result[i - nums[j]]
20+
return result[-1]

0 commit comments

Comments
 (0)