Skip to content

Commit b85bdff

Browse files
authored
Create minimum-number-of-increments-on-subarrays-to-form-a-target-array.py
1 parent e881282 commit b85bdff

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def minNumberOperations(self, target):
6+
"""
7+
:type target: List[int]
8+
:rtype: int
9+
"""
10+
return target[0]+sum(max(target[i]-target[i-1], 0) for i in xrange(1, len(target)))
11+
12+
13+
# Time: O(n)
14+
# Space: O(n)
15+
import itertools
16+
17+
18+
class Solution2(object):
19+
def minNumberOperations(self, target):
20+
"""
21+
:type target: List[int]
22+
:rtype: int
23+
"""
24+
return sum(max(b-a, 0) for b, a in itertools.izip(target, [0]+target))

0 commit comments

Comments
 (0)