-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path330_PatchingArray.py
41 lines (38 loc) · 1.17 KB
/
330_PatchingArray.py
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
41
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: [email protected]
# According to:
# https://leetcode.com/discuss/82822/solution-explanation
class Solution(object):
"""
Let miss_num be the smallest sum in [0,n] that we might be missing.
Meaning we already know we can build all sums in [0,miss). Then
1. If we have a number num <= miss in the given array,
we can add it to those smaller sums to build all sums in [0,miss+num).
2. If we don't, then we must add such a number to the array,
and it's best(GREEDY) to add miss itself, to maximize the reach.
Here is a thinking process, maybe helpful.
https://leetcode.com/discuss/83272/share-my-thinking-process
"""
def minPatches(self, nums, n):
miss_num = 1
index = 0
patch_cnt = 0
length = len(nums)
while miss_num <= n:
if index < length and nums[index] <= miss_num:
miss_num += nums[index]
index += 1
else:
patch_cnt += 1
miss_num <<= 1
# miss_num += miss_num
return patch_cnt
"""
[1,3]
6
[1, 5, 10]
20
[1, 2, 2]
5
"""