Goodland Electricity

  • + 1 comment

    wow, this solution looks very good and simple. Can you explain what the various variables are for? it's hard to see how it works. This is my much longer ruby solution:

    n,k = gets.split.map &:to_i
    r = gets.strip.split.join; c = r.split('1').map &:size; i = 2
    c.unshift(0) if r[0] == '1'; c.push(0) if r[-1] == '1'
    c[0..1] = 1+c[0..1].reduce(:+) while c[0..1].reduce(:+)<k-1
    c[-2..-1] = 1+c[-2..-1].reduce(:+) while c[-2..-1].reduce(:+)<k-1
    while c[i+1]
    	while c[i-1..i].reduce(:+) < k*2-2 and c[i+1] do
        	c[i-1..i] = 1+c[i-1..i].reduce(:+) end; i += 1 end
    puts (!c.index{|x| x > 2*k-2}.nil? or c[0] > k-1 or c[-1] > k-1) ? -1 : c.size-1