From 147a19e84a743f1379f05bf2f444143b4afd7bd6 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Fri, 18 Jun 2021 12:58:44 +1000 Subject: Updated. --- microposts/boyer-moore.org | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 microposts/boyer-moore.org (limited to 'microposts/boyer-moore.org') 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& 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 -- cgit v1.2.3