Skip to content

Commit

Permalink
Update sort-array-by-moving-items-to-empty-space.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Nov 4, 2022
1 parent d6a72dc commit 01a4943
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Python/sort-array-by-moving-items-to-empty-space.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ def sortArray(self, nums):
:type nums: List[int]
:rtype: int
"""
def index(x, d):
return d*(len(nums)-1) if x == 0 else x-d


def min_moves(d):
def index(x):
return d*(len(nums)-1) if x == 0 else x-d

a = nums[:]
result = 0
for i in xrange(len(a)):
l, found = 1, (a[i] == 0)
while index(a[i], d) != i:
j = index(a[i], d)
while index(a[i]) != i:
j = index(a[i])
a[i], a[j] = a[j], a[i]
l += 1
found |= (a[i] == 0)
Expand Down

0 comments on commit 01a4943

Please sign in to comment.