Last active
February 20, 2022 23:25
-
-
Save msg7086/744d4cf2d81b42e27cb73069ecff939d to your computer and use it in GitHub Desktop.
Revisions
-
msg7086 revised this gist
Feb 20, 2022 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,6 +3,8 @@ def can_partition(nums) sum = nums.sum return false if sum.odd? nums.sort! nums.reverse! half_sum = sum / 2 dp = Set.new 0.upto(nums.size-1) do |i| -
msg7086 revised this gist
Feb 20, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ def can_partition(nums) dp = Set.new 0.upto(nums.size-1) do |i| num = nums[i] dp += dp.map { |x| x + num } dp << num return true if dp.include?(half_sum) dp.delete_if { |x| x > half_sum} -
msg7086 revised this gist
Feb 20, 2022 . 1 changed file with 6 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,15 +3,14 @@ def can_partition(nums) sum = nums.sum return false if sum.odd? half_sum = sum / 2 dp = Set.new 0.upto(nums.size-1) do |i| num = nums[i] dp += dp.map { |i| i + num } dp << num return true if dp.include?(half_sum) dp.delete_if { |x| x > half_sum} end false end -
msg7086 created this gist
Feb 20, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ # @param {Integer[]} nums # @return {Boolean} def can_partition(nums) sum = nums.sum return false if sum.odd? nums.sort! nums.reverse! half_sum = sum / 2 dp_map = Array.new(nums.length) { |i| Set.new([nums[i]]) } 1.upto(nums.size-1) do |i| num = nums[i] dp_map[i] += 0.upto(i-1).flat_map { |j| dp_map[j].map { |k| k + num } } return true if dp_map[i].include?(half_sum) dp_map[i].delete_if { |x| x > half_sum} end false end