You are viewing a single comment's thread. Return to all comments →
def highestValuePalindrome(s, n, k) arr = s.chars left = arr.take(n/2).map(&:to_i) right = arr.reverse.take(n/2).map(&:to_i) middle = (n%2==0) ? [] : [s[n/2].to_i] counter = 0 (n/2).times { |i| counter +=1 if left[i] != right[i] } return "-1" if counter > k changes = k-counter answer = [] (n/2).times do |i| if left[i] == right[i] and left[i] == 9 answer << 9; elsif left[i] == right[i] and left[i] < 9 and changes >=2 answer << 9; changes -= 2; elsif left[i] == right[i] and left[i] < 9 answer << left[i]; elsif left[i] != right[i] and [left[i],right[i]].max == 9 answer << 9; elsif left[i] != right[i] and changes >=1 answer << 9; changes -=1; else answer << [left[i],right[i]].max end end p answer middle = [9] if n%2 !=0 and changes > 0 (answer+ middle + answer.reverse)*"" end
Seems like cookies are disabled on this browser, please enable them to open this website
Highest Value Palindrome
You are viewing a single comment's thread. Return to all comments →
Ruby