Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2054 from saip7795/sp/permutations-ruby
Browse files Browse the repository at this point in the history
Create: 0046-Permutations.rb
  • Loading branch information
saip7795 authored Jan 17, 2023
2 parents 2416657 + 16a8041 commit 55a2f29
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ruby/0046-permutations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def permute(nums)
return [[]] if nums.empty?

perms = []

(0...nums.length).each do |i|
el = nums[i]
rest = nums.take(i) + nums.drop(i + 1)
new_perms = permute(rest).map { |perm| perm.unshift(el) }
perms.concat(new_perms)
end

perms
end

0 comments on commit 55a2f29

Please sign in to comment.