Skip to content

Commit 531b435

Browse files
authored
Merge pull request #1149 from pranavbabu/patch-3
Fix O(n2) to be O(n) for generate method
2 parents 377b0e8 + b7c6a33 commit 531b435

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

lib/money/money/allocation.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ def self.generate(amount, parts, whole_amounts = true)
3535

3636
result = []
3737
remaining_amount = amount
38-
38+
parts_sum = parts.sum
39+
3940
until parts.empty? do
40-
parts_sum = parts.inject(0, :+)
4141
part = parts.pop
4242

4343
current_split = 0
@@ -48,6 +48,7 @@ def self.generate(amount, parts, whole_amounts = true)
4848

4949
result.unshift current_split
5050
remaining_amount -= current_split
51+
parts_sum -= part
5152
end
5253

5354
result

0 commit comments

Comments
 (0)