ruby - Codility Permuation Assigment -



ruby - Codility Permuation Assigment -

i' m doing permutation task on codility.com. goal check if the array passed 1 element matching permutation in size. i.e. array size n should include values 1,2,3...n each once. managed logic right how ever score on complexity horrible 0(n**2). how can improve this?

def solution(a) homecoming 0 unless a.uniq == set = (1..a.size).to_a n in set homecoming 0 unless a.include? n end 1 end

i'd inclined follows. have not dealt possibilities of a beingness empty or containing elements other fixnums.

def solution(a) (a.uniq == && a.min == 1 && a.max == a.size) ? 1 : 0 end solution([1,2,3,4,5]) #=> 1 solution([1,2,5,3,4]) #=> 1 solution([1,2,3,5,6]) #=> 0 solution([1,2,5,4,2]) #=> 0 solution([1,2,2,4,5]) #=> 0 solution([1,2,5,4,6]) #=> 0

ruby algorithm big-o

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -