Skip to content

Commit 6601c4a

Browse files
authored
Create minimum-swaps-to-group-all-1s-together.py
1 parent 5f5fc8d commit 6601c4a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def minSwaps(self, data):
6+
"""
7+
:type data: List[int]
8+
:rtype: int
9+
"""
10+
total_count = sum(data)
11+
result, count, left = 0, 0, 0
12+
for i in xrange(len(data)):
13+
count += data[i]
14+
if i-left+1 > total_count:
15+
count -= data[left]
16+
left += 1
17+
result = max(result, count)
18+
return total_count-result

0 commit comments

Comments
 (0)