Skip to content

Instantly share code, notes, and snippets.

@prettymuchbryce
Created September 18, 2012 19:56
Show Gist options
  • Save prettymuchbryce/3745462 to your computer and use it in GitHub Desktop.
Save prettymuchbryce/3745462 to your computer and use it in GitHub Desktop.
Find minimum swaps to order a string of 0's and 1's
def findOperations(string)
operations = 0
list = string.split("")
startIndex = 0
endIndex = list.length-1
while startIndex < endIndex
if list[endIndex]=="0"
if list[startIndex] == "1"
list[startIndex], list[endIndex] = list[endIndex], list[startIndex]
operations = operations+1
endIndex-=1
end
startIndex+=1
else
endIndex-=1
end
end
operations
end
input = gets
returnValue = findOperations(input)
puts returnValue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment