diff options
Diffstat (limited to 'microposts/boyer-moore.org')
-rw-r--r-- | microposts/boyer-moore.org | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/microposts/boyer-moore.org b/microposts/boyer-moore.org new file mode 100644 index 0000000..6298454 --- /dev/null +++ b/microposts/boyer-moore.org @@ -0,0 +1,21 @@ +#+title: boyer-moore + +#+date: <2018-06-04> + +The +[[https://en.wikipedia.org/wiki/Boyer–Moore_majority_vote_algorithm][Boyer-Moore +algorithm for finding the majority of a sequence of elements]] falls in +the category of "very clever algorithms". + +#+begin_example + int majorityElement(vector<int>& xs) { + int count = 0; + int maj = xs[0]; + for (auto x : xs) { + if (x == maj) count++; + else if (count == 0) maj = x; + else count--; + } + return maj; + } +#+end_example |