aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Smith <js5@sanger.ac.uk>2022-11-25 23:17:27 +0000
committerGitHub <noreply@github.com>2022-11-25 23:17:27 +0000
commit28e71ae69e07d6e3b2fb2b6c952f4281fac374a6 (patch)
treebaa4f30ea693f22247b97d8e1a4e3e9d4ac376c4
parent40cb69909abb1058a29c5792afb12ba3d9b4537c (diff)
downloadperlweeklychallenge-club-28e71ae69e07d6e3b2fb2b6c952f4281fac374a6.tar.gz
perlweeklychallenge-club-28e71ae69e07d6e3b2fb2b6c952f4281fac374a6.tar.bz2
perlweeklychallenge-club-28e71ae69e07d6e3b2fb2b6c952f4281fac374a6.zip
Update README.md
-rw-r--r--challenge-192/james-smith/README.md16
1 files changed, 16 insertions, 0 deletions
diff --git a/challenge-192/james-smith/README.md b/challenge-192/james-smith/README.md
index 76dac905f8..e509b7da91 100644
--- a/challenge-192/james-smith/README.md
+++ b/challenge-192/james-smith/README.md
@@ -118,3 +118,19 @@ So first thing we do is work out the sum and see if it is divisible by the lengt
Second is how to work out the number of steps. This is easier than you think. We only have to consider the case where we move numbers right to left or left to right, starting at the left. We are not bothered whether any number becomes negative.
So (1) how much do we need to move? This is simply `$av - $A[$p]`. So we borrow it from the next number so `$A[$p+1] = $A[$p+1] - $av + $A[$p]` and the number of steps is just `abs($av-$A[$p])`. Giving the code above.
+
+### A minor optimization
+
+We can do away with the array - and just use a scalar for the "size" of the next bin - giving us:
+
+```perl
+sub equal_dis2 {
+ my($av,$k,$f) = (0,0,$_[0]);
+ $av+=$_ for @_;
+ return -1 if $av%@_;
+ $av/=@_;
+ $k+=abs($av-$f),$f=$_[$_]-$av+$f for 1..$#_;
+ $k;
+}
+```
+