leetcode 330. 按要求补齐数组

给定一个已排序的正整数数组 nums,和一个正整数 n 。从 [1, n] 区间内选取任意个数字补充到 nums 中,使得 [1, n] 区间内的任何数字都可以用 nums 中某几个数字的和来表示。请输出满足上述要求的最少需要补充的数字个数。

示例 1:

输入: nums = [1,3], n = 6
输出: 1 
解释:
根据 nums 里现有的组合 [1], [3], [1,3],可以得出 1, 3, 4。
现在如果我们将 2 添加到 nums 中, 组合变为: [1], [2], [3], [1,3], [2,3], [1,2,3]。
其和可以表示数字 1, 2, 3, 4, 5, 6,能够覆盖 [1, 6] 区间里所有的数。
所以我们最少需要添加一个数字。

示例 2:

输入: nums = [1,5,10], n = 20
输出: 2
解释: 我们需要添加 [2, 4]。

示例 3:

输入: nums = [1,2,2], n = 5
输出: 0
  • mysolution
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
def greedy(nums,n):
needed = set(range(1,n+1))#all need to
covered = set()#cover to all
tmpcover = []
full = []
for i in nums:
if i <= n:full.append(i)
pick = needed-set(full)#can choose num
for i in full:
for j in covered:
tmpcover.append(i+j)
tmpcover.append(i)
covered = set(tmpcover)
needed -= covered
count = 0
while needed:
state_covered = set()
k = 0
for i in pick:#find suitable picknum to maxsuit needed
tmpcover = []
for j in covered:
tmpcover.append(i+j)
tmpcover.append(i)
state_covered = set(tmpcover)
state_covered = needed&state_covered
if len(state_covered) > k:
k = len(state_covered)
covered = state_covered
needed -= covered
count += 1
return count
  • clear solution
1
2
3
4
5
6
7
8
9
10
def greedy(nums,n):
miss,added,i=1,0,0
while miss <= n:
if i < len(nums) and nums[i] <= miss:
miss += nums[i]
i += 1
else:
miss += miss
added += 1
return added