diff options
author | Yuchen Pei <me@ypei.me> | 2018-06-04 16:48:00 +0200 |
---|---|---|
committer | Yuchen Pei <me@ypei.me> | 2018-06-04 16:48:00 +0200 |
commit | 03aace9fe549c89f1f1043f40456098eeb3499c4 (patch) | |
tree | 2c93399dd1100b6b3480cdd9489bd51d75153be4 | |
parent | dd11e5c21f03582c278ec4126101f9d78cb80cc9 (diff) |
added a micropost
-rw-r--r-- | microposts/boyer-moore.md | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/microposts/boyer-moore.md b/microposts/boyer-moore.md new file mode 100644 index 0000000..e3e0f9c --- /dev/null +++ b/microposts/boyer-moore.md @@ -0,0 +1,17 @@ +--- +date: 2018-06-04 +--- + +The [Boyer-Moore algorithm for finding the majority of a sequence of elements](https://en.wikipedia.org/wiki/Boyer–Moore_majority_vote_algorithm) falls in the category of "very clever algorithms". + + 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; + } + |