Skip to content

Commit 16a156f

Browse files
authored
Create minimum-number-of-operations-to-make-array-continuous.py
1 parent cea33df commit 16a156f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def minOperations(self, nums):
6+
"""
7+
:type nums: List[int]
8+
:rtype: int
9+
"""
10+
n = len(nums)
11+
nums = sorted(set(nums))
12+
result = l = 0
13+
for i in xrange(len(nums)):
14+
if nums[i] <= nums[i-l]+n-1:
15+
l += 1
16+
return n-l
17+
18+
19+
# Time: O(nlogn)
20+
# Space: O(n)
21+
class Solution2(object):
22+
def minOperations(self, nums):
23+
"""
24+
:type nums: List[int]
25+
:rtype: int
26+
"""
27+
n = len(nums)
28+
nums = sorted(set(nums))
29+
result = right = 0
30+
for left in xrange(len(nums)):
31+
while right < len(nums) and nums[right] <= nums[left]+n-1:
32+
right += 1
33+
result = max(result, right-left)
34+
return n-result

0 commit comments

Comments
 (0)