Skip to content

Instantly share code, notes, and snippets.

@mostlyerror
Created September 5, 2014 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mostlyerror/c3a9463f474ce96d7693 to your computer and use it in GitHub Desktop.
Save mostlyerror/c3a9463f474ce96d7693 to your computer and use it in GitHub Desktop.
Hackerrank cut_the_sticks
n = gets.chomp.to_i
sticks = gets.chomp.split.map(&:to_i)
until sticks.empty? do
new_sticks = sticks.map {|x| x - sticks.min}
cut = 0
new_sticks.each_index {|i| cut += 1 if new_sticks[i] != sticks[i] }
p cut
sticks = new_sticks.reject(&:zero?)
end
@mostlyerror
Copy link
Author

revised solution:

gets
sticks = gets.chomp.split.map &:to_i
min = sticks.min
until sticks.all? &:zero?
  cuts = 0
  min = sticks.reject(&:zero?).min
  sticks.each_index do |i|
    if sticks[i].nonzero?
      sticks[i] -= min
      cuts += 1
    end
  end
  puts cuts
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment